二叉树的查找

  二叉树的查找

    给定一个例子,如下图所示,找到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));

 

     

posted @ 2021-01-19 21:56  GDOUJKZZ  阅读(2420)  评论(0编辑  收藏  举报