Java基于二叉查找树实现排序功能示例
作者:冷豪 时间:2022-09-04 21:50:52
本文实例讲述了Java基于二叉查找树实现排序功能。分享给大家供大家参考,具体如下:
/**
* 无论排序的对象是什么,都要实现Comparable接口
*
* @param <T>
*/
public class BinaryNode<T extends Comparable<T>> {
private static int index = 0; // 排序下标
private static int len = 0; // 最大数组长度
private T t; // 根节点
private BinaryNode<T> left; // 左侧叶子节点
private BinaryNode<T> right; // 右侧叶子节点
public BinaryNode(T t) {
len++;
this.t = t;
}
/**
* 往一颗书中插入值,在本质上都通过根节点一层层的判断。
* 如果根节点不存在则新建节点
* 如果根节点存在则判断应该在左侧还是在右侧插入,通常是左小右大
*
* @param t
*/
public void insert(T t) {
if (this.t.compareTo(t) > 0) {
if (this.left == null) {
BinaryNode<T> node = new BinaryNode<T>(t);
this.left = node;
} else {
this.left.insert(t);
}
} else {
if (this.right == null) {
BinaryNode<T> node = new BinaryNode<T>(t);
this.right = node;
} else {
this.right.insert(t);
}
}
}
/**
* 调用私有方法
*
* @return
*/
public Comparable<?>[] order() {
Comparable<?>[] os = new Comparable[len];
order(this, os);
return os;
}
/**
* 利用中序遍历查找整颗树
*
* @param bn
* @param os
*/
private void order(BinaryNode<T> bn, Comparable<?>[] os) {
if (bn.left == null) {
os[index++] = bn.t;
} else {
order(bn.left, os);
os[index++] = bn.t;
}
if (bn.right == null) {
return;
} else {
order(bn.right, os);
}
}
}
希望本文所述对大家java程序设计有所帮助。
来源:http://www.cnblogs.com/learnhow/p/6047421.html
标签:Java,二叉查找树,排序
![](/images/zang.png)
![](/images/jiucuo.png)
猜你喜欢
Java经典面试题最全汇总208道(四)
2023-11-08 23:59:26
![](https://img.aspxhome.com/file/2023/5/59065_0s.png)
Springboot之idea之pom文件图标不对问题
2021-12-31 07:27:32
![](https://img.aspxhome.com/file/2023/0/62520_0s.png)
Java数据结构之链表相关知识总结
2023-11-02 00:29:28
![](https://img.aspxhome.com/file/2023/4/58724_0s.png)
Spring Bean实例的创建及构造器的挑选
2021-08-02 09:35:57
Java VisualVM监控远程JVM(详解)
2022-07-10 15:59:15
![](https://img.aspxhome.com/file/2023/8/61588_0s.jpg)
Java数据结构之队列(动力节点Java学院整理)
2021-09-22 04:58:19
![](https://img.aspxhome.com/file/2023/6/65746_0s.png)
Java设计模式之java备忘录模式详解
2023-08-22 19:31:07
![](https://img.aspxhome.com/file/2023/4/58434_0s.png)
Mybatis Log Plugin的使用方式
2021-08-27 19:02:31
![](https://img.aspxhome.com/file/2023/8/61828_0s.jpg)
解决maven没有打包xml文件的问题
2023-11-27 14:51:01
深入理解Java设计模式之备忘录模式
2023-09-20 06:16:43
![](https://img.aspxhome.com/file/2023/3/58433_0s.jpg)
简述Java图像倾斜方法及实例 <font color=red>原创</font>
2023-08-16 09:38:47
详解Mybatis动态sql
2023-09-16 15:41:38
![](https://img.aspxhome.com/file/2023/4/69924_0s.png)
深入理解C#之枚举
2023-01-02 14:08:33
C#获取真实IP地址(IP转为长整形、判断是否内网IP的方法)
2022-01-31 08:50:06
![](https://img.aspxhome.com/file/2023/8/69218_0s.jpg)
了解Java虚拟机JVM的基本结构及JVM的内存溢出方式
2023-02-20 03:08:51
![](https://img.aspxhome.com/file/2023/8/63288_0s.png)
java 使用异常的好处总结
2023-11-29 13:35:49
Hibernate持久化对象生命周期原理解析
2022-08-14 19:44:27
![](https://img.aspxhome.com/file/2023/2/77482_0s.png)
java调用oracle分页存储过程示例
2021-06-08 12:44:16
selenium+java破解极验滑动验证码的示例代码
2022-11-19 21:52:01
![](https://img.aspxhome.com/file/2023/1/74891_0s.png)
关于Lambda表达式的方法引用和构造器引用简的单示例
2022-09-18 05:02:08
![](https://img.aspxhome.com/file/2023/1/75681_0s.jpg)