Exemple #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);
     }
 }
 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;
         }
     }
 }
 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);
     }
 }
Exemple #4
0
 /**
  * Delete a number from the tree (if it exists)
  */
 public void delete(int number)
 {
     if(root != null ) root = root.delete(number);
 }