0
0
DSA C++programming~10 mins

Tree Traversal Preorder Root Left Right in DSA C++ - 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 C++
struct Node {
  int data;
  Node* left;
  Node* right;
};

void preorder(Node* root) {
  if (!root) return;
  std::cout << root->data << " ";
  preorder(root->left);
  preorder(root->right);
}
This code prints nodes in preorder: root first, then left subtree, then right subtree.
Execution Table
StepOperationNode VisitedLeft/RightTree StatePointer ChangesVisual State
1Start at root1Root1 / \ 2 3current = root(1)1 / \ 2 3
2Visit node1Root1 / \ 2 3print 11 / \ 2 3
3Traverse left subtree2Left1 / \ 2 3current = 21 / \ 2 3
4Visit node2Left1 / \ 2 3print 21 / \ 2 3
5Traverse left subtree4Left of 21 / \ 2 3 / 4current = 41 / \ 2 3 / 4
6Visit node4Left of 21 / \ 2 3 / 4print 41 / \ 2 3 / 4
7Traverse left subtreenullLeft of 41 / \ 2 3 / 4current = null1 / \ 2 3 / 4
8Return from nullnullLeft of 41 / \ 2 3 / 4return1 / \ 2 3 / 4
9Traverse right subtreenullRight of 41 / \ 2 3 / 4current = null1 / \ 2 3 / 4
10Return from nullnullRight of 41 / \ 2 3 / 4return1 / \ 2 3 / 4
11Return to node 22Left1 / \ 2 3 / 4current = 21 / \ 2 3 / 4
12Traverse right subtree5Right of 21 / \ 2 3 / \ 4 5current = 51 / \ 2 3 / \ 4 5
13Visit node5Right of 21 / \ 2 3 / \ 4 5print 51 / \ 2 3 / \ 4 5
14Traverse left subtreenullLeft of 51 / \ 2 3 / \ 4 5current = null1 / \ 2 3 / \ 4 5
15Return from nullnullLeft of 51 / \ 2 3 / \ 4 5return1 / \ 2 3 / \ 4 5
16Traverse right subtreenullRight of 51 / \ 2 3 / \ 4 5current = null1 / \ 2 3 / \ 4 5
17Return from nullnullRight of 51 / \ 2 3 / \ 4 5return1 / \ 2 3 / \ 4 5
18Return to node 11Root1 / \ 2 3 / \ 4 5current = 11 / \ 2 3 / \ 4 5
19Traverse right subtree3Right1 / \ 2 3 / \ 4 5current = 31 / \ 2 3 / \ 4 5
20Visit node3Right1 / \ 2 3 / \ 4 5print 31 / \ 2 3 / \ 4 5
21Traverse left subtreenullLeft of 31 / \ 2 3 / \ 4 5current = null1 / \ 2 3 / \ 4 5
22Return from nullnullLeft of 31 / \ 2 3 / \ 4 5return1 / \ 2 3 / \ 4 5
23Traverse right subtreenullRight of 31 / \ 2 3 / \ 4 5current = null1 / \ 2 3 / \ 4 5
24Return from nullnullRight of 31 / \ 2 3 / \ 4 5return1 / \ 2 3 / \ 4 5
25Traversal completenullNone1 / \ 2 3 / \ 4 5return1 / \ 2 3 / \ 4 5
💡 All nodes visited in preorder: root, left subtree, right subtree; recursion ends when null nodes reached
Variable Tracker
VariableStartAfter Step 3After Step 12After Step 19Final
currentroot(1)node(2)node(5)node(3)null
print_output"""1 2""1 2 4 5""1 2 4 5 3""1 2 4 5 3"
Key Moments - 3 Insights
Why do we print the node's data before traversing its children?
Because preorder traversal visits the root node first, as shown in execution_table rows 2, 4, 6, 13, and 20 where printing happens before recursive calls.
Why do we return immediately when the current node is null?
Returning on null nodes stops recursion, preventing errors and infinite loops, as seen in rows 7, 9, 14, 16, 21, and 23 where traversal returns without action.
How does the traversal know when to move from left subtree to right subtree?
After finishing left subtree traversal (returning from null), the code proceeds to right subtree calls, visible in execution_table rows 11 to 12 and 18 to 19.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution_table, what node is visited at step 13?
ANode 5
BNode 4
CNode 3
DNode 2
💡 Hint
Check the 'Node Visited' column at step 13 in execution_table
At which step does the traversal move from left subtree of node 2 to its right subtree?
AStep 11
BStep 12
CStep 7
DStep 18
💡 Hint
Look for the step where 'Traverse right subtree' of node 2 starts in execution_table
If the tree had no right child for node 1, how would the traversal end?
ATraversal would skip left subtree
BTraversal would continue infinitely
CTraversal ends after visiting left subtree nodes
DTraversal would print only root node
💡 Hint
Refer to variable_tracker and execution_table showing traversal stops when null nodes reached
Concept Snapshot
Preorder Tree Traversal (Root, Left, Right):
- Visit root node first (print/process)
- Recursively traverse left subtree
- Recursively traverse right subtree
- Stops when node is null
- Prints nodes in root-left-right order
Full Transcript
Preorder traversal visits the root node first, then the left subtree, and finally the right subtree. The code starts at the root, prints its data, then recursively calls preorder on the left child, and then on the right child. When a null node is reached, the recursion returns immediately. This process continues until all nodes are visited in preorder sequence. The example tree with nodes 1, 2, 3, 4, and 5 is traversed in the order 1 2 4 5 3.