数据结构之二叉树的查找

二叉树的查找方式有三种,分别是先序中序和后序。
先序思路:
判断当前的节点中的No和传过来要查找的是否一致
相等返回
不相等判断当前节点左子节点是否为空 如果不为空 继续比较 查找返回
如果 为空 或者没有找到 查看右子节点 查到返回否则返回null
中序思路:
判断当前的左子节点是否为空,不为空递归查询左子节点 查到返回
左子节点为空或者左边没有查到,查询当前节点 查到返回
根节点查不到 查询右子节点 查到返回否则返回null
后序思路:
判断当前的左子节点是否为空,不为空递归查询左子节点 查到返回
左子节点为空或者左边没有查到 查询右子节点 没查到或为空
查询当前节点 查到返回否则返回null

代码实现:

package tree;

public class BinaryTree {

    public static void main(String[] args) {
        //创建二叉树(手动非递归)
        BinaryTreeClass bt = new BinaryTreeClass();
        HreoNode root = new HreoNode(1, "宋江");
        HreoNode h2 = new HreoNode(2, "卢俊义");
        HreoNode h3 = new HreoNode(3, "吴用");
        HreoNode h4 = new HreoNode(4, "公孙胜");
        HreoNode h5 = new HreoNode(5, "关胜");

        root.setLeft(h2);
        root.setRight(h3);
        h3.setRight(h4);
        h4.setLeft(h5);
        bt.setRoot(root);

        //测试
      /*  //前序
        System.out.println("前序");
        bt.preOrder();
        //中序
        System.out.println("中序");
        bt.midOrder();
        //后序
        System.out.println("后 序");
        bt.postOrder();*/

        //查找
        System.out.println("前序");
        System.out.println(bt.preSerach(1));
        System.out.println("中序");
        System.out.println(bt.preSerach(3));
        System.out.println("后序");
        System.out.println(bt.preSerach(5));

    }


}


//二叉树类
class BinaryTreeClass {
    private HreoNode root;

    //前序遍历------------------------------------------
    public void preOrder() {
        if (root != null) root.preOrder();
        else System.out.println("当前二叉树为空");
    }

    //中序遍历
    public void midOrder() {
        if (root != null) root.midOrder();
        else System.out.println("当前二叉树为空");
    }

    //后序遍历
    public void postOrder() {
        if (root != null) root.postOrder();
        else System.out.println("当前二叉树为空");
    }

    //前序查找 --------------------------------------------
    public HreoNode preSerach(int no) {
        if (root != null) return root.midSreach(no);
        else return null;
    }

    //中序查找
    public HreoNode midSreach(int no) {
        if (root != null)return root.midSreach(no);
        else return null;
    }
    //后序查找
    public HreoNode postSreach(int no) {
        if (root != null)return root.postSreach(no);
        else return null;
    }


    public HreoNode getRoot() {
        return root;
    }

    public void setRoot(HreoNode root) {
        this.root = root;
    }
}


//节点类
class HreoNode {
    private int no;
    private String name;
    //默认null
    private HreoNode left;
    //默认null
    private HreoNode right;


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

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

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


    //查找
    //先序
    public HreoNode preSerach(int no) {
        if (this.no == no) return this;
        HreoNode hreoNode = null;
        if (this.left != null) hreoNode = this.left.preSerach(no);
        //左找到返回
        if (hreoNode != null) return hreoNode;
        if (this.right != null) hreoNode = this.right.preSerach(no);
        return hreoNode;
    }
    //中序

    public HreoNode midSreach(int no) {
        HreoNode hreoNode = null;
        if (this.left != null) hreoNode = this.left.midSreach(no);
        if (hreoNode != null) return hreoNode;
        if (this.no == no) return this;
        if (this.right != null) hreoNode = this.right.midSreach(no);
        return hreoNode;

    }

    //后序
    public HreoNode postSreach(int no) {
        HreoNode hreoNode = null;
        if (this.left != null) hreoNode = this.left.postSreach(no);
        if (hreoNode != null) return hreoNode;
        if (this.right != null) hreoNode = this.right.postSreach(no);
        if (hreoNode != null) return hreoNode;
        if (this.no == no) return this;
        return hreoNode;
    }


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

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

    public HreoNode getRight() {
        return right;
    }

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

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

结果:
在这里插入图片描述

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值