BST Delete Operation in DSA Javascript - Time & Space Complexity
We want to understand how long it takes to remove a value from a Binary Search Tree (BST).
Specifically, how the time changes as the tree grows bigger.
Analyze the time complexity of the following code snippet.
function deleteNode(root, key) {
if (!root) return null;
if (key < root.val) {
root.left = deleteNode(root.left, key);
} else if (key > root.val) {
root.right = deleteNode(root.right, key);
} else {
if (!root.left) return root.right;
if (!root.right) return root.left;
let minLargerNode = root.right;
while (minLargerNode.left) minLargerNode = minLargerNode.left;
root.val = minLargerNode.val;
root.right = deleteNode(root.right, minLargerNode.val);
}
return root;
}
This code removes a node with a given key from a BST, adjusting the tree to keep its order.
- Primary operation: Recursive search down the tree to find the node to delete.
- How many times: At most once per tree level, moving left or right.
- Additional operation: Finding the smallest node in the right subtree (while loop) when deleting a node with two children.
As the tree grows taller, the number of steps to find and delete a node grows roughly with the tree's height.
| Input Size (n) | Approx. Operations (tree height) |
|---|---|
| 10 | About 3 to 4 steps |
| 100 | About 6 to 7 steps |
| 1000 | About 9 to 10 steps |
Pattern observation: The steps grow slowly as the tree grows, depending on how balanced it is.
Time Complexity: O(h)
This means the time to delete depends on the tree height, which is the number of levels from root to leaf.
[X] Wrong: "Deleting a node always takes the same time no matter the tree size."
[OK] Correct: The time depends on how tall the tree is. A taller tree means more steps to find and fix nodes.
Understanding how delete works in BSTs helps you show you know tree operations well, a common topic in interviews.
"What if the BST is always perfectly balanced? How would that affect the delete operation's time complexity?"
Practice
Solution
Step 1: Understand leaf node deletion
A leaf node has no children, so removing it does not affect other nodes.Step 2: Effect on BST structure
Since no children exist, simply remove the node and update its parent's pointer to null.Final Answer:
The node is simply removed without affecting other nodes. -> Option AQuick Check:
Leaf node deletion = simple removal [OK]
- Thinking children replace the node when none exist
- Assuming root always changes
- Believing tree rebuild is needed
key from a BST in JavaScript?Solution
Step 1: Identify common BST delete method
In many JavaScript BST implementations, the method to delete a node is nameddelete.Step 2: Check method naming conventions
Methods likeremove,erase, ordeleteNodeare not standard or common in JavaScript BST examples.Final Answer:
bst.delete(key); -> Option BQuick Check:
Standard BST delete method = delete() [OK]
- Using remove() or erase() which are not standard
- Confusing delete keyword with method name
- Using deleteNode() which is uncommon
10
/ \
5 15
/ \
12 20
What will be the in-order traversal output after deleting node with value 15?Solution
Step 1: Identify node to delete and its children
Node 15 has two children: 12 (left) and 20 (right).Step 2: Replace node 15 with its in-order successor
The in-order successor is the smallest node in right subtree, which is 20. Replace 15 with 20 and delete original 20 node.Step 3: Perform in-order traversal
In-order traversal visits nodes in ascending order: 5, 10, 12, 20.Final Answer:
[5, 10, 12, 20] -> Option CQuick Check:
In-order after deleting 15 = [5, 10, 12, 20] [OK]
- Removing 15 without replacement
- Using in-order predecessor instead of successor
- Forgetting to delete the successor node
function deleteNode(root, key) {
if (!root) return null;
if (key < root.val) root.left = deleteNode(root.left, key);
else if (key > root.val) root.right = deleteNode(root.right, key);
else {
if (!root.left) return root.right;
else if (!root.right) return root.left;
let minNode = findMin(root.right);
root.val = minNode.val;
// Missing deletion of minNode here
}
return root;
}
function findMin(node) {
while (node.left) node = node.left;
return node;
}Solution
Step 1: Analyze deletion of node with two children
When deleting a node with two children, we replace its value with the in-order successor's value.Step 2: Check if in-order successor node is deleted
The code copies the successor's value but does not delete the successor node itself, causing duplicate values and incorrect tree.Final Answer:
The code does not delete the in-order successor node after copying its value. -> Option DQuick Check:
Must delete successor node after value copy [OK]
- Forgetting to delete the in-order successor node
- Missing base case (though present here)
- Incorrectly handling leaf nodes
- Using wrong comparison operators
After deleting node 3, what is the correct in-order traversal of the BST?
Solution
Step 1: Identify node 3 and its children
Node 3 has two children: 1 (left) and 6 (right). 6 has children 4 and 7.Step 2: Delete node 3 by replacing with in-order successor
The in-order successor of 3 is 4 (smallest in right subtree). Replace 3 with 4, then delete original 4 node.Step 3: Perform in-order traversal after deletion
In-order traversal visits nodes in ascending order: 1, 4, 6, 7, 8, 10, 13, 14.Final Answer:
[1, 4, 6, 7, 8, 10, 13, 14] -> Option AQuick Check:
In-order after deleting 3 = [1, 4, 6, 7, 8, 10, 13, 14] [OK]
- Forgetting to delete the in-order successor node
- Using in-order predecessor instead
- Skipping nodes in traversal
Ready to prove it?
Pass the test and this pattern moves unknown
