Sub topic 12
Deleting from a Binary Search Tree
Assume you know recursion, object references, linked lists, and Big-O notation. Let the BST contain distinct keys. Its property is:
- Every key in the left subtree is smaller than the node’s key.
- Every key in the right subtree is larger.
Deletion must remove one key while preserving this property.
The three deletion cases
To delete key x, first search for the node containing x.
Case 1: The node is a leaf
A leaf has no children. Remove it by returning None to its parent.
Case 2: The node has one child
Replace the node with its only child. The child can be either on the left or the right.
Case 3: The node has two children
The node cannot be removed directly because both subtrees must remain connected. Replace its key with a nearby key that preserves ordering:
- The inorder successor is the smallest key in the right subtree.
- The inorder predecessor is the largest key in the left subtree.
This chapter uses the inorder successor. After copying the successor's key into the target node, delete the successor from the right subtree. The successor has no left child, so that second deletion is Case 1 or Case 2.
Recursive implementation
The function returns the new root of the subtree. This return value is essential when deletion removes the subtree root or replaces it with a child.
class Node:
def __init__(self, key):
self.key = key
self.left = None
self.right = None
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:
# Case 1 or 2: zero or one child
if node.left is None:
return node.right
if node.right is None:
return node.left
# Case 3: two children
successor = node.right
while successor.left is not None:
successor = successor.left
node.key = successor.key
node.right = delete(node.right, successor.key)
return node
def inorder(node):
if node is None:
return []
return inorder(node.left) + [node.key] + inorder(node.right)
root = Node(50)
root.left = Node(30)
root.right = Node(70)
root.right.left = Node(60)
root.right.right = Node(80)
root = delete(root, 50)
print(inorder(root)) # [30, 60, 70, 80]The original root key 50 becomes 60. The original node containing 60 is then removed from the right subtree.
Invariants and common errors
After every recursive return, the subtree must still be a valid BST. Common mistakes include:
- Forgetting to assign
node.left = delete(...)ornode.right = delete(...). - Returning the old node after deleting its only child.
- Choosing an arbitrary node from the right subtree instead of its minimum.
- Copying only a key when the node also stores associated data. In that situation, copy the complete record or move the successor node consistently.
- Treating a missing key as an error when the intended behavior is to leave the tree unchanged.
| Case | Replacement returned to the parent | Extra search |
|---|---|---|
| Leaf | `None` | None |
| One child | The existing child | None |
| Two children | The original node after key replacement | Find successor |
Cost
If the tree height is h, searching for the key and finding the successor both take O(h) time. Deletion therefore costs O(h) time and O(h) stack space in this recursive version.
For a balanced tree, h = O(log n). For a skewed tree, h = O(n). Thus an ordinary BST does not guarantee logarithmic deletion time.
Exercise
A node with key 40 has a left child but no right child. What should the deletion algorithm return when it finds this node?
Show answer
It should return the left child. The parent assigns that returned child to its appropriate link, replacing the deleted node.