|
| 1 | +// Java Program to print Bottom View of Binary Tree |
| 2 | +import java.util.*; |
| 3 | +import java.util.Map.Entry; |
| 4 | + |
| 5 | +// Tree node class |
| 6 | +class Node |
| 7 | +{ |
| 8 | + int data; //data of the node |
| 9 | + int hd; //horizontal distance of the node |
| 10 | + Node left, right; //left and right references |
| 11 | + |
| 12 | + // Constructor of tree node |
| 13 | + public Node(int key) |
| 14 | + { |
| 15 | + data = key; |
| 16 | + hd = Integer.MAX_VALUE; |
| 17 | + left = right = null; |
| 18 | + } |
| 19 | +} |
| 20 | + |
| 21 | +//Tree class |
| 22 | +class Tree |
| 23 | +{ |
| 24 | + Node root; //root node of tree |
| 25 | + |
| 26 | + // Default constructor |
| 27 | + public Tree() {} |
| 28 | + |
| 29 | + // Parameterized tree constructor |
| 30 | + public Tree(Node node) |
| 31 | + { |
| 32 | + root = node; |
| 33 | + } |
| 34 | + |
| 35 | + // Method that prints the bottom view. |
| 36 | + public void bottomView() |
| 37 | + { |
| 38 | + if (root == null) |
| 39 | + return; |
| 40 | + |
| 41 | + // Initialize a variable 'hd' with 0 for the root element. |
| 42 | + int hd = 0; |
| 43 | + |
| 44 | + // TreeMap which stores key value pair sorted on key value |
| 45 | + Map<Integer, Integer> map = new TreeMap<>(); |
| 46 | + |
| 47 | + // Queue to store tree nodes in level order traversal |
| 48 | + Queue<Node> queue = new LinkedList<Node>(); |
| 49 | + |
| 50 | + // Assign initialized horizontal distance value to root |
| 51 | + // node and add it to the queue. |
| 52 | + root.hd = hd; |
| 53 | + queue.add(root); |
| 54 | + |
| 55 | + // Loop until the queue is empty (standard level order loop) |
| 56 | + while (!queue.isEmpty()) |
| 57 | + { |
| 58 | + Node temp = queue.remove(); |
| 59 | + |
| 60 | + // Extract the horizontal distance value from the |
| 61 | + // dequeued tree node. |
| 62 | + hd = temp.hd; |
| 63 | + |
| 64 | + // Put the dequeued tree node to TreeMap having key |
| 65 | + // as horizontal distance. Every time we find a node |
| 66 | + // having same horizontal distance we need to replace |
| 67 | + // the data in the map. |
| 68 | + map.put(hd, temp.data); |
| 69 | + |
| 70 | + // If the dequeued node has a left child add it to the |
| 71 | + // queue with a horizontal distance hd-1. |
| 72 | + if (temp.left != null) |
| 73 | + { |
| 74 | + temp.left.hd = hd-1; |
| 75 | + queue.add(temp.left); |
| 76 | + } |
| 77 | + // If the dequeued node has a right child add it to the |
| 78 | + // queue with a horizontal distance hd+1. |
| 79 | + if (temp.right != null) |
| 80 | + { |
| 81 | + temp.right.hd = hd+1; |
| 82 | + queue.add(temp.right); |
| 83 | + } |
| 84 | + } |
| 85 | + |
| 86 | + // Extract the entries of map into a set to traverse |
| 87 | + // an iterator over that. |
| 88 | + Set<Entry<Integer, Integer>> set = map.entrySet(); |
| 89 | + |
| 90 | + // Make an iterator |
| 91 | + Iterator<Entry<Integer, Integer>> iterator = set.iterator(); |
| 92 | + |
| 93 | + // Traverse the map elements using the iterator. |
| 94 | + while (iterator.hasNext()) |
| 95 | + { |
| 96 | + Map.Entry<Integer, Integer> me = iterator.next(); |
| 97 | + System.out.print(me.getValue()+" "); |
| 98 | + } |
| 99 | + } |
| 100 | +} |
| 101 | + |
| 102 | +// Main driver class |
| 103 | +public class BottomView |
| 104 | +{ |
| 105 | + public static void main(String[] args) |
| 106 | + { |
| 107 | + Node root = new Node(20); |
| 108 | + root.left = new Node(8); |
| 109 | + root.right = new Node(22); |
| 110 | + root.left.left = new Node(5); |
| 111 | + root.left.right = new Node(3); |
| 112 | + root.right.left = new Node(4); |
| 113 | + root.right.right = new Node(25); |
| 114 | + root.left.right.left = new Node(10); |
| 115 | + root.left.right.right = new Node(14); |
| 116 | + Tree tree = new Tree(root); |
| 117 | + System.out.println("Bottom view of the given binary tree:"); |
| 118 | + tree.bottomView(); |
| 119 | + } |
| 120 | +} |
0 commit comments