What are the three main cases to consider when deleting a node from a Binary Search Tree (BST)?
1. Node to delete has no children (leaf node). 2. Node to delete has one child. 3. Node to delete has two children.
Click to reveal answer
intermediate
In BST deletion, how do you handle deleting a node with two children?
Replace the node's value with its inorder successor (smallest value in right subtree) or inorder predecessor (largest value in left subtree), then delete that successor or predecessor node.
Click to reveal answer
intermediate
Why do we use the inorder successor or predecessor when deleting a node with two children in a BST?
Because replacing with inorder successor or predecessor maintains the BST property by ensuring all left subtree values remain smaller and right subtree values remain larger than the replaced node.
Click to reveal answer
beginner
What happens when you delete a leaf node in a BST?
The leaf node is simply removed by setting its parent's pointer to null (or None in Rust), as it has no children.
Click to reveal answer
beginner
How does deleting a node with one child work in a BST?
The node is removed and its child is linked directly to the node's parent, preserving the BST structure.
Click to reveal answer
Which node do you replace a node with when deleting a node with two children in a BST?
AParent node
BRoot node
CRandom leaf node
DInorder successor or inorder predecessor
✗ Incorrect
Replacing with inorder successor or predecessor maintains the BST property.
What is the first step when deleting a node from a BST?
ADelete all children first
BFind the node to delete
CReplace the node with root
DBalance the tree
✗ Incorrect
You must first find the node to delete before any operation.
If a node to delete has no children, what happens?
ARemove the node directly
BReplace with inorder successor
CReplace with inorder predecessor
DSwap with root
✗ Incorrect
Leaf nodes can be removed directly without further adjustments.
When deleting a node with one child, how is the BST structure maintained?
ALink the child directly to the node's parent
BDelete the entire subtree
CReplace with root node
DSwap with a leaf node
✗ Incorrect
Linking the child to the parent keeps the BST valid.
What property must be preserved after deleting a node in a BST?
AAll nodes have two children
BTree is balanced
CLeft subtree values < node < right subtree values
DRoot is always the smallest value
✗ Incorrect
The BST property requires left subtree values to be smaller and right subtree values to be larger than the node.
Explain the steps to delete a node with two children in a BST.
Think about how to keep the BST property intact.
You got /4 concepts.
Describe how deleting a node with one child differs from deleting a leaf node in a BST.
Focus on how the parent's pointer changes.
You got /3 concepts.
Practice
(1/5)
1. What happens when you delete a leaf node (a node with no children) from a Binary Search Tree (BST)?
easy
A. The entire tree is rebuilt from scratch.
B. The node's children replace it in the tree.
C. The node is simply removed with no further changes needed.
D. The tree becomes unbalanced and must be rebalanced immediately.
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, no rearrangement is needed; just remove the node.
Final Answer:
The node is simply removed with no further changes needed. -> Option C
Quick Check:
Leaf node deletion = simple removal [OK]
Hint: Leaf nodes delete by direct removal, no child rearrangement needed [OK]
Common Mistakes:
Thinking children must replace the node
Assuming tree rebalancing is always needed
Confusing leaf with internal node deletion
2. Which of the following Rust function signatures correctly represents a BST node deletion method that returns the updated tree root?
easy
A. fn delete_node(root: Option, key: i32) -> Option
B. fn delete_node(root: &Node, key: i32) -> Node
C. fn delete_node(root: Box<Node>, key: i32) -> Box<Node>
D. fn delete_node(root: &mut Node, key: i32) -> &Node
Solution
Step 1: Understand Rust ownership for BST nodes
BST nodes are often wrapped in Option to allow empty children and ownership transfer.
Step 2: Correct function signature for deletion
Deletion may change the root, so returning Option is needed to reflect updated tree.
Final Answer:
fn delete_node(root: Option, key: i32) -> Option -> Option A
Quick Check:
Ownership and optional root = Option [OK]
Hint: Use Option for nullable owned nodes in Rust [OK]
Common Mistakes:
Using references without ownership transfer
Returning plain Node instead of Option
Using &Node which is immutable reference
3. Given the BST with nodes [10, 5, 15, 3, 7, 12, 18], what is the in-order traversal output after deleting node 15?
medium
A. [3, 5, 7, 10, 12, 18]
B. [3, 5, 7, 10, 12]
C. [3, 5, 7, 10, 18]
D. [3, 5, 7, 10, 12, 15, 18]
Solution
Step 1: Identify node 15 deletion case
Node 15 has two children: 12 (left) and 18 (right). We replace 15 with its in-order successor (minimum in right subtree), which is 18.
Step 2: Perform deletion and in-order traversal
Replace 15 with 18, then remove original 18 node (leaf). The tree nodes become [10, 5, 18, 3, 7, 12]. In-order traversal is sorted: [3, 5, 7, 10, 12, 18].
Final Answer:
[3, 5, 7, 10, 12, 18] -> Option A
Quick Check:
In-order after deletion = sorted nodes without 15 [OK]
Hint: Replace deleted node with in-order successor for two children case [OK]
Common Mistakes:
Removing 18 instead of replacing 15 with 18
Forgetting to remove original successor node
Incorrect in-order traversal order
4. Consider this Rust code snippet for BST deletion. What is the bug?
A. find_min should take ownership, not a reference.
B. Using unwrap() causes panic if root is None.
C. Returning node.right or node.left directly causes ownership errors.
D. The function does not handle the case when key equals node.val.
Solution
Step 1: Analyze None handling
The code calls root.unwrap() without checking if root is None, causing a panic on empty tree.
Step 2: Correct handling
Add "if root.is_none() { return None; }" before unwrap.
Final Answer:
Using unwrap() causes panic if root is None. -> Option B
Quick Check:
unwrap without None check = panic [OK]
Hint: Check Option::is_none() before unwrap() [OK]
Common Mistakes:
Ignoring Rust ownership rules on Option
Using unwrap() without checking None
Misunderstanding reference vs ownership in find_min
5. You have a BST with nodes [20, 10, 30, 5, 15, 25, 35]. You delete node 10, which has two children. After deletion, what is the correct new in-order traversal of the BST?
hard
A. [5, 15, 20, 25, 30, 35, 10]
B. [5, 10, 15, 20, 25, 30, 35]
C. [5, 15, 20, 25, 30]
D. [5, 15, 20, 25, 30, 35]
Solution
Step 1: Identify deletion of node with two children
Node 10 has children 5 and 15. We replace 10 with its in-order successor, which is 15.
Step 2: Adjust tree and perform in-order traversal
Replace 10 with 15, then remove original 15 node. The BST nodes become [20, 15, 30, 5, 25, 35]. In-order traversal is sorted: [5, 15, 20, 25, 30, 35].
Final Answer:
[5, 15, 20, 25, 30, 35] -> Option D
Quick Check:
In-order after deletion = sorted nodes without 10 [OK]
Hint: Replace deleted node with in-order successor for two-child nodes [OK]
Common Mistakes:
Leaving deleted node in traversal
Not replacing with in-order successor
Incorrect traversal order
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.