28.前中后序查找

1.前中后序查找思路分析
1.1前序查找
- 判斷與當(dāng)前節(jié)點(diǎn)是否相等,如果相等則返回當(dāng)前節(jié)點(diǎn),否則判斷當(dāng)前節(jié)點(diǎn)的左子節(jié)點(diǎn)是否為空
- 如果左子節(jié)點(diǎn)不為空徘铝,則遞歸前序查找左子樹南用,找到就返回節(jié)點(diǎn),否則就繼續(xù)判斷當(dāng)前節(jié)點(diǎn)右子節(jié)點(diǎn)是否為空
- 如果右子節(jié)點(diǎn)不為空仿野,則遞歸前序查找右子樹,找到就返回節(jié)點(diǎn),否則返回null
//前序查找
    public HeroNode preIndexSearch(int no){
        //判斷與當(dāng)前節(jié)點(diǎn)no是否相同典尾,相同返回
        if(this.no==no){
            return this;
        }
        //判斷左子節(jié)點(diǎn)是否為空,如果不為空糊探,遞歸前序查詢左子樹,如果相等钾埂,返回
        HeroNode temp = null;
        if(this.left!=null){
            temp =  this.left.preIndexSearch(no);
        }
        if(temp!=null){
            return temp;
        }
        if(this.right!=null){
            temp = this.right.preIndexSearch(no);
        }
        return temp;
    }
1.2中序查找
- 判斷當(dāng)前節(jié)點(diǎn)的左子節(jié)點(diǎn)是否為空,如果不為空就遞歸中序查找科平,找到就返回
- 否則與當(dāng)前節(jié)點(diǎn)比較褥紫,如果相等就返回,否則繼續(xù)判斷當(dāng)前節(jié)點(diǎn)的右子節(jié)點(diǎn)是否為空
-如果右子節(jié)點(diǎn)不為空瞪慧,就遞歸中序查找右子樹故源,找到就返回,否則返回null
 //中序查找
    public HeroNode infixIndexSearch(int no){
        HeroNode temp = null;
        if(this.left!=null){
            temp = this.left.infixIndexSearch(no);
        }
        if(temp!=null){
            return temp;
        }
        if (this.no==no){
            return this;
        }
        if(this.right!=null){
            temp = this.right.infixIndexSearch(no);
        }
        return temp;
    }
1.3后序查找
- 判斷當(dāng)前節(jié)點(diǎn)的左子樹是否為空汞贸,如果不為空绳军,則遞歸后序查找左子樹,找到就返回
-否則判斷當(dāng)前節(jié)點(diǎn)的右子節(jié)點(diǎn)是否為空矢腻,如果不為空门驾,則遞歸查找右子樹,找到就返回
- 否則與當(dāng)前節(jié)點(diǎn)比較多柑,相等返回當(dāng)前節(jié)點(diǎn)奶是,否則返回null
//后序查找
    public HeroNode postIndexSearch(int no){
        HeroNode temp = null;
        if(this.left!=null){
            temp = this.left.postIndexSearch(no);
        }
        if(temp!=null){
            return temp;
        }
        if(this.right!=null){
            temp = this.right.postIndexSearch(no);
        }
        if(temp!=null){
            return temp;
        }
        if(this.no==no){
            return this;
        }
        return temp;
    }

2.完整代碼

package com.yc.day06;

public class BinaryTreeDemo {
    public static void main(String[] args) {
        BinaryTree binaryTree = new BinaryTree();
        HeroNode root = new HeroNode(1, "宋江");
        HeroNode node2 = new HeroNode(2, "吳用");
        HeroNode node3 = new HeroNode(3, "盧俊義");
        HeroNode node4 = new HeroNode(4, "林沖");
        binaryTree.setRoot(root);
        root.setLeft(node2);
        root.setRight(node3);
        node3.setRight(node4);
        System.out.println("前序遍歷:");
        binaryTree.preSort();
        System.out.println("");
        HeroNode heroNode = binaryTree.preIndexSearch(2);
        System.out.println("前序查找結(jié)果:"+heroNode);
        HeroNode heroNode1 = binaryTree.infixIndexSearch(2);
        System.out.println("中序查找結(jié)果:"+heroNode1);
        HeroNode heroNode2 = binaryTree.postIndexSearch(2);
        System.out.println("后序查找結(jié)果:"+heroNode2);
    }
}
//創(chuàng)建二叉樹
class BinaryTree{
    HeroNode root;
    public void setRoot(HeroNode heroNode){
        this.root = heroNode;
    }
    //前序遍歷
    public void preSort(){
        root.preSort();
    }
    //中序遍歷
    public void infixSort(){
        root.infixSort();
    }
    //后序遍歷
    public void postSort(){
        root.postSort();
    }
    //前序查找
    public HeroNode preIndexSearch(int no){
        if(root!=null){
            return root.preIndexSearch(no);
        }
       return null;
    }
    //中序查找
    public HeroNode infixIndexSearch(int no){
        if(root!=null){
            return root.infixIndexSearch(no);
        }
        return null;
    }
    //后序查找
    public HeroNode postIndexSearch(int no){
        if(root!=null) {
            return root.postIndexSearch(no);
        }
        return null;
    }
}
//創(chuàng)建節(jié)點(diǎn)類
class HeroNode{
    private int no;
    private String name;
    private HeroNode left;
    private HeroNode right;

    public HeroNode() {
    }

    public HeroNode(int no, String name) {
        this.no = no;
        this.name = name;
    }

    public int getNo() {
        return no;
    }

    public void setNo(int no) {
        this.no = no;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public HeroNode getLeft() {
        return left;
    }

    public void setLeft(HeroNode left) {
        this.left = left;
    }

    public HeroNode getRight() {
        return right;
    }

    public void setRight(HeroNode right) {
        this.right = right;
    }

    @Override
    public String toString() {
        return "HeroNode{" +
                "no=" + no +
                ", name='" + name + '\'' +
                '}';
    }
    //前序遍歷
    public void preSort(){
        System.out.println(this);
        if(this.left!=null){
            this.left.preSort();
        }
        if(this.right!=null){
            this.right.preSort();
        }
    }
    //中序遍歷
    public void infixSort(){
        if(this.left!=null){
            this.left.infixSort();
        }
        System.out.println(this);
        if(this.right!=null){
            this.right.infixSort();
        }
    }
    //后序遍歷
    public void postSort(){
        if(this.left!=null){
            this.left.postSort();
        }
        if(this.right!=null){
            this.right.postSort();
        }
        System.out.println(this);
    }
    //前序查找
    public HeroNode preIndexSearch(int no){
        //判斷與當(dāng)前節(jié)點(diǎn)no是否相同,相同返回
        if(this.no==no){
            return this;
        }
        //判斷左子節(jié)點(diǎn)是否為空,如果不為空聂沙,遞歸前序查詢左子樹,如果相等秆麸,返回
        HeroNode temp = null;
        if(this.left!=null){
            temp =  this.left.preIndexSearch(no);
        }
        if(temp!=null){
            return temp;
        }
        if(this.right!=null){
            temp = this.right.preIndexSearch(no);
        }
        return temp;
    }
    //中序查找
    public HeroNode infixIndexSearch(int no){
        HeroNode temp = null;
        if(this.left!=null){
            temp = this.left.infixIndexSearch(no);
        }
        if(temp!=null){
            return temp;
        }
        if (this.no==no){
            return this;
        }
        if(this.right!=null){
            temp = this.right.infixIndexSearch(no);
        }
        return temp;
    }
    //后序查找
    public HeroNode postIndexSearch(int no){
        HeroNode temp = null;
        if(this.left!=null){
            temp = this.left.postIndexSearch(no);
        }
        if(temp!=null){
            return temp;
        }
        if(this.right!=null){
            temp = this.right.postIndexSearch(no);
        }
        if(temp!=null){
            return temp;
        }
        if(this.no==no){
            return this;
        }
        return temp;
    }
}
最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
  • 序言:七十年代末,一起剝皮案震驚了整個(gè)濱河市及汉,隨后出現(xiàn)的幾起案子沮趣,更是在濱河造成了極大的恐慌,老刑警劉巖坷随,帶你破解...
    沈念sama閱讀 217,734評(píng)論 6 505
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件房铭,死亡現(xiàn)場(chǎng)離奇詭異,居然都是意外死亡温眉,警方通過查閱死者的電腦和手機(jī)缸匪,發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 92,931評(píng)論 3 394
  • 文/潘曉璐 我一進(jìn)店門,熙熙樓的掌柜王于貴愁眉苦臉地迎上來类溢,“玉大人凌蔬,你說我怎么就攤上這事〈忱洌” “怎么了砂心?”我有些...
    開封第一講書人閱讀 164,133評(píng)論 0 354
  • 文/不壞的土叔 我叫張陵,是天一觀的道長(zhǎng)窃躲。 經(jīng)常有香客問我计贰,道長(zhǎng),這世上最難降的妖魔是什么蒂窒? 我笑而不...
    開封第一講書人閱讀 58,532評(píng)論 1 293
  • 正文 為了忘掉前任躁倒,我火速辦了婚禮,結(jié)果婚禮上洒琢,老公的妹妹穿的比我還像新娘秧秉。我一直安慰自己,他們只是感情好衰抑,可當(dāng)我...
    茶點(diǎn)故事閱讀 67,585評(píng)論 6 392
  • 文/花漫 我一把揭開白布象迎。 她就那樣靜靜地躺著,像睡著了一般呛踊。 火紅的嫁衣襯著肌膚如雪砾淌。 梳的紋絲不亂的頭發(fā)上,一...
    開封第一講書人閱讀 51,462評(píng)論 1 302
  • 那天谭网,我揣著相機(jī)與錄音汪厨,去河邊找鬼。 笑死愉择,一個(gè)胖子當(dāng)著我的面吹牛劫乱,可吹牛的內(nèi)容都是我干的织中。 我是一名探鬼主播,決...
    沈念sama閱讀 40,262評(píng)論 3 418
  • 文/蒼蘭香墨 我猛地睜開眼衷戈,長(zhǎng)吁一口氣:“原來是場(chǎng)噩夢(mèng)啊……” “哼狭吼!你這毒婦竟也來了?” 一聲冷哼從身側(cè)響起殖妇,我...
    開封第一講書人閱讀 39,153評(píng)論 0 276
  • 序言:老撾萬榮一對(duì)情侶失蹤刁笙,失蹤者是張志新(化名)和其女友劉穎,沒想到半個(gè)月后拉一,有當(dāng)?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體采盒,經(jīng)...
    沈念sama閱讀 45,587評(píng)論 1 314
  • 正文 獨(dú)居荒郊野嶺守林人離奇死亡旧乞,尸身上長(zhǎng)有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點(diǎn)故事閱讀 37,792評(píng)論 3 336
  • 正文 我和宋清朗相戀三年蔚润,在試婚紗的時(shí)候發(fā)現(xiàn)自己被綠了。 大學(xué)時(shí)的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片尺栖。...
    茶點(diǎn)故事閱讀 39,919評(píng)論 1 348
  • 序言:一個(gè)原本活蹦亂跳的男人離奇死亡嫡纠,死狀恐怖,靈堂內(nèi)的尸體忽然破棺而出延赌,到底是詐尸還是另有隱情除盏,我是刑警寧澤,帶...
    沈念sama閱讀 35,635評(píng)論 5 345
  • 正文 年R本政府宣布挫以,位于F島的核電站者蠕,受9級(jí)特大地震影響,放射性物質(zhì)發(fā)生泄漏掐松。R本人自食惡果不足惜踱侣,卻給世界環(huán)境...
    茶點(diǎn)故事閱讀 41,237評(píng)論 3 329
  • 文/蒙蒙 一、第九天 我趴在偏房一處隱蔽的房頂上張望大磺。 院中可真熱鬧抡句,春花似錦、人聲如沸杠愧。這莊子的主人今日做“春日...
    開封第一講書人閱讀 31,855評(píng)論 0 22
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽流济。三九已至锐锣,卻和暖如春,著一層夾襖步出監(jiān)牢的瞬間绳瘟,已是汗流浹背雕憔。 一陣腳步聲響...
    開封第一講書人閱讀 32,983評(píng)論 1 269
  • 我被黑心中介騙來泰國(guó)打工, 沒想到剛下飛機(jī)就差點(diǎn)兒被人妖公主榨干…… 1. 我叫王不留稽荧,地道東北人橘茉。 一個(gè)月前我還...
    沈念sama閱讀 48,048評(píng)論 3 370
  • 正文 我出身青樓工腋,卻偏偏與公主長(zhǎng)得像,于是被迫代替她去往敵國(guó)和親畅卓。 傳聞我的和親對(duì)象是個(gè)殘疾皇子擅腰,可洞房花燭夜當(dāng)晚...
    茶點(diǎn)故事閱讀 44,864評(píng)論 2 354

推薦閱讀更多精彩內(nèi)容