二叉树 public static void main(String[] args) { //先需要创建一颗二叉树 BinaryTree binaryTreenew BinaryTree(); //创建需要的节点 HeroNode rootnew HeroNode(1,宋江); HeroNode node2new HeroNode(2,mao); HeroNode node3new HeroNode(3,gou); HeroNode node4new HeroNode(4,zhu); HeroNode node5new HeroNode(5,ma); //先手动创建 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 resNodebinaryTree.postSearch(5); if(resNode!null) System.out.printf(找到了信息为no%d name%s,resNode.getVal(),resNode.getName()); else { System.out.printf(没有找到 no %d 的英雄,2); } } } //定义二叉树 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 preSearch(int val) { if(this.root!null) return this.root.preOrdersearch(val); else System.out.println(无法遍历); return null; } public HeroNode infixSearch(int val) { if(this.root!null) return this.root.infixOrdersearch(val); else System.out.println(无法遍历); return null; } public HeroNode postSearch(int val) { if(this.root!null) return this.root.postOrdersearch(val); else System.out.println(无法遍历); return null; } } //先创建HeroNode class HeroNode { private int val; private String name; private HeroNode left; private HeroNode Right; public HeroNode(int val, String name) { this.val val; this.name name; } public int getVal() { return val; } public void setVal(int val) { this.val val; } 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) { Right right; } Override public String toString() { return HeroNode{ val val , 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); } //前序遍历查找 找到 返回该node 没找到 返回null public HeroNode preOrdersearch(int no) { //比较当前节点是不是 if(this.valno) return this; HeroNode resNodenull; if(this.left!null) { resNode this.left.preOrdersearch(no); } if(resNode!null) { System.out.println(找到了); //说明找到 return resNode; } if(this.Right!null) { resNode this.Right.preOrdersearch(no); } return resNode; } //中序遍历查找 找到 返回该node 没找到 返回null public HeroNode infixOrdersearch(int no) { HeroNode resNodenull; if(this.left!null) { resNode this.left.infixOrdersearch(no); } if(resNode!null) { //说明找到 return resNode; } System.out.println(进入中序查找); //比较当前节点是不是 if(this.valno) return this; if(this.Right!null) { resNode this.Right.infixOrdersearch(no); } return resNode; } //后序遍历查找 找到 返回该node 没找到 返回null public HeroNode postOrdersearch(int no) { HeroNode resNodenull; 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; } System.out.println(后续遍历); //比较当前节点是不是 if(this.valno) return this; return resNode; }

相关新闻

最新新闻

SerenityOS 命令行选项解析指南:getopt 与 getopt_long 用法、返回值与底层实现

SerenityOS 命令行选项解析指南:getopt 与 getopt_long 用法、返回值与底层实现

SerenityOS 命令行选项解析指南:getopt 与 getopt_long 用法、返回值与底层实现 【免费下载链接】serenity The Serenity Operating System 🐞 项目地址: https://gitcode.com/GitHub_Trending/se/serenity 导读 本文以 getopt(3) 手册 为核心&a…

2026/9/25 12:45:43
轻量服务器还是ECS?大促云服务器选购与避坑实战指南

轻量服务器还是ECS?大促云服务器选购与避坑实战指南

每年大促节点,群里永远有人在问同一个问题:“38元的轻量服务器到底怎么抢?为什么我每次点进去都是已售罄?68元直购和99元的ECS我到底选哪个?”作为一个常年帮团队和自己采购云服务器的老用户,我太清楚这种纠…

2026/9/24 14:25:52
为 AI 代理的 Review 动作编写 Cedar 审批门控策略:review-agent-governance 策略编写实战指南

为 AI 代理的 Review 动作编写 Cedar 审批门控策略:review-agent-governance 策略编写实战指南

为 AI 代理的 Review 动作编写 Cedar 审批门控策略:review-agent-governance 策略编写实战指南 【免费下载链接】agents Multi-harness agentic plugin marketplace for Claude Code, Codex, Cursor, OpenCode, GitHub Copilot, and Google Antigravity 项目地址:…

2026/9/26 3:42:08
PaddleOCR 手写数学公式识别算法 CAN 实战指南:Counting-Aware Network 训练、评估与推理部署

PaddleOCR 手写数学公式识别算法 CAN 实战指南:Counting-Aware Network 训练、评估与推理部署

PaddleOCR 手写数学公式识别算法 CAN 实战指南:Counting-Aware Network 训练、评估与推理部署 【免费下载链接】PaddleOCR Turn any PDF or image document into structured data for your AI. A powerful, lightweight OCR toolkit that bridges the gap between i…

2026/9/23 8:01:38
Spring源码解析:构造器注入的类型转换与候选匹配机制

Spring源码解析:构造器注入的类型转换与候选匹配机制

/* MD / 富文本中的 .toc(含博客园搬家等嵌套结构);.toc-box 在侧栏,不受影响 */#content_views .toc,/* 编辑器常在目录前后插入空 p(:empty 仍占 20px),一并去掉避免顶空隙 */#content_views.markdown_views > p:empty:has(+ .toc),#content_views.markdown_views …

2026/9/26 4:08:27
openai-agents-python 多模型接入指南:深入解析 AnyLLMModel 适配层与 any-llm 路由

openai-agents-python 多模型接入指南:深入解析 AnyLLMModel 适配层与 any-llm 路由

openai-agents-python 多模型接入指南:深入解析 AnyLLMModel 适配层与 any-llm 路由 【免费下载链接】openai-agents-python A lightweight, powerful framework for multi-agent workflows 项目地址: https://gitcode.com/GitHub_Trending/op/openai-agents-pyth…

2026/9/25 15:49:36

日新闻

周新闻