0
0
DSA Javascriptprogramming~10 mins

BST Search Operation in DSA Javascript - Execution Trace

Choose your learning style9 modes available
Concept Flow - BST Search Operation
Start at root node
Compare target with current node value
Target < current?
Go left
Is current null?
YesNot found, stop
No
Is current value == target?
YesFound, 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.