0
0
DSA C++programming~10 mins

Vertical Order Traversal of Binary Tree in DSA C++ - Execution Trace

Choose your learning style9 modes available
Concept Flow - Vertical Order Traversal of Binary Tree
Start at root node
Assign horizontal distance = 0
Use queue for BFS traversal
Pop node from queue
Add node value to map at horizontal distance
If left child exists
Push left child with hd-1
If right child exists
Push right child with hd+1
Repeat until queue empty
Extract map entries in order
Output vertical order traversal
Traverse the tree level by level, track horizontal distances, group nodes by these distances, then output groups from left to right.
Execution Sample
DSA C++
struct Node {
  int val;
  Node* left;
  Node* right;
};

void verticalOrder(Node* root) {
  // BFS with horizontal distance tracking
}
This code performs a breadth-first traversal of the binary tree, tracking each node's horizontal distance to group nodes vertically.
Execution Table
StepOperationNode VisitedHorizontal Distance (hd)Queue State (node, hd)Map State (hd: [nodes])Visual State
1StartRoot (1)0[(1,0)]{0: []}Root node 1 at hd=0
2Pop from queue10[]{0: [1]}Visited 1, map[0] = [1]
3Push left child2-1[(2,-1)]{0: [1]}Left child 2 at hd=-1
4Push right child31[(2,-1),(3,1)]{0: [1]}Right child 3 at hd=1
5Pop from queue2-1[(3,1)]{0: [1], -1: [2]}Visited 2, map[-1] = [2]
6Push left child4-2[(3,1),(4,-2)]{0: [1], -1: [2]}Left child 4 at hd=-2
7Push right child50[(3,1),(4,-2),(5,0)]{0: [1], -1: [2]}Right child 5 at hd=0
8Pop from queue31[(4,-2),(5,0)]{0: [1], -1: [2], 1: [3]}Visited 3, map[1] = [3]
9Push left child60[(4,-2),(5,0),(6,0)]{0: [1], -1: [2], 1: [3]}Left child 6 at hd=0
10Push right child72[(4,-2),(5,0),(6,0),(7,2)]{0: [1], -1: [2], 1: [3]}Right child 7 at hd=2
11Pop from queue4-2[(5,0),(6,0),(7,2)]{0: [1], -1: [2], 1: [3], -2: [4]}Visited 4, map[-2] = [4]
12Pop from queue50[(6,0),(7,2)]{0: [1,5], -1: [2], 1: [3], -2: [4]}Visited 5, map[0] = [1,5]
13Pop from queue60[(7,2)]{0: [1,5,6], -1: [2], 1: [3], -2: [4]}Visited 6, map[0] = [1,5,6]
14Pop from queue72[]{0: [1,5,6], -1: [2], 1: [3], -2: [4], 2: [7]}Visited 7, map[2] = [7]
15EndNoneNone[]{-2: [4], -1: [2], 0: [1,5,6], 1: [3], 2: [7]}Traversal complete, output vertical order
💡 Queue empty, all nodes visited and grouped by horizontal distance
Variable Tracker
VariableStartAfter Step 2After Step 5After Step 8After Step 12After Step 14Final
Queue[(1,0)][][(3,1)][(4,-2),(5,0)][(6,0),(7,2)][][]
Map{}{0: [1]}{0: [1], -1: [2]}{0: [1], -1: [2], 1: [3]}{0: [1,5], -1: [2], 1: [3], -2: [4]}{0: [1,5,6], -1: [2], 1: [3], -2: [4], 2: [7]}{-2: [4], -1: [2], 0: [1,5,6], 1: [3], 2: [7]}
Key Moments - 3 Insights
Why do we use a queue with horizontal distance instead of just a normal queue?
Because we need to know each node's horizontal distance to group nodes vertically. The queue stores pairs (node, hd) so we can track and update the map correctly as shown in execution_table steps 3,4,6,7,9,10.
Why do nodes with the same horizontal distance appear in the order they are visited?
Because we use BFS traversal, nodes are visited level by level. So nodes at the same horizontal distance are added to the map in the order they appear in the queue, as seen in steps 12 and 13 where nodes 5 and 6 both have hd=0.
What happens if the tree is empty?
The queue starts empty, so the loop never runs and the map remains empty. The traversal ends immediately as shown by the exit_note.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution_table at step 12, what is the map state for horizontal distance 0?
A[1, 5]
B[1, 5, 6]
C[5, 6]
D[1]
💡 Hint
Check the 'Map State' column at step 12 in execution_table
At which step does the queue become empty, ending the traversal?
AStep 14
BStep 15
CStep 13
DStep 2
💡 Hint
Look at the 'Queue State' column and 'Exit Note' in execution_table
If the left child of node 3 was missing, how would the map change at step 10?
ANo node added at hd=0 at step 9
BNo node added at hd=2 at step 10
CNo node added at hd=1 at step 8
DNo change in map
💡 Hint
Check steps 9 and 10 where left and right children of node 3 are pushed
Concept Snapshot
Vertical Order Traversal of Binary Tree:
- Use BFS with a queue storing (node, horizontal distance)
- Start root at hd=0
- Left child hd = parent hd - 1
- Right child hd = parent hd + 1
- Group nodes by hd in a map
- Output map entries from smallest to largest hd
- Nodes at same hd appear in BFS order
Full Transcript
Vertical Order Traversal visits nodes of a binary tree grouped by their horizontal distance from the root. We start at the root with horizontal distance zero. Using a queue, we do a breadth-first search, storing each node with its horizontal distance. For each node visited, we add its value to a map keyed by horizontal distance. Left children have hd one less than their parent, right children one more. After visiting all nodes, we output the nodes grouped by horizontal distance from leftmost to rightmost. This method ensures nodes are output vertically and in top-to-bottom order within each vertical line.