COMPLETE TREE PROGRAM IN JAVA

 class TNode  
 {  
      TNode left,right;  
      int data;  
      public TNode(int value)  
      {  
      data = value;  
      }  
 }  
 class MyBinarySearchTree   
 {  
      TNode root;  
      public MyBinarySearchTree()  
      {  
      }  
 /**=======================INSERT==================================*/  
 public void insert(int tdata)  
      {  
           TNode newnode = new TNode(tdata);  
           TNode current = root;  
           if (root==null)  
           {  
                root = newnode;  
           }  
           else  
           {  
                while (true)  
                {  
                     if (current.data>tdata)  
                     {  
                          if (current.left==null)  
                          {  
                               current.left = newnode;  
                               break;  
                          }  
                          current = current.left;  
                     }  
                     if (current.data<tdata)  
                     {  
                          if (current.right==null)  
                          {  
                               current.right = newnode;  
                               break;  
                          }  
                               current = current.right;  
                     }  
                }  
           }  
      }  
 /*====================================================================**/  
 public void preorder(TNode temp)  
      {  
           if (temp!=null)  
           {  
                System.out.println(temp.data);  
                preorder(temp.left);  
                preorder(temp.right);  
           }  
           else  
                return;  
      }  
 /*====================================================================**/  
 public void inorder(TNode temp)  
      {  
           if (temp!=null)  
           {  
                inorder(temp.left);  
                System.out.println(temp.data);  
                inorder(temp.right);  
           }  
           else  
                return;  
      }  
 /*====================================================================**/  
 public void postorder(TNode temp)  
      {  
           if (temp!=null)  
           {  
                postorder(temp.left);  
                postorder(temp.right);  
                System.out.println(temp.data);  
           }  
           else  
                return;  
      }  
 /*====================================================================**/  
 public static void main(String[] args)   
      {  
           MyBinarySearchTree obj = new MyBinarySearchTree();  
           obj.insert(5);obj.insert(4);obj.insert(7);  
           obj.insert(1);obj.insert(6);obj.insert(9);  
           obj.insert(3);obj.insert(2);  
           System.out.println(":::::::::::::: preorder :::::::::::::::\n");  
           obj.preorder(obj.root);  
           System.out.println(":::::::::::::: Inorder :::::::::::::::\n");  
           obj.inorder(obj.root);  
           System.out.println(":::::::::::::: Postorder :::::::::::::::\n");  
           obj.postorder(obj.root);  
      }  
 }  

Post a Comment

0 Comments