0
0
DSA Javascriptprogramming

BST Find Minimum Element in DSA Javascript

Choose your learning style9 modes available
Mental Model
In a Binary Search Tree, the smallest value is always found by going left until no more left child exists.
Analogy: Imagine a family tree where the oldest ancestor is always on the far left branch; to find the oldest, you keep moving left until you can't go further.
      10
     /  \
    5    15
   / 
  2  
 ↑ (start here at root)
Dry Run Walkthrough
Input: BST: 10 -> 5 -> 15 -> 2, find minimum element
Goal: Find the smallest value in the BST by moving left until no left child exists
Step 1: Start at root node with value 10
10 -> 5 -> 15 -> 2, current at 10 ↑
Why: We begin searching from the root to find the minimum
Step 2: Move left from 10 to node 5
10 -> [curr->5] -> 15 -> 2
Why: Minimum is always in the left subtree, so we go left
Step 3: Move left from 5 to node 2
10 -> 5 -> 15 -> [curr->2]
Why: Keep moving left to find smaller values
Step 4: Node 2 has no left child, stop here
10 -> 5 -> 15 -> 2 [curr], left child is null
Why: No more left nodes means current node is minimum
Result:
Minimum element found: 2
BST structure: 10 -> 5 -> 15 -> 2
Annotated Code
DSA Javascript
class Node {
  constructor(value) {
    this.value = value;
    this.left = null;
    this.right = null;
  }
}

class BST {
  constructor() {
    this.root = null;
  }

  insert(value) {
    const newNode = new Node(value);
    if (!this.root) {
      this.root = newNode;
      return;
    }
    let curr = this.root;
    while (true) {
      if (value < curr.value) {
        if (!curr.left) {
          curr.left = newNode;
          return;
        }
        curr = curr.left; // advance left
      } else {
        if (!curr.right) {
          curr.right = newNode;
          return;
        }
        curr = curr.right; // advance right
      }
    }
  }

  findMin() {
    if (!this.root) return null; // empty tree
    let curr = this.root;
    while (curr.left) {
      curr = curr.left; // move left to find min
    }
    return curr.value; // leftmost node value
  }
}

// Driver code
const bst = new BST();
bst.insert(10);
bst.insert(5);
bst.insert(15);
bst.insert(2);
console.log("Minimum element found:", bst.findMin());
while (curr.left) {
keep moving left to find the smallest value
curr = curr.left;
advance current node to its left child
return curr.value;
return the value of the leftmost node as minimum
OutputSuccess
Minimum element found: 2
Complexity Analysis
Time: O(h) where h is the height of the tree because we move down only the left children
Space: O(1) because we use only a few variables and no extra data structures
vs Alternative: Compared to scanning all nodes (O(n)), this is faster because BST structure guides directly to minimum
Edge Cases
Empty tree
Returns null since there is no minimum
DSA Javascript
if (!this.root) return null;
Tree with only one node
Returns the root value as minimum
DSA Javascript
while (curr.left) { ... } returns root value if no left child
All nodes only on right side (no left children)
Returns root value as minimum since no left child exists
DSA Javascript
while (curr.left) { ... } stops immediately if no left child
When to Use This Pattern
When asked to find the smallest element in a BST, look for the leftmost node by following left children until none remain.
Common Mistakes
Mistake: Trying to check right children or scanning entire tree for minimum
Fix: Only follow left children from root until left child is null
Mistake: Not handling empty tree and returning undefined or error
Fix: Add a check for empty root and return null
Summary
Finds the smallest value in a BST by moving left until no left child exists.
Use when you need the minimum element quickly in a BST.
The smallest element is always the leftmost node in the BST.