Пример #1
0
 /**
  * Inserts the value into the binary search tree
  */
 public void insert(int number)
 {
     if (root == null)
     {
         root = new BSTNode(number);
     }
     else
     {
         root.insert(number);
     }
 }
Пример #2
0
 public BSTNode delete(int number)
 {
     if (number < this.number)
     {
         if (this.left != null)
         {
             this.left = this.left.delete(number);
         }
         return this;
     }
     else if (number > this.number)
     {
         if (this.right != null)
         {
             this.right = this.right.delete(number);
         }
         return this;
     }
     else
     {
         if (this.left == null)
         {
             return this.right;
         }
         else if (this.right == null)
         {
             return this.left;
         }
         else
         {
             this.number = this.right.min();
             this.right = right.delete(this.number);
             return this;
         }
     }
 }
Пример #3
0
 public void insert(int number)
 {
     if (number < this.number)
     {
         // Smaller value, insert it into the left subtree
         if (this.left == null)
             this.left = new BSTNode(number);
         else
             this.left.insert(number);
     }
     else
     {
         // Larger value, insert it in the right subtree
         if (this.right == null)
             this.right = new BSTNode(number);
         else
             this.right.insert(number);
     }
 }
Пример #4
0
 /**
  * Delete a number from the tree (if it exists)
  */
 public void delete(int number)
 {
     if(root != null ) root = root.delete(number);
 }