BST Find Minimum Element in DSA Javascript - Time & Space Complexity
We want to know how long it takes to find the smallest value in a Binary Search Tree (BST).
How does the time grow as the tree gets bigger?
Analyze the time complexity of the following code snippet.
function findMin(root) {
let current = root;
while (current.left !== null) {
current = current.left;
}
return current.value;
}
This code finds the smallest value by going 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 height is 4) |
| 100 | Up to 7 (if tree height is 7) |
| 1000 | Up to 10 (if tree height is 10) |
Pattern observation: The steps grow with the tree height, which depends on how balanced the tree is.
Time Complexity: O(h)
This means the time depends on the height of the tree, going down one level at a time.
[X] Wrong: "Finding the minimum always takes O(1) time because it's just one step."
[OK] Correct: The minimum is found by moving down the left side, which can take many steps if the tree is tall.
Understanding how tree height affects search time helps you explain why balanced trees are faster and shows your grasp of data structure efficiency.
"What if the BST is perfectly balanced? How would the time complexity change?"
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 as far left as possible.Final Answer:
At the leftmost node -> Option CQuick Check:
Minimum = leftmost node [OK]
- Thinking minimum is at root
- Confusing leftmost with rightmost
- Assuming minimum can be any leaf
left and value properties?Solution
Step 1: Check traversal direction
Minimum is found by moving left, so loop should checknode.left.Step 2: Verify code correctness
function findMin(node) { while(node.left) { node = node.left; } return node.value; } loops whilenode.leftexists, then returnsnode.value, which is correct.Final Answer:
function findMin(node) { while(node.left) { node = node.left; } return node.value; } -> Option AQuick Check:
Traverse left until no left child [OK]
- Looping right child instead of left
- Returning node.value without traversal
- Recursing on right child for minimum
findMin that finds the minimum value, what will be the output?const root = {
value: 10,
left: { value: 5, left: { value: 2, left: null, right: null }, right: null },
right: { value: 15, left: null, right: null }
};
function findMin(node) {
while(node.left) {
node = node.left;
}
return node.value;
}
console.log(findMin(root));Solution
Step 1: Trace left children from root
Start at 10, move left to 5, then left to 2, which has no left child.Step 2: Return value of leftmost node
Leftmost node is 2, so function returns 2.Final Answer:
2 -> Option DQuick Check:
Leftmost node value = 2 [OK]
- Returning root value instead of leftmost
- Confusing left and right child traversal
- Misreading tree structure
function findMin(node) {
while(node.right) {
node = node.right;
}
return node.value;
}Solution
Step 1: Analyze traversal direction
The code moves tonode.rightrepeatedly, which finds the maximum, not minimum.Step 2: Identify correct traversal for minimum
Minimum requires moving tonode.left, so the bug is wrong direction.Final Answer:
It incorrectly traverses right child instead of left -> Option AQuick Check:
Minimum = leftmost, not rightmost [OK]
- Confusing left and right child traversal
- Assuming right child leads to minimum
- Not testing code with example trees
null if the tree is empty?Solution
Step 1: Check for empty tree
function findMin(node) { if(!node) return null; while(node.left) { node = node.left; } return node.value; } returnsnullimmediately ifnodeisnull, handling empty tree.Step 2: Traverse left children safely
Then it loops left until no left child, returning minimum value correctly.Final Answer:
function findMin(node) { if(!node) return null; while(node.left) { node = node.left; } return node.value; } -> Option BQuick Check:
Check null first, then traverse left [OK]
- Not checking for null input
- Returning null incorrectly when node exists
- Recursing without base case for null
Ready to prove it?
Pass the test and this pattern moves unknown
