Sub topic 9
Deleting a key: leaf, one child, two children
This chapter assumes you know the BST ordering property, the Node class, and recursive insert and search. It also assumes you know how to find the minimum of a subtree by walking left.
Why deletion is harder
Insertion always adds a new leaf. Deletion removes a node from anywhere in the tree, and the remaining nodes must still satisfy the ordering property: every left descendant is smaller, and every right descendant is larger. The fix depends on how many children the node has.
The three cases
First, find the node by comparing keys, as in search. Then handle it by case:
- Leaf (no children). Remove it by returning
Noneto its parent. - One child. Replace the node with its only child. The child's whole subtree already sits on the correct side of the node's parent.
- Two children. You cannot promote either child, because the other would have no place to go. Instead, copy the in-order successor's key into the node. The successor is the minimum of the right subtree. Then delete the successor from the right subtree.
The successor works because it is larger than everything in the left subtree and not larger than anything else in the right subtree. The successor has no left child, so removing it falls under case 1 or 2.
Implementation
The function returns the new root of the subtree. The caller must store that return value.
class Node:
def __init__(self, key):
self.key = key
self.left = None
self.right = None
def insert(node, key):
if node is None:
return Node(key)
if key < node.key:
node.left = insert(node.left, key)
elif key > node.key:
node.right = insert(node.right, key)
return node
def minimum(node):
while node.left is not None:
node = node.left
return node
def delete(node, key):
if node is None:
return None
if key < node.key:
node.left = delete(node.left, key)
elif key > node.key:
node.right = delete(node.right, key)
else:
if node.left is None:
return node.right
if node.right is None:
return node.left
successor = minimum(node.right)
node.key = successor.key
node.right = delete(node.right, successor.key)
return node
def inorder(node):
if node is not None:
yield from inorder(node.left)
yield node.key
yield from inorder(node.right)
root = None
for k in [50, 30, 70, 20, 40, 60, 80]:
root = insert(root, k)
root = delete(root, 20) # leaf
root = delete(root, 30) # one child (40 remains)
root = delete(root, 50) # two children, successor is 60
print(list(inorder(root))) # [40, 60, 70, 80]Deleting 20 removes a leaf. Deleting 30 then leaves it with only the child 40, so 40 takes its place. Deleting 50 copies 60 (the minimum of the right subtree) into the root, then removes the original 60 leaf.
Common errors
- Dropping the return value. Writing
delete(root, 50)withoutroot = ...loses the change when the root itself is removed. Inside the function, the same mistake means the parent keeps pointing at the old node. - Forgetting to remove the successor. Copying its key without the final recursive delete leaves a duplicate key in the tree.
- Assuming the key exists. If the key is absent, the search reaches
Noneand the tree is returned unchanged. That is usually the right behavior, but check it deliberately. - Mixing up the two-child rule. The predecessor, which is the maximum of the left subtree, works equally well. Pick one and use it consistently.
Exercise
A BST was built by inserting 8, 4, 12, 2, 6, 10, 14 in that order. After you delete 8 using the in-order successor, which key is at the root, and which node loses a child?
Show answer
The root becomes 10. The successor is the minimum of the right subtree (12's left child, 10). Its key is copied into the root, then the original 10 leaf is removed, so node 12 loses its left child.