m2skills

bottom view java

Jun 1st, 2018
965
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.66 KB | None | 0 0
  1. // http://code2begin.blogspot.com
  2. // program to print the bottom view of a given binary tree
  3.  
  4. /**
  5.  * Created by MOHIT on 26-05-2018.
  6.  */
  7.  
  8. import java.util.*;
  9.  
  10. import static java.lang.Integer.max;
  11.  
  12.  
  13. // node class
  14. class node{
  15.     int data;
  16.     int hd;
  17.     node left;
  18.     node right;
  19.  
  20.     // function that returns a pointer to new node
  21.     public node(int element){
  22.         this.data = element;
  23.         this.hd = -1;
  24.         this.left = null;
  25.         this.right = null;
  26.  
  27.     }
  28. };
  29.  
  30.  
  31. public class Bottomview {
  32.     // function to print the bottom view of the binary tree
  33.     static void bottom_view(node root) {
  34.         if (root == null) {
  35.             return;
  36.         }
  37.  
  38.         // we keep track of the horizontal distance of the node from the root node
  39.         // to print the bottom view
  40.         int hd = 0; // hd = horizontal distance from the root node
  41.         hd = root.hd;
  42.  
  43.         // creating a Queue for storing node for level wise traversal
  44.         // and a map for mapping horizontal distances with the node data
  45.         ArrayList Q = new ArrayList<node>();
  46.         Map M = new HashMap<Integer, Integer>();
  47.  
  48.         Q.add(root);
  49.         while (!Q.isEmpty()) {
  50.  
  51.             // pop the first node from the queue
  52.             node p = (node) Q.get(0);
  53.             Q.remove(0);
  54.  
  55.             hd = p.hd;
  56.             M.put(hd, p.data);
  57.  
  58.             // increase the horizontal distance from the root node in negative direction
  59.             // and push the node on queue
  60.             if (p.left != null) {
  61.                 p.left.hd = hd - 1;
  62.                 Q.add(p.left);
  63.             }
  64.  
  65.             // increase the horizontal distance from the root node in positive direction
  66.             // and push the node on queue
  67.             if (p.right != null) {
  68.                 p.right.hd = hd + 1;
  69.                 Q.add(p.right);
  70.             }
  71.         }
  72.  
  73.         // print the generated map
  74.  
  75.         System.out.print("Bottom view of the binary tree is : ");
  76.         System.out.print(M.values() + " ");
  77.     }
  78.    
  79.     public static void main(String arg[]) {
  80.         node head = new node(1);
  81.         head.left = new node(2);
  82.         head.right = new node(3);
  83.         head.left.left = new node(4);
  84.         head.left.right = new node(5);
  85.         head.right.right = new node(6);
  86.         head.left.left.right = new node(7);
  87.         head.right.right.left = new node(8);
  88.         head.left.left.right.left = new node(9);
  89.         head.left.left.right.left.left = new node(10);
  90.         head.right.right.left.right = new node(11);
  91.         head.hd = 0;
  92.  
  93.         System.out.print("Bottom view of the binary tree is : ");
  94.         bottom_view(head);
  95.     }
  96. }
Add Comment
Please, Sign In to add comment