最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

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

 更新時(shí)間:2019年03月13日 11:30:00   作者:水中魚之1999  
這篇文章主要介紹了Java實(shí)現(xiàn)二叉樹的建立、計(jì)算高度與遞歸輸出操作,結(jié)合實(shí)例形式分析了Java二叉樹的創(chuàng)建、遍歷、計(jì)算等相關(guān)算法實(shí)現(xiàn)技巧,需要的朋友可以參考下

本文實(shí)例講述了Java實(shí)現(xiàn)二叉樹的建立、計(jì)算高度與遞歸輸出操作。分享給大家供大家參考,具體如下:

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建立 左 子樹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建立 右 子樹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;
    }
    /*
     * 輸出樹
     */
    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訪問 左子樹:");
                output( head.GetLtree() );
            }
            if(head.GetRtree() != null){
                System.out.println("\n訪問 右子樹:");
                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() );//先訪問根
        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;//判斷哪邊回來
                flag = 1;
                choose = 1;
                if(head == head.GetFronttree().GetRtree()){
                    area = 1;//右邊回來
                    flag = 0;
                    choose = 0;
                }
                if(head == head.GetFronttree().GetLtree()){
                    area = 2;//左邊回來
                    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問完返回
                    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;//判斷哪邊回來
                flag = 1;
                choose = 1;
                if(head == head.GetFronttree().GetRtree()){
                    area = 1;//右邊回來
                    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;
    }
    /*
     * 樹內(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("獲得左子樹");
        return this.Ltree;
    }
    /*
     * 設(shè)置右子女:
     */
    public void SetRtree(Tree Rtree){
        this.Rtree = Rtree;
    }
    public Tree GetRtree(){
        System.out.println("獲得右子樹");
        return this.Rtree;
    }
}

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

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二叉樹高度-->" + link_1st.GetSave());
        link_1st.output(head);
        System.out.println("Output Over  !");
        System.out.println("\n\n<----------------前------------------>\n前序訪問根:");
        link_1st.Front_Traverse(head);
        System.out.println("\n\n<----------------中------------------>\n中序訪問根:");
        link_1st.Center_Traverse(head);
        System.out.println("\n\n<----------------后------------------>\n后序訪問根:");
        link_1st.Bottom_Traverse(head);
        System.out.println("\n\n\n\nText over !\n\n\n");
    }
}

更多關(guān)于java算法相關(guān)內(nèi)容感興趣的讀者可查看本站專題:《Java數(shù)據(jù)結(jié)構(gòu)與算法教程》、《Java操作DOM節(jié)點(diǎn)技巧總結(jié)》、《Java文件與目錄操作技巧匯總》和《Java緩存操作技巧匯總

希望本文所述對大家java程序設(shè)計(jì)有所幫助。

相關(guān)文章

  • Spring溫故而知新系列教程之AOP代理

    Spring溫故而知新系列教程之AOP代理

    Spring AOP 是代理模式的應(yīng)用,可以使用JDK提供的Proxy類或通過字節(jié)碼增強(qiáng)來實(shí)現(xiàn)。下面這篇文章主要給大家介紹了關(guān)于Spring之AOP代理的相關(guān)資料,文中通過示例代碼介紹的非常詳細(xì),需要的朋友可以參考下
    2018-05-05
  • 詳解java調(diào)用存儲(chǔ)過程并封裝成map

    詳解java調(diào)用存儲(chǔ)過程并封裝成map

    這篇文章主要介紹了詳解java調(diào)用存儲(chǔ)過程并封裝成map的相關(guān)資料,希望通過本文能幫助到大家實(shí)現(xiàn)這樣的功能,需要的朋友可以參考下
    2017-09-09
  • java實(shí)現(xiàn)圖片上傳至本地實(shí)例詳解

    java實(shí)現(xiàn)圖片上傳至本地實(shí)例詳解

    我們給大家分享了關(guān)于java實(shí)現(xiàn)圖片上傳至本地的實(shí)例以及相關(guān)代碼,有需要的朋友參考下。
    2018-08-08
  • 基于Springboot2.0構(gòu)建ES的多客戶端

    基于Springboot2.0構(gòu)建ES的多客戶端

    這篇文章主要為大家詳細(xì)介紹了基于Springboot2.0構(gòu)建ES的多客戶端,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2019-04-04
  • springboot集成測試容器重啟問題的處理

    springboot集成測試容器重啟問題的處理

    這篇文章主要介紹了springboot集成測試容器重啟問題的處理,具有很好的參考價(jià)值,希望對大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2021-11-11
  • Java中的PowerMock使用實(shí)踐

    Java中的PowerMock使用實(shí)踐

    這篇文章主要介紹了Java中的PowerMock使用實(shí)踐,@PrepareForTest和@RunWith是成對出現(xiàn)的,一般@RunWith(PowerMockRunner.class),@PrepareForTest的值是引用的靜態(tài)方法或私有方法的類,需要的朋友可以參考下
    2023-12-12
  • Spring Boot配置application.yml及根據(jù)application.yml選擇啟動(dòng)配置的操作方法

    Spring Boot配置application.yml及根據(jù)application.yml選擇啟動(dòng)配置的操作

    Spring Boot中可以選擇applicant.properties 作為配置文件,也可以通過在application.yml中進(jìn)行配置,讓Spring Boot根據(jù)你的選擇進(jìn)行加載啟動(dòng)配置文件,本文給大家介紹Spring Boot配置application.yml及根據(jù)application.yml選擇啟動(dòng)配置的操作方法,感興趣的朋友一起看看吧
    2023-10-10
  • Java字符串詳解的實(shí)例介紹

    Java字符串詳解的實(shí)例介紹

    本篇文章介紹了,在Java中關(guān)于字符串詳解一些實(shí)例操作,需要的朋友參考下
    2013-04-04
  • Mybatis實(shí)現(xiàn)單個(gè)和批量定義別名typeAliases

    Mybatis實(shí)現(xiàn)單個(gè)和批量定義別名typeAliases

    這篇文章主要介紹了Mybatis實(shí)現(xiàn)單個(gè)和批量定義別名typeAliases,具有很好的參考價(jià)值,希望對大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2021-09-09
  • Java數(shù)據(jù)結(jié)構(gòu)之線索化二叉樹的實(shí)現(xiàn)

    Java數(shù)據(jù)結(jié)構(gòu)之線索化二叉樹的實(shí)現(xiàn)

    在二叉樹的結(jié)點(diǎn)上加上線索的二叉樹稱為線索二叉樹,對二叉樹以某種遍歷方式進(jìn)行遍歷,使其變?yōu)榫€索二叉樹的過程稱為對二叉樹進(jìn)行線索化。本文將詳解如何實(shí)現(xiàn)線索化二叉樹,需要的可以參考一下
    2022-05-05

最新評(píng)論

沙河市| 南安市| 修文县| 宾阳县| 通辽市| 富锦市| 永康市| 巴东县| 中牟县| 泸西县| 喀什市| 平凉市| 安达市| 深水埗区| 苏尼特左旗| 永安市| 鸡西市| 江山市| 宝清县| 扎赉特旗| 东港市| 蛟河市| 霍州市| 岗巴县| 潼关县| 革吉县| 泰顺县| 剑川县| 彭州市| 齐齐哈尔市| 斗六市| 阳新县| 南涧| 云龙县| 江阴市| 大理市| 宿迁市| 桂林市| 巴楚县| 巴林左旗| 景德镇市|