0
0
DSA Typescriptprogramming~10 mins

BST Search Operation in DSA Typescript - Execution Trace

Choose your learning style9 modes available
Concept Flow - BST Search Operation
Start at root node
Compare target with current node value
Found node, stop
Go to left child
Go to right child
Is current node null?
YesNot found, stop
Back to compare step
Start at the root, compare target with current node, move left or right accordingly, repeat until found or null.
Execution Sample
DSA Typescript
function searchBST(root: TreeNode | null, target: number): TreeNode | null {
  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 nodes until found or end.
Execution Table
StepOperationCurrent Node ValueComparisonNext PointerVisual State
1Start at root88 == 5? No5 < 8 → go left8 / \ 5 12 / \ \ 3 6 15
2Move to left child55 == 5? YesFound node8 / \ 5* 12 / \ \ 3 6 15
3Stop5Found targetN/A8 / \ 5* 12 / \ \ 3 6 15
💡 Target 5 found at step 2, search stops.
Variable Tracker
VariableStartAfter Step 1After Step 2Final
current8 (root)5 (left child of 8)5 (found node)5 (found node)
target5555
Key Moments - 3 Insights
Why do we move left when target is less than current node value?
Because in a BST, left child nodes hold smaller values. As shown in step 1 of execution_table, target 5 is less than 8, so we move left.
What happens if the target is not in the tree?
The search continues moving left or right until current becomes null, meaning no node matches target. This is shown by the exit condition in the concept_flow.
Why do we stop when current node value equals target?
Because we found the node containing the target value, so no further search is needed. This is shown in step 2 and 3 of execution_table.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution_table, what is the current node value at step 2?
A5
B8
C12
D3
💡 Hint
Check the 'Current Node Value' column in execution_table row for step 2.
At which step does the search stop because the target is found?
AStep 1
BStep 2
CStep 3
DStep 4
💡 Hint
Look at the 'Operation' and 'Comparison' columns in execution_table to find when target equals current node.
If the target was 10 instead of 5, which direction would the search go from the root node at step 1?
ALeft
BRight
CStay at root
DStop immediately
💡 Hint
Refer to concept_flow and execution_table step 1: target < current goes left, target > current goes right.
Concept Snapshot
BST Search Operation:
- Start at root node
- Compare target with current node value
- If equal, return node
- If target < current, go left
- If target > current, go right
- Repeat until found or null
Full Transcript
This visual trace shows how to search a value in a Binary Search Tree (BST). We start at the root node and compare the target value with the current node's value. If they match, we stop and return the node. If the target is smaller, we move to the left child; if larger, to the right child. We repeat this process until we find the target or reach a null pointer, meaning the target is not in the tree. The execution table tracks each step, showing the current node, comparison, next pointer direction, and the tree's visual state. Variable tracking shows how the current node pointer changes during the search. Key moments clarify why we move left or right and when the search stops. The quiz tests understanding of these steps and decisions.