二叉树 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; }

相关新闻

最新新闻

3 连接mysql 数据库 进行数据的存储和读取

3 连接mysql 数据库 进行数据的存储和读取

1下载node 连接模块npm i mysql -s2 在 src 下创建 conf 文件夹 用于存放配置文件 新建db.js通过环境参数的不同进行线上和线下配置const env process.env.NODE_ENV//环境参数let MYSQL_CONFif (env dev) {MYSQL_CONF {host: localhost,user: root,password: ,port: 3306,d…

2026/7/28 16:51:56
SteamAutoCrack完整教程:3步快速移除游戏DRM保护实现离线游戏

SteamAutoCrack完整教程:3步快速移除游戏DRM保护实现离线游戏

SteamAutoCrack完整教程:3步快速移除游戏DRM保护实现离线游戏 【免费下载链接】Steam-auto-crack Steam Game Automatic Cracker 项目地址: https://gitcode.com/gh_mirrors/st/Steam-auto-crack SteamAutoCrack是一款专业的Steam游戏自动破解工具&#xff0…

2026/7/28 16:51:56
【独家】MIT+DeepMind联合泄露报告:2026年前AI将突破因果推理瓶颈——附5个已验证的产业级应用信号

【独家】MIT+DeepMind联合泄露报告:2026年前AI将突破因果推理瓶颈——附5个已验证的产业级应用信号

更多请点击: https://intelliparadigm.com 第一章:AI因果推理突破的范式转移与历史坐标 传统机器学习长期囿于相关性建模,将“统计拟合”误作“机制理解”。而近年来,以Pearl的do-calculus为理论基石、结合结构因果模型&#xff…

2026/7/28 16:51:56
2012-2018普及组第一题题解

2012-2018普及组第一题题解

noip2018 标题统计 【题目描述】 凯凯刚写了一篇美妙的作文,请问这篇作文的标题中有多少个字符? 注意:标题中可能包含大、小写英文字母、数字字符、空格和换行符。统计标题字符数时,空格和换行符不计算在内。 【输入】 输入文…

2026/7/28 16:51:56
免费轻量级散热控制:3分钟让你的Dell G15告别过热卡顿

免费轻量级散热控制:3分钟让你的Dell G15告别过热卡顿

免费轻量级散热控制:3分钟让你的Dell G15告别过热卡顿 【免费下载链接】tcc-g15 Thermal Control Center for Dell G15 - open source alternative to AWCC 项目地址: https://gitcode.com/gh_mirrors/tc/tcc-g15 还在为游戏本散热问题烦恼吗?当你…

2026/7/28 16:51:56
SSD缓存DRAM深度解析:有DRAM和没DRAM的固态硬盘到底差在哪?为什么选购SSD必须关注DRAM缓存?

SSD缓存DRAM深度解析:有DRAM和没DRAM的固态硬盘到底差在哪?为什么选购SSD必须关注DRAM缓存?

摘要:DRAM缓存是SSD的"高速办公桌",存放着FTL映射表和读写缓冲数据。有DRAM的SSD随机性能稳定、寿命更长;无DRAM方案(DRAM-less)通过HMB借用主机内存降低成本。本文从DRAM的角色定位、容量计算、管理策略、有…

2026/7/28 16:46:56

月新闻