Delete a BST key whose node has two children
Question
A shop keeps order ids in a binary search tree (BST). Keys are distinct. The starter builds this tree by inserting 50, 20, 80, 60, 90, 55, 70, 58:
50
/ \
20 80
/ \
60 90
/ \
55 70
\
58Write delete(root, key). It removes key if it is in the tree and returns the new root. The tree must stay a BST and keep every other key.
What does the tree look like after delete(root, 50)? Where does 58 end up?
class Node: def __init__(self, key): self.key, self.left, self.right = key, None, None def insert(root, key): if root is None: return Node(key) if key < root.key: root.left = insert(root.left, key) else: root.right = insert(root.right, key) return root def preorder(node): if node is None: return [] return [node.key] + preorder(node.left) + preorder(node.right) def delete(root, key): ... root = None for key in [50, 20, 80, 60, 90, 55, 70, 58]: root = insert(root, key)
Show hints (3)
- "EASY CASES FIRST"
A leaf can simply go. A node with one child is replaced by that child. Only a node with two children needs real thought.
- "WHO TAKES ITS PLACE"
The replacement key must be larger than everything on the left and smaller than everything else on the right. The smallest key in the right subtree fits. Here that is 55.
- "DON'T DROP 58"
Copy 55 into the root, then delete 55 from the right subtree. 55 has no left child, so it is an easy case: replace it with its right child, 58.
Show checked explanation
For a node with two children, find the smallest key in its right subtree, called the successor. Copy that key into the node, then delete the successor from the right subtree. The successor has no left child, so its right child simply takes its place. Always return the subtree's root and store it.
def delete(root, key): if root is None: return None # key not in the tree if key < root.key: root.left = delete(root.left, key) elif key > root.key: root.right = delete(root.right, key) elif root.left is None or root.right is None: return root.left or root.right # 0 or 1 child else: # 2 children successor = root.right while successor.left is not None: successor = successor.left root.key = successor.key root.right = delete(root.right, successor.key) return root root = delete(root, 50) # root comes from the starter print(preorder(root))
[55, 20, 80, 60, 58, 70, 90]
Code run on CPython 3.12 with the starter above. Checked by an editor other than the author.
Walkthrough
The solution runs after the starter, which builds the tree and defines preorder. Deleting 50 takes three steps.
- Find the successor. Go right once, to 80, then left as far as possible: 60, then 55. 55 has no left child, so it is the smallest key on the right.
- Copy it up. The root's key becomes 55. 55 is larger than 20, the whole left side. It is smaller than 58, 60, 70, 80 and 90, everything else on the right.
- Remove the old 55. Call
delete(root.right, 55). At 55 there is one child, 58, so the call returns 58. One level up,root.left = ...makes 58 the left child of 60.
The result:
55
/ \
20 80
/ \
60 90
/ \
58 70Its preorder is [55, 20, 80, 60, 58, 70, 90]. Sorted, the keys are 20, 55, 58, 60, 70, 80, 90: all of them except 50.
Why is step 3 safe? The successor never has a left child: that child would be smaller, so the successor would not be the smallest. Removing it is always the zero or one child case. It can have a right child, like 58 here.
Every call returns the root of its subtree, and the parent stores it. A removed leaf returns None, and a removed node with one child returns that child. This covers the root too, so the caller writes root = delete(root, key).
The cost is O(h) for a tree of height h: one walk down to the key, then one walk down its right subtree.
Common mistakes
- Setting the successor's parent link to
None. 60 loses its left child, and 58 disappears with it. - Calling
delete(root, key)without storing the result. Deleting the only node returnsNone, butrootstill points at the deleted node. - Updating the wrong link of the successor's parent. Delete 80: its successor, 90, is its own right child. Code that always sets
parent.left = successor.rightcuts off 60, 55, 58 and 70. - Copying only the key when nodes also hold data, such as an order's price. The new key then sits next to the old price.
Follow-up questions
- "Could you use the predecessor instead?" Yes. The predecessor is the largest key in the left subtree. Here it is 20, a leaf. Copy 20 into the root and remove the old 20. The preorder becomes
[20, 80, 60, 55, 58, 70, 90]. Both trees are valid BSTs. - "How fast is delete on a big tree?" O(h). In a balanced tree with n keys, h is about log₂ n, so a million keys need about 20 levels. Plain delete never rebalances. Sorted inserts build a chain where h equals n.
What a strong answer covers
Handles zero, one and two children with one recursive function that returns the new subtree root. Explains why the successor fits and why it has no left child. Keeps the successor's right child. Mentions the root case and O(h).
Open in interactive practice deck →