TreesAnimated
BST Deletion
Remove a leaf, bypass a single child, or replace with the in-order successor.
Time
O(h)
Extra space
O(1)
Use it for
Maintaining BSTs.
How it works
- Find the node to delete.
- Leaf: just remove it. One child: replace the node by that child.
- Two children: find the in-order successor (the smallest key in the right subtree), copy its key into the node, then delete the successor from the right subtree.
- The tree stays a valid BST. O(height).
Watch it run
Loading animation…