Java实现二叉树的前中后序查找完整代码示例
结点类
package DataStrcture.binarytreedemo_1;
public class BinaryTreeNodeSe {
//结点类
private BinaryTreeNodeSe leftNode;
private BinaryTreeNodeSe rightNode;
private int id;
private String name;
//构造方法,toString, setget方法
public BinaryTreeNodeSe(int id, String name) {
this.id = id;
this.name = name;
}
public String toString() {
return "id: " + id + ", name: " + name;
}
public void setLeftNode(BinaryTreeNodeSe node) {
this.leftNode = node;
}
public void setRightNode(BinaryTreeNodeSe node) {
this.rightNode = node;
}
//二叉树查找
public BinaryTreeNodeSe preOrderSearchById(int id) {
if (this.id == id) return this;
BinaryTreeNodeSe res = null;
if (this.leftNode != null) {
res = this.leftNode.preOrderSearchById(id);
}
if (res != null) return res; //左子树找到了的话直接返回, 下面的也不用管了.
if (this.rightNode != null) {
res = this.rightNode.preOrderSearchById(id);
}
return res;
}
public BinaryTreeNodeSe midOrderSearchById(int id) {
//找到了, 直接返回
if (this.id == id) return this;
//左子树非空, 递归左子树查询
BinaryTreeNodeSe res = null;
if (this.leftNode != null) {
res = this.leftNode.midOrderSearchById(id);
}
//左子树查到了(res不为空), 直接返回即可, 下面的递归右子树直接略过
if (res != null) return res;
if (this.rightNode != null) {
res = this.rightNode.midOrderSearchById(id);
}
return res;
}
public BinaryTreeNodeSe postOrderSearchById(int id) {
if (this.id == id) return this;
BinaryTreeNodeSe res = null;
if (this.leftNode != null) {
res = this.leftNode.postOrderSearchById(id);
}
if (res != null) return res;
if (this.rightNode != null) {
res = this.rightNode.postOrderSearchById(id);
}
return res;
}
}
二叉树类
package DataStrcture.binarytreedemo_1;
public class BinaryTreeSe {
//
private BinaryTreeNodeSe root;
public BinaryTreeSe(BinaryTreeNodeSe node){
this.root = node;
}
//1. 前序遍历查找
public void preOrderSe(int id){
if(root != null){
BinaryTreeNodeSe res = root.preOrderSearchById(id);
if(res != null){
System.out.println("找到了id= "+id+"的二叉树结点, 具体的信息如下: ");
System.out.println(res);
}else{
System.out.println("二叉树上没有id="+id+"的结点");
}
}else{
System.out.println("二叉树为空, 查找失败! ");
}
}
// 2. 中序遍历查找
public void midOrderSe(int id){
//第一层判断, 二叉树是否为空
if( root != null){
BinaryTreeNodeSe res = root.midOrderSearchById(id);
// 第二层判断, 是否查到对应id 的结点
if(res != null){
System.out.println("找到了id= "+id+"的二叉树结点, 具体的信息如下: ");
System.out.println(res);
}else{
System.out.println("二叉树上没有id="+id+"的结点");
}
}else{
System.out.println("二叉树为空, 查找失败! ");
}
}
//3. 二叉树后序查找
public void postOrderSe(int id){
if( root != null){
BinaryTreeNodeSe res = root.postOrderSearchById(id);
if(res != null){
System.out.println("找到了id= "+id+"的二叉树结点, 具体的信息如下: ");
System.out.println(res);
}else{
System.out.println("二叉树上没有id="+id+"的结点");
}
}else{
System.out.println("二叉树为空, 查找失败! ");
}
}
public static void main(String[] args) {
//建立二叉树结点
BinaryTreeNodeSe node1 = new BinaryTreeNodeSe(1, "宋江");
BinaryTreeNodeSe node2 = new BinaryTreeNodeSe(2, "吴用");
BinaryTreeNodeSe node3 = new BinaryTreeNodeSe(3, "卢俊义");
BinaryTreeNodeSe node4 = new BinaryTreeNodeSe(4, "公孙胜");
BinaryTreeNodeSe node5 = new BinaryTreeNodeSe(5, "关俊");
//建立二叉树
BinaryTreeSe tree = new BinaryTreeSe(node1);
//结点放到二叉树中
node1.setLeftNode(node2);
node1.setRightNode(node3);
node3.setLeftNode(node4);
node3.setRightNode(node5);
//查找
System.out.println("二叉树前序查找结果如下: ");
tree.preOrderSe(5);
//查找
System.out.println("二叉中序查找结果如下: ");
tree.midOrderSe(5);
//查找
System.out.println("二叉树后序查找结果如下: ");
tree.postOrderSe(5);
}
}
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 全程不用写代码,我用AI程序员写了一个飞机大战
· DeepSeek 开源周回顾「GitHub 热点速览」
· 记一次.NET内存居高不下排查解决与启示
· MongoDB 8.0这个新功能碉堡了,比商业数据库还牛
· .NET10 - 预览版1新功能体验(一)