遍历和查找二叉树
package A;
import java.util.Scanner;
public class JosePhu {
public static void main(String[] args) {
BinaryTree binaryTree = new BinaryTree();
Emp root=new Emp(1,"zml");
Emp node2=new Emp(2,"zml2");
Emp node3=new Emp(3,"zml3");
Emp node4=new Emp(4,"zml4");
Emp node5=new Emp(5,"zml5");
Emp node6=new Emp(6,"zml6");
Emp node7=new Emp(7,"zml7");
root.setLeft(node2);
root.setRight(node3);
node2.setLeft(node4);
node2.setRight(node5);
node3.setLeft(node6);
node3.setRight(node7);
binaryTree.setRoot(root);
binaryTree.qian();
System.out.println("=======");
binaryTree.zhong();
System.out.println("===========");
binaryTree.hou();
System.out.println("前序遍历的方式~");
Emp resNode=binaryTree.q(5);
if (resNode!=null){
System.out.printf("找到了,信息为no=%d name=%s",resNode.getId(),resNode.getName());
}else {
System.out.printf("没有找到 no=%d 的英雄",5);
}
}
}
/*
* 写盒子该有的东西,比如id,name。因为我看人家一般用private,所以我也用了,但是用了private就要写get,set方法
* 写一个有参构造器
* 实现get,set方法
* 重写toString方法*/
class Emp {//盒子部分
private int id;//任何的private都需要get,set方法
private String name;
private Emp left;
private Emp right;
public Emp(int id, String name) {
this.id = id;
this.name = name;
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public Emp getLeft() {
return left;
}
public void setLeft(Emp left) {
this.left = left;
}
public Emp getRight() {
return right;
}
public void setRight(Emp right) {
this.right = right;
}
@Override
public String toString() {
return "Emp{" +
"id=" + id +
", 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 midOrder(){
if (this.left!=null){
this.left.midOrder();
}
System.out.println(this);
if (this.right!=null){
this.right.midOrder();
}
}
public void suffixOrder(){
if (this.left!=null){
this.left.suffixOrder();
}
if (this.right!=null){
this.right.suffixOrder();
}
System.out.println(this);
}
public Emp preSearch(int id){
if (this.id==id){
return this;
}
Emp emp=null;
if (this.left!=null){
emp=this.left.preSearch(id);
}
if (emp!=null){
return emp;
}
if (this.right!=null){
emp=this.right.preSearch(id);
}
return emp;
}
public Emp midSearch(int id){
Emp emp=null;
if (this.left!=null){
emp=this.left.preSearch(id);
}
if (emp!=null){
return emp;
}
if (this.id==id){
return this;
}
if (this.right!=null){
emp=this.right.preSearch(id);
}
return emp;
}
public Emp suffixSearch(int id){
Emp emp=null;
if (this.left!=null){
emp=this.left.preSearch(id);
}
if (emp!=null){
return emp;
}
if (this.right!=null){
emp=this.right.preSearch(id);
}
if (emp!=null){
return emp;
}
if (this.id==id){
return this;
}
return emp;
}
}
class BinaryTree {
//这其实跟哈希表一样的结构
private Emp root;
public void setRoot(Emp root) {
this.root = root;
}
public void qian() {
if (this.root != null) {
this.root.preOrder();
} else {
System.out.println("empty");
}
}
public void zhong() {
if (this.root != null) {
this.root.midOrder();
} else {
System.out.println("empty");
}
}
public void hou() {
if (this.root != null) {
this.root.suffixOrder();
} else {
System.out.println("empty");
}
}
public Emp q(int id){
System.out.println("进入前序遍历");
if (root!=null){
return root.preSearch(id);
}else {
return null;
}
}
public Emp z(int id){
if (root!=null){
return root.midSearch(id);
}else {
return null;
}
}
public Emp h(int id){
if (root!=null){
return root.suffixSearch(id);
}else {
return null;
}
}
}
这一路,灯火通明
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· AI与.NET技术实操系列:基于图像分类模型对图像进行分类
· go语言实现终端里的倒计时
· 如何编写易于单元测试的代码
· 10年+ .NET Coder 心语,封装的思维:从隐藏、稳定开始理解其本质意义
· .NET Core 中如何实现缓存的预热?
· 分享一个免费、快速、无限量使用的满血 DeepSeek R1 模型,支持深度思考和联网搜索!
· 基于 Docker 搭建 FRP 内网穿透开源项目(很简单哒)
· 25岁的心里话
· ollama系列01:轻松3步本地部署deepseek,普通电脑可用
· 按钮权限的设计及实现