数据结构之二叉树查询

二叉树之查询

二叉树的查询是建立遍历的基础上更进一步的对比查询,下面分别从前序、中序、后续三种方法中查询对应的节点。

前序查找

java 复制代码
    /**
     * 前序查询
     */
    public Person preSelectById(Integer id) {
        Person person = null;
        if (this.data.getId().equals(id)) {
            person = this.data;
        }
        if (Objects.isNull(person) && Objects.nonNull(this.left)) {
            person = this.left.preSelectById(id);
        }
        if (Objects.isNull(person) && Objects.nonNull(this.right)) {
            person = this.right.preSelectById(id);
        }
        return person;
    }

中序查找

java 复制代码
    /**
     * 中序查询
     */
    public Person midSelectById(Integer id) {
        Person person = null;
        if (Objects.isNull(person) && Objects.nonNull(this.left)) {
            person = this.left.preSelectById(id);
        }
        if (this.data.getId().equals(id)) {
            person = this.data;
        }
        if (Objects.isNull(person) && Objects.nonNull(this.right)) {
            person = this.right.preSelectById(id);
        }
        return person;
    }

后序查找

java 复制代码
    /**
     * 后序查询
     */
    public Person postSelectById(Integer id) {
        Person person = null;
        if (Objects.isNull(person) && Objects.nonNull(this.left)) {
            person = this.left.preSelectById(id);
        }
        if (Objects.isNull(person) && Objects.nonNull(this.right)) {
            person = this.right.preSelectById(id);
        }
        if (this.data.getId().equals(id)) {
            person = this.data;
        }
        return person;
    }

源码与测试案例

gitee地址

相关推荐
漂流瓶jz27 分钟前
UVA-1612 猜名次 题解答案代码 算法竞赛入门经典第二版
数据结构·算法·题解·排序·aoapc·算法竞赛入门经典
A boy CDEF girl6 小时前
算法题专栏:优选算法
数据结构·算法·leetcode
图灵-M10 小时前
【数据结构】静态 & 动态顺序表
c语言·数据结构·笔记
careathers10 小时前
【数据结构】基于比较的排序算法 - 选择排序
数据结构·算法·排序算法
XiYang-DING12 小时前
P4104.环形链表判断
数据结构·链表
逆向编程13 小时前
MATLAB高级数据结构:cell、struct与table深度应用
开发语言·数据结构·matlab
m0_639906941 天前
Redis入门|五种数据结构、使用场景、持久化RDB&AOF
数据结构·数据库·redis
2601_970703831 天前
工业除湿机租赁:把一份租金合同拆开看
大数据·数据结构·算法·数据库架构·推荐算法
丘山望岳1 天前
单调栈和单调队列
数据结构
UIU1142 天前
P16237 [蓝桥杯 2026 省 B] 应急布线
数据结构·c++·算法·蓝桥杯