首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > JAVA > J2SE开发 >

帮看看我这先序遍历如何写?多谢

2011-12-22 
帮看看我这先序遍历怎么写??谢谢源程序如下://二叉树查找插入显示classNode{intiDatadoublefDataNodelef

帮看看我这先序遍历怎么写??谢谢
源程序如下:
//二叉树查找   插入   显示
class   Node{
      int   iData;
      double   fData;
      Node   leftChild;
      Node   rightChild;
     
      public   void   displayNode(){
      //
      System.out.println( "{ ");
      System.out.println(iData);
      System.out.println( ", ");
      System.out.println(fData);
      System.out.println( "} ");
      }
}//end   Node

class   Tree{
private   Node   root;//
//public   Node   root;//
//public   void   find(int   key){//不可以用void
public   Node   find(int   key){
//
Node   current   =root;
while(current.iData   !=key){
        if(key <current.iData)  
        current   =current.leftChild;
        else   current   =current.rightChild;
        if   (current   ==null)  
            return   null;
}
    return   current;
}
public   void   insert(int   id,double   dd){
//
Node   newNode   =new   Node();
newNode.iData   =id;
newNode.fData   =dd;
if(root   ==null)   root   =newNode;
else   {
    Node   current   =root;
    Node   parent;
    while(true){
    parent   =current;
    if(id   <current.iData){//go   left
    current   =current.leftChild;
    if   (current   ==null){//if   end   of   the   line
          parent.leftChild=newNode;
          return;
    }
        }//end   go   left
    //}//end   while
    else{                                   //go   right
        current   =current.rightChild;
              if(current   ==null){//if   end   of   the   line
                    parent.rightChild=newNode;
                    return;
              }//end   if   end   of   the   line
    }//end   go   right
}//
}
}//end   insert
public   void   delete(int   id){
//public   boolean   delete(int   key){
//code   too   more
}


//}
///*
private   void   preOrder(Node   localRoot){//递归先续遍历   ????

if   (localRoot!=null){
System.out.println(localRoot.fData+ " ");//打印出先序的树出来???
preOrder(localRoot.leftChild);
preOrder(localRoot.rightChild);
}
}
//*/

}//end   Tree

class   TreeApp{


          public   static   void   main(String[]   args){
                Tree   theTree   =new   Tree();
              System.out.println( "start   insert   {50,1.5},{25,1.7},{75,1.9} ");
          theTree.insert(50,1.5);
          theTree.insert(25,1.7);
          theTree.insert(75,1.9);
         
         
                //theTree.preOrder(25);//????怎么给先序赋值??
         
          //Tree   found   =theTree.find(25);
          Node   found   =theTree.find(25);
          //Node   found   =   theTree.find(25);
          if(found   !=null)   {
          System.out.println( "found   the   node   with   key   25   is: ");
                  found.displayNode();
                System.out.println( "\n ");}
          else  
          System.out.println( "could   not   found   the   node   with   key   25   is: ");
         
          }//end   main
}//end   TreeApp

我怎么把输入的或固定的数据按照先序便利   打印出来??怎么赋值初始化,谢谢??

[解决办法]
private oid inOrder(Node node)
{
if(node != null)
{
System.out.println(node.iData+ " "+node.fData);
inOrder(node.leftChild);
inOrder(node.rightChild);
}
}
调用的时候把root给这个方法做参数就ok
[解决办法]
root已经是一个Node对象,直接用不就行了
theTree.preOrder(root);

热点排行