二叉樹的查找


  二叉樹的查找

    給定一個例子,如下圖所示,找到6號矮腳虎,請用代碼實現。

              

    這就是一個簡單的二叉樹的查找問題,我們可以通過這個來 

    二叉樹的查找,可以分為前序查找,中序查找,后序查找,和二叉樹的遍歷類似。

    前序查找的思路:

      (1)首先拿當根節點的no和當前比較,如果相等,則直接返回。

      (2)如果不相等,則左遞歸前序查找。

      (3)如果左遞歸找到了結果,直接返回,如果找不到,則開始右遞歸前序查找。

      (4)如果右遞歸找到則返回,沒找到則返回空。

    中序查找的思路:

                    (1)先進行左遞歸查找,如果找到,直接返回,如果找不到則和跟節點比較。

      (2)拿當根節點的no和當前比較,如果相等,則直接返回。

      (3)如果不相等,則右遞歸前序查找。

      (4)如果右遞歸找到則返回,沒扎到則返回空。

    后序查找的思路:

      (1)先進行左遞歸查找,如果找到,直接返回,如果找不到則進行右遞歸查找。

      (2)右遞歸查找,如果找到則直接返回,如果找不到則,和跟節點對比

      (3)如果和跟節點比較不相等,則返回空。

    具體代碼實現如下:作為一個初學者可能比較好奇的就是你遍歷完左子樹了,你怎么去到右子樹呢。 其實這個可以利用遞歸帶返回值來做到。

    下面請看具體代碼:

    先定義個HeroNode:

public class HeroNode {

    private  int no;
    private String nickName;
    private String heroName;

    public int getNo() {
        return no;
    }

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

    public String getNickName() {
        return nickName;
    }

    public HeroNode setNickName(String nickName) {
        this.nickName = nickName;
        return this;
    }

    public String getHeroName() {
        return heroName;
    }

    public HeroNode setHeroName(String heroName) {
        this.heroName = heroName;
        return this;
    }

    private HeroNode left;
    private HeroNode right;

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

    @Override
    public String toString() {
        final StringBuilder sb = new StringBuilder("HeroNode{");
        sb.append("no=").append(no);
        sb.append(", nickName='").append(nickName).append('\'');
        sb.append(", heroName='").append(heroName).append('\'');
        sb.append('}');
        return sb.toString();
    }

    public HeroNode getLeft() {
        return left;
    }

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

    public HeroNode getRight() {
        return right;
    }

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

    //前序
    public void preOrder(){
        System.out.println(this);
        if(this.left!=null){
            this.left.preOrder();
        }
        if(this.right!=null){
            this.right.preOrder();
        }
    }

    //中序
    public void infixOrder(){
        if(this.left!=null){
            this.left.infixOrder();
        }
        System.out.println(this);
        if(this.right!=null){
             this.right.infixOrder();
        }
    }

    public void postOrder(){
        if(this.left!=null){
            this.left.postOrder();
        }
        if(this.right!=null){
            this.right.postOrder();
        }
        System.out.println(this);
    }


}

    構建一顆樹的結構

         

    代碼如下:

        HeroNode node1=new HeroNode(1,"及時雨","宋江");
        HeroNode node2=new HeroNode(2,"玉麒麟","盧俊義");
        HeroNode node3=new HeroNode(3,"智多星","吳用");
        HeroNode node4=new HeroNode(4,"花和尚","魯智深");
        HeroNode node5=new HeroNode(5,"豹子頭","林沖");
        HeroNode node6=new HeroNode(6,"矮腳虎","王英");

        node1.setLeft(node2);
        node1.setRight(node3);
        node2.setLeft(node4);
        node2.setRight(node5);
        node3.setRight(node6);    

    構建好這顆樹之后,接下來就開始查找的代碼編寫

    1、前序查找

  public  static HeroNode preLookUp(HeroNode node,int no){
        if(node.getNo()==no){
            return node;
        }
        HeroNode result=null;
        if(node.getLeft()!=null){
            result= preLookUp(node.getLeft(),no);
        }
        if(result!=null){
            return result;
        }
        if(node.getRight()!=null){
            result=preLookUp(node.getRight(),no);
        }
        return result;
    }

    2、中序查找

public static HeroNode middleLookUp(HeroNode node,int no){
        HeroNode result=null;
        if(node.getLeft()!=null){
            result=middleLookUp(node.getLeft(),no);
        }
        if(result!=null){
            return result;
        }
        if(node.getNo()==no){
            return node;
        }
        if(node.getRight()!=null){
            result=middleLookUp(node.getRight(),no);
        }
        return  result;
    }

    3、后序查找

 public static HeroNode postLookUp(HeroNode node,int no){
        HeroNode result=null;
        if(node.getLeft()!=null){
            result=middleLookUp(node.getLeft(),no);
        }
        if(result!=null){
            return result;
        }
        if(node.getRight()!=null){
            result=middleLookUp(node.getRight(),no);
        }
        if(result!=null){
            return result;
        }
        if(node.getNo()==no){
            return node;
        }
        return  result;
    }

   測試代碼如下

        System.out.println("===========前序查找=============");
        System.out.println(preLookUp(node1,6));


        System.out.println("===========中序查找=============");
        System.out.println(middleLookUp(node1,6));

        System.out.println("===========后序查找=============");
        System.out.println(postLookUp(node1,6));

 

     


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM