class Node { int data; Node left, right; Node(int data) { this.data = data; left = right = null; } } class BinaryTree { Node root; void topView() { Map<Integer, Integer> topViewMap = new TreeMap<>(); Queue<Node> queue = new LinkedList<>(); Queue<Integer> hdQueue = new LinkedList<>(); queue.add(root); hdQueue.add(0); while (!queue.isEmpty()) { Node temp = queue.poll(); int hd = hdQueue.poll(); if (!topViewMap.containsKey(hd)) { topViewMap.put(hd, temp.data); } if (temp.left != null) { queue.add(temp.left); hdQueue.add(hd - 1); } if (temp.right != null) { queue.add(temp.right); hdQueue.add(hd + 1); } } for (int value : topViewMap.values()) { System.out.print(value + " "); } } } public class Main { public static void main(String[] args) { BinaryTree tree = new BinaryTree(); tree.root = new Node(1); tree.root.left = new Node(2); tree.root.right = new Node(3); tree.root.left.right = new Node(4); tree.root.left.right.right = new Node(5); tree.root.left.right.right.right = new Node(6); tree.topView(); } }
The top view prints nodes visible when the tree is viewed from above. It uses horizontal distance (hd) from root. The first node encountered at each hd is printed.
Here, nodes with hd -1, 0, 1, 2 are 2, 1, 3, 6 respectively.
Horizontal distance is the horizontal offset from the root node. Root is at hd=0, left child is hd-1, right child is hd+1, and so on.
class Node { int data; Node left, right; Node(int data) { this.data = data; left = right = null; } } class BinaryTree { Node root; void topView() { Map<Integer, Integer> topViewMap = new HashMap<>(); Queue<Node> queue = new LinkedList<>(); Queue<Integer> hdQueue = new LinkedList<>(); queue.add(root); hdQueue.add(0); while (!queue.isEmpty()) { Node temp = queue.poll(); int hd = hdQueue.poll(); if (!topViewMap.containsKey(hd)) { topViewMap.put(hd, temp.data); } if (temp.left != null) { queue.add(temp.left); hdQueue.add(hd - 1); } if (temp.right != null) { queue.add(temp.right); hdQueue.add(hd + 1); } } for (int value : topViewMap.values()) { System.out.print(value + " "); } } }
The code uses HashMap which does not maintain keys in sorted order. So the printed top view nodes are not in horizontal distance order.
To fix, use TreeMap instead of HashMap.
class Node { int data; Node left, right; Node(int data) { this.data = data; left = right = null; } } class BinaryTree { Node root; void topView() { Map<Integer, Integer> topViewMap = new TreeMap<>(); Queue<Node> queue = new LinkedList<>(); Queue<Integer> hdQueue = new LinkedList<>(); queue.add(root); hdQueue.add(0); while (!queue.isEmpty()) { Node temp = queue.poll(); int hd = hdQueue.poll(); if (!topViewMap.containsKey(hd)) { topViewMap.put(hd, temp.data); } if (temp.left != null) { queue.add(temp.left); hdQueue.add(hd - 1); } if (temp.right != null) { queue.add(temp.right); hdQueue.add(hd + 1); } } for (int value : topViewMap.values()) { System.out.print(value + " "); } } } public class Main { public static void main(String[] args) { BinaryTree tree = new BinaryTree(); tree.root = new Node(10); tree.root.left = new Node(20); tree.root.right = new Node(30); tree.root.left.left = new Node(40); tree.root.left.right = new Node(50); tree.root.right.left = new Node(60); tree.root.right.right = new Node(70); tree.root.right.left.right = new Node(80); tree.root.right.right.right = new Node(90); tree.topView(); } }
Horizontal distances and first nodes are:
- hd -2: 40
- hd -1: 20
- hd 0: 10
- hd 1: 30
- hd 2: 70
- hd 3: 90
Nodes 60 and 80 are hidden behind others at their horizontal distances.
A perfect binary tree of height 2 has 7 nodes arranged in 3 levels.
Horizontal distances range from -2 to 2, so 5 unique horizontal distances.
Top view prints one node per horizontal distance, so 5 nodes.
