BST Find Minimum Element in DSA Rust - Time & Space Complexity
We want to understand how long it takes to find the smallest value in a Binary Search Tree (BST).
How does the time needed change as the tree grows bigger?
Analyze the time complexity of the following code snippet.
fn find_min(root: &Option>) -> Option {
let mut current = root;
while let Some(node) = current {
if node.left.is_none() {
return Some(node.val);
}
current = &node.left;
}
None
}
This code finds the smallest value by moving left until no more left child exists.
Identify the loops, recursion, array traversals that repeat.
- Primary operation: Moving down the left child nodes in the tree.
- How many times: At most once per level of the tree, until the leftmost node is reached.
As the tree grows taller, the number of steps to find the minimum grows roughly with the height of the tree.
| Input Size (n) | Approx. Operations (steps down left) |
|---|---|
| 10 | Up to 4 (if tree is balanced) |
| 100 | Up to 7 (if tree is balanced) |
| 1000 | Up to 10 (if tree is balanced) |
Pattern observation: The steps grow slowly with input size if the tree is balanced, but can be as large as n if the tree is a straight line.
Time Complexity: O(h)
This means the time depends on the tree height, which is the number of levels from root to the leftmost leaf.
[X] Wrong: "Finding the minimum always takes O(n) time because we might check every node."
[OK] Correct: We only follow the left children, so we do not visit all nodes, just one path down the tree.
Knowing how tree height affects search time helps you explain why balanced trees are faster and why this method is efficient for BSTs.
"What if the tree is always balanced? How does that affect the time complexity compared to a skewed tree?"
Practice
Solution
Step 1: Understand BST property
In a BST, left child nodes have smaller values than their parents.Step 2: Locate minimum element
The smallest value is found by going left until no more left child exists.Final Answer:
At the leftmost node -> Option AQuick Check:
Minimum element = leftmost node [OK]
- Thinking minimum is at root
- Confusing leftmost with rightmost
- Assuming minimum can be any leaf
Solution
Step 1: Traverse left children
To find minimum, move left while left child exists.Step 2: Return current node's value
When no left child, current node holds minimum value.Final Answer:
while let Some(ref left) = current.left { current = left; } current.value -> Option AQuick Check:
Traverse left until None, then return value [OK]
- Traversing right instead of left
- Returning wrong node's value
- Using if incorrectly without loop
struct Node {
value: i32,
left: Option,
right: Option,
}
fn find_min(node: &Node) -> i32 {
let mut current = node;
while let Some(ref left) = current.left {
current = left;
}
current.value
}
fn main() {
let root = Node {
value: 10,
left: Some(Box::new(Node {
value: 5,
left: Some(Box::new(Node {
value: 2,
left: None,
right: None,
})),
right: None,
})),
right: None,
};
println!("{}", find_min(&root));
}Solution
Step 1: Traverse left nodes
Starting at root (10), move left to 5, then left to 2, which has no left child.Step 2: Return value of leftmost node
Node with value 2 is leftmost, so function returns 2.Final Answer:
2 -> Option BQuick Check:
Leftmost node value = 2 [OK]
- Returning root value instead of leftmost
- Confusing left and right traversal
- Expecting None instead of value
fn find_min(node: &Node) -> i32 {
let mut current = node;
while current.left.is_some() {
current = current.right.as_ref().unwrap();
}
current.value
}Solution
Step 1: Analyze traversal logic
The loop condition checks if left child exists, but inside loop moves to right child incorrectly.Step 2: Correct traversal direction
To find minimum, traversal must move to left child, not right.Final Answer:
It traverses right child instead of left inside the loop -> Option CQuick Check:
Traverse left child to find minimum [OK]
- Mixing left and right child traversal
- Ignoring unwrap panic possibility
- Not checking for None before unwrap
Solution
Step 1: Understand duplicates in BST
Duplicates can appear anywhere; leftmost node may not be unique minimum.Step 2: Use in-order traversal to find minimum unique
In-order traversal visits nodes in sorted order; track first unique minimum value.Step 3: Implement tracking of unique values
Keep a set or variable to remember seen values and update minimum unique accordingly.Final Answer:
Traverse entire tree in-order and track minimum unique value -> Option DQuick Check:
In-order traversal finds sorted unique minimum [OK]
- Assuming leftmost node is unique minimum
- Ignoring duplicates in traversal
- Returning root value blindly
Ready to prove it?
Pass the test and this pattern moves unknown
