m2skills

top view java

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