What is BST Deletion?
Deleting from a Binary Search Tree starts the same way insertion does — you search for the value by comparing and moving left or right — but once you find it, keeping the tree a valid BST afterward takes more care than insertion ever did, because removing a node can leave a gap that needs to be patched correctly.
How Does It Work?
There are exactly three shapes the node-to-delete can have, and each is handled differently: a leaf is simply removed, a node with one child is replaced by that child, and a node with two children is trickier — you can't just delete it without breaking the ordering, so you borrow a replacement value from elsewhere in the tree.
Algorithm Steps
- Search for the value the same way you would for lookup
- Once found, check how many children the node has:
- Zero children → remove it directly
- One child → replace the node with that child
- Two children → find the in-order successor, copy its value up, then delete the successor
- Return the (possibly modified) subtree to the parent call
Time Complexity
- Best/Average Case: Roughly balanced tree → O(log n).
- Worst Case: Degenerate/skewed tree → O(n).
Time Complexity Analysis
Space Complexity
Deletion needs O(1) extra space beyond the recursion stack — no matter which of the three cases applies, only a constant number of pointers get rewired.
Repeated deletions (and insertions) can gradually unbalance a BST even if it started out balanced, which is exactly the problem self-balancing trees like AVL and Red-Black trees are designed to prevent — they perform extra rotation work on every insert/delete specifically to keep the height close to log n.