0
0
DSA Goprogramming~10 mins

Tree Traversal Preorder Root Left Right in DSA Go - 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
Return to Parent or End
Start at the root, visit it, then recursively visit left subtree, then right subtree.
Execution Sample
DSA Go
func preorder(node *Node) {
  if node == nil {
    return
  }
  fmt.Println(node.value)
  preorder(node.left)
  preorder(node.right)
}
This code prints nodes in preorder: root first, then left subtree, then right subtree.
Execution Table
StepOperationNode VisitedPointer ChangesTree State (Visual)
1Visit rootAcurrent = AA ├─ B └─ C
2Traverse left subtreeBcurrent = BA ├─ B └─ C
3Visit nodeBnoneA ├─ B └─ C
4Traverse left subtreeDcurrent = DA ├─ B │ ├─ D │ └─ E └─ C
5Visit nodeDnoneA ├─ B │ ├─ D │ └─ E └─ C
6Traverse left subtreenilcurrent = nilA ├─ B │ ├─ D │ └─ E └─ C
7Return from nilnilback to DA ├─ B │ ├─ D │ └─ E └─ C
8Traverse right subtreenilcurrent = nilA ├─ B │ ├─ D │ └─ E └─ C
9Return from nilnilback to BA ├─ B │ ├─ D │ └─ E └─ C
10Traverse right subtreeEcurrent = EA ├─ B │ ├─ D │ └─ E └─ C
11Visit nodeEnoneA ├─ B │ ├─ D │ └─ E └─ C
12Traverse left subtreenilcurrent = nilA ├─ B │ ├─ D │ └─ E └─ C
13Return from nilnilback to EA ├─ B │ ├─ D │ └─ E └─ C
14Traverse right subtreenilcurrent = nilA ├─ B │ ├─ D │ └─ E └─ C
15Return from nilnilback to BA ├─ B │ ├─ D │ └─ E └─ C
16Return from BBback to AA ├─ B │ ├─ D │ └─ E └─ C
17Traverse right subtreeCcurrent = CA ├─ B │ ├─ D │ └─ E └─ C ├─ F └─ G
18Visit nodeCnoneA ├─ B │ ├─ D │ └─ E └─ C ├─ F └─ G
19Traverse left subtreeFcurrent = FA ├─ B │ ├─ D │ └─ E └─ C ├─ F └─ G
20Visit nodeFnoneA ├─ B │ ├─ D │ └─ E └─ C ├─ F └─ G
21Traverse left subtreenilcurrent = nilA ├─ B │ ├─ D │ └─ E └─ C ├─ F └─ G
22Return from nilnilback to FA ├─ B │ ├─ D │ └─ E └─ C ├─ F └─ G
23Traverse right subtreenilcurrent = nilA ├─ B │ ├─ D │ └─ E └─ C ├─ F └─ G
24Return from nilnilback to CA ├─ B │ ├─ D │ └─ E └─ C ├─ F └─ G
25Traverse right subtreeGcurrent = GA ├─ B │ ├─ D │ └─ E └─ C ├─ F └─ G
26Visit nodeGnoneA ├─ B │ ├─ D │ └─ E └─ C ├─ F └─ G
27Traverse left subtreenilcurrent = nilA ├─ B │ ├─ D │ └─ E └─ C ├─ F └─ G
28Return from nilnilback to GA ├─ B │ ├─ D │ └─ E └─ C ├─ F └─ G
29Traverse right subtreenilcurrent = nilA ├─ B │ ├─ D │ └─ E └─ C ├─ F └─ G
30Return from nilnilback to CA ├─ B │ ├─ D │ └─ E └─ C ├─ F └─ G
31Return from CCback to AA ├─ B │ ├─ D │ └─ E └─ C ├─ F └─ G
32Return from rootATraversal completeA ├─ B │ ├─ D │ └─ E └─ C ├─ F └─ G
💡 Traversal ends after visiting all nodes in preorder: A, B, D, E, C, F, G
Variable Tracker
VariableStartAfter Step 1After Step 4After Step 10After Step 17After Step 25Final
currentnilADECGnil
visited_nodes[][A][A, B, D][A, B, D, E][A, B, D, E, C][A, B, D, E, C, F, G][A, B, D, E, C, F, G]
Key Moments - 3 Insights
Why do we visit the node before traversing its left and right children?
Because preorder means Root-Left-Right, so visiting the node first is shown in execution_table rows 1, 3, 5, 11, 18, 20, 26 where the node is printed before moving to children.
What happens when the traversal reaches a nil (empty) child?
The function returns immediately without visiting, as shown in steps 6, 8, 12, 14, 21, 23, 27, 29 where current becomes nil and returns back to parent node.
How does the traversal know when to return to the parent node?
When both left and right children are nil or fully visited, the recursion returns to the previous call, as seen in steps 9, 15, 16, 24, 30, 31, and finally 32 when traversal ends.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution_table, what node is visited at step 10?
AE
BD
CB
DC
💡 Hint
Check the 'Node Visited' column at step 10 in execution_table.
At which step does the traversal first return from a nil child?
AStep 15
BStep 9
CStep 6
DStep 21
💡 Hint
Look for 'Return from nil' in the 'Operation' column in execution_table.
If the left child of node B was missing, how would the visited_nodes list change after step 10?
A[A, B, D, E]
B[A, B, E]
C[A, B]
D[A, B, D]
💡 Hint
Refer to variable_tracker's 'visited_nodes' and consider skipping node D.
Concept Snapshot
Preorder Traversal visits nodes in Root-Left-Right order.
Start at root, visit node, then recursively traverse left subtree, then right subtree.
If node is nil, return immediately.
Useful for copying trees or prefix expressions.
Output order example: A, B, D, E, C, F, G.
Full Transcript
Preorder tree traversal means visiting the root node first, then the left subtree, and finally the right subtree. The code visits a node by printing its value, then recursively calls itself on the left child, then on the right child. When a nil child is reached, the function returns immediately. The traversal ends after all nodes are visited in this order. This method is useful for tasks like copying a tree or evaluating prefix expressions.