0
0
DSA Goprogramming~10 mins

Top View of Binary Tree in DSA Go - 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
Dequeue node
Check if horizontal distance seen before?
Add node to top view
Enqueue left child with hd-1
Enqueue right child with hd+1
Repeat until queue empty
Sort nodes by horizontal distance
Print top view nodes
Traverse the tree level by level, track horizontal distances, and record the first node at each horizontal distance to get the top view.
Execution Sample
DSA Go
package main

import (
  "fmt"
  "sort"
)

type Node struct {
  Val   int
  Left  *Node
  Right *Node
}

func TopView(root *Node) []int {
  if root == nil {
    return []int{}
  }
  type pair struct {
    node *Node
    hd   int
  }
  queue := []pair{{root, 0}}
  m := make(map[int]int)
  for len(queue) > 0 {
    p := queue[0]
    queue = queue[1:]
    if _, ok := m[p.hd]; !ok {
      m[p.hd] = p.node.Val
    }
    if p.node.Left != nil {
      queue = append(queue, pair{p.node.Left, p.hd - 1})
    }
    if p.node.Right != nil {
      queue = append(queue, pair{p.node.Right, p.hd + 1})
    }
  }
  keys := make([]int, 0, len(m))
  for k := range m {
    keys = append(keys, k)
  }
  sort.Ints(keys)
  res := make([]int, len(keys))
  for i, k := range keys {
    res[i] = m[k]
  }
  return res
}

func main() {
  root := &Node{Val: 1}
  root.Left = &Node{Val: 2}
  root.Right = &Node{Val: 3}
  root.Left.Left = &Node{Val: 4}
  root.Left.Right = &Node{Val: 5}
  root.Right.Left = &Node{Val: 6}
  root.Right.Right = &Node{Val: 7}

  topView := TopView(root)
  fmt.Println(topView) // Output: [4 2 1 3 7]
}
This code performs a breadth-first search on the binary tree, tracking horizontal distances to find the top view nodes.
Execution Table
StepOperationCurrent Node (Val)Horizontal Distance (hd)Top View Map (hd: node val)Queue State (node val, hd)Visual State
1Start at root10{}[(1,0)]Root node 1 at hd=0
2Dequeue node10{0:1}[]Add node 1 to top view at hd=0
3Enqueue left child2-1{0:1}[(2,-1)]Left child 2 at hd=-1
4Enqueue right child31{0:1}[(2,-1),(3,1)]Right child 3 at hd=1
5Dequeue node2-1{0:1,-1:2}[(3,1)]Add node 2 to top view at hd=-1
6Enqueue left child4-2{0:1,-1:2}[(3,1),(4,-2)]Left child 4 at hd=-2
7Enqueue right child50{0:1,-1:2}[(3,1),(4,-2),(5,0)]Right child 5 at hd=0 (hd=0 already seen, skip)
8Dequeue node31{0:1,-1:2,1:3}[(4,-2),(5,0)]Add node 3 to top view at hd=1
9Enqueue left child60{0:1,-1:2,1:3}[(4,-2),(5,0),(6,0)]Left child 6 at hd=0 (hd=0 seen, skip)
10Enqueue right child72{0:1,-1:2,1:3}[(4,-2),(5,0),(6,0),(7,2)]Right child 7 at hd=2
11Dequeue node4-2{0:1,-1:2,1:3,-2:4}[(5,0),(6,0),(7,2)]Add node 4 to top view at hd=-2
12Dequeue node50{0:1,-1:2,1:3,-2:4}[(6,0),(7,2)]hd=0 seen, skip node 5
13Dequeue node60{0:1,-1:2,1:3,-2:4}[(7,2)]hd=0 seen, skip node 6
14Dequeue node72{0:1,-1:2,1:3,-2:4,2:7}[]Add node 7 to top view at hd=2
15Queue empty--{-2:4,-1:2,0:1,1:3,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 14Final
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}{-2:4,-1:2,0:1,1:3,2:7}{-2:4,-1:2,0:1,1:3,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 seen at each horizontal distance from the top. Later nodes at the same horizontal distance are hidden behind the first one. See execution_table rows 2, 5, 8, and 14 where nodes are added only if hd is new.
Why do we use a queue and BFS instead of DFS for top view?
BFS processes nodes level by level, ensuring the first node at each horizontal distance is the topmost. DFS might visit deeper nodes first, causing incorrect top view. The queue state in execution_table shows level order traversal.
What does the horizontal distance represent and how is it updated?
Horizontal distance (hd) measures how far left or right a node is from the root (hd=0). Left child hd = parent hd - 1, right child hd = parent hd + 1. This is shown in execution_table column 'Horizontal Distance (hd)'.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution_table at step 5, what nodes are in the top view map?
A{-1:2}
B{0:1}
C{0:1, -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 get added to the top view map?
AStep 14
BStep 10
CStep 7
DStep 11
💡 Hint
Look for when hd=2 appears in 'Top View Map' column in execution_table
If the node with value 5 was missing, how would the queue state change at step 7?
AQueue would be [(3,1),(4,-2)]
BQueue would be [(3,1),(4,-2),(7,2)]
CQueue would be [(3,1),(4,-2),(6,0),(7,2)]
DQueue would be [(3,1),(5,0),(6,0),(7,2)]
💡 Hint
Check enqueue operations in execution_table steps 6-10 and consider missing node 5
Concept Snapshot
Top View of Binary Tree:
- Use BFS (queue) with horizontal distance (hd) tracking
- Root hd = 0; left child hd = hd-1; right child hd = hd+1
- Record first node at each hd in a map
- After traversal, sort map by hd and print nodes
- Shows nodes visible from top, ignoring hidden ones
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 (BFS). For each node dequeued, if its horizontal distance is not yet recorded, we add it to the top view map. We enqueue left and right children with hd-1 and hd+1 respectively. This ensures the first node at each horizontal distance is recorded, representing the top view. After processing all nodes, we sort the recorded nodes by horizontal distance and print them. This method ensures we see the nodes visible from the top, ignoring nodes hidden behind others.