当前位置:网站首页>二分查找判定树(二分查找树平均查找长度)
二分查找判定树(二分查找树平均查找长度)
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
边栏推荐
- 剑指 Offer 笔记: T53 - II. 0~n-1 中缺失的数字
- JS parasitic combinatorial inheritance
- TapNet: Multivariate Time Series Classification with Attentional Prototypical Network
- 【机器学习-白板推导系列】学习笔记---条件随机场
- STM32编译出现error: L6235E: More than one section matches selector - cannot all be FIRST/L
- MySQL数据库主从复制集群原理概念以及搭建流程
- kazoo使用教程
- Wilcoxon rank sum and signed rank
- Weibo comment crawler + visualization
- compute_class_weight() takes 1 positional argument but 3 were given
猜你喜欢
随机推荐
Ask the big guys, is there transaction control for using flick sink data to MySQL? If at a checkpoint
Leetcode 02: sword finger offer 58 - I. flip the word order (simple); T123. Verify palindrome string; T9. Palindromes
Shell编程之正则表达式(Shell脚本文本三剑客之grep)
Newton Raphson iterative method
Sword finger offer notes: t57 - I. and two numbers of S
EfficientNet
Docker Mysql的使用note
意外收获史诗级分布式资源,从基础到进阶都干货满满,大佬就是强!
源码编译安装LAMP
数据库 cli 工具 docker 镜像
Can you really write binary search - variant binary search
Unity Shader 一 激光特效Shader[通俗易懂]
Temporary use of solo, difficult choice of Blog
mysql分页查询实例_mysql分页查询实例讲解「建议收藏」
Difference quotient approximation of wechat quotient
V-show failure
Solution of digital tube flash back after proteus8 professional version cracking
日本福岛废堆安全监视协议会认可排海计划“安全”
LAN SDN technology hard core insider 12 cloud CP's daily love - hardware vxlan forwarding plane
剑指 Offer 笔记: T53 - II. 0~n-1 中缺失的数字









