1. 程式人生 > >二叉樹java資料結構所有操作方法

二叉樹java資料結構所有操作方法

 為了節省篇幅,主函式沒有寫,各位技術大佬看完作者其他文章也可以給提一下意見,作者是初學者,很多地方寫的不夠好

package 演算法.二叉樹;
import java.util.Scanner;
import java.util.concurrent.Callable;

class CBType{
    String data;
    CBType left;
    CBType right;
}


public class erchashu {
    static  final  int Maxlen=20;
    static Scanner input=new Scanner(System.in);


    //c初始化二叉樹的根
    CBType IntTree()
    {
        CBType node;

        if ((node=new CBType())!=null)
        {
            System.out.print("請先輸入一個根節點\n");
            node.data=input.next();
            node.left=null;
            node.right=null;
            if (node!=null)
            {
                return  node;
            }return null;
        }
        return null;
    }

    //新增結點
    void Adddata(CBType treenode)
    {
        CBType pnode,parent;
        String data;
        int mesuel;
        if ((pnode=new CBType())!=null) {
            System.out.print("請先輸入一個根節點\n");
           pnode.data = input.next();
            pnode.left = null;
            pnode.right = null;

            System.out.println("輸入該節點的父節點資料");
            data=input.next();

            parent=Treefindnode(treenode,data);
            if (parent==null)
            {
                System.out.println("未找到父節點");
                pnode=null;
                return;
            }
            System.out.printf("1.新增該節點到左子樹\n2.新增該節點到右子樹\n");
            do {
                mesuel=input.nextInt();
                //輸入選擇項
                if (mesuel==1||mesuel==2)
                {
                    if (parent==null){
                        System.out.println("不存在父節點,請先設定一下父節點");
                    }
                    else {
                        switch (mesuel) {
                            //將資料插入到左子樹去
                            case 1:
                            if (parent.left != null)
                            {
                                System.out.println("左子樹結點不為空");
                            }else {
                                parent.left=pnode;
                            }
                            break;
                            case 2:
                                if (parent.right != null)
                                {
                                    System.out.println("右子樹結點不為空");
                                }else {
                                    parent.right=pnode;
                                }
                                break;
                                default:
                                    System.out.println("無效引數");
                        }
                    }
                }
            }while (mesuel!=1&&mesuel!=2);
        }
    }

    //查詢結點
    private CBType Treefindnode(CBType treenode, String data) {
      CBType ptr ;
      if (treenode==null)
      {
          return null;
      }
      else {
          if (treenode.data.equals(data))
          {
              return treenode;
          }else {
              if ((ptr=Treefindnode(treenode.left,data))!=null)
              {
                  return ptr;
              }else if ((ptr=Treefindnode(treenode.right,data))!=null)
              {
                  return ptr;
              }
              else 
              {
                  return null;
              }
          }
      }
    }
    
    //獲取左子樹
    CBType Treefinleft(CBType treenode)
    {
        if (treenode!=null)
        {
            return treenode.left;
        }else {
            return null;
        }
    }
    
    
    
    //獲取右子樹
    CBType Treefindright(CBType treenode)
    {
        if (treenode!=null)
        {
            return treenode.right;
        }else {
            return null;
        }
    }
    
    //判斷空樹
   int Treefindemptyt(CBType treenode)
    {
        if (treenode!=null)
        {
            return 0;
        }else {
            return 1;
        }
    }
    
    int Treefinddeep(CBType treenode)
    {
        if (treenode==null)
        {
            return 0;

        }
        else 
        {
            int ldeep=Treefinddeep(treenode.left);
            int rdeep=Treefinddeep(treenode.right);
            if (ldeep>rdeep)
            {
                return ldeep+1;
            }else 
            {
                return rdeep+1;
            }
        }
    }

    void clear(CBType treenode)
    {
        if (treenode!=null)
        {
            clear(treenode.left);
            clear(treenode.right);
            treenode=null;
        }
    }
    
    
    //顯示結點資料
    void Treeshow(CBType p)
    {
        System.out.printf("%s",p.data);
    }
    
    
    //按層遍歷
    void ancengbianli(CBType treenode)
    {
        CBType p;
        CBType q[]=new CBType[Maxlen];
        int head=0;
        int tail=0;
        if (treenode!=null)
        {
            tail=(tail+1)%Maxlen;
            q[tail]=treenode;
            /**
             * 計算迴圈佇列尾序號
             * 將二叉樹引進隊
             */
            while (head!=tail)
            {
                //佇列不為空,進行迴圈
                head=(head+1)%Maxlen;
               p=q[head];
               Treeshow(p);
               if (p.left!=null)
               {
                   tail=(tail+1)%Maxlen;
                   q[tail]=p.left;
               }
                if (p.right!=null)
                {
                    tail=(tail+1)%Maxlen;
                    q[tail]=p.right;
                }
        }
    }
    
    
}

    /**
     * 這裡面是先序中序和後序遍歷的寫法
     * @param treenode
     */
    void xianxubianli(CBType treenode)
{
    if (treenode!=null)
    {
        Treeshow(treenode);
        xianxubianli(treenode.left);
        xianxubianli(treenode.right);
    }
}
void  zhongxubianli(CBType treenode)
{
    if (treenode!=null)
    {
        zhongxubianli(treenode.left);
        Treeshow(treenode);
        zhongxubianli(treenode.right);
    }
}
void  houxubianli(CBType treenode)
{
    if (treenode!=null)
    {
        zhongxubianli(treenode.left);
        zhongxubianli(treenode.right);
        Treeshow(treenode);
    }
    
}
}