Advertisement
cr34tiv3

[JAVA] BinaryTree height

Jan 9th, 2013
47
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.28 KB | None | 0 0
  1.  
  2. public class BinaryTree2 {
  3.  
  4.     BinaryTree baum = new BinaryTree(7);   
  5.     BinaryTree lbaum = new BinaryTree(4);
  6.     BinaryTree llbaum = new BinaryTree(5);
  7.     BinaryTree lrbaum = new BinaryTree(9);
  8.     BinaryTree rbaum = new BinaryTree(9);
  9.     BinaryTree rlbaum = new BinaryTree(2);
  10.     BinaryTree rrbaum = new BinaryTree(8);
  11.     BinaryTree rrrbaum = new BinaryTree(8);
  12.     BinaryTree rrrrbaum = new BinaryTree(8);
  13.    
  14.    
  15.    
  16.     int zähler=0;
  17.     int zähler2=0;
  18.     int zähler3=0;
  19.     int höhegesamt=0;
  20.    
  21.    public BinaryTree2()
  22.    {
  23.         lbaum.setLeftTree(llbaum);
  24.         lbaum.setRightTree(lrbaum);
  25.         rbaum.setLeftTree(rlbaum);
  26.         rbaum.setRightTree(rrbaum);
  27.         rrbaum.setRightTree(rrrbaum);
  28.         rrrbaum.setRightTree(rrrrbaum);
  29.         baum.setLeftTree(lbaum);
  30.         baum.setRightTree(rbaum);
  31.         System.out.println(""+hoehe(lbaum));
  32.         System.out.println(""+hoehe(rbaum));
  33.    }
  34.    
  35.     public int hoehe(BinaryTree pTree){
  36.        
  37.         if(!pTree.getLeftTree().isEmpty()){
  38.             zähler++;
  39.             hoehe(pTree.getLeftTree());        
  40.            
  41.         }
  42.         if(!pTree.getRightTree().isEmpty()){
  43.             zähler++;
  44.             hoehe(pTree.getRightTree());       
  45.                        
  46.         }
  47.         if(pTree.getRightTree().isEmpty()&&pTree.getLeftTree().isEmpty()){
  48.             if(zähler>=höhegesamt){
  49.                 höhegesamt=zähler+1; 
  50.                
  51.         }
  52.             zähler=0;
  53.         }
  54.         return höhegesamt;
  55.        
  56.     }
  57.    
  58.    
  59.    
  60. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement