真实的国产乱ⅩXXX66竹夫人,五月香六月婷婷激情综合,亚洲日本VA一区二区三区,亚洲精品一区二区三区麻豆

成都創(chuàng)新互聯(lián)網(wǎng)站制作重慶分公司

Java如何實(shí)現(xiàn)二叉樹(shù)的建立、計(jì)算高度與遞歸輸出操作

這篇文章主要介紹Java如何實(shí)現(xiàn)二叉樹(shù)的建立、計(jì)算高度與遞歸輸出操作,文中介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們一定要看完!

創(chuàng)新互聯(lián)建站專注于余干企業(yè)網(wǎng)站建設(shè),響應(yīng)式網(wǎng)站,商城建設(shè)。余干網(wǎng)站建設(shè)公司,為余干等地區(qū)提供建站服務(wù)。全流程按需設(shè)計(jì),專業(yè)設(shè)計(jì),全程項(xiàng)目跟蹤,創(chuàng)新互聯(lián)建站專業(yè)和態(tài)度為您提供的服務(wù)

具體如下:

1. 建立 遞歸輸出 計(jì)算高度 前中后三種非遞歸輸出

public class Tree_Link {
    private int save = 0;
    private int now = 0;
    Scanner sc = new Scanner(System.in);
    /*
     * 構(gòu)造函數(shù)
     */
    Tree_Link(){
    }
    /*
     * 鏈表建立
     */
    public Tree Link_Build(Tree head){
//        Tree head = new Tree();//頭節(jié)點(diǎn)
        System.out.println("繼續(xù)code:1");
        int flag = sc.nextInt();
        if(flag != 1){
            return head;
        }else{
            System.out.println("\n\n\n輸入 節(jié)點(diǎn)信息:");
            head.SetCode(sc.nextInt());
            System.out.println("\n建立 左 子樹(shù)code:1  否則:0");
            flag = sc.nextInt();
            if(flag == 1){
                now++;
                Tree LTree = new Tree();
                head.SetLtree(LTree);  
                LTree.SetFronttree(head);//設(shè)置父母節(jié)點(diǎn)
                Link_Build( head.GetLtree() );
            }
            System.out.println("\n當(dāng)前位置:" + head.GetCode());
            System.out.println("\n建立 右 子樹(shù)code:1  否則:0");
            flag = sc.nextInt();
            if(flag == 1){
                now++;
                Tree Rtree = new Tree();
                head.SetRtree(Rtree);
                Rtree.SetFronttree(head);//設(shè)置父母節(jié)點(diǎn)
                Link_Build( head.GetRtree() );
            }
            if( now > save ){
                save = now;
            }
            now--;
        }
        return head;
    }
    /*
     * 輸出樹(shù)
     */
    public Tree output(Tree head){
        int flag;
        if(head.GetCode() == -1){
            return head;
        }else{
            System.out.println("\n當(dāng)前位置:" + head.GetCode());
            System.out.println(head.GetLtree() != null);
            if(head.GetLtree() != null){
                System.out.println("\n訪問(wèn) 左子樹(shù):");
                output( head.GetLtree() );
            }
            if(head.GetRtree() != null){
                System.out.println("\n訪問(wèn) 右子樹(shù):");
                output( head.GetRtree() );
            }
        }
        return head;
    }
    /*
     * 獲得高度
     */
    public int GetSave(){
        return this.save;
    }
    /*
     * 非遞歸 前序遍歷
     */
    public void Front_Traverse(Tree head){
        Tree star = head;//退出標(biāo)記
        int choose = 1; //左
        int flag = 1;  //右
        System.out.println( "<---前序遍歷--->" + head.GetCode() );//先訪問(wèn)根
        while(true){
            if( head.GetLtree() != null && choose != 0 ){
                head = head.GetLtree();
                System.out.println( "<---前序遍歷--->" + head.GetCode() );//獲得信息
                flag = 1;
            }else if( head.GetRtree() != null && flag != 0 ){
                head = head.GetRtree();
                System.out.println( "<---前序遍歷--->" + head.GetCode() );
                choose = 1;
            }else if( flag == 0 && choose == 0 && head == star){
                break;
            }else{
                if(head == head.GetFronttree().GetRtree()){
                    flag = 0;
                    choose = 0;
                }
                if(head == head.GetFronttree().GetLtree()){
                    choose = 0;
                    flag = 1;
                }
                head = head.GetFronttree();
                System.out.println("獲得 父母" + head.GetCode());
                System.out.println( "\n\n\n" );
            }
        }
    }
    /*
     * 非遞歸 中序遍歷
     */
    public void Center_Traverse(Tree head){
        Tree star = head;//退出標(biāo)記
        int choose = 1; //左
        int flag = 1;  //右
        while(true){
            if( head.GetLtree() != null && choose != 0 ){
                head = head.GetLtree();
                flag = 1;
            }else if( head.GetRtree() != null && flag != 0 ){
                if(head.GetLtree() == null){//因?yàn)樽筮厼榭斩祷?
                    System.out.println( "<-1--中序遍歷--->" + head.GetCode());
                }
                head = head.GetRtree();
                choose = 1;
            }else if( flag == 0 && choose == 0 && head == star){
                break;
            }else{
                int area = 0;//判斷哪邊回來(lái)
                flag = 1;
                choose = 1;
                if(head == head.GetFronttree().GetRtree()){
                    area = 1;//右邊回來(lái)
                    flag = 0;
                    choose = 0;
                }
                if(head == head.GetFronttree().GetLtree()){
                    area = 2;//左邊回來(lái)
                    choose = 0;
                    flag = 1;
                }
                if( head.GetLtree() == null && head.GetRtree() == null ){//因?yàn)樽筮厼榭斩祷?
                    System.out.println( "<-2--中序遍歷--->" + head.GetCode());
                }
                head = head.GetFronttree();
                if( area == 2){//因?yàn)樽筮呍L問(wèn)完返回
                    System.out.println( "<-3--中序遍歷--->" + head.GetCode());
                }
                System.out.println("獲得 父母" + head.GetCode());
                System.out.println( "\n\n\n" );
            }
        }
    }
    /*
     * 非遞歸 后續(xù)遍歷
     */
    public void Bottom_Traverse(Tree head){
        Tree star = head;//退出標(biāo)記
        int choose = 1; //左
        int flag = 1;  //右
        while(true){
            if( head.GetLtree() != null && choose != 0 ){
                head = head.GetLtree();
                flag = 1;
            }else if( head.GetRtree() != null && flag != 0 ){
                head = head.GetRtree();
                choose = 1;
            }else if( flag == 0 && choose == 0 && head == star){
                break;
            }else{
                int area = 0;//判斷哪邊回來(lái)
                flag = 1;
                choose = 1;
                if(head == head.GetFronttree().GetRtree()){
                    area = 1;//右邊回來(lái)
                    flag = 0;
                    choose = 0;
                }
                if(head == head.GetFronttree().GetLtree()){
                    choose = 0;
                    flag = 1;
                }
                if(head.GetRtree() == null){//因?yàn)橛疫厼榭斩祷?
                    System.out.println( "<-1--后序遍歷--->" + head.GetCode());
                }
                head = head.GetFronttree();
                if( area == 1){
                    System.out.println( "<-2--后序遍歷--->" + head.GetCode());
                }
                System.out.println("獲得 父母" + head.GetCode());
                System.out.println( "\n\n\n" );
            }
        }
    }
}

2. Tree 類實(shí)現(xiàn):

public class Tree {
    private int code = -1;
    private Tree Fonttree;
    private Tree Ltree;
    private Tree Rtree;
    Tree(){
        this.code = -1;
        this.Ltree = null;
        this.Rtree = null;
    }
    /*
     * 樹(shù)內(nèi)容查看方法:
     */
    public void SetCode(int code){//設(shè)置編號(hào)
        this.code = code;
    }
    public int GetCode(){     //獲取編號(hào)
        return this.code;
    }
    /*
     * 設(shè)置父母指針:
     */
    public void SetFronttree(Tree Front){
        this.Fonttree = Front;
    }
    public Tree GetFronttree(){
        System.out.println("獲得 父母");
        return this.Fonttree;
    }
    /*
     * 設(shè)置左子女:
     */
    public void SetLtree(Tree Ltree){
        this.Ltree = Ltree;
    }
    public Tree GetLtree(){
        System.out.println("獲得左子樹(shù)");
        return this.Ltree;
    }
    /*
     * 設(shè)置右子女:
     */
    public void SetRtree(Tree Rtree){
        this.Rtree = Rtree;
    }
    public Tree GetRtree(){
        System.out.println("獲得右子樹(shù)");
        return this.Rtree;
    }
}

3. 主函數(shù)測(cè)試:

public class MainActivity {
    Scanner sc = new Scanner(System.in);
    public static void main(String[] args) {
        Tree head = new Tree();
        Tree_Link link_1st = new Tree_Link();
        head = link_1st.Link_Build(head);
        System.out.println("Build succeed !");
        System.out.println("\n二叉樹(shù)高度-->" + link_1st.GetSave());
        link_1st.output(head);
        System.out.println("Output Over  !");
        System.out.println("\n\n<----------------前------------------>\n前序訪問(wèn)根:");
        link_1st.Front_Traverse(head);
        System.out.println("\n\n<----------------中------------------>\n中序訪問(wèn)根:");
        link_1st.Center_Traverse(head);
        System.out.println("\n\n<----------------后------------------>\n后序訪問(wèn)根:");
        link_1st.Bottom_Traverse(head);
        System.out.println("\n\n\n\nText over !\n\n\n");
    }
}

以上是“Java如何實(shí)現(xiàn)二叉樹(shù)的建立、計(jì)算高度與遞歸輸出操作”這篇文章的所有內(nèi)容,感謝各位的閱讀!希望分享的內(nèi)容對(duì)大家有幫助,更多相關(guān)知識(shí),歡迎關(guān)注創(chuàng)新互聯(lián)行業(yè)資訊頻道!


分享名稱:Java如何實(shí)現(xiàn)二叉樹(shù)的建立、計(jì)算高度與遞歸輸出操作
文章網(wǎng)址:http://weahome.cn/article/psdsoc.html

其他資訊

在線咨詢

微信咨詢

電話咨詢

028-86922220(工作日)

18980820575(7×24)

提交需求

返回頂部