Bird
Raised Fist0
DSA Javascriptprogramming~10 mins

BST Search Operation in DSA Javascript - Execution Trace

Choose your learning style10 modes available
Concept Flow - BST Search Operation
Start at root node
↓
Compare target with current node value
↓
Target < current?
↓
Go left
↓
Is current null?
Yes→Not found, stop
No↓
Is current value == target?
Yes→Found, stop
↩Back to compare step
Start at the root and compare the target value with the current node. Move left if target is smaller, right if larger. Repeat until found or reach null.
Execution Sample
DSA Javascript
function searchBST(root, target) {
  let current = root;
  while (current !== null) {
    if (current.val === target) return current;
    current = target < current.val ? current.left : current.right;
  }
  return null;
}
Searches for a target value in a BST by traversing left or right based on comparisons.
Execution Table
StepOperationCurrent Node ValueComparisonPointer ChangeVisual State
1Start at root8target=5 < 8Move left8 / 3 10 / \ 1 6 14
2Compare with 33target=5 > 3Move right8 / 3 10 / \ 1 6 14
3Compare with 66target=5 < 6Move left8 / 3 10 / \ 1 6 14
4Compare with nullnullReached nullStop8 / 3 10 / \ 1 6 14
5ResultnullNot foundReturn null8 / 3 10 / \ 1 6 14
💡 Reached null pointer at step 4, target not found in BST
Variable Tracker
VariableStartAfter Step 1After Step 2After Step 3After Step 4Final
current8 (root)3 (left child)6 (right child of 3)null (left child of 6)nullnull
target555555
Key Moments - 3 Insights
Why do we stop when current becomes null?
Because null means we reached a leaf's child without finding the target, so the target is not in the tree. See execution_table step 4.
Why do we move left when target is less than current node value?
In a BST, left children hold smaller values, so if target < current, we search left. See execution_table step 1 and 3.
What happens if the target equals the current node value?
We have found the target and stop searching immediately. This is shown in the code's if condition and would appear in execution if target was found.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution_table, what is the value of 'current' after step 2?
A3
B6
Cnull
D8
💡 Hint
Check the 'Pointer Change' and 'Current Node Value' columns at step 2 in execution_table.
At which step does the search stop because it reached a null pointer?
AStep 3
BStep 4
CStep 5
DStep 2
💡 Hint
Look for the step where 'Current Node Value' is null and 'Pointer Change' says 'Stop' in execution_table.
If the target was 6 instead of 5, at which step would the search find it?
AStep 1
BStep 2
CStep 3
DStep 4
💡 Hint
Check where the current node value equals the target in execution_table or code logic.
Concept Snapshot
BST Search Operation:
- Start at root node.
- Compare target with current node value.
- If equal, found target.
- If target < current, go left.
- If target > current, go right.
- Stop if current is null (target not found).
Full Transcript
The BST search operation starts at the root node and compares the target value with the current node's value. If the target equals the current node's value, the search stops successfully. If the target is less, the search moves to the left child; if greater, to the right child. This process repeats until the target is found or the search reaches a null pointer, indicating the target is not in the tree. The execution table traces each step, showing the current node, comparison, pointer changes, and the visual state of the BST. Variables like 'current' update as the search moves through the tree. Key moments clarify why the search stops at null and why direction depends on comparison. The visual quiz tests understanding of pointer changes and stopping conditions.

Practice

(1/5)
1. What is the main purpose of the search operation in a Binary Search Tree (BST)?
easy
A. To insert a new value into the BST
B. To find whether a given value exists in the BST
C. To delete a value from the BST
D. To balance the BST

Solution

  1. Step 1: Understand the search operation

    The search operation in a BST is used to check if a specific value is present in the tree.
  2. Step 2: Differentiate from other operations

    Insertion adds new values, deletion removes values, and balancing adjusts the tree structure. Search only checks for existence.
  3. Final Answer:

    To find whether a given value exists in the BST -> Option B
  4. Quick Check:

    Search = Find value [OK]
Hint: Search checks presence, not insertion or deletion [OK]
Common Mistakes:
  • Confusing search with insert or delete
  • Thinking search changes the tree
  • Assuming search balances the tree
2. Which of the following is the correct syntax to start a search function for a BST node in JavaScript?
easy
A. function searchBST(value, root) { }
B. function searchBST(value) { return root; }
C. function searchBST(root) { return value; }
D. function searchBST(root, value) { }

Solution

  1. Step 1: Identify parameters needed for BST search

    The search needs the root node to start and the value to find, so parameters are (root, value).
  2. Step 2: Check function signature correctness

    function searchBST(root, value) { } correctly uses (root, value). Options A, C, and D have wrong parameter order or missing parameters.
  3. Final Answer:

    function searchBST(root, value) { } -> Option D
  4. Quick Check:

    Correct parameters order = function searchBST(root, value) { } [OK]
Hint: Root node first, value second in search function [OK]
Common Mistakes:
  • Swapping parameter order
  • Missing one parameter
  • Returning wrong variable immediately
3. Consider the following BST and the search code snippet:
class Node {
  constructor(val) {
    this.val = val;
    this.left = null;
    this.right = null;
  }
}

const root = new Node(10);
root.left = new Node(5);
root.right = new Node(15);

function searchBST(root, val) {
  if (!root) return false;
  if (root.val === val) return true;
  if (val < root.val) return searchBST(root.left, val);
  else return searchBST(root.right, val);
}

console.log(searchBST(root, 15));
What will be the output?
medium
A. true
B. false
C. undefined
D. Error

Solution

  1. Step 1: Trace the search for value 15

    Start at root (10). Since 15 > 10, move to right child (15).
  2. Step 2: Check if current node value matches 15

    Node value is 15, which matches the search value, so return true.
  3. Final Answer:

    true -> Option A
  4. Quick Check:

    Value found = true [OK]
Hint: Compare value with node, go left or right [OK]
Common Mistakes:
  • Returning false too early
  • Not checking node value before recursion
  • Confusing left and right child directions
4. The following code is intended to search a value in a BST but does not work correctly. What is the error?
function searchBST(root, val) {
  if (root === null) return false;
  if (root.val === val) return true;
  if (val > root.val) return searchBST(root.left, val);
  else return searchBST(root.right, val);
}
medium
A. Incorrect return type
B. Missing base case for empty tree
C. The comparison directions for left and right child are swapped
D. Function parameters are wrong

Solution

  1. Step 1: Analyze comparison logic for recursion

    If val > root.val, search should go right, but code goes left. This is reversed.
  2. Step 2: Confirm correct child direction for BST search

    For val > root.val, search right subtree; for val < root.val, search left subtree.
  3. Final Answer:

    The comparison directions for left and right child are swapped -> Option C
  4. Quick Check:

    Left/right child direction swapped = The comparison directions for left and right child are swapped [OK]
Hint: Greater value goes right, smaller goes left [OK]
Common Mistakes:
  • Swapping left and right child in recursion
  • Forgetting base case
  • Returning wrong data type
5. You have a BST with nodes: 20 (root), 10 (left child), 30 (right child), 5 (left of 10), 15 (right of 10). You want to search for value 12. Which nodes will the search visit in order?
hard
A. 20 -> 10 -> 15
B. 20 -> 30 -> 15
C. 20 -> 10 -> 5
D. 20 -> 15 -> 10

Solution

  1. Step 1: Start at root node 20 and compare with 12

    Since 12 < 20, move to left child 10.
  2. Step 2: Compare 12 with node 10

    12 > 10, so move to right child 15.
  3. Step 3: Compare 12 with node 15

    12 < 15, so move to left child, which is null (not in tree), search ends.
  4. Final Answer:

    20 -> 10 -> 15 -> Option A
  5. Quick Check:

    Visited nodes = 20, 10, 15 [OK]
Hint: Go left if smaller, right if bigger, stop if null [OK]
Common Mistakes:
  • Visiting wrong child nodes
  • Assuming 12 is in the tree
  • Skipping nodes in path
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