BST Find Minimum Element in DSA C - 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.
// Find minimum value in BST
int findMin(struct Node* root) {
struct Node* current = root;
while (current != NULL && current->left != NULL) {
current = current->left;
}
return current->data;
}
This code moves left down the tree until it finds the leftmost node, which holds the minimum value.
Identify the loops, recursion, array traversals that repeat.
- Primary operation: Moving down the left child nodes in a loop.
- How many times: Once for each level down the left side until the smallest node is found.
As the tree grows, the time depends on how deep the left branch is.
| Input Size (n) | Approx. Operations |
|---|---|
| 10 | Up to 4 steps (if tree is balanced) |
| 100 | Up to 7 steps |
| 1000 | Up to 10 steps |
Pattern observation: The number of steps grows slowly, roughly with the height of the tree, not the total nodes.
Time Complexity: O(h)
This means the time depends on the height of the tree, which is how many levels it has from top to bottom.
[X] Wrong: "Finding the minimum always takes time proportional to the total number of nodes (n)."
[OK] Correct: The search only follows the left branch, so it depends on the tree height, not total nodes.
Understanding how tree height affects search time helps you explain why balanced trees are faster and why this operation is efficient.
"What if the BST is completely unbalanced and looks like a linked list? How would the time complexity change?"
Practice
Solution
Step 1: Understand BST property
In a BST, left child nodes have smaller values than their parent nodes.Step 2: Locate minimum element
Because smaller values go left, the minimum is at the leftmost node.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: Check loop condition for minimum
Minimum is found by moving left while left child exists.Step 2: Verify code correctness
while(root->left != NULL) root = root->left; return root->data; moves left until no left child, then returns data correctly.Final Answer:
while(root->left != NULL) root = root->left; return root->data; -> Option BQuick Check:
Traverse left to find minimum [OK]
- Traversing right child instead of left
- Returning root data without traversal
- Incorrect loop condition
struct Node {
int data;
struct Node* left;
struct Node* right;
};
struct Node* findMin(struct Node* root) {
while(root->left != NULL) {
root = root->left;
}
return root;
}
// Assume BST is:
// 10
// / \
// 5 15
// /
// 2
int main() {
struct Node n2 = {2, NULL, NULL};
struct Node n5 = {5, &n2, NULL};
struct Node n15 = {15, NULL, NULL};
struct Node n10 = {10, &n5, &n15};
struct Node* minNode = findMin(&n10);
printf("%d", minNode->data);
return 0;
}Solution
Step 1: Trace findMin function
Function moves left from root 10 to 5, then to 2, stops as 2 has no left child.Step 2: Identify returned node data
Returned node is 2, so printf prints 2.Final Answer:
2 -> Option CQuick Check:
Leftmost node data = 2 [OK]
- Confusing leftmost with root
- Assuming right child is minimum
- Not following pointer traversal
struct Node* findMin(struct Node* root) {
while(root->left == NULL) {
root = root->left;
}
return root;
}Solution
Step 1: Analyze loop condition
Loop runs while root->left == NULL, which means it stops if left child exists, so it never moves left.Step 2: Correct loop condition
It should run while root->left != NULL to move left until no left child.Final Answer:
Loop condition should be root->left != NULL -> Option AQuick Check:
Traverse left while left child exists [OK]
- Using == NULL instead of != NULL in loop
- Ignoring NULL root check
- Confusing left and right pointers
findMin function to safely find the minimum element without causing a runtime error if the tree is empty?Solution
Step 1: Handle empty tree case
Check if root is NULL at start to avoid dereferencing NULL pointer.Step 2: Traverse left children safely
If root is not NULL, move left while left child exists to find minimum.Final Answer:
Add a check at start: if(root == NULL) return NULL; then traverse left -> Option DQuick Check:
Check NULL root before traversal [OK]
- Not checking for empty tree causing crash
- Traversing right child for minimum
- Missing base case in recursion
Ready to prove it?
Pass the test and this pattern moves unknown
