java 二叉查找树实例代码
作者:lqh 时间:2022-07-23 22:54:28
java 二叉查找树实例代码
1.左边<中间<右边
2.前序遍历 左中右
3.中序遍历 中左右
4.后序遍历 左右中
public class BinaryTree {
// 二叉树的根节点
public TreeNode rootNode ;
// 记录搜索深度
public int count;
/**
* 利用传入一个数组来建立二叉树
*/
public BinaryTree(int[] data) {
for (int i = 0; i < data. length; i++) {
addNodeToTree(data[i]);
}
}
/**
* 将指定的值加入到二叉树中适当的节点
*/
private void addNodeToTree(int value) {
TreeNode currentNode = rootNode;
// 建立树根
if (rootNode == null) {
rootNode = new TreeNode(value);
return;
}
// 建立二叉树
while (true) {
// 新增的value比节点的value小,则在左子树
if (value < currentNode.value ) {
if (currentNode.leftNode == null) {
currentNode.leftNode = new TreeNode(value);
return;
} else {
currentNode = currentNode.leftNode;
}
} else { // 新增的value比节点的value大,在右子树
if (currentNode.rightNode == null) {
currentNode. rightNode = new TreeNode(value);
return;
} else {
currentNode = currentNode. rightNode;
}
}
}
}
/**
* 中序遍历(左子树 -树根- 右子树)
*/
public void inOrder(TreeNode node) {
if (node != null) {
inOrder(node. leftNode);
System. out.print("[" + node.value + "]");
inOrder(node. rightNode);
}
}
/**
* 前序遍历(树根 -左子树- 右子树)
*/
public void preOrder(TreeNode node) {
if (node != null) {
System. out.print("[" + node.value + "]");
preOrder(node. leftNode);
preOrder(node. rightNode);
}
}
/**
* 后序遍历(左子树 -右子树- 树根)
*/
public void postOrder(TreeNode node) {
if (node != null) {
postOrder(node. leftNode);
postOrder(node. rightNode);
System. out.print("[" + node.value + "]");
}
}
/**
* 从二叉树中查找指定value
*/
public boolean findTree(TreeNode node, int value) {
if (node == null) {
System. out.println("共搜索" + count + "次");
return false;
} else if (node.value == value) {
System. out.println("共搜索" + count + "次");
return true;
} else if (value < node.value) {
count++;
return findTree(node.leftNode , value);
} else {
count++;
return findTree(node.rightNode , value);
}
}
/**
* 利用中序遍历进行排序
*/
public void sort() {
this.inOrder(rootNode );
}
class TreeNode {
int value ;
TreeNode leftNode;
TreeNode rightNode;
public TreeNode(int value) {
this.value = value;
this.leftNode = null;
this.rightNode = null;
}
}
public static void main(String[] args) {
int[] content = { 50, 35, 27, 45, 40, 48, 78, 56, 90 };
BinaryTree tree = new BinaryTree(content);
System. out.println("前序遍历:" );
tree.preOrder(tree. rootNode);
System. out.println("\n中序遍历:" );
tree.inOrder(tree. rootNode);
System. out.println("\n后序遍历:" );
tree.postOrder(tree. rootNode);
System. out.println("\n\n开始搜索:" );
boolean isFind = tree.findTree(tree.rootNode, 48);
System. out.println("是否搜索到" + 48 + ":" + isFind);
System. out.println("\n进行排序:" );
tree.sort();
}
}
前序遍历:
[50][35][27][45][40][48][78][56][90]
中序遍历:
[27][35][40][45][48][50][56][78][90]
后序遍历:
[27][40][48][45][35][56][90][78][50]
开始搜索:
共搜索3次
是否搜索到48:true
进行排序:
[27][35][40][45][48][50][56][78][90]
感谢阅读,希望能帮助到大家,谢谢大家对本站的支持!
来源:https://my.oschina.net/u/1037605/blog/775018
标签:java,二叉查找树,二叉树
![](/images/zang.png)
![](/images/jiucuo.png)
猜你喜欢
SpringMVC接收复杂集合对象(参数)代码示例
2023-01-29 18:33:51
Struts 2中实现Ajax的三种方式
2022-04-30 05:46:28
![](https://img.aspxhome.com/file/2023/7/65307_0s.jpg)
浅谈JVM垃圾回收有哪些常用算法
2022-02-28 16:51:56
C#使用Aspose.Cells创建和读取Excel文件
2022-11-24 17:47:23
![](https://img.aspxhome.com/file/2023/2/75592_0s.jpg)
SpringBoot项目集成Flyway进行数据库版本控制的详细教程
2023-11-24 05:20:33
![](https://img.aspxhome.com/file/2023/0/59390_0s.png)
springboot项目启动慢的问题排查方式
2023-06-19 18:58:40
![](https://img.aspxhome.com/file/2023/0/57590_0s.png)
PageHelper插件实现一对多查询时的分页问题
2021-11-05 07:02:34
浅谈Java自定义类加载器及JVM自带的类加载器之间的交互关系
2021-09-12 23:37:24
![](https://img.aspxhome.com/file/2023/0/66510_0s.jpg)
Springcloud seata nacos环境搭建过程图解
2022-11-15 00:34:14
![](https://img.aspxhome.com/file/2023/1/61341_0s.png)
SpringBoot之Json的序列化和反序列化问题
2021-11-12 07:17:29
Winform 实现进度条弹窗和任务控制
2023-06-20 04:27:09
![](https://img.aspxhome.com/file/2023/4/66134_0s.png)
常用Maven库,镜像库及maven/gradle配置(小结)
2023-11-20 23:44:00
Spring+SpringMVC+MyBatis深入学习及搭建(二)之MyBatis原始Dao开发和mapper代理开发
2021-07-24 06:36:00
![](https://img.aspxhome.com/file/2023/4/67124_0s.png)
java实现输入输出流代码分享
2023-11-18 01:03:45
SpringBoot集成Elasticsearch过程实例
2022-07-30 20:08:56
![](https://img.aspxhome.com/file/2023/6/70486_0s.png)
java中struts2实现文件上传下载功能实例解析
2022-03-31 06:47:38
Java回调函数与观察者模式实例代码
2023-11-16 17:30:11
基于RxJava实现酷炫启动页
2023-09-26 21:50:47
![](https://img.aspxhome.com/file/2023/7/86007_0s.gif)
mybatis自定义类型处理器TypehHandler示例详解
2023-10-11 04:30:40
![](https://img.aspxhome.com/file/2023/3/69133_0s.png)
spring框架cacheAnnotation缓存注释声明解析
2022-04-14 17:13:05
![](https://img.aspxhome.com/file/2023/3/65323_0s.png)