当前位置: 代码迷 >> Java相关 >> 关于二叉树的问题
  详细解决方案

关于二叉树的问题

热度:288   发布时间:2009-10-05 10:22:41.0
关于二叉树的问题
public class Tree{
      Node pera;
      //Node pera1=new Node();
     //建树
     public  Node creattree(Node b,int a){
           
           if(pera==null){
                return (new Node(a));
               
           }else if(a>=pera.data){
                    
                     
                      pera.right=creattree(pera.right,a);
                      return pera;
                       
                     }else{
                        
                       pera.left=creattree(pera.left,a);
                         return pera;
                        
                         }
            
             //return pera;     
         }

    //遍历
      public void treeout(Node pera1){
         
              if(pera==null)
               return;
              System.out.println(pera.data);
              treeout(pera.left);
              treeout(pera.right);
         
          }
     //查找
      public  boolean chazhao(Node pera,int a){
         
               boolean k=false;
           if(pera.data==a)
                  k=true;
                chazhao(pera.left,a);
                chazhao(pera.right,a);
                 return k;
          }
         
      //计算高度
          public int leaves(){
              
                int leaveno;
                if(pera==null)
                 return -1;
               
                int leaveleft=(pera.left==null?0:leaves());
                int leaveright=(pera.right==null?0:leaves());
                leaveno=(leaveleft<leaveright)?leaveleft:leaveright;
                return leaveno;
              }
}

class Node{
   int data;
   Node left,right;
   
   public Node(){
      
        data=0;
        left=right=null;
      
       }
      
   public Node(int data){
      
        this.data=data;
        left=right=null;
      
       }

}



class text1{

   public static void main(String[] arge){
           
           Tree B=new Tree();
           //Tree C=new Tree();
            for(int i=1;i<5;i++){
                  
                  Node A=new Node(i);
            
                  B.creattree(A,i);
               
                }
               
               
             B.treeout(B.pera);   
         boolean k=B.chazhao(B.pera,4);
             int a=B.leaves();
             System.out.println(a);
             System.out.println(k);
       }

}

这段程序编译通过了,但执行就找不到入口不知道是什么原因?
高手指点一下!!
搜索更多相关的解决方案: 二叉树  

----------------解决方案--------------------------------------------------------
main函数只能出现在公共类中也就是public class中
----------------解决方案--------------------------------------------------------
也就是必须在主类中

----------------解决方案--------------------------------------------------------
  相关解决方案