|
| 1 | +// Java program to print top |
| 2 | +// view of binary tree |
| 3 | +import java.util.LinkedList; |
| 4 | +import java.util.Map; |
| 5 | +import java.util.Map.Entry; |
| 6 | +import java.util.Queue; |
| 7 | +import java.util.TreeMap; |
| 8 | + |
| 9 | +// class to create a node |
| 10 | +class Node { |
| 11 | + int data; |
| 12 | + Node left, right; |
| 13 | + |
| 14 | + public Node(int data) |
| 15 | + { |
| 16 | + this.data = data; |
| 17 | + left = right = null; |
| 18 | + } |
| 19 | +} |
| 20 | + |
| 21 | +// class of binary tree |
| 22 | +class BinaryTree { |
| 23 | + Node root; |
| 24 | + |
| 25 | + public BinaryTree() { root = null; } |
| 26 | + |
| 27 | + // function should print the topView of |
| 28 | + // the binary tree |
| 29 | + private void TopView(Node root) |
| 30 | + { |
| 31 | + class QueueObj { |
| 32 | + Node node; |
| 33 | + int hd; |
| 34 | + |
| 35 | + QueueObj(Node node, int hd) |
| 36 | + { |
| 37 | + this.node = node; |
| 38 | + this.hd = hd; |
| 39 | + } |
| 40 | + } |
| 41 | + Queue<QueueObj> q = new LinkedList<QueueObj>(); |
| 42 | + Map<Integer, Node> topViewMap |
| 43 | + = new TreeMap<Integer, Node>(); |
| 44 | + |
| 45 | + if (root == null) { |
| 46 | + return; |
| 47 | + } |
| 48 | + else { |
| 49 | + q.add(new QueueObj(root, 0)); |
| 50 | + } |
| 51 | + |
| 52 | + System.out.println( |
| 53 | + "The top view of the tree is : "); |
| 54 | + |
| 55 | + // count function returns 1 if the container |
| 56 | + // contains an element whose key is equivalent |
| 57 | + // to hd, or returns zero otherwise. |
| 58 | + while (!q.isEmpty()) { |
| 59 | + QueueObj tmpNode = q.poll(); |
| 60 | + if (!topViewMap.containsKey(tmpNode.hd)) { |
| 61 | + topViewMap.put(tmpNode.hd, tmpNode.node); |
| 62 | + } |
| 63 | + |
| 64 | + if (tmpNode.node.left != null) { |
| 65 | + q.add(new QueueObj(tmpNode.node.left, |
| 66 | + tmpNode.hd - 1)); |
| 67 | + } |
| 68 | + if (tmpNode.node.right != null) { |
| 69 | + q.add(new QueueObj(tmpNode.node.right, |
| 70 | + tmpNode.hd + 1)); |
| 71 | + } |
| 72 | + } |
| 73 | + for (Entry<Integer, Node> entry : |
| 74 | + topViewMap.entrySet()) { |
| 75 | + System.out.print(entry.getValue().data); |
| 76 | + } |
| 77 | + } |
| 78 | + |
| 79 | + // Driver Program to test above functions |
| 80 | + public static void main(String[] args) |
| 81 | + { |
| 82 | + /* Create following Binary Tree |
| 83 | + 1 |
| 84 | + / \ |
| 85 | + 2 3 |
| 86 | + \ |
| 87 | + 4 |
| 88 | + \ |
| 89 | + 5 |
| 90 | + \ |
| 91 | + 6*/ |
| 92 | + BinaryTree tree = new BinaryTree(); |
| 93 | + tree.root = new Node(1); |
| 94 | + tree.root.left = new Node(2); |
| 95 | + tree.root.right = new Node(3); |
| 96 | + tree.root.left.right = new Node(4); |
| 97 | + tree.root.left.right.right = new Node(5); |
| 98 | + tree.root.left.right.right.right = new Node(6); |
| 99 | + System.out.println( |
| 100 | + "Following are nodes in top view of Binary Tree"); |
| 101 | + tree.TopView(tree.root); |
| 102 | + } |
| 103 | +} |
0 commit comments