Advertisement
Guest User

Untitled

a guest
Mar 28th, 2017
106
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.82 KB | None | 0 0
  1.  
  2. public class Q1 {
  3.     public static int numNodes (BinNode <Integer>bt){
  4.         if(bt==null){
  5.             return 0;
  6.            
  7.         }
  8.         return numNodes(bt.getLeft())+numNodes(bt.getRight())+1;
  9.     }
  10.     public static int sumNodes(BinNode<Integer>bt)
  11.     {
  12.         if(bt==null)
  13.             return 0;
  14.         return sumNodes(bt.getLeft())+sumNodes(bt.getRight())+ bt.getValue();
  15.     }
  16.     public static int numPositive(BinNode<Integer>bt)
  17.  {
  18.      if(bt==null)
  19.             return 0;
  20.     int out =0;
  21.     if(bt.getValue()>0)
  22.         out =1;
  23.     return numPositive(bt.getLeft())+numPositive(bt.getRight())+out;
  24.    
  25.  }
  26.     public static int maxInTree (BinNode <Integer> bt){
  27.        
  28.         if (bt ==null)
  29.              return 0;
  30.        
  31.             int maxLeft=maxInTree(bt.getLeft());
  32.             int maxRight=maxInTree(bt.getRight());
  33.             int bigger=Math.max(maxLeft,maxRight);
  34.             return Math.max(bigger,bt.getValue());
  35.     }
  36.     public static void main(String[] args) {
  37.     public static boolean isExist(BinNode <Integer> bt,int num1){
  38.         if (bt.getValue==null)
  39.             return 0;
  40.            
  41.        
  42.         return isExist()
  43.        
  44.     }
  45.         BinNode<Integer> bt1= new BinNode <Integer>(3);
  46.          
  47.        
  48.         bt1.setRight(new BinNode<Integer>(6));
  49.         bt1.setLeft(new BinNode<Integer>(10));
  50.         BinNode pos=bt1.getLeft();
  51.         pos.setRight(new BinNode<Integer>(4));
  52.         pos.setLeft(new BinNode<Integer>(-2));
  53.         pos=bt1.getRight();
  54.         pos.setRight(new BinNode<Integer>(9));
  55.         pos.setLeft(new BinNode<Integer>(6));              
  56.         System.out.println(numNodes(bt1));
  57.         System.out.println(numPositive(bt1));
  58.         System.out.println(sumNodes(bt1));
  59.         System.out.println(maxInTree(bt1));
  60.     }
  61.      public static boolean isLeaf(BinNode<Integer>bt)
  62.         {
  63.             if(bt.getLeft()==null&&bt.getRight()==null)
  64.             {
  65.                 return true ;
  66.             }  
  67.             return false;
  68.         }
  69.      
  70. public static countLeaves(BinNode<Integer>bt)
  71.     {
  72.         if(bt==null)
  73.             return 0;
  74.         int count=0;
  75.         if(bt.getRight()==null&&0 )
  76.     }
  77. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement