当前位置:网站首页>------实现二叉搜索树BST
------实现二叉搜索树BST
2022-07-30 05:46:00 【RSDYS】
二叉搜索树,复习记录:
二叉查找树(Binary Search Tree),(又:二叉搜索树,二叉排序树)它或者是一棵空树,或者是具有下列性质的二叉树: 若它的左子树不空,则左子树上所有结点的值均小于它的根结点的值; 若它的右子树不空,则右子树上所有结点的值均大于它的根结点的值; 它的左、右子树也分别为二叉排序树。二叉搜索树作为一种经典的数据结构,它既有链表的快速插入与删除操作的特点,又有数组快速查找的优势;所以应用十分广泛,例如在文件系统和数据库系统一般会采用这种数据结构进行高效率的排序与检索操作。
代码:
public class BinarySortTreeDemo {
public static void main(String[] args) {
int[] arr = {7, 3, 10, 12, 5, 1, 9};
BinarySortTree binarySortTree = new BinarySortTree();
for(int i : arr){
binarySortTree.add(new Node(i));
}
//中序遍历
System.out.println("中序遍历二叉搜索树");
binarySortTree.infixOrder();
}
}
//创建二叉排序树
class BinarySortTree{
private Node root;
public void add(Node node){
if(root == null){
root = node;//空树时直接指到这个结点就行
}else{
root.add(node);
}
}
//中序遍历
public void infixOrder(){
if(root != null){
root.infixOrder();
}else{
System.out.println("为空,不能遍历");
}
}
}
//创建Node结点
class Node{
int value;
Node left;
Node right;
public Node(int value) {
this.value = value;
}
//添加结点的方法
//递归的形式添加节点
public void add(Node node){
if(node == null){
return;
}
//判断传入的结点的值,和当前子树的根节点的值关系
if(node.value < this.value){
if(this.left == null){
this.left = node;
}else{
this.left.add(node);
}
}else{
if(this.right == null){
this.right = node;
}else{
this.right.add(node);
}
}
}
public void infixOrder(){
if(this.left != null){
this.left.infixOrder();
}
System.out.println(this.value);
if(this.right != null){
this.right.infixOrder();
}
}
}边栏推荐
- >>> /deep/ ::v-deep 深度作用选择器
- QT serial port dynamically displays a large number of data waveform curves in real time (5) ======== "Final perfect solution version"
- Through the bit operations to convert the characters are case sensitive
- About map custom sorting of keys
- 关于报错vscode
- Massive remote sensing data processing and application of GEE cloud computing technology [basic, advanced]
- openssl1.1.1ARM dual compilation
- 昆仑通态屏幕制作(连载1)---接触篇
- Diwen serial screen production (serialization 1) ===== preparation work
- sizeof和strlen最全区别,以及指针和数组运算解析
猜你喜欢
![Massive remote sensing data processing and application of GEE cloud computing technology [basic, advanced]](/img/38/239933ac987da762135db2d13902d0.png)
Massive remote sensing data processing and application of GEE cloud computing technology [basic, advanced]

pdf和word等文档添加水印

Word使用中常用的快捷键
CPU的三种工作模式:实模式、保护模式、长模式

电子工程师怎么才能规范设计标准、提高设计效率?

TCP为什么要三次握手,握手过程中丢包会怎么样?

Cannnot download sources不能下载源码百分百超详细解决方案

QT每周技巧(3)~~~~~~~~~串口添加

给Vscode配置ESlint语法检查 — ESLint 插件自动格式化设置(实现Ctrl+S 按照ESLint规则自动格式化代码)

干货:线上下单应知应会,快来了解下
随机推荐
华秋第八届硬创赛与安创加速器达成战略合作,助力硬科技项目成长
QT连载1:readyRead()函数,数据分包不完整解决办法
js 替换字符串中所有 “ 引号 —— 数据处理
About map custom sorting of keys
三种内核结构---宏内核、微内核、混合内核
survivor区对象何时进入老年代(深入理解jvm中表述不准确的地方)
顺序二叉树---实现数组的二叉树前序遍历输出
2020-09-03 Solve the very slow installation of pip install [Errno 101] Network unreachable problem
[Jiangsu University of Science and Technology Automation Association stm32F103c8t6] Notes [Initial 32 MCU and TIM timing interrupt initialization parameter configuration]
sizeof和strlen最全区别,以及指针和数组运算解析
Antd简单启动一个企业级项目
给Vscode配置ESlint语法检查 — ESLint 插件自动格式化设置(实现Ctrl+S 按照ESLint规则自动格式化代码)
Difference between logical shift right and arithmetic right shift
关于 PCB 多层板制程能力不得不说的那些事儿
【江科大自化协stm32F103c8t6】笔记之【入门32单片机及利用TIM输出比较配置PWM】
QT serial 3: LORA test platform based on QT and STM32H750 (2)
【已解决:el-input标签无法输入或不显示文字】
VsCode打开终端的方法
Antd 树拖拽一些细节,官网没有,摸坑篇
jvm之逃逸分析