0
0
DSA C++programming~10 mins

Top View of Binary Tree in DSA C++ - Execution Trace

Choose your learning style9 modes available
Concept Flow - Top View of Binary Tree
Start at root node
Assign horizontal distance = 0
Use queue for level order traversal
Pop node from queue
Check if horizontal distance seen before?
Print/store node
Add left child with hd-1 to queue
Add right child with hd+1 to queue
Repeat until queue empty
Output collected nodes in order of horizontal distance
Traverse the tree level by level, track horizontal distances, and print the first node seen at each horizontal distance to get the top view.
Execution Sample
DSA C++
struct Node {
  int data;
  Node* left;
  Node* right;
};

void topView(Node* root) {
  // Implementation
}
This code prints the top view of a binary tree by level order traversal with horizontal distance tracking.
Execution Table
StepOperationNode Visited (data)Horizontal Distance (hd)Queue State (node:hd)Top View Map (hd:data)Visual State
1Start at root10[ (1:0) ]{}Tree: 1 Queue: 1:0 TopView: {}
2Pop node10[]{0:1}Tree: 1 Queue: empty TopView: 0->1
3Add left child2-1[ (2:-1) ]{0:1}Tree: 1 Queue: 2:-1 TopView: 0->1
4Add right child31[ (2:-1), (3:1) ]{0:1}Tree: 1 Queue: 2:-1,3:1 TopView: 0->1
5Pop node2-1[ (3:1) ]{0:1, -1:2}Tree: 1 Queue: 3:1 TopView: -1->2,0->1
6Add left child4-2[ (3:1), (4:-2) ]{0:1, -1:2}Tree: 1 Queue: 3:1,4:-2 TopView: -1->2,0->1
7Add right child50[ (3:1), (4:-2), (5:0) ]{0:1, -1:2}Tree: 1 Queue: 3:1,4:-2,5:0 TopView: -1->2,0->1
8Pop node31[ (4:-2), (5:0) ]{0:1, -1:2, 1:3}Tree: 1 Queue: 4:-2,5:0 TopView: -1->2,0->1,1->3
9Add left child60[ (4:-2), (5:0), (6:0) ]{0:1, -1:2, 1:3}Tree: 1 Queue: 4:-2,5:0,6:0 TopView: -1->2,0->1,1->3
10Add right child72[ (4:-2), (5:0), (6:0), (7:2) ]{0:1, -1:2, 1:3}Tree: 1 Queue: 4:-2,5:0,6:0,7:2 TopView: -1->2,0->1,1->3
11Pop node4-2[ (5:0), (6:0), (7:2) ]{0:1, -1:2, 1:3, -2:4}Tree: 1 Queue: 5:0,6:0,7:2 TopView: -2->4,-1->2,0->1,1->3
12No children to add--[ (5:0), (6:0), (7:2) ]{0:1, -1:2, 1:3, -2:4}Tree: 1 Queue: 5:0,6:0,7:2 TopView: -2->4,-1->2,0->1,1->3
13Pop node50[ (6:0), (7:2) ]{0:1, -1:2, 1:3, -2:4}Tree: 1 Queue: 6:0,7:2 TopView: -2->4,-1->2,0->1,1->3
14No children to add--[ (6:0), (7:2) ]{0:1, -1:2, 1:3, -2:4}Tree: 1 Queue: 6:0,7:2 TopView: -2->4,-1->2,0->1,1->3
15Pop node60[ (7:2) ]{0:1, -1:2, 1:3, -2:4}Tree: 1 Queue: 7:2 TopView: -2->4,-1->2,0->1,1->3
16No children to add--[ (7:2) ]{0:1, -1:2, 1:3, -2:4}Tree: 1 Queue: 7:2 TopView: -2->4,-1->2,0->1,1->3
17Pop node72[]{0:1, -1:2, 1:3, -2:4, 2:7}Tree: 1 Queue: empty TopView: -2->4,-1->2,0->1,1->3,2->7
18No children to add--[]{0:1, -1:2, 1:3, -2:4, 2:7}Tree: 1 Queue: empty TopView: -2->4,-1->2,0->1,1->3,2->7
19Queue empty--[]{0:1, -1:2, 1:3, -2:4, 2:7}Traversal complete, top view collected
💡 Queue is empty, all nodes processed
Variable Tracker
VariableStartAfter Step 2After Step 5After Step 8After Step 11After Step 17Final
Queue[ (1:0) ][][ (3:1) ][ (4:-2), (5:0) ][ (5:0), (6:0), (7:2) ][][]
Top View Map{}{0:1}{0:1, -1:2}{0:1, -1:2, 1:3}{0:1, -1:2, 1:3, -2:4}{0:1, -1:2, 1:3, -2:4, 2:7}{0:1, -1:2, 1:3, -2:4, 2:7}
Key Moments - 3 Insights
Why do we only add a node to the top view map if its horizontal distance is not already present?
Because the top view shows the first node visible at each horizontal distance from the top. Later nodes at the same horizontal distance are hidden behind the first one. See execution_table steps 2, 5, and 8 where nodes are added only if hd is new.
Why do we use a queue and level order traversal instead of DFS?
Level order traversal ensures we visit nodes top-down, so the first node at each horizontal distance is the topmost. DFS might visit lower nodes first, causing incorrect top view. This is shown in the queue states in execution_table.
What does the horizontal distance represent and how is it updated?
Horizontal distance (hd) is the relative position from root: root is 0, left child is hd-1, right child is hd+1. This helps group nodes vertically. See execution_table steps 3, 4, 6, 7 for hd updates.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution_table at step 5. What is the top view map after visiting node 2?
A{0:1, -1:2}
B{0:1}
C{-1:2}
D{}
💡 Hint
Check the 'Top View Map' column at step 5 in execution_table.
At which step does the horizontal distance 2 first appear in the top view map?
AStep 11
BStep 17
CStep 8
DStep 2
💡 Hint
Look for when hd=2 is added to 'Top View Map' in execution_table.
If the node with data 5 had a left child, how would the queue state change after step 14?
AA new node with hd= -1 would be added to the queue
BA new node with hd= 1 would be added to the queue
CA new node with hd= 0 would be added to the queue
DNo change to the queue
💡 Hint
Children's hd is parent's hd ± 1; node 5 has hd=0, so left child hd=0-1= -1, right child hd=0+1=1.
Concept Snapshot
Top View of Binary Tree:
- Use level order traversal with a queue
- Track horizontal distance (hd) for each node
- For each hd, store first node encountered
- Left child hd = parent hd - 1
- Right child hd = parent hd + 1
- Output nodes sorted by hd for top view
Full Transcript
To find the top view of a binary tree, we start at the root node with horizontal distance zero. We use a queue to do a level order traversal, visiting nodes level by level. For each node, we check if its horizontal distance has been seen before. If not, we add it to the top view map. We add left and right children to the queue with horizontal distances decreased or increased by one respectively. We continue until the queue is empty. The top view is the collection of first nodes seen at each horizontal distance, printed from leftmost to rightmost. This method ensures we see the topmost nodes when looking from above the tree.