Bird
Raised Fist0
DSA Javascriptprogramming

BST Property and Why It Matters in DSA Javascript

Choose your learning style10 modes available
Mental Model
A Binary Search Tree keeps smaller values on the left and bigger values on the right, making it easy to find things fast.
Analogy: Imagine a library where books are arranged so all books with titles starting with A-M are on the left shelves and N-Z on the right shelves. This helps you find a book quickly without checking every shelf.
      10
     /  \
    5    15
   / \     \
  3   7     20
Dry Run Walkthrough
Input: BST with nodes 10, 5, 15, 3, 7, 20; search for value 7
Goal: Show how BST property helps find 7 quickly by deciding left or right at each step
Step 1: Start at root node 10
      [10↑]
     /    \
    5      15
   / \       \
  3   7       20
Why: We always start searching from the root
Step 2: Compare 7 with 10; 7 is less, go left
      10
     /    \
    [5↑]   15
   /  \      \
  3    7      20
Why: BST property says smaller values are on the left
Step 3: Compare 7 with 5; 7 is greater, go right
      10
     /    \
    5      15
   /   \     \
  3    [7↑]   20
Why: BST property says bigger values are on the right
Step 4: Found node with value 7
      10
     /    \
    5      15
   /   \     \
  3    7↑    20
Why: We stop when we find the value
Result:
      10
     /    \
    5      15
   /   \     \
  3    7↑    20
Answer: Found 7
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; // move left
      } else {
        if (!curr.right) {
          curr.right = newNode;
          return;
        }
        curr = curr.right; // move right
      }
    }
  }

  search(value) {
    let curr = this.root;
    while (curr) {
      if (value === curr.value) {
        return true; // found
      } else if (value < curr.value) {
        curr = curr.left; // go left
      } else {
        curr = curr.right; // go right
      }
    }
    return false; // not found
  }
}

// Driver code
const bst = new BST();
[10, 5, 15, 3, 7, 20].forEach(v => bst.insert(v));
const found = bst.search(7);
console.log(found ? 'Found 7' : '7 not found');
let curr = this.root;
start searching from root node
if (value === curr.value) { return true; }
check if current node is the target
else if (value < curr.value) { curr = curr.left; }
go left if target is smaller
else { curr = curr.right; }
go right if target is bigger
OutputSuccess
Found 7
Complexity Analysis
Time: O(h) where h is tree height because we follow one path down the tree
Space: O(1) because search uses only a few variables, no extra space grows with input
vs Alternative: Compared to searching an unsorted tree or list which is O(n), BST search is faster if tree is balanced
Edge Cases
searching in an empty tree
returns false immediately because root is null
DSA Javascript
let curr = this.root;
searching for a value smaller than all nodes
traverses left until null and returns false
DSA Javascript
curr = curr.left;
searching for a value larger than all nodes
traverses right until null and returns false
DSA Javascript
curr = curr.right;
When to Use This Pattern
When you need fast search, insert, or delete in a sorted collection, look for BST property to guide your tree traversal efficiently.
Common Mistakes
Mistake: Not comparing values correctly and going the wrong way in the tree
Fix: Always compare target with current node value and go left if smaller, right if bigger
Summary
It keeps smaller values on the left and bigger on the right to speed up search.
Use it when you want to find, add, or remove items quickly in a sorted way.
The key is deciding direction at each node by comparing values, so you don't check everything.

Practice

(1/5)
1. What is the main property that defines a Binary Search Tree (BST)?
easy
A. Each node has exactly two children.
B. For each node, all values in the left subtree are smaller, and all values in the right subtree are larger.
C. All nodes have the same value.
D. Nodes are arranged in a circular linked list.

Solution

  1. Step 1: Understand BST node value rules

    In a BST, the left subtree contains values smaller than the node, and the right subtree contains values larger than the node.
  2. Step 2: Compare options with BST definition

    For each node, all values in the left subtree are smaller, and all values in the right subtree are larger. matches the BST property exactly, while others describe different structures.
  3. Final Answer:

    For each node, all values in the left subtree are smaller, and all values in the right subtree are larger. -> Option B
  4. Quick Check:

    BST property = left < node < right [OK]
Hint: Remember: left < node < right defines BST [OK]
Common Mistakes:
  • Confusing BST with binary tree (not ordered)
  • Thinking all nodes must have two children
  • Assuming all values are equal
2. Which of the following is the correct way to check if a value should go to the left subtree in a BST insertion function in JavaScript?
easy
A. if (value < node.value) { insertLeft(); }
B. if (value > node.value) { insertLeft(); }
C. if (value == node.value) { insertLeft(); }
D. if (value >= node.value) { insertLeft(); }

Solution

  1. Step 1: Recall BST insertion rule

    Values smaller than the current node go to the left subtree.
  2. Step 2: Match condition to insertion side

    Only the condition 'value < node.value' correctly directs insertion to the left.
  3. Final Answer:

    if (value < node.value) { insertLeft(); } -> Option A
  4. Quick Check:

    Smaller values go left [OK]
Hint: Use < for left subtree insertion [OK]
Common Mistakes:
  • Using > instead of < for left subtree
  • Using == which is not for left or right
  • Using >= which includes equal values incorrectly
3. What will be the in-order traversal output of the following BST?
    10
   /  \
  5   15
 / \    \
3   7    20
medium
A. [20, 15, 10, 7, 5, 3]
B. [10, 5, 3, 7, 15, 20]
C. [3, 5, 7, 10, 15, 20]
D. [5, 3, 7, 10, 15, 20]

Solution

  1. Step 1: Understand in-order traversal

    In-order traversal visits left subtree, then node, then right subtree recursively.
  2. Step 2: Apply in-order to given tree

    Left subtree of 10: 3, 5, 7; then 10; then right subtree: 15, 20. Combined: [3, 5, 7, 10, 15, 20]
  3. Final Answer:

    [3, 5, 7, 10, 15, 20] -> Option C
  4. Quick Check:

    In-order = sorted BST values [OK]
Hint: In-order traversal of BST gives sorted values [OK]
Common Mistakes:
  • Confusing pre-order or post-order with in-order
  • Listing nodes in insertion order
  • Reversing the traversal order
4. Consider this BST insertion code snippet in JavaScript:
function insert(node, value) {
  if (!node) return { value, left: null, right: null };
  if (value <= node.value) {
    node.left = insert(node.left, value);
  } else {
    node.right = insert(node.right, value);
  }
  return node;
}

What is the main issue with this code regarding BST property?
medium
A. It inserts larger values on the left subtree incorrectly.
B. It does not handle the case when node is null.
C. It returns undefined instead of the node.
D. It allows duplicate values on the left subtree, which may break strict BST rules.

Solution

  1. Step 1: Analyze duplicate handling in insertion

    The condition 'value <= node.value' inserts duplicates to the left subtree.
  2. Step 2: Understand BST strictness

    Strict BSTs require left subtree values strictly less than node value; duplicates on left can break this.
  3. Final Answer:

    It allows duplicate values on the left subtree, which may break strict BST rules. -> Option D
  4. Quick Check:

    Duplicates on left break strict BST [OK]
Hint: Use < for left to avoid duplicates on left [OK]
Common Mistakes:
  • Ignoring duplicate value placement
  • Thinking null check is missing
  • Confusing left and right subtree insertion
5. You have a BST where some nodes violate the BST property because their left child has a value greater than the node. Which approach best fixes the tree without rebuilding it completely?
hard
A. Swap the values of the violating nodes with their children to restore BST property locally.
B. Ignore the violations as they do not affect search operations.
C. Delete all nodes and insert them again in correct order.
D. Perform an in-order traversal, collect values, then rebuild the BST from sorted values.

Solution

  1. Step 1: Understand the violation type

    Left child having greater value than node breaks BST property locally.
  2. Step 2: Choose minimal fix without full rebuild

    Swapping values of violating nodes with their children can restore local BST property without full rebuild.
  3. Step 3: Evaluate other options

    Perform an in-order traversal, collect values, then rebuild the BST from sorted values. rebuilds fully, Delete all nodes and insert them again in correct order. deletes all nodes (costly), Ignore the violations as they do not affect search operations. ignores problem (wrong).
  4. Final Answer:

    Swap the values of the violating nodes with their children to restore BST property locally. -> Option A
  5. Quick Check:

    Local swaps fix BST violations efficiently [OK]
Hint: Fix BST by swapping violating node values locally [OK]
Common Mistakes:
  • Rebuilding entire tree unnecessarily
  • Ignoring violations thinking no impact
  • Deleting all nodes instead of fixing
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard