Advertisement
MarMar_IV

bin-tree insert - non recursive

Jul 8th, 2015
176
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 1.57 KB | None | 0 0
  1.         public void insert(T val) {
  2.             if (this.head == null){
  3.                 this.head = new Leave<T>(val);
  4.                 this.head.setAsHead();
  5.             } else {
  6.                 Leave<T> current = this.head;
  7.  
  8.                 int c = 0;
  9.                 while(c == 0)
  10.                 {
  11.                     T cv = current.getValue();
  12.  
  13.                     if (current.haveChildrens())
  14.                     {                        
  15.                         Leave<T> L = current.getLeave_L();
  16.                         Leave<T> R = current.getLeave_R();
  17.  
  18.                         if (L != null)
  19.                         {
  20.                             if (L.getValue().CompareTo(cv) < 0)
  21.                             {
  22.                                 current = L;
  23.                             }
  24.                         }
  25.                         else if (R != null)
  26.                         {
  27.                             if (R.getValue().CompareTo(cv) < 0)
  28.                             {
  29.                                 current = R;
  30.                             }
  31.                         }
  32.                     }
  33.                     else {
  34.                         int comp = val.CompareTo(cv);
  35.                         if (comp < 0)
  36.                         {
  37.                             current.setLeave_L(val);
  38.                         }
  39.                         else
  40.                         {
  41.                             current.setLeave_R(val);
  42.                         }
  43.                         c++;
  44.                     }
  45.                 }
  46.             }
  47.         }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement