Esempio n. 1
0
 private BinaryNode <T> Remove(T x, BinaryNode <T> root)
 {
     if (root == null)
     {
         throw new NoNullAllowedException("Item does not exist");
     }
     if (x.CompareTo(root.getElement()) < 0)
     {
         root.setLeft(Remove(x, root.getleft()));
     }
     else if (x.CompareTo(root.getElement()) < 0)
     {
         root.setRight(Remove(x, root.getRight()));
     }
     else if (root.getleft() != null && root.getRight() != null)
     {
         root.setElement(FindMin(root.getRight()).getElement());
         root.setRight(RemoveMin(root.getRight()));
     }
     else
     {
         root = (root.getleft() != null) ? root.getleft() : root.getRight();
     }
     return(root);
 }
Esempio n. 2
0
 private BinaryNode <T> FindMin(BinaryNode <T> root)
 {
     if (root != null)
     {
         while (root.getleft() != null)
         {
             root = root.getleft();
         }
     }
     return(root);
 }
Esempio n. 3
0
 private BinaryNode <T> RemoveMin(BinaryNode <T> root)
 {
     if (root == null)
     {
         throw new NoNullAllowedException("Item does not exist");
     }
     else if (root.getleft() != null)
     {
         root.setLeft(RemoveMin(root.getleft()));
         return(root);
     }
     else
     {
         return(root.getRight());
     }
 }
Esempio n. 4
0
 private BinaryNode <T> Find(T x, BinaryNode <T> root)
 {
     while (root != null)
     {
         if (x.CompareTo(root.getElement()) < 0)
         {
             root = root.getleft();
         }
         else if (x.CompareTo(root.getElement()) > 0)
         {
             root = root.getRight();
         }
         else
         {
             return(root);
         }
     }
     return(null);
 }