二叉树-查找指定节点

简介: 二叉树-查找指定节点

二叉树-查找指定节点

要求:

请编写前序查找,中序查找和后序查找的方法。

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

代码示例:

package com.wxit.tree;

/**
 * @Author wj
 **/
public class BinaryTreeDemo {
    public static void main(String[] args) {
        //先创建一颗二叉树
        BinaryTree binaryTree = new BinaryTree();
        //创建需要的节点
        HeroNode root = new HeroNode(1, "吴杰");
        HeroNode node2 = new HeroNode(2, "吴昊");
        HeroNode node3 = new HeroNode(3, "小昊");
        HeroNode node4 = new HeroNode(4, "张三");
        HeroNode node5 = new HeroNode(5, "李婷");

        //先手动创建二叉树
        root.setLeft(node2);
        root.setRight(node3);
        node3.setRight(node4);
        node3.setLeft(node5);
        binaryTree.setRoot(root);

        //测试
        System.out.println("前序遍历");
        binaryTree.preOrder();

        System.out.println("中序遍历");
        binaryTree.infixOrder();

        System.out.println("后序遍历");
        binaryTree.postOrder();

        //测试
        System.out.println("前序遍历查找");
        HeroNode resNode = binaryTree.preOrderSearch(5);
        if (resNode != null){
            System.out.printf("找到了,信息为no=%d name=%s",resNode.getNo(),resNode.getName());
        } else {
            System.out.printf("没有找到no = %d 的英雄",5);
        }
    }
}

//创建HeroNode节点
class HeroNode{
    private int no;
    private String name;
    private HeroNode left;
    private HeroNode right;

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

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

    public HeroNode getRight() {
        return right;
    }

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

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

    //编写前序遍历的方法
    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);
    }

    //前序遍历查找
    public HeroNode preOrderSearch(int no){
        //比较当前节点是不是
        if (this.no == no){
            return this;
        }
        //判断当前节点的左子节点是否为空,如果不为空,则递归前序查找,如果左递归前序查找,找到节点,则返回
        HeroNode resNode = null;
        if (this.left != null){
            resNode = this.left.preOrderSearch(no);
        }
        if (resNode != null){
            //说明左子树找到
            return resNode;
        }
        //左子节点没有找到,继续判断,判断当前节点的右子节点是否为空,如果不为空,就继续向右递归查找
        if (this.right != null){
            resNode = this.right.preOrderSearch(no);
        }
        return resNode;
    }

    //中序遍历查找
    public HeroNode infixOrderSearch(int no){
        //判断当前节点的左子节点是否为空,如果不为空,则递归中序查找
        HeroNode resNode = null;
        if (this.left != null){
            resNode = this.left.infixOrderSearch(no);
        }
        if (resNode != null){
            return resNode;
        }
        //没找到,就和当前节点比较,如果是,就返回
        if (this.no == no){
            return this;
        }
        //否则继续向右递归进行中序查找
        if (this.right != null){
            resNode = this.right.infixOrderSearch(no);
        }
        return resNode;
    }

    //后序遍历查找
    public HeroNode postOrderSearch(int no){
        //判断当前节点的左子节点是否为空,如果不为空,则递归后序查找
        HeroNode resNode = null;
        if (this.left != null){
            resNode = this.left.postOrderSearch(no);
        }
        if (resNode != null){
            return resNode;
        }
        //如果左子树没有找到,则向右子树递归进行后序遍历查找
        if (this.right != null){
            resNode = this.right.postOrderSearch(no);
        }
        if (resNode != null){
            return resNode;
        }
        //如果右子树没有找到,就比较当前节点是不是
        if (this.no == no){
            return this;
        }
        return resNode;
    }
}

//定义二叉树
class BinaryTree{
    private HeroNode root;

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

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

    //中序遍历
    public void infixOrder(){
        if (this.root != null){
            this.root.infixOrder();
        } else {
            System.out.println("二叉树为空,不能遍历");
        }
    }

    //后去遍历
    public void postOrder(){
        if (this.root != null){
            this.root.postOrder();
        } else {
            System.out.println("二叉树为空,不能遍历");
        }
    }

    //前序遍历
    public HeroNode preOrderSearch(int no){
        if (root != null){
            return root.preOrderSearch(no);
        }else {
            return null;
        }
    }

    //中序遍历
    public HeroNode infixOrderSearch(int no){
        if (root != null){
            return root.infixOrderSearch(no);
        } else {
            return null;
        }
    }

    //后序遍历
    public HeroNode postOrderSearch(int no){
        if (root != null){
            return postOrderSearch(no);
        } else {
            return null;
        }
    }
}
相关文章
|
机器学习/深度学习 人工智能 达摩院
10万亿!达摩院发布全球最大AI预训练模型M6
今天,阿里巴巴达摩院公布多模态大模型M6最新进展,其参数已从万亿跃迁至10万亿,成为全球最大的AI预训练模型。
9779 0
10万亿!达摩院发布全球最大AI预训练模型M6
|
3月前
|
JSON API 数据库
跨境ERP开发实践:Ozon商品详情全套接口接入思路
本文详解Ozon Seller API开发痛点:商品信息分散于多接口,需联合调用与数据组装。涵盖鉴权规范、接口清单、Python实现、数据库设计及线上避坑经验,助力ERP系统高效完成商品同步、草稿生成与库存价格管理。(239字)
|
5月前
|
人工智能 前端开发 API
装了Hermes Agent三个月,我终于搞明白前端该怎么选了
本文对比Hermes两大使用方案:Open WebUI(方案A)侧重日常写作、图表渲染与团队共享;Hermes Desktop(方案B)专注Agent深度调教、技能管理与轻量部署。三类真实场景帮你对号入座,选最适合自己的那一套——或两者兼用,各司其职。
493 0
装了Hermes Agent三个月,我终于搞明白前端该怎么选了
|
4月前
|
数据采集 存储 Web App开发
豆瓣读书爬虫:爬取TOP250图书评分与书评,做推荐系统数据源
本文详解如何爬取豆瓣读书TOP250数据构建图书推荐系统:剖析反爬机制(User-Agent伪装、IP限频、Cookie登录态),提供完整Python爬虫代码(含隧道代理集成)、CSV/数据库存储方案,并延伸至协同过滤与内容推荐实践,兼顾合规性与工程落地。
535 0
|
6月前
|
消息中间件 NoSQL 调度
团播爆发下的传统直播源码架构迭代:百人同屏连麦与IM消息高并发实战拆解
2026年的直播行业正在经历一场结构性的转变。据相关报告数据,2025年团播市场规模已突破150亿元,日均开播量突破8000个,业内预计2026年有望冲击400亿元。资本与平台正在加速涌入这一赛道。
|
6月前
|
SQL 人工智能 中间件
GraphRAG + Multi-Agent 凭什么登上 Nature?
2026年5月《Nature Scientific Reports》刊发突破性论文:首创GraphRAG+Multi-Agent+多模态融合的五层统一平台,将Multi-hop QA准确率提升46%,实现真实业务落地(ATS/Text-to-SQL/Research Assistant),工程可复现、架构可复用。(239字)
503 0
|
10月前
|
人工智能 算法 安全
Gemini 3 Deep Think 升级:Codeforces 3455 分,科研级推理能力全面开放
Gemini 3 Deep Think 是Google DeepMind推出的高推理模式AI,在ARC-AGI-2达84.6%、Codeforces获3455 Elo(全球前10),并逼近数理化奥赛金牌水平。它专注科研级推理、工程建模与成本优化,标志AI从“解题”迈向“协同科研”。
|
12月前
|
存储 关系型数据库 MySQL
MySQL 项目实战入门:从 0 到 1 搭建电商数据库
本文以电商系统为实战场景,从环境搭建、需求分析入手,详解MySQL数据库的表结构设计、SQL编写、关联查询及性能优化,帮助初学者掌握从零构建完整数据库项目的全流程,提升实操能力。
679 0
|
负载均衡 监控 Linux
Linux网络中断优化配置(提升服务器网络性能的实用指南)
本文教你如何优化Linux网络中断处理,通过启用网卡多队列和配置中断亲和性,实现CPU负载均衡,提升高并发下的网络性能,适合新手快速上手。