BST delete; successor and predecessor
Deleting from a BST has three cases: a leaf, one child, or two children replaced by the in-order successor. Find successors by going right, then left.
Before you start: BST: search, insert, min and max.
The idea
Removing a leaf is easy. But if 3 goes, what holds 1 and 6 under 8?
The tree must still be a BST after the delete. So the gap needs a key that fits between the left and the right subtree of 3.
Intuition
A delete has three cases. The node has no child, one child, or two children.
With two children, use the successor: the next key in sorted order. It is the leftmost key of the right subtree.
In sorted order the keys read 1, 3, 4, 6, 7, 8. The key right after 3 is 4. It is bigger than everything on the left and smaller than the rest on the right.
Complexity
Delete walks down to the key, then on to its successor. Both walks lie on one path, so delete costs O(h).
h is about log2 n in a balanced tree but can be n in a chain. The recursive code also uses O(h) stack space; a loop version needs O(1).
Pitfalls
In the original tree, deleting 8 uses 10, so 14 must move up. A successor never has a left child but may have a right one.
The code handles this by deleting the successor with the same function. That call hits the one-child case.
A common bug returns None for a node with one child. Deleting 14 then drops its whole subtree, so 13 is lost too.
The correct code returns the one child (lines 9 to 12), so 13 moves up into the place of 14.
Variations
The predecessor works too: the largest key of the left subtree. Go left once, then right to the end.
Some libraries alternate between the two, so repeated deletes do not tilt the tree to one side.
Code
Pseudocode
delete(node, key)
if node = null: return null
if key < node.val: node.left ← delete(node.left, key)
else if key > node.val: node.right ← delete(node.right, key)
else
if node.left = null: return node.right
if node.right = null: return node.left
s ← node.right
while s.left ≠ null: s ← s.left
node.val ← s.val
node.right ← delete(node.right, s.val)
return node
Python
def delete_node(root, key):
if root is None:
return None
if key < root.val:
root.left = delete_node(root.left, key)
elif key > root.val:
root.right = delete_node(root.right, key)
else:
if root.left is None:
return root.right
if root.right is None:
return root.left
succ = root.right
while succ.left:
succ = succ.left
root.val = succ.val
root.right = delete_node(root.right, succ.val)
return root
JavaScript
function deleteNode(root, 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 {
if (root.left === null) return root.right;
if (root.right === null) return root.left;
let succ = root.right;
while (succ.left !== null) succ = succ.left;
root.val = succ.val;
root.right = deleteNode(root.right, succ.val);
}
return root;
}
Practice problems (7)
Pick the tree after a delete Warm-up
Delete 6 from the BST with level order [8, 3, 10, 1, 6, null, 14, null, null, 4, 7], using its in-order successor. Pick the resulting tree.
Successor without a right child Warm-up
What is the in-order successor of 7 in this tree? Answer with a key.
Delete a node with two children Standard
Delete 3 from this BST. 3 has two children, so its successor takes its place.
Answer each question before the step plays.
Implement delete Standard
Delete the node with key key from the BST with the given root and return the root of the result. If key is not in the tree, return the tree unchanged.
When the node has two children, copy the key of its in-order successor (the smallest key in its right subtree) into it, then delete the successor from the right subtree. The tests expect exactly this shape.
Successor Standard
Given the root of a BST and a number x, return the smallest key in the tree that is bigger than x. Return -1 if no key is bigger. x does not have to be in the tree.
Spot the bug: lost subtree Standard
This delete loses keys for some inputs. Click the line with the bug, then fix it.
Delete with the predecessor instead Challenge
Delete the node with key key from the BST with the given root and return the root of the result. If key is not in the tree, return the tree unchanged.
When the node has two children, copy the key of its in-order predecessor (the largest key in its left subtree) into it, then delete the predecessor from the left subtree. The tests expect exactly this shape.
Related topics
Next on the path: Validating a BST and the in-order property