0
0
DSA C++programming~10 mins

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

Choose your learning style9 modes available
Concept Flow - Bottom View of Binary Tree
Start at root node
Assign horizontal distance = 0
Use queue for level order traversal
For each node dequeued:
Update map with node at HD
Enqueue right child with HD+1
Repeat until queue empty
Extract bottom view from map sorted by HD
Print bottom view nodes left to right
Traverse the tree level by level, track horizontal distances, update bottom-most node at each horizontal distance, then print nodes from leftmost to rightmost horizontal distance.
Execution Sample
DSA C++
struct Node {
  int data;
  Node* left;
  Node* right;
};

// Bottom view function uses level order traversal with HD tracking
This code traverses the binary tree level-wise, tracks horizontal distances, and updates the bottom view nodes.
Execution Table
StepOperationNode Visited (data)Horizontal Distance (HD)Map State (HD:Node)Queue State (Node, HD)Visual State
1Start at root200{0:20}[(20,0)]20(HD=0)
2Dequeue node200{0:20}[]20(HD=0)
3Enqueue left child8-1{0:20}[(8,-1)]20(0) -> 8(-1)
4Enqueue right child221{0:20}[(8,-1),(22,1)]8(-1) <- 20(0) -> 22(1)
5Dequeue node8-1{0:20, -1:8}[(22,1)]8(-1) -> 20(0) -> 22(1)
6Enqueue left child5-2{0:20, -1:8}[(22,1),(5,-2)]5(-2) -> 8(-1) -> 20(0) -> 22(1)
7Enqueue right child30{0:20, -1:8}[(22,1),(5,-2),(3,0)]5(-2) -> 8(-1) -> 3(0) -> 20(0) -> 22(1)
8Dequeue node221{0:20, -1:8, 1:22}[(5,-2),(3,0)]5(-2) -> 8(-1) -> 3(0) -> 20(0) -> 22(1)
9Enqueue right child252{0:20, -1:8, 1:22}[(5,-2),(3,0),(25,2)]5(-2) -> 8(-1) -> 3(0) -> 20(0) -> 22(1) -> 25(2)
10Dequeue node5-2{0:20, -1:8, 1:22, -2:5}[(3,0),(25,2)]5(-2) -> 8(-1) -> 3(0) -> 20(0) -> 22(1) -> 25(2)
11Dequeue node30{0:3, -1:8, 1:22, -2:5}[(25,2)]5(-2) -> 8(-1) -> 3(0) -> 22(1) -> 25(2)
12Dequeue node252{0:3, -1:8, 1:22, -2:5, 2:25}[]5(-2) -> 8(-1) -> 3(0) -> 22(1) -> 25(2)
13Queue empty--{-2:5, -1:8, 0:3, 1:22, 2:25}[]Bottom view nodes: 5 -> 8 -> 3 -> 22 -> 25
💡 Queue is empty, traversal complete, bottom view map finalized.
Variable Tracker
VariableStartAfter Step 2After Step 5After Step 8After Step 11Final
Queue[(20,0)][][(22,1)][(5,-2),(3,0)][(25,2)][]
Map (HD:Node){}{0:20}{0:20, -1:8}{0:20, -1:8, 1:22}{0:3, -1:8, 1:22, -2:5}{-2:5, -1:8, 0:3, 1:22, 2:25}
Current Node20822325-
Key Moments - 3 Insights
Why does the map update the node at horizontal distance 0 from 20 to 3 at step 11?
Because node 3 is lower (visited later in level order) at the same horizontal distance 0, it replaces 20 in the map as the bottom view node at HD 0 (see execution_table step 11).
Why do we use horizontal distance (HD) to track nodes?
HD helps identify vertical alignment of nodes. Nodes with the same HD are in the same vertical line. The bottom view shows the lowest node at each HD (see concept_flow and execution_table).
Why do we use a queue for traversal?
Queue enables level order traversal, visiting nodes top to bottom and left to right, ensuring bottom-most nodes overwrite earlier ones in the map (see execution_table steps showing queue state).
Visual Quiz - 3 Questions
Test your understanding
Look at the execution_table at step 7, what nodes are currently in the queue?
A[(22,1), (5,-2), (3,0)]
B[(8,-1), (22,1), (5,-2)]
C[(5,-2), (3,0)]
D[(3,0), (25,2)]
💡 Hint
Check the 'Queue State' column at step 7 in the execution_table.
At which step does the map first get updated with horizontal distance -2?
AStep 6
BStep 5
CStep 10
DStep 9
💡 Hint
Look at the 'Map State' column for when HD -2 appears in the execution_table.
If the node with data 3 was not present, what would be the bottom view node at HD 0 after traversal?
A22
B20
C8
D5
💡 Hint
Refer to the map updates in execution_table steps 11 and 12 where node 3 replaces 20 at HD 0.
Concept Snapshot
Bottom View of Binary Tree:
- Traverse tree level order with queue
- Track horizontal distance (HD) for each node
- Update map with latest node at each HD
- After traversal, print nodes sorted by HD
- Shows lowest visible nodes from bottom
- Uses map and queue for O(n) time
Full Transcript
The bottom view of a binary tree shows the nodes visible when the tree is viewed from the bottom. We start at the root with horizontal distance zero. Using a queue, we traverse the tree level by level. For each node, we record its horizontal distance. We update a map to keep the latest node at each horizontal distance, which represents the bottom-most node. We enqueue left and right children with horizontal distances decremented and incremented respectively. After the queue is empty, the map contains the bottom view nodes. We print these nodes sorted by their horizontal distance from left to right. This approach ensures we see the lowest nodes at each vertical line of the tree.