当前位置:网站首页>B-树的应用以及添加和删除操作
B-树的应用以及添加和删除操作
2022-07-27 00:13:00 【Aries_Ro】
B-树
B-树的应用
B-树适用于磁盘存储应用中。
因为磁盘操作相较于CPU读取操作比较费时,而B-树相较于二叉树来说,有更少的查找次数,效率更高。
用一个例子说明:
假如有1024个数据,用二叉树来存储的话,会有10层的树的高度。检索数据时从根节点到叶子节点是需要经过多次的数据比对操作。而在磁盘操作中每次对比后找下一个节点就是磁盘寻址。如果层比较高的话,就会寻找很多次。相对来说B-树的寻址次数会少很多。(因为一个节点包含了多个key值,总的来说会减少了树的高度)。
B-树的特点
一颗M阶B-树,满足以下条件(M阶是指该B-树是M叉树):
1. 每个节点至多拥有M颗子树
2. 根节点至少拥有两颗子树
3. 除了根节点以外,其余每个分支节点至少拥有M/2颗子树
4. 所有的叶子节点都在同一层上
5. 有k个子树的分支节点则存在k-1个关键字,关键字按照递增顺序进行排序
6. 根节点外,关键字数量满足ceil(M/2)-1 <= n <= M-1
分析一下这些条件:
如图是一个6阶B-树

条件1:比如(0012 0016 0022 0025 0028)这个节点,最多只能连6根线下去;
条件2比较好理解
条件3:就是每个分支节点必须要有3颗及以上的子树
条件4:叶子节点都在一层,保持绝对平衡。
条件5:比如(0003 0006)这个节点有2个关键字,它就有3个子树。因为有三个范围,分别对应三个子树。例如{x < 0003; 0003 < x < 0006; x > 0006}三个范围。
条件6:这个6阶B-树中,关键字的数量满足 2≤n≤5。
满足这6个条件就是B-树了。
B-树的操作
值的添加
B-树中值的添加涉及到分裂操作。因为当添加到一个节点的关键字数等于阶数时,就会通过分裂操作来满足B-树的性质。
还是以6阶B-树为例:
从(0001 0002 0003 0004 0005)该节点再添加0006关键字,
当节点超过5个key值时,会执行分裂操作。
值的删除
B-树中值的删除涉及到合并和借位操作。
以6阶B-树为例:
合并操作
删除0001关键字,

由于删除0001后,该节点只有0002一个关键字了,不满足条件6,因此需要合并操作。
借位操作
删除0002关键字,

由于删除0002后,该节点只有0003一个关键字了,这时候就借位根节点0006,将0007升级为根节点。
边栏推荐
- Blog competition dare to try BAC for beginners
- Tabbar of customized wechat applet on uni app
- 人们为什么热衷于给事物排序
- Leetcode skimming -- no.238 -- product of arrays other than itself
- F8 catch traffic, F9 catch rabbits, f10turttle
- com.fasterxml.jackson.databind.exc.InvalidDefinitionException
- 解决小程序报错getLocation:fail the api need to be declared in the requiredPrivateInfos field in app.json
- Heisei thousand words (Heisei, September 10, 2012) (Shidu Mingzuo) the universe is far away, the Milky way is permanent, the sun and moon are running disorderly, the earth is open, the seasons shift,
- Plato Farm通过LaaS协议Elephant Swap,为社区用户带来全新体验
- I was fired at the age of 30. I want to understand a few things
猜你喜欢

人们为什么热衷于给事物排序

手动从0搭建ABP框架-ABP官方完整解决方案和手动搭建简化解决方案实践

Rust web (I) -- self built TCP server

Redis installation and operation (Linux)

用swiper分类图标

多线程的具体使用

Graduated and entered HW, from test engineer to project manager. Now I earn millions in goose factory every year. My suggestions to you

数据资产管理的概念

CS224W fall 课程 ---- 1.1 why Graphs ?

C language program compilation (preprocessing)
随机推荐
Okaleido tiger logged into binance NFT on July 27, and has achieved good results in the first round
time模块: 时间戳、结构化时间、格式化时间的获取与相互转化
人们为什么热衷于给事物排序
JS utils fragmented
Concept of data asset management
系统安全测试要怎么做,详细来说说
Quick sort
Play a parallel multithreaded mcu-mc3172
bp 插件临时代码记录
快速排序(Quick sort)
JMeter interface test, quickly complete a single interface request
Tabbar of customized wechat applet on uni app
How to do the system security test? Let's talk about it in detail
#博客大赛# 斗胆尝试浅入门之BAC
Rust web (I) -- self built TCP server
Scheduling of processes
【RYU】安装RYU常见问题及解决办法
Comprehensive summary of shell analysis log file commands
Rust Web(一)—— 自建TCP Server
I was fired at the age of 30. I want to understand a few things