Sunday, September 26, 2010

Binary Tree :Preorder,Inorder,Postorder

import java.io.*;
class Node
{
     int info;
Node l,r;
Node(int data)
{
       info=data;
       l=null;
       r=null;  
}
 }
 class Binary
 {
    Node root;
    Binary()
   {
    root=null;
   }

   void insert(Node nnode)throws Exception
   {
    Node q,p;
q=null;
p=root;
    while(p!=null)
{
q=p;
if(p.info>nnode.info)
  p=p.l;
else
  p=p.r;
}
p=nnode;
if(q.info>p.info)
q.l=p;
else
q.r=p;

    System.out.println("value inserted....");  
  }
  
  void search(int data)throws Exception
   {
    Node p;
p=root;
int x=0;
    while(p!=null)
{
if(p.info==data)
{
 x=1;
      System.out.println("value found.....");
      break;  
}
else
{
 if(p.info>data)
   p=p.l;
 else
  p=p.r;
}
    }
if(x==0)
System.out.println("value does not found.....");
  }
 
   void preorder(Node roo)
{
if(roo!=null)
{
 System.out.println(roo.info+"  ");
 preorder(roo.l);
 preorder(roo.r);
}
}

void inorder(Node roo)
{
if(roo!=null)
{
 inorder(roo.l);
 System.out.println(roo.info+"  ");
 inorder(roo.r);
}
}

    void postorder(Node roo)
{
if(roo!=null)
 {
  postorder(roo.l);
  postorder(roo.r);
  System.out.println(roo.info+"  ");
 }
}

void del(int data)throws Exception
   {
     Node q,p;
q=null;
p=root;
     while(p!=null)
{
  if(p.info==data)
    break;  
  if(p.info>data)
  {
    q=p;
    p=p.l;
  }
  else
  {
   q=p;
   p=p.r;
  }
}
if(p!=null)
{
 if(p.l==null&&p.r==null)
 {
   if(q.info>p.info)
    q.l=null;
   else
    q.r=null;
 }
 if(p.l==null&&p.r!=null)
 {
   if(q.info>p.info)
     q.l=p.r;
   else
    q.r=p.r;
 }
 if(p.l!=null&&p.r==null)
 {
   if(q.info>p.info)
     q.l=p.l;
   else
     q.r=p.l;
 }
 if(p.l!=null&&p.r!=null)
 {
   Node x=p,z=null;
   while(x.l!=null)
        {
     z=x;
      x=x.l;
   }
   p.info=x.info;
   z.l=null;
 }
 System.out.print("value Deleted : "+data);
}
else
  System.out.println("value does not found.....");
 }
   public static void main(String args[ ])throws IOException
   {
     Binary ob=new Binary();
     int c,a=1,x;
DataInputStream dis=new DataInputStream(System.in);
     while(a==1)
     {
       System.out.println("\n  Main Menu");
       System.out.println("\n1:Insert value\n2:Display");
       System.out.println("3:Search value\n4:Delete");
       System.out.println("5:EXIT");
       System.out.println();
       System.out.print("Enter your choice:");
          
        c=Integer.parseInt(dis.readLine());
    
        switch(c)
         {
           case 1: try
          {
                    System.out.print("Enter your value:");
                     x=Integer.parseInt(dis.readLine());
                     Node nnode=new Node(x);
                   if(ob.root==null)
                      ob.root=nnode;
               else
                 ob.insert(nnode);

  }
                   catch( Exception e)
                   {}
                    break;
          
          case 2:   System.out.println("1:preorder");
                    System.out.println("1:inorder");
System.out.println("1:postorder");
                    System.out.print("Enter your choice:");
int z=Integer.parseInt(dis.readLine());
switch(z)
{
case 1:ob.preorder(ob.root);
       break;
case 2:ob.inorder(ob.root);
       break;
case 3:ob.postorder(ob.root);
       break;
}
break;
              
          case 3: try
        {
                System.out.print("Enter value for search:");
                  x=Integer.parseInt(dis.readLine());
         ob.search(x);
         }
catch(Exception e)
{}
                  break;
        
 case 4: try
        {
                System.out.print("Enter value for Delete : ");
                  x=Integer.parseInt(dis.readLine());
         ob.del(x);
         }
catch(Exception e)
{}
                  break;

          case 5: System.out.println("....EXIT....");
         System.exit(1);
                  break;
        }
     }
   }
}

/*
OUTPUT
C:\java\bin>javac Binary.java

C:\java\bin>java Binary

  Main Menu

1:Insert value
2:Display
3:Search value
4:Delete
5:EXIT

Enter your choice:1
Enter your value:56

  Main Menu

1:Insert value
2:Display
3:Search value
4:Delete
5:EXIT

Enter your choice:1
Enter your value:67
value inserted....

  Main Menu

1:Insert value
2:Display
3:Search value
4:Delete
5:EXIT

Enter your choice:1
Enter your value:45
value inserted....

  Main Menu

1:Insert value
2:Display
3:Search value
4:Delete
5:EXIT

Enter your choice:1
Enter your value:12
value inserted....

  Main Menu

1:Insert value
2:Display
3:Search value
4:Delete
5:EXIT

Enter your choice:1
Enter your value:78
value inserted....

  Main Menu

1:Insert value
2:Display
3:Search value
4:Delete
5:EXIT

Enter your choice:80

  Main Menu

1:Insert value
2:Display
3:Search value
4:Delete
5:EXIT

Enter your choice:2
1:preorder
1:inorder
1:postorder
Enter your choice:2
12
45
56
67
78

  Main Menu

1:Insert value
2:Display
3:Search value
4:Delete
5:EXIT

Enter your choice:3
Enter value for search:78
value found.....

  Main Menu

1:Insert value
2:Display
3:Search value
4:Delete
5:EXIT

Enter your choice:4
Enter value for Delete : 67
value Deleted : 67
  Main Menu

1:Insert value
2:Display
3:Search value
4:Delete
5:EXIT

Enter your choice:2
1:preorder
1:inorder
1:postorder
Enter your choice:2
12
45
56
78

  Main Menu

1:Insert value
2:Display
3:Search value
4:Delete
5:EXIT

Enter your choice:5
....EXIT....

*/

No comments:

Post a Comment