当前位置:网站首页>B树和B+树
B树和B+树
2022-07-25 12:39:00 【51CTO】
B树

B树是多路平衡查找树。每个节点存放的键值对,即索引和数据。m阶B树的定义:
每个节点最多有m个子节点,最多有m-1个关键字,最少有m/2个关键字。(根节点最少可以只有一个元素)
每个节点中的关键字从小到大排序,每个关键字的左子树中所有关键字都小于它,右子树中所有关键字都大于它。
所有叶子节点位于同一层。
B树插入:
当关键字插入某一个节点后,如果节点中关键字个数小于等于m-1,插入完成,否则将节点中间的关键字放入到父节点,剩下左右两部分分裂为父节点的左子树和右子树。
例子:在5阶B树中,节点最多有4个关键字,最少有两个关键字:

插入23,25,39:

此时左子树的关键字已经大于4个了,需要进行分裂:

B树删除:
首先查找B树中需删除的元素,如果该元素在B树中存在,则将该元素在其结点中进行删除;删除该元素后,首先判断该元素是否有左右孩子结点,如果有,则上移孩子结点中的某相近元素(“左孩子最右边的节点”或“右孩子最左边的节点”)到父节点中,然后是移动之后的情况;如果没有,直接删除。
删除叶子节点中的元素:
- 删除后结点中元素数目小于m/2,则需要看其某相邻兄弟结点是否丰满;
- 如果丰满,则向父节点借一个元素,然后相邻节点的第一个或最后一个元素上移到父节点;
- 如果其相邻兄弟都不丰满,即其结点数目等于m/2,则该结点与其相邻的某一兄弟结点进行“合并”成一个结点;





B+树

m阶B+树:
每个节点最多有m个子节点,每个子树对应一个关键字,最多有m个关键字,最少有m/2个关键字。
与B树的不同点:
1.B+树的数据都存储在叶子节点,内部节点不存储数据,只存储索引。所以B+树的查询时间复杂度固定为logn,而B树的查询时间复杂度不固定,与key在树中的位置有关,最好为O(1)。B+树由于内部节点只存储索引,所以每个节点能索引的范围更大,B+树更适合外部存储。
2.每个叶子节点都有相邻叶子结点的指针,所有叶子节点按关键字大小自小而大链接。非常适用于范围查询。
3.父节点中有每一个子节点的第一个关键字。

B+树插入:
当节点元素数量大于m-1时,按中间元素分裂成左右两部分,中间元素分裂到父节点当作索引存储,但是本身中间元素还是在分裂右边部分。
例子:5阶B+树种,节点最少2个元素,最多4个元素。插入5,10,15,20:

插入25,此时元素数量大于4个了,分裂:

接着插入26,30,继续分裂:


参考:
https://segmentfault.com/a/1190000020416577
https://blog.csdn.net/weixin_43156699/article/details/117216784
https://blog.csdn.net/qq_34515959/article/details/109155630
https://blog.csdn.net/a519640026/article/details/106940115
边栏推荐
- 2022.07.24(LC_6125_相等行列对)
- flinkcdc可以一起导mongodb数据库中的多张表吗?
- Detailed explanation of flex box
- Mid 2022 review | latest progress of large model technology Lanzhou Technology
- 迁移PaloAlto HA高可用防火墙到Panorama
- [operation and maintenance, implementation of high-quality products] interview skills for technical positions with a monthly salary of 10k+
- OAuth,JWT ,OIDC你们搞得我好乱啊
- Zero basic learning canoe panel (16) -- clock control/panel control/start stop control/tab control
- Keeping MySQL highly available
- [ROS advanced chapter] Lecture 9 programming optimization of URDF and use of xacro
猜你喜欢

The larger the convolution kernel, the stronger the performance? An interpretation of replknet model

【C语言进阶】动态内存管理
![Detailed explanation of switch link aggregation [Huawei ENSP]](/img/34/dff118b52404e35f74a8f06b2517be.png)
Detailed explanation of switch link aggregation [Huawei ENSP]

2022.07.24 (lc_6124_the first letter that appears twice)

如何用因果推断和实验驱动用户增长? | 7月28日TF67
![[operation and maintenance, implementation of high-quality products] interview skills for technical positions with a monthly salary of 10k+](/img/d8/90116f967ef0f5920848eca1f55cdc.png)
[operation and maintenance, implementation of high-quality products] interview skills for technical positions with a monthly salary of 10k+

If you want to do a good job in software testing, you can first understand ast, SCA and penetration testing

【运维、实施精品】月薪10k+的技术岗位面试技巧

微软提出CodeT:代码生成新SOTA,20个点的性能提升

2022.07.24(LC_6124_第一个出现两次的字母)
随机推荐
Use of hystrix
2022.07.24 (lc_6125_equal row and column pairs)
If you want to do a good job in software testing, you can first understand ast, SCA and penetration testing
微软提出CodeT:代码生成新SOTA,20个点的性能提升
【AI4Code】《CodeBERT: A Pre-Trained Model for Programming and Natural Languages》 EMNLP 2020
Shell Basics (exit control, input and output, etc.)
If you want to do a good job in software testing, you can first understand ast, SCA and penetration testing
Cmake learning notes (II) generation and use of Library
2022 Henan Mengxin League game (3): Henan University I - Travel
Azure Devops (XIV) use azure's private nuget warehouse
Does MySQL have flush privileges
Is the securities account opened by qiniu safe? How to open an account
零基础学习CANoe Panel(12)—— 进度条(Progress Bar)
[advanced C language] dynamic memory management
零基础学习CANoe Panel(16)—— Clock Control/Panel Control/Start Stop Control/Tab Control
Microsoft proposed CodeT: a new SOTA for code generation, with 20 points of performance improvement
跌荡的人生
Experimental reproduction of image classification (reasoning only) based on caffe resnet-50 network
2022.07.24 (lc_6126_design food scoring system)
SSTI template injection vulnerability summary [bjdctf2020]cookie is so stable