Java实现二叉树的前中后序查找完整代码示例

结点类

package DataStrcture.binarytreedemo_1;

public class BinaryTreeNodeSe {
    //结点类
    private BinaryTreeNodeSe leftNode;
    private BinaryTreeNodeSe rightNode;
    private int id;
    private String name;

    //构造方法,toString, setget方法
    public BinaryTreeNodeSe(int id, String name) {
        this.id = id;
        this.name = name;
    }

    public String toString() {
        return "id: " + id + ", name: " + name;
    }

    public void setLeftNode(BinaryTreeNodeSe node) {
        this.leftNode = node;
    }

    public void setRightNode(BinaryTreeNodeSe node) {
        this.rightNode = node;
    }

    //二叉树查找
    public BinaryTreeNodeSe preOrderSearchById(int id) {
        if (this.id == id) return this;

        BinaryTreeNodeSe res = null;
        if (this.leftNode != null) {
            res = this.leftNode.preOrderSearchById(id);
        }

        if (res != null) return res; //左子树找到了的话直接返回, 下面的也不用管了.

        if (this.rightNode != null) {
            res = this.rightNode.preOrderSearchById(id);
        }
        return res;
    }

    public BinaryTreeNodeSe midOrderSearchById(int id) {
        //找到了, 直接返回
        if (this.id == id) return this;
        //左子树非空, 递归左子树查询
        BinaryTreeNodeSe res = null;
        if (this.leftNode != null) {
            res = this.leftNode.midOrderSearchById(id);
        }
        //左子树查到了(res不为空), 直接返回即可, 下面的递归右子树直接略过
        if (res != null) return res;

        if (this.rightNode != null) {
            res = this.rightNode.midOrderSearchById(id);
        }

        return res;
    }

    public BinaryTreeNodeSe postOrderSearchById(int id) {
        if (this.id == id) return this;

        BinaryTreeNodeSe res = null;
        if (this.leftNode != null) {
            res = this.leftNode.postOrderSearchById(id);
        }

        if (res != null) return res;

        if (this.rightNode != null) {
            res = this.rightNode.postOrderSearchById(id);
        }
        return res;
    }
}

二叉树类

package DataStrcture.binarytreedemo_1;

public class BinaryTreeSe {
    //
    private BinaryTreeNodeSe root;
    public BinaryTreeSe(BinaryTreeNodeSe node){
        this.root = node;
    }
    //1. 前序遍历查找
    public void preOrderSe(int id){
        if(root != null){
            BinaryTreeNodeSe res = root.preOrderSearchById(id);
            if(res != null){
                System.out.println("找到了id= "+id+"的二叉树结点, 具体的信息如下: ");
                System.out.println(res);
            }else{
                System.out.println("二叉树上没有id="+id+"的结点");
            }
        }else{
            System.out.println("二叉树为空, 查找失败! ");
        }
    }
    // 2. 中序遍历查找
    public void midOrderSe(int id){
        //第一层判断, 二叉树是否为空
        if( root != null){
            BinaryTreeNodeSe res  = root.midOrderSearchById(id);
            // 第二层判断, 是否查到对应id 的结点
            if(res != null){
                System.out.println("找到了id= "+id+"的二叉树结点, 具体的信息如下: ");
                System.out.println(res);
            }else{
                System.out.println("二叉树上没有id="+id+"的结点");
            }
        }else{
            System.out.println("二叉树为空, 查找失败! ");
        }
    }
    //3. 二叉树后序查找
    public void postOrderSe(int id){
        if( root != null){
            BinaryTreeNodeSe res = root.postOrderSearchById(id);
            if(res != null){
                System.out.println("找到了id= "+id+"的二叉树结点, 具体的信息如下: ");
                System.out.println(res);
            }else{
                System.out.println("二叉树上没有id="+id+"的结点");
            }
        }else{
            System.out.println("二叉树为空, 查找失败! ");
        }
    }



    public static void main(String[] args) {
        //建立二叉树结点
        BinaryTreeNodeSe node1 = new BinaryTreeNodeSe(1, "宋江");
        BinaryTreeNodeSe node2 = new BinaryTreeNodeSe(2, "吴用");
        BinaryTreeNodeSe node3 = new BinaryTreeNodeSe(3, "卢俊义");
        BinaryTreeNodeSe node4 = new BinaryTreeNodeSe(4, "公孙胜");
        BinaryTreeNodeSe node5 = new BinaryTreeNodeSe(5, "关俊");
        //建立二叉树
        BinaryTreeSe tree = new BinaryTreeSe(node1);
        //结点放到二叉树中
        node1.setLeftNode(node2);
        node1.setRightNode(node3);
        node3.setLeftNode(node4);
        node3.setRightNode(node5);
        //查找
        System.out.println("二叉树前序查找结果如下: ");
        tree.preOrderSe(5);

        //查找
        System.out.println("二叉中序查找结果如下: ");
        tree.midOrderSe(5);

        //查找
        System.out.println("二叉树后序查找结果如下: ");
        tree.postOrderSe(5);
    }
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值