BINARY SEARCH TREE INSERT METHOD IN JAVA

 /**================BINARY SEARCH TREE 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;  
                     }  
                }  
           }  
      }  
 /*====================================================================**/  

Post a Comment

0 Comments