BST Delete Operation in DSA C - Time & Space Complexity
We want to understand how the time to delete a node in a Binary Search Tree changes as the tree grows.
Specifically, how does the number of steps depend on the number of nodes in the tree?
Analyze the time complexity of the following code snippet.
// Delete a node from BST
Node* deleteNode(Node* root, int key) {
if (root == NULL) return root;
if (key < root->val) {
root->left = deleteNode(root->left, key);
} else if (key > root->val) {
root->right = deleteNode(root->right, key);
} else {
// Node found: handle 3 cases
if (root->left == NULL) {
Node* temp = root->right;
free(root);
return temp;
} else if (root->right == NULL) {
Node* temp = root->left;
free(root);
return temp;
}
// Node with two children: find inorder successor
Node* temp = minValueNode(root->right);
root->val = temp->val;
root->right = deleteNode(root->right, temp->val);
}
return root;
}
This code deletes a node with a given key from a BST by searching for it and then removing it while maintaining BST properties.
Identify the loops, recursion, array traversals that repeat.
- Primary operation: Recursive traversal down the tree to find the node to delete.
- How many times: At most once per level of the tree, from root to leaf.
- Additional operation: Finding the inorder successor involves traversing down the right subtree's leftmost path.
As the tree grows, the number of steps depends on the height of the tree.
| Input Size (n) | Approx. Operations (steps down tree) |
|---|---|
| 10 | Up to 4 steps (if tree is balanced) |
| 100 | Up to 7 steps |
| 1000 | Up to 10 steps |
Pattern observation: The steps grow slowly as the tree grows, roughly proportional to the tree height.
Time Complexity: O(h) where h is the height of the BST.
This means the time to delete depends on how tall the tree is, not just how many nodes it has.
[X] Wrong: "Deleting a node always takes the same time regardless of tree shape."
[OK] Correct: If the tree is unbalanced and looks like a list, the height is large, so deletion takes longer. Balanced trees keep height small, making deletion faster.
Understanding how tree height affects deletion helps you explain efficiency clearly and shows you know how data structure shape impacts performance.
"What if the BST is always balanced, like an AVL tree? How would the time complexity change?"
Practice
Solution
Step 1: Understand leaf node deletion
A leaf node has no children, so removing it does not affect other nodes.Step 2: Update parent's pointer
After removal, the parent's pointer to this node is set to null to disconnect it.Final Answer:
The node is simply removed and its parent's pointer is set to null. -> Option DQuick Check:
Leaf node deletion = remove node and set parent's pointer null [OK]
- Replacing leaf node with child when none exists
- Restructuring entire tree unnecessarily
- Confusing leaf node with internal node deletion
key from a BST in C?Solution
Step 1: Understand function call and assignment
Deleting a node may change the root, so the returned root must be assigned back.Step 2: Check syntax correctness
root = deleteNode(root, key); correctly assigns the returned root from deleteNode to root.Final Answer:
root = deleteNode(root, key); -> Option AQuick Check:
Assign returned root after deletion [OK]
- Not assigning returned root causing lost tree updates
- Using incorrect function names
- Using object-oriented syntax in C
20
/ \
10 30
\ \
15 40
root = deleteNode(root, 15);
What will be the in-order traversal of the BST after deletion?Solution
Step 1: Delete node 15 from BST
Node 15 is a leaf node, so it is removed directly.Step 2: Perform in-order traversal after deletion
In-order traversal visits nodes in ascending order: 10, 20, 30, 40.Final Answer:
[10, 20, 30, 40] -> Option CQuick Check:
In-order after deleting 15 = [10, 20, 30, 40] [OK]
- Including deleted node in traversal
- Confusing traversal order
- Assuming node replaced incorrectly
struct Node* deleteNode(struct Node* root, int key) {
if (root == NULL) return root;
if (key < root->data)
deleteNode(root->left, key);
else if (key > root->data)
deleteNode(root->right, key);
else {
// deletion logic here
}
return root;
}
What is the main bug in this code?Solution
Step 1: Analyze recursive calls
Recursive calls are made but their results are not assigned back to root->left or root->right.Step 2: Understand pointer update necessity
Without updating pointers, the tree structure won't change after deletion.Final Answer:
The recursive calls do not update root->left or root->right pointers. -> Option BQuick Check:
Update child pointers with recursive call results [OK]
- Ignoring pointer updates after recursion
- Missing base case for NULL root
- Confusing return types
Solution
Step 1: Identify node to replace root on deletion
When deleting a node with two children, replace it with the inorder successor (minimum in right subtree).Step 2: Find minimum node in right subtree of 50
Right subtree nodes: 70, 60, 80. Minimum is 60.Final Answer:
The minimum node from the right subtree (60) -> Option AQuick Check:
Root replacement = inorder successor (min right subtree) [OK]
- Replacing with max left subtree instead of min right
- Replacing with direct child without checking BST rules
- Confusing inorder predecessor and successor
Ready to prove it?
Pass the test and this pattern moves unknown
