Advertisement
strydez

Untitled

May 6th, 2021
855
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.27 KB | None | 0 0
  1. public class TreeNode {
  2.    
  3.         private int data;
  4.         private TreeNode rightChild;
  5.         private TreeNode leftChild;
  6.         private static int COUNT;
  7.        
  8.         public TreeNode(int data) {
  9.             this.data = data;
  10.             this.rightChild = null;
  11.             this.leftChild = null;
  12.         }
  13.        
  14.         public static void fillInLevel(int level, TreeNode root) {
  15.            
  16.             helper( root, level);
  17.         }
  18.        
  19.    
  20.        
  21.         public static void helper( TreeNode node, int level) {
  22.            
  23.             // If it's empty and within level, fill -1
  24.            
  25.             if ( node == null)
  26.                 node = new TreeNode(-1);
  27.            
  28.             if ( node.leftChild == null )
  29.                 node.setLeftChild(new TreeNode(-1));
  30.            
  31.             if ( node.rightChild == null )
  32.                 node.setRightChild(new TreeNode(-1));
  33.            
  34.             while( level > 1) {
  35.                 helper(node.leftChild, level-=1);
  36.                 helper(node.rightChild, level-=1);
  37.             }
  38.             return;
  39.            
  40.         }
  41.    
  42.         public TreeNode getRightChild() {
  43.             return rightChild;
  44.         }
  45.    
  46.         public void setRightChild(TreeNode rightChild) {
  47.             COUNT++;
  48.             this.rightChild = rightChild;
  49.         }
  50.    
  51.         public TreeNode getLeftChild() {
  52.             return leftChild;
  53.         }
  54.    
  55.         public void setLeftChild(TreeNode leftChild) {
  56.             COUNT++;
  57.             this.leftChild = leftChild;
  58.         }
  59.        
  60.         static void print2DUtil(TreeNode root, int space)
  61.         {
  62.             // Base case
  63.             if (root == null)
  64.                 return;
  65.          
  66.             // Increase distance between levels
  67.             space += COUNT;
  68.          
  69.             // Process right child first
  70.             print2DUtil(root.rightChild, space);
  71.          
  72.             // Print current node after space
  73.             // count
  74.             System.out.print("\n");
  75.             for (int i = COUNT; i < space; i++)
  76.                 System.out.print(" ");
  77.             System.out.print(root.data + "\n");
  78.          
  79.             // Process left child
  80.             print2DUtil(root.leftChild, space);
  81.         }
  82.          
  83.         // Wrapper over print2DUtil()
  84.         static void print2D(TreeNode root)
  85.         {
  86.             // Pass initial space count as 0
  87.             print2DUtil(root, 0);
  88.         }
  89.  
  90.  
  91. Current result
  92.  
  93.                       -1
  94.  
  95.            7
  96.  
  97.                       -1
  98.  
  99. 2
  100.  
  101.                                  -1
  102.  
  103.                       13
  104.  
  105.                                  -1
  106.  
  107.            5
  108.  
  109.                                  11
  110.  
  111.                       3
  112.  
  113.                                             31
  114.  
  115.                                  29
  116.        
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement