0
0
DSA Javascriptprogramming~10 mins

Tree Traversal Preorder Root Left Right in DSA Javascript - Execution Trace

Choose your learning style9 modes available
Concept Flow - Tree Traversal Preorder Root Left Right
Start at Root Node
Visit Node: Process Data
Traverse Left Subtree
Traverse Right Subtree
Done
Preorder traversal visits the root node first, then recursively visits the left subtree, followed by the right subtree.
Execution Sample
DSA Javascript
function preorder(node) {
  if (!node) return;
  console.log(node.val);
  preorder(node.left);
  preorder(node.right);
}
This function prints nodes in preorder: root first, then left subtree, then right subtree.
Execution Table
StepOperationNode VisitedPointer ChangesTree State (Visual)
1Start at rootACurrent = AA ├─ B └─ C
2Visit nodeAPrint AA ├─ B └─ C
3Traverse left subtreeBCurrent = BA ├─ B └─ C
4Visit nodeBPrint BA ├─ B └─ C
5Traverse left subtreeDCurrent = DA ├─ B │ ├─ D │ └─ E └─ C
6Visit nodeDPrint DA ├─ B │ ├─ D │ └─ E └─ C
7Traverse left subtreenullCurrent = nullA ├─ B │ ├─ D │ └─ E └─ C
8Return from nullnullBack to DA ├─ B │ ├─ D │ └─ E └─ C
9Traverse right subtreenullCurrent = nullA ├─ B │ ├─ D │ └─ E └─ C
10Return from nullnullBack to BA ├─ B │ ├─ D │ └─ E └─ C
11Traverse right subtreeECurrent = EA ├─ B │ ├─ D │ └─ E └─ C
12Visit nodeEPrint EA ├─ B │ ├─ D │ └─ E └─ C
13Traverse left subtreenullCurrent = nullA ├─ B │ ├─ D │ └─ E └─ C
14Return from nullnullBack to EA ├─ B │ ├─ D │ └─ E └─ C
15Traverse right subtreenullCurrent = nullA ├─ B │ ├─ D │ └─ E └─ C
16Return from nullnullBack to BA ├─ B │ ├─ D │ └─ E └─ C
17Return from BBBack to AA ├─ B │ ├─ D │ └─ E └─ C
18Traverse right subtreeCCurrent = CA ├─ B │ ├─ D │ └─ E └─ C
19Visit nodeCPrint CA ├─ B │ ├─ D │ └─ E └─ C
20Traverse left subtreenullCurrent = nullA ├─ B │ ├─ D │ └─ E └─ C
21Return from nullnullBack to CA ├─ B │ ├─ D │ └─ E └─ C
22Traverse right subtreenullCurrent = nullA ├─ B │ ├─ D │ └─ E └─ C
23Return from nullnullBack to CA ├─ B │ ├─ D │ └─ E └─ C
24Return from CCBack to AA ├─ B │ ├─ D │ └─ E └─ C
25Return from AATraversal completeA ├─ B │ ├─ D │ └─ E └─ C
💡 Traversal ends after visiting all nodes in preorder: root, left subtree, right subtree.
Variable Tracker
VariableStartAfter Step 1After Step 3After Step 5After Step 11After Step 18Final
Current NodenullABDECnull
Printed Nodes[][A][A, B][A, B, D][A, B, D, E][A, B, D, E, C][A, B, D, E, C]
Key Moments - 3 Insights
Why do we print the node value before traversing left and right children?
Because preorder traversal visits the root node first, as shown in execution_table rows 2, 4, 6, 12, and 19 where the node is printed before moving to children.
What happens when the current node is null during traversal?
The function returns immediately without printing or traversing further, as seen in rows 7, 9, 13, 15, 20, and 22 where traversal returns from null nodes.
How does the traversal know when to backtrack to a parent node?
When both left and right children are null or fully visited, the traversal returns to the previous call, shown in rows like 8, 10, 14, 16, 21, 23, and 24 where it returns back up the tree.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution_table, what node is visited at Step 12?
AC
BD
CE
DB
💡 Hint
Check the 'Node Visited' column at Step 12 in the execution_table.
At which step does the traversal first return from a null node?
AStep 7
BStep 8
CStep 9
DStep 10
💡 Hint
Look for the first 'Return from null' operation in the execution_table.
If the left child of node B was null, how would the printed nodes change after Step 4?
A[A, B, E, C]
B[A, B, E, D, C]
C[A, B, D, E, C]
D[A, B, C]
💡 Hint
Refer to variable_tracker and consider skipping D if B's left child is null.
Concept Snapshot
Preorder Tree Traversal:
- Visit root node first
- Recursively traverse left subtree
- Recursively traverse right subtree
- Prints nodes in Root-Left-Right order
- Stops when all nodes visited
Full Transcript
Preorder traversal visits the root node first, then the left subtree, then the right subtree. The provided JavaScript function prints the value of each node as it visits. The execution table shows each step: starting at the root, printing it, then moving to the left child, printing it, and so on. When a null child is reached, the function returns to the previous node to continue traversal. The variable tracker shows how the current node and printed nodes change over time. Key moments clarify why nodes are printed before children, what happens at null nodes, and how backtracking works. The visual quiz tests understanding of node visits, returns from null, and effects of missing children. The snapshot summarizes preorder traversal as Root-Left-Right visiting order.