Advertisement
Guest User

Untitled

a guest
Apr 1st, 2015
246
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.69 KB | None | 0 0
  1. /* package whatever; // don't place package name! */
  2.  
  3. import java.util.*;
  4. import java.lang.*;
  5. import java.io.*;
  6.  
  7. /* Name of the class has to be "Main" only if the class is public. */
  8. class Ideone
  9. {
  10.     static class Node {
  11.         Node left;
  12.         Node right;
  13.         int value;
  14.         boolean set;
  15.         public Node(int value) {
  16.             this.value = value;
  17.             set = true;
  18.         }
  19.         public Node(boolean set) {
  20.             set = set;
  21.         }
  22.     }
  23.    
  24.     public static Node convertToTree(int[] arr) {
  25.         if (arr.length == 0) { return null; }
  26.         return cttHelper(arr, 0, arr.length-1);
  27.     }
  28.  
  29.     public static Node cttHelper(int[] arr, int i, int j) {
  30.         if (i==j) {
  31.             return new Node(arr[i]);
  32.         } else {
  33.             int mid = (i+j) / 2;
  34.             Node root = new Node(arr[mid]);
  35.             root.right = cttHelper(arr, mid+1, j);
  36.             if (mid-1 >= i) {
  37.                 root.left = cttHelper(arr, i, mid-1);
  38.             }
  39.             return root;
  40.         }
  41.     }
  42.    
  43.     public static void printTree(Node n) {
  44.         if (n == null) { System.out.println("null"); return; }
  45.        
  46.         Queue<Node> q1 = new LinkedList<Node>();
  47.         Queue<Node> q2 = new LinkedList<Node>();
  48.        
  49.         int d = getMaxDepth(n);
  50.         int spacing = (int) Math.pow(2, d-1) * 4;
  51.        
  52.         q1.add(n);
  53.         while (!q1.isEmpty()) {
  54.             // Because we're adding dummy nodes, need to know when to quit
  55.             if (d-- < 1) {
  56.                 break;
  57.             }
  58.            
  59.             for (Node node : q1) {
  60.                 if (node.left != null) {
  61.                     q2.add(node.left);
  62.                 } else {
  63.                     q2.add(new Node(false));
  64.                 }
  65.                 if (node.right != null) {
  66.                     q2.add(node.right);
  67.                 } else {
  68.                     q2.add(new Node(false));
  69.                 }
  70.             }
  71.            
  72.             if (q1.peek().set) {
  73.                 System.out.printf("%"+spacing/2+"d", q1.remove().value);
  74.             } else {
  75.                 insertSpaces(spacing/2);
  76.                 q1.remove();
  77.             }
  78.            
  79.             while(!q1.isEmpty()) {
  80.                 if (q1.peek().set) {
  81.                     System.out.printf("%"+spacing+"d", q1.remove().value);
  82.                 } else {
  83.                     insertSpaces(spacing);
  84.                     q1.remove();
  85.                 }
  86.             }
  87.            
  88.             while(!q2.isEmpty()) {
  89.                 q1.add(q2.remove());
  90.             }
  91.            
  92.             spacing/=2;
  93.             System.out.println();
  94.         }
  95.     }
  96.    
  97.     public static void insertSpaces(int d) {
  98.         for (int i = 0; i < d; ++i) {
  99.             System.out.print(" ");
  100.         }
  101.     }
  102.    
  103.     public static int getMaxDepth(Node n) {
  104.         if (n == null) {
  105.             return -1;
  106.         }
  107.        
  108.         return getMaxDepthHelper(n, 0);
  109.     }
  110.    
  111.     public static int getMaxDepthHelper(Node n, int d) {
  112.         if (n == null) {
  113.             return d;
  114.         } else {
  115.             int leftMaxDepth = getMaxDepthHelper(n.left, d+1);
  116.             int rightMaxDepth = getMaxDepthHelper(n.right, d+1);
  117.             return Math.max(leftMaxDepth, rightMaxDepth);
  118.         }
  119.     }
  120.    
  121.     public static void main (String[] args) throws java.lang.Exception
  122.     {
  123.         // your code goes here
  124.         int[] arr = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12};
  125.         Node root = convertToTree(arr);
  126.         printTree(root);
  127.     }
  128. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement