Intermediate · Module 13 · Lesson 4

BST operations

Search, insert, delete, predecessor, and successor.

15–25 min readLesson 73 of 123 Interactive Lab

Lesson 73 of 123

1d streak

Interactive Visualization

See how bst operations works

Change the inputs, run an operation, and connect each visible state change to the explanation below.

Interactive data structure

Change the structure and watch the operation

Try an operation and connect the visible movement to its runtime cost.

40221230655278
readyAt every node: go left for a smaller value and right for a larger value.

BST Node Deletion: The 3 Critical Cases

Deleting a node from a BST is one of the most frequently tested interview operations. It requires handling three distinct topological cases to preserve the BST invariant.

Visualizing the 3 Deletion Cases
CASE 1: Node has NO children (Leaf Node):
Simply unlink and return null.
    [ 5 ]                 [ 5 ]
   /     \       ->      /
 [ 2 ]   [ 8 ]         [ 2 ]  (8 removed)

CASE 2: Node has ONE child:
Bypass the node and connect parent directly to child.
    [ 5 ]                 [ 5 ]
   /     \       ->      /     \
 [ 2 ]   [ 8 ]         [ 2 ]   [ 9 ]  (8 replaced with 9)
           \
           [ 9 ]

CASE 3: Node has TWO children:
Find the IN-ORDER SUCCESSOR (smallest node in the right subtree).
Copy successor's value into the target node, then delete the successor!
Target node to delete: [ 5 ]
         [ 5 ]                        [ 6 ] (Successor copied!)
       /       \                    /       \
    [ 2 ]     [ 8 ]       ->      [ 2 ]     [ 8 ]
             /     \                        /     \
   Successor:[ 6 ] [ 9 ]                 (deleted) [ 9 ]
BstDelete.java
public TreeNode deleteNode(TreeNode root, int key) {
    if (root == null) return null;

    if (key < root.val) {
        root.left = deleteNode(root.left, key);
    } else if (key > root.val) {
        root.right = deleteNode(root.right, key);
    } else {
        // Found the node to delete!
        // Case 1 & 2: Zero or one child
        if (root.left == null) return root.right;
        if (root.right == null) return root.left;

        // Case 3: Two children
        // Find in-order successor (minimum in right subtree)
        TreeNode successor = findMin(root.right);
        root.val = successor.val; // Copy successor's value
        // Recursively delete the successor from the right subtree
        root.right = deleteNode(root.right, successor.val);
    }
    return root;
}

private TreeNode findMin(TreeNode node) {
    while (node.left != null) {
        node = node.left;
    }
    return node;
}

Cost Model

Complexity & Trade-offs

Search / insert / delete
O(h)
O(h) recursive
The bound follows from the number of states visited and the work performed per transition.

When to use it

  • Hierarchies, ordered sets/maps, expression trees, and recursive aggregation.
  • Path, ancestor, depth, and subtree questions.
  • Maintaining sorted dynamic data with balanced trees.

Common mistakes

  • Assuming a BST is balanced without a balancing guarantee.
  • Confusing node depth with subtree height.
  • Discarding information needed by the parent instead of returning it.

Practice prompts

  • Reproduce the worked BST operations example without looking at the trace.
  • Change one input condition and explain whether the BST operations invariant still holds.
  • Implement the approach, derive its cost, and compare it with a straightforward baseline.

Mastery check

Can you derive it instead of memorizing it?

State the invariant before looking at code.
Explain why every transition is safe.
Construct a worst-case input.
Name one alternative and its trade-off.