0
0
DSA Javascriptprogramming~10 mins

BST Find Minimum Element in DSA Javascript - Execution Trace

Choose your learning style9 modes available
Concept Flow - BST Find Minimum Element
Start at root node
Check if left child exists?
NoCurrent node is minimum
Yes
Move to left child
Back to check left child
Start at the root and keep moving left until no left child exists; that node is the minimum.
Execution Sample
DSA Javascript
function findMin(root) {
  let current = root;
  while (current.left !== null) {
    current = current.left;
  }
  return current.value;
}
This code finds the smallest value in a BST by moving left until no left child remains.
Execution Table
StepOperationCurrent Node ValueLeft Child Exists?ActionVisual State
1Start at root10YesMove to left child10 / \ 5 15
2At node 55YesMove to left child10 / \ 5 15 / 2
3At node 22NoStop, found minimum10 / \ 5 15 / 2
ExitMinimum found2N/AReturn 2Final minimum node is 2
💡 Left child does not exist at node 2, so 2 is the minimum.
Variable Tracker
VariableStartAfter Step 1After Step 2After Step 3Final
currentroot (10)5222
Key Moments - 3 Insights
Why do we keep moving left to find the minimum?
Because in a BST, smaller values are always on the left. The execution_table shows moving left from 10 to 5, then 5 to 2, until no left child exists.
What if the tree has only one node?
The loop never runs because current.left is null at the start. The execution_table step 1 would show no left child, so the root is the minimum.
Why do we stop when current.left is null, not when current is null?
Stopping at current.left null means current is the smallest node. If we went further, current would become null and lose the node value. See step 3 in execution_table.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution_table, what is the value of 'current' after step 2?
A2
B5
C10
D15
💡 Hint
Check the 'current' variable in variable_tracker after step 2.
At which step does the algorithm stop moving left?
AStep 1
BStep 3
CStep 2
DExit
💡 Hint
Look at the 'Left Child Exists?' column in execution_table.
If the root node had no left child, what would the minimum be?
ANull
BThe right child's value
CThe root node's value
DThe smallest right child's leftmost node
💡 Hint
Refer to key_moments about single-node or no-left-child trees.
Concept Snapshot
BST Find Minimum Element:
- Start at root node
- Move left while left child exists
- When no left child, current node is minimum
- Return current node's value
- Works because BST left children are smaller
Full Transcript
To find the minimum element in a Binary Search Tree (BST), start at the root node. Check if the current node has a left child. If yes, move to that left child and repeat. Continue moving left until you reach a node with no left child. That node holds the minimum value in the BST. This works because BSTs store smaller values on the left side. The code uses a loop to move left until current.left is null, then returns current.value. If the tree has only one node or the root has no left child, the root itself is the minimum. Stopping when current.left is null ensures we do not lose the node's value by moving beyond the smallest node.