m2skills

vertical order bt java

Jun 1st, 2018
44
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 3.00 KB | None | 0 0
  1. // http://code2begin.blogspot.com
  2. // program to print the given binary tree in vertical order
  3.  
  4. /**
  5.  * Created by MOHIT on 25-05-2018.
  6.  */
  7.  
  8. import java.io.*;
  9. import java.lang.reflect.Array;
  10. import java.util.*;
  11.  
  12. import static java.lang.Integer.max;
  13.  
  14.  
  15. // node class
  16. class node{
  17.     int data;
  18.     int hd;
  19.     node left;
  20.     node right;
  21.  
  22.     // function that returns a pointer to new node
  23.     public node(int element){
  24.         this.data = element;
  25.         this.hd = -1;
  26.         this.left = null;
  27.         this.right = null;
  28.        
  29.     }
  30. };
  31.  
  32.  
  33. public class BinaryTree {
  34.    
  35.     // function to print the nodes of the binary tree in vertical order
  36.     // using horizontal distance of a node from the root node
  37.     static void vertical_order(node root){
  38.         if(root == null){
  39.             return;
  40.         }
  41.  
  42.         // initialize a map and queue
  43.         // we will use iterative BFS traversal to solve this
  44.         Map M = new HashMap<Integer, Integer>();
  45.         ArrayList<ArrayList<Integer>> A = new ArrayList();
  46.         ArrayList Q = new ArrayList<node>();;
  47.  
  48.         // push root in queue and initialize horizontal distance
  49.         Q.add(root);
  50.         int horizontal_distance = root.hd;
  51.  
  52.         while(!Q.isEmpty()){
  53.             node current = (node)Q.get(0);
  54.             Q.remove(0);
  55.             if(M.containsKey(current.hd)){
  56.                 int index = (int)M.get(current.hd);
  57.                 A.get(index).add(current.data);
  58.             }else{
  59.                 A.add(new ArrayList<>());
  60.                 int index = A.size() - 1;
  61.                 A.get(index).add(current.data);
  62.                 M.put(current.hd, index);
  63.             }
  64.  
  65.             // update horizontal distance of the left child and put it in the queue
  66.             if(current.left != null){
  67.                 current.left.hd = current.hd - 1;
  68.                 Q.add(current.left);
  69.             }
  70.             // update horizontal distance of the right child and put it in the queue
  71.             if(current.right != null) {
  72.                 current.right.hd = current.hd + 1;
  73.                 Q.add(current.right);
  74.             }
  75.         }
  76.  
  77.         List<Integer> a = new ArrayList<>(M.keySet());
  78.         for(int hd:a){
  79.             int index = (int)M.get(hd);
  80.             System.out.println("Nodes at horizontal distance " + hd + " are : " + A.get(index));
  81.         }
  82.     }
  83.  
  84.     public static void main(String arg[]) {
  85.         node head = new node(1);
  86.         head.left = new node(2);
  87.         head.right = new node(3);
  88.         head.left.left = new node(4);
  89.         head.left.right = new node(5);
  90.         head.right.right = new node(6);
  91.         head.left.left.right = new node(7);
  92.         head.right.right.left = new node(8);
  93.         head.left.left.right.left = new node(9);
  94.         head.left.left.right.left.left = new node(10);
  95.         head.right.right.left.right = new node(11);
  96.         head.hd = 0;
  97.  
  98.         System.out.println("Vertical order traversal of the given tree is : ");
  99.         vertical_order(head);
  100.  
  101.     }
  102. }
Advertisement
Add Comment
Please, Sign In to add comment