Deletion in BST in Data Structures Theory - Time & Space Complexity
Start learning this pattern below
Jump into concepts and practice - no test required
When deleting a node in a Binary Search Tree (BST), it is important to understand how the time taken grows as the tree size increases.
We want to know how the number of steps changes when the tree has more nodes.
Analyze the time complexity of the following deletion process in a BST.
function deleteNode(root, key) {
if (root == null) return null;
if (key < root.value) {
root.left = deleteNode(root.left, key);
} else if (key > root.value) {
root.right = deleteNode(root.right, key);
} else {
// Node found: handle 3 cases here
// 1. No child, 2. One child, 3. Two children
}
return root;
}
This code searches for the node with the given key and deletes it by adjusting pointers depending on the node's children.
Identify the loops, recursion, array traversals that repeat.
- Primary operation: Recursive traversal down the tree to find the node.
- How many times: At most once per level of the tree, from root to leaf.
As the tree grows, the number of steps depends on the height of the tree.
| Input Size (n) | Approx. Operations (steps down tree) |
|---|---|
| 10 | About 3 to 4 steps |
| 100 | About 6 to 7 steps |
| 1000 | About 9 to 10 steps |
Pattern observation: The steps grow roughly with the height of the tree, which is about log of the number of nodes if the tree is balanced.
Time Complexity: O(h) where h is the tree height
This means the time to delete depends on how tall the tree is, not just how many nodes it has.
[X] Wrong: "Deletion always takes the same time regardless of tree shape."
[OK] Correct: If the tree is very unbalanced (like a linked list), deletion can take much longer because you must go through many nodes.
Understanding deletion time helps you explain how tree shape affects performance, a key skill in data structure questions.
"What if the BST is always perfectly balanced? How would that change the deletion time complexity?"
Practice
Solution
Step 1: Understand BST node children possibilities
In a BST, each node can have zero, one, or two children. There is no case of three children because BST nodes have at most two children.Step 2: Identify valid deletion cases
Deletion handles nodes with zero, one, or two children. Three children is impossible, so it is not a valid case.Final Answer:
Deleting a node with three children -> Option CQuick Check:
BST nodes have max two children = Deleting node with three children [OK]
- Thinking a node can have more than two children
- Confusing deletion cases with other tree types
- Ignoring the leaf node deletion case
Solution
Step 1: Identify deletion method for two children
When deleting a node with two children, the standard method is to replace it with its inorder successor, which is the smallest node in its right subtree.Step 2: Confirm replacement correctness
The inorder successor maintains BST properties after replacement, unlike arbitrary leaf or parent nodes.Final Answer:
Replace the node with its inorder successor (smallest in right subtree) -> Option DQuick Check:
Two children deletion uses inorder successor = Replace the node with its inorder successor (smallest in right subtree) [OK]
- Using inorder predecessor instead of successor
- Replacing with any leaf node
- Replacing with parent node
15
/ \
10 20
/ / \
8 17 25If we delete node
20, what will be the new right child of 15?Solution
Step 1: Identify node to delete and its children
Node 20 has two children: 17 (left) and 25 (right).Step 2: Find inorder successor of node 20
The inorder successor is the smallest node in the right subtree of 20, which is 25. But since 25 has no left child, 25 is the successor.Step 3: Replace node 20 with its inorder successor
Replace 20 with 25. Then remove original 25 node (which is a leaf). The new right child of 15 becomes 25.Final Answer:
25 -> Option BQuick Check:
Inorder successor of 20 is 25 = 25 [OK]
- Choosing 17 as successor instead of 25
- Not updating parent's child pointer
- Confusing inorder predecessor with successor
if node.left is not None:
return node.left
else:
return node.rightSolution
Step 1: Analyze deletion logic for one child
The code returns the child node directly but does not update the parent's pointer to this child, which breaks the BST links.Step 2: Identify missing link update
Proper deletion requires updating the parent's child pointer to the returned node to maintain tree structure.Final Answer:
It incorrectly returns the child without updating parent links -> Option AQuick Check:
Missing parent link update = It incorrectly returns the child without updating parent links [OK]
- Ignoring parent pointer updates
- Deleting both children unnecessarily
- Using inorder successor for one-child deletion
Solution
Step 1: Replace root with inorder successor
The root is replaced by its inorder successor, which is the immediate right child with no left child.Step 2: Adjust inorder successor's original position
Since the inorder successor has no left child, replace it with its right child (which may be None) to maintain BST links.Final Answer:
Replace the inorder successor with its right child -> Option AQuick Check:
Inorder successor replaced by right child = Replace the inorder successor with its right child [OK]
- Forgetting to remove successor from original spot
- Replacing successor with left child (which doesn't exist)
- Assuming no further action needed
