Top View of Binary Tree
Level-order visualizer: first node per horizontal distance
Python Code
6 first = {}7 while queue:● node, hd = queue.popleft()9 if hd not in first:10 first[hd] = node.data11 if node.left:12 queue.append((node.left, hd - 1))13 if node.right:14 queue.append((node.right, hd + 1))15 return [first[key] for key in sorted(first)]Current Line (8): Pop Queue Head
Tree Structure
Operation:POP
Traversal Progress
Current Node
-
Phase
Pop Queue
Result Array
Traversal result appears here...
Step 1: POP
Processing Stack
Stack is empty. Click Next to begin!
Step Explanation
Ready to Start
Click "Next Step" to begin Top View traversal. The flow is level-order (BFS).
- > For each horizontal distance, only the first visible node is captured.
Unvisited
Left
Current
Right
Done