java数据结构与算法之二叉树-------查找指定节点

1.二叉树-------查找指定节点

  1)编写前序查找,中序查找和后序查找的方法

  2)并分别用三种查找的方式,查找heroNO=5的节点

 3)分析各种查找方式,分别比较查找了多少次

4)思路分析图解

2. 代码实现 

package BinaryTreeDemo;

public class BinaryTreeSearch {
    public static void main(String[] args) {
        //创建一颗二叉树
        BinaryTree1 binaryTree1=new BinaryTree1();
        //创建需要的节点
        HeroNode1 root=new HeroNode1(1,"宋江");
        HeroNode1 node2=new HeroNode1(2,"吴用");
        HeroNode1 node3=new HeroNode1(3,"卢俊义");
        HeroNode1 node4=new HeroNode1(4,"林冲");
        HeroNode1 node5=new HeroNode1(5,"关胜");
        //手动创建二叉树
        root.setLeft(node2);
        root.setRight(node3);
        node3.setLeft(node4);
        node3.setRight(node5);
        binaryTree1.setRoot(root);

        //前序遍历
        System.out.println("前序遍历的方式--");
        HeroNode1 heroNode = binaryTree1.preOrdersearch(5);
        if (heroNode!=null){
            System.out.printf("找到了,信息为no=%d name=%s",heroNode.getNo(),heroNode.getName());
        }else {
            System.out.println("没有找到");
        }

//        //中序遍历
//        System.out.println("中序遍历的方式--");
//        HeroNode1 heroNode1 = binaryTree1.infixOrdersearch(5);
//        if (heroNode1!=null){
//            System.out.printf("找到了,信息为no=%d name=%s",heroNode1.getNo(),heroNode1.getName());
//        }else {
//            System.out.println("没有找到");
//        }
//
//        //后序遍历
//        System.out.println("后序遍历的方式--");
//        HeroNode1 heroNode2 = binaryTree1.postOrdersearch(5);
//        if (heroNode2!=null){
//            System.out.printf("找到了,信息为no=%d name=%s",heroNode2.getNo(),heroNode2.getName());
//        }else {
//            System.out.println("没有找到");
//        }
    }



}


//定义BinaryTree二叉树
class BinaryTree1{
    private HeroNode1 root;
    public void setRoot(HeroNode1 root){
        this.root=root;
    }
    //前序遍历
    public HeroNode1 preOrdersearch(int no){
        if (root!=null){
            return root.preOrdersearch(no);
        }else {
            return null;
        }
    }
    //中序遍历
    public HeroNode1 infixOrdersearch(int no){
        if (root!=null){
            return root.infixOrdersearch(no);
        }else {
            return null;
        }
    }

    //后序遍历
    public HeroNode1 postOrdersearch(int no){
        if (root!=null){
            return root.postOrdersearch(no);
        }else {
            return null;
        }
    }
}
//先创建heroNode节点
class HeroNode1{
    private int no;
    private String name;
    private HeroNode1 left;
    private HeroNode1 right;

    public HeroNode1(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 HeroNode1 getLeft() {
        return left;
    }

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

    public HeroNode1 getRight() {
        return right;
    }

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

    @Override
    public String toString() {
        return "HeroNode1{" +
                "no=" + no +
                ", name='" + name + '\'' +
                '}';
    }

    //前序遍历查找
    public HeroNode1 preOrdersearch(int no){
        System.out.println("进入前序遍历");
        //比较当前节点是不是
        if (this.no==no){
            return this;
        }
        //1.判断当前节点的左子节点是否为空,如果不为空,则递归前序查找
        //2.如果左递归前序查找,找到节点,返回
        HeroNode1 resNode=null;
        if (this.left!=null){
            resNode=this.left.preOrdersearch(no);
        }
        if (resNode!=null){
            //说明左子树找到
            return resNode;
        }
        //1.右递归前序遍历,找到节点就返回空,不然继续判断
        //2.当前右节点是否为空,如果不为空,就继续向右节点前序查找
        if (this.right!=null){
            resNode=this.right.preOrdersearch(no);
        }
        if (resNode!=null){
            //说明左子树找到
            return resNode;
        }
        return resNode;
    }

    //中序遍历查找
    public HeroNode1 infixOrdersearch(int no){
        //1.判断当前节点的左子节点是否为空,如果不为空,则递归中序查找
        //2.如果左递归前序查找,找到节点,返回
        HeroNode1 resNode=null;
        if (this.left!=null){
            resNode=this.left.infixOrdersearch(no);
        }
        if (resNode!=null){
            //说明左子树找到
            return resNode;
        }
        System.out.println("进入中序遍历");
        //比较当前节点是不是
        if (this.no==no){
            return this;
        }
        //1.右递归中序遍历,找到节点就返回空,不然继续判断
        //2.当前右节点是否为空,如果不为空,就继续向右节点中序查找
        if (this.right!=null){
            resNode=this.right.infixOrdersearch(no);
        }
        return resNode;
    }

    //后序遍历查找
    public HeroNode1 postOrdersearch(int no){
        //1.判断当前节点的左子节点是否为空,如果不为空,则递归后序查找
        //2.如果左递归前序查找,找到节点,返回
        HeroNode1 resNode=null;
        if (this.left!=null){
            resNode=this.left.postOrdersearch(no);
        }
        if (resNode!=null){
            //说明左子树找到
            return resNode;
        }
        //1.右递归后序遍历,找到节点就返回空,不然继续判断
        //2.当前右节点是否为空,如果不为空,就继续向右节点后序查找
        if (this.right!=null){
            resNode=this.right.postOrdersearch(no);
        }
        if (resNode!=null){
            //说明右子树找到
            return resNode;
        }
        System.out.println("进入后序遍历");
        //比较当前节点是不是
        if (this.no==no){
            return this;
        }
        return resNode;

    }

}

 

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值