当前位置:网站首页>二分查找判定树(二分查找树平均查找长度)
二分查找判定树(二分查找树平均查找长度)
2022-07-27 11:36:00 【全栈程序员站长】
大家好,又见面了,我是你们的朋友全栈君。
Don’t say much, just go to the code.
package org.bood.tree;
/** * 二分查找树 * ps:如果 data[0] 等于一组数据中最小的,那么就会增加查找的时间复杂度。<br/> * 平衡二叉树(追求极致的平衡),现实需求很难满足,红黑数孕育而生 <br/> * * @author bood * @since 2020/10/16 */
public class BinarySearchTree {
/** * 根节点数 */
int data;
/** * 左边的数 */
BinarySearchTree left;
/** * 右边的数 */
BinarySearchTree rigth;
public BinarySearchTree(int data) {
this.data = data;
this.left = null;
this.rigth = null;
}
// 二分查找
public void insert(BinarySearchTree root, int data) {
// 数大于根节点数,右边
if (data > root.data) {
// 右边是空的直接插入
if (null == root.rigth) {
root.rigth = new BinarySearchTree(data);
} else {
insert(root.rigth, data);
}
// 数大于根节点数,左边
} else {
// 左边是空的直接插入
if (null == root.left) {
root.left = new BinarySearchTree(data);
} else {
insert(root.left, data);
}
}
}
// 中序遍历
public void in(BinarySearchTree root) {
if (null != root) {
in(root.left);
System.out.print(root.data + " ");
in(root.rigth);
}
}
public static void main(String[] args) {
int[] data = {
5, 6, 1, 7, 8, 9, 2, 4, 10};
BinarySearchTree root = new BinarySearchTree(data[0]);
for (int i = 0; i < data.length; i++) {
root.insert(root, data[i]);
}
System.out.println("中序遍历:");
root.in(root);
}
}发布者:全栈程序员栈长,转载请注明出处:https://javaforall.cn/128209.html原文链接:https://javaforall.cn
边栏推荐
- go入门篇 (5)
- LNMP架构搭建(部署Discuz论坛)
- 意外收获史诗级分布式资源,从基础到进阶都干货满满,大佬就是强!
- 【机器学习-白板推导系列】学习笔记---条件随机场
- LAN SDN hard core technology insider 24 outlook for the future - RDMA (middle)
- Interaction free shell programming
- LAN SDN hard core technology insider 25 looking forward to the future - RDMA (Part 2)
- NewTicker使用
- Docker Mysql的使用note
- Wilcoxon rank-sum 和 signed-rank
猜你喜欢

How to make a graph? Multiple subgraphs in a graph are histogram (or other graphs)

广东:剧本杀等新行业新业态场所,消防安全监管不再“缺位”

Regular expression of shell programming (grep of shell script text three swordsmen)

Newton Raphson iterative method

Could not load dynamic library ‘libcudnn.so.8‘;

解决方案:idea project没有显示树状图

Shell script text three swordsman awk

Beyond compare 3 next difference segment / down search arrow not found

IDEA: Can‘t use Subversion command line client:svn 解决方案

MySQL数据库主从复制集群原理概念以及搭建流程
随机推荐
Newton-Raphson迭代法
Sword finger offer notes: T53 - ii Missing numbers from 0 to n-1
[untitled] multimodal model clip
Keil MDK编译出现..\USER\stm32f10x.h(428): error: #67: expected a “}“错误的解决办法
剑指 Offer 笔记: T58 - II. 左旋转字符串
omitempty在go中的使用
Interaction free shell programming
JS string method summary
Synchronous use reference of the new version of data warehouse (for beginners)
Matlab S-function详解
NPM step pit
Matlab draws Bode diagram with time delay system
Proteus8专业版破解后用数码管闪退的解决
Sword finger offer notes: t57 - ii Continuous positive sequence with sum s
日本福岛废堆安全监视协议会认可排海计划“安全”
compute_class_weight() takes 1 positional argument but 3 were given
哈希表 详细讲解
deeplab系列详解(简单实用年度总结)
Pytorch shows the summary like tensorflow
一些MathType常用快捷键