Advertisement
milkywayw

Untitled

May 19th, 2014
253
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 10.26 KB | None | 0 0
  1. import java.util.ArrayDeque;
  2. import java.util.Stack;
  3. import java.util.HashSet;
  4. import java.util.ArrayList;
  5.  
  6. public class Tree
  7. {
  8.     static int sum;
  9.    
  10.     private static class TreeNode
  11.     {
  12.         private TreeNode left, right;
  13.         private char data;
  14.         private static char id = 'a';
  15.         private int depth, height, localMax;
  16.  
  17.         public TreeNode(char x)
  18.         {
  19.             data = x;
  20.             depth = -1;
  21.         }
  22.  
  23.         public void setLeft(TreeNode n)
  24.         {
  25.             left = n;
  26.         }
  27.  
  28.         public void setRight(TreeNode n)
  29.         {
  30.             right = n;        
  31.         }
  32.  
  33.         public String toString()
  34.         {
  35.             return Character.toString(data);
  36.         }
  37.  
  38.         public char value()
  39.         {
  40.             return data;
  41.         }
  42.  
  43.         public void branch(int depth)
  44.         {
  45.             if(depth <= 0)
  46.                return;
  47.  
  48.             this.left = new TreeNode(++id);
  49.             this.right = new TreeNode(++id);
  50.  
  51.             this.left.branch(depth - 1);
  52.             this.right.branch(depth - 1);
  53.            
  54.             /*
  55.             if(this.data == 'c')
  56.                 this.right.branch(3);
  57.             */
  58.            
  59.         }
  60.  
  61.         public void _print(String soFar)
  62.         {
  63.             if(this.right != null)
  64.                 this.right._print(soFar + " .. ");
  65.  
  66.             System.out.println(soFar + this);
  67.            
  68.             if(this.left != null)
  69.                 this.left._print(soFar + " .. ");
  70.         }
  71.        
  72.         public void inOrder()
  73.         {
  74.            
  75.             if(this.left != null)
  76.                 this.left.inOrder();                
  77.            
  78.             System.out.print(this + " ");
  79.            
  80.             if(this.right != null)
  81.                 this.right.inOrder();
  82.         }
  83.        
  84.         public void preOrder()
  85.         {
  86.             System.out.print(this + " ");
  87.            
  88.             if(this.left != null)
  89.                 this.left.preOrder();
  90.            
  91.             if(this.right != null)
  92.                 this.right.preOrder();
  93.         }
  94.        
  95.         public void postOrder()
  96.         {
  97.             if(this.left != null)
  98.                 this.left.postOrder();
  99.            
  100.             if(this.right != null)
  101.                 this.right.postOrder();
  102.            
  103.             System.out.print(this + " ");
  104.         }
  105.        
  106.         public void levelOrder()
  107.         {
  108.             ArrayDeque<TreeNode> q = new ArrayDeque<TreeNode>();
  109.             TreeNode curr;
  110.            
  111.             q.add(this);
  112.            
  113.             while( ! q.isEmpty())
  114.             {
  115.                 curr = q.remove();
  116.                 System.out.print(curr + " ");
  117.                
  118.                 if(curr.left != null)
  119.                     q.add(curr.left);
  120.                    
  121.                 if(curr.right != null)                    
  122.                     q.add(curr.right);
  123.             }
  124.         }
  125.        
  126.         public void printDepths(String soFar)
  127.         {
  128.             if(this.right != null)
  129.                 this.right.printDepths(soFar + " .. ");
  130.  
  131.             System.out.println(soFar + this + "("+Integer.toString(depth)+")");
  132.            
  133.             if(this.left != null)
  134.                 this.left.printDepths(soFar + " .. ");
  135.              
  136.         }
  137.        
  138.         public void printHeights(String soFar)
  139.         {
  140.             if(this.right != null)
  141.                 this.right.printHeights(soFar + " .. ");
  142.  
  143.             System.out.println(soFar + this + "("+Integer.toString(height)+")");
  144.            
  145.             if(this.left != null)
  146.                 this.left.printHeights(soFar + " .. ");
  147.              
  148.         }
  149.        
  150.         public void calcDepths(int depth)
  151.         {
  152.             this.depth = depth;
  153.            
  154.             if(this.left != null)
  155.                 this.left.calcDepths(depth + 1);
  156.                
  157.             if(this.right != null)
  158.                 this.right.calcDepths(depth + 1);
  159.         }
  160.        
  161.         public void calcHeights()
  162.         {
  163.             if(this.left == null && this.right == null)
  164.             {
  165.                 this.height = 0;
  166.                 return;
  167.             }
  168.            
  169.             this.left.calcHeights();
  170.             this.right.calcHeights();
  171.            
  172.             this.height = 1 + Math.max(this.left.height, this.right.height);
  173.         }
  174.        
  175.         public void _calcHeights()
  176.         {
  177.             Stack<TreeNode> s = new Stack<TreeNode>();
  178.             HashSet<TreeNode> r = new HashSet<TreeNode>();
  179.            
  180.             TreeNode node = this;
  181.            
  182.             s.push(node);
  183.             r.add(node);
  184.            
  185.             while(! s.isEmpty())
  186.             {
  187.                 node = s.peek();
  188.                
  189.                 TreeNode leftChild = node.left;
  190.                 TreeNode rightChild = node.right;
  191.                
  192.                 if(leftChild == null && rightChild == null)
  193.                 {
  194.                     node.height = 0;
  195.                     s.pop();
  196.                     continue;
  197.                 }
  198.                
  199.                 if(leftChild != null && ! r.contains(leftChild))
  200.                 {
  201.                     s.push(leftChild);
  202.                     r.add(leftChild);
  203.                     continue;
  204.                 }
  205.                
  206.                 if(rightChild != null && ! r.contains(rightChild))
  207.                 {
  208.                     s.push(rightChild);
  209.                     r.add(rightChild);
  210.                     continue;
  211.                 }
  212.                
  213.                 int left = (leftChild == null) ? -1 : leftChild.height;
  214.                 int right = (rightChild == null) ? -1 : rightChild.height;
  215.                
  216.                 node.height = 1 + Math.max(left, right);
  217.                 s.pop();
  218.             }
  219.         }
  220.        
  221.         public int maxPath()
  222.         {
  223.             if(this.left == null && this.right == null)
  224.                 return 0;
  225.                
  226.             int maxPath = 0;
  227.            
  228.             if(this.left != null)
  229.                 maxPath += 1 + this.left.height;
  230.              
  231.              if(this.right != null)
  232.                 maxPath += 1 + this.right.height;
  233.            
  234.             int left = this.left.maxPath();
  235.             int right = this.right.maxPath();
  236.            
  237.             return Math.max(maxPath, Math.max(left,right));
  238.         }
  239.     }
  240.  
  241.     private TreeNode root;
  242.  
  243.     public void inOrderPrint()
  244.     {
  245.         root.inOrder();
  246.     }
  247.    
  248.     public void preOrderPrint()
  249.     {
  250.         root.preOrder();
  251.     }
  252.    
  253.     public void postOrderPrint()
  254.     {
  255.         root.postOrder();
  256.     }
  257.    
  258.     public void levelOrderPrint()
  259.     {
  260.         root.levelOrder();
  261.     }
  262.  
  263.     private Tree(char data)
  264.     {
  265.         root = new TreeNode(data);
  266.     }
  267.  
  268.     public void print()
  269.     {
  270.         root._print("");
  271.     }
  272.    
  273.     public void printDepths()
  274.     {
  275.         root.calcDepths(0);
  276.         root.printDepths("");
  277.     }
  278.    
  279.     public void printHeights()
  280.     {
  281.         root._calcHeights();
  282.         root.printHeights("");
  283.     }
  284.    
  285.     public int maxPath()
  286.     {
  287.         root._calcHeights();
  288.         return root.maxPath();
  289.     }
  290.    
  291.     public TreeNode root()
  292.     {
  293.         return root;
  294.     }
  295.  
  296.     public Tree(int depth)
  297.     {
  298.         root = new TreeNode('a');
  299.         root.branch(depth);
  300.     }
  301.        
  302.     public void getPathsFromLeft(TreeNode root, String soFar,
  303.         ArrayList<String> list)
  304.     {
  305.         if(root == null)
  306.             return;
  307.            
  308.         if(root.right != null)    
  309.             getPathsFromLeft(root.right, root.data + soFar, list);
  310.            
  311.         list.add(root.data + soFar);    
  312.        
  313.         if(root.left != null)
  314.             getPathsFromLeft(root.left, root.data + soFar, list);
  315.     }
  316.    
  317.     public void getPathsFromRight(TreeNode root, String soFar,
  318.         ArrayList<String> list)
  319.     {
  320.         if(root == null)
  321.             return;
  322.        
  323.         if(root.right != null)    
  324.             getPathsFromRight(root.right, soFar + root.data, list);
  325.            
  326.         list.add(soFar + root.data);    
  327.        
  328.         if(root.left != null)
  329.             getPathsFromRight(root.left, soFar + root.data, list);
  330.     }
  331.    
  332.     public void printPathsFromNode(TreeNode root)
  333.     {
  334.         if(root == null)
  335.             return;
  336.            
  337.         int counter = 0;
  338.        
  339.         ArrayList<String> left = new ArrayList<String>();
  340.         getPathsFromLeft(root.left, "", left);
  341.        
  342.         ArrayList<String> right = new ArrayList<String>();
  343.         getPathsFromRight(root.right, "", right);
  344.            
  345.         for(String r : right)
  346.             counter++;//System.out.println(root.data + r);
  347.            
  348.         counter++;//System.out.println(root);    
  349.        
  350.         for(String s : left)
  351.             counter++;//System.out.println(s + root.data);
  352.            
  353.         for(String r : right)
  354.             for(String s : left)
  355.                 counter++;//System.out.println(s + root + r);
  356.                
  357.         //System.out.println(counter);
  358.         sum += counter;
  359.     }
  360.    
  361.     public void printPaths()
  362.     {
  363.         _printPaths(root);
  364.     }
  365.    
  366.     public void _printPaths(TreeNode root)
  367.     {
  368.         printPathsFromNode(root);
  369.        
  370.         if(root.left != null)
  371.             _printPaths(root.left);
  372.            
  373.         if(root.right != null)
  374.             _printPaths(root.right);
  375.     }
  376.  
  377.     public static void main(String[] args)
  378.     {
  379.         Tree tree = new Tree(5);
  380.         //tree.print();
  381.        
  382.         /*
  383.         System.out.println("\n\ninorder:");
  384.         tree.inOrderPrint();
  385.        
  386.         System.out.println("\n\npreorder:");
  387.         tree.preOrderPrint();
  388.        
  389.         System.out.println("\n\npostorder:");
  390.         tree.postOrderPrint();
  391.        
  392.         System.out.println("\n\nlevelorder:");
  393.         tree.levelOrderPrint();
  394.        
  395.         System.out.println("\n\ndepths");
  396.         tree.printDepths();
  397.        
  398.         System.out.println("\n\nheights");
  399.         tree.printHeights();
  400.        
  401.         System.out.println("\n\nmaxpath = " + tree.maxPath());
  402.         */
  403.        
  404.         tree.printPaths();
  405.         System.out.println(sum);
  406.     }
  407. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement