public BSTNode delete(int value) { if (value < number) { if (left != null) { left = left.delete(value); } return this; } if (value > number) { if (right !=null) { right = right.delete(value); } return this; } if (left == null && right == null) { return null; } if(left == null) { return right; } if (right == null) { return left; } this.number = left.max(); left.delete(this.number); return this; }
/** * Delete a number from the tree (if it exists) */ public void delete(int number) { if (root == null) { return; } root = root.delete(number); }
/** * 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 int depth(BSTNode aNode) { if(aNode == null) return 0; int lefth = depth(aNode.left); int righth = depth(aNode.right); if (lefth > righth) return lefth + 1; else return righth + 1; }
/** * Add a number in the subtree of this node */ 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); } } }