可视化网址
先分享一个可视化网址:https://www.cs.usfca.edu/~galles/visualization/Algorithms.html
底部可以调速度和暂停
首先说一下节点node就是一整块,关键字key就是每一个值
B树
献上维基百科的定义
According to Knuth’s definition, a B-tree of order m is a tree which satisfies the following properties:
- Every node has at most m children.
- Every internal node has at least ⌈m/2⌉ children.
- Every non-leaf node has at least two children.
- All leaves appear on the same level and carry no information.
- A non-leaf node with k children contains k−1 keys.
就是M阶的B树会有:(M阶树是每个节点的最大的孩子数量是M)
children:ceil(M/2) ~ M
每个节点中的keys个数:ceil(M/2) – 1 ~ M–1 (如果阶 M = 3,每个节点内keys有 ⌈3/2⌉-1 ~ 3-1 个,即1~2个 keys)
A non-leaf node with M children contains M−1 keys
意思是:一个节点有M个子节点(有M个指针),因为node 左右+中间 分割的,所以这个node的指针会比这个节点的内部key 多一个,如图看这个B+树:
B+树
定义:https://en.wikipedia.org/wiki/B%2B_tree
- Each node except root can have a maximum of M children and at least ceil(M/2) children.
- Each node can contain a maximum of M – 1 keys and a minimum of ceil(M/2) – 1 keys.
- The root has at least two children and at least one search key.
- While insertion overflow of the node occurs when it contains more than M – 1 search key values.
M阶B+树,M is the order of B+ tree:
每个节点的 children 个数:ceil(M/2) ~ M
每个节点内部的 keys 个数:ceil(M/2) – 1 ~ M– 1
B+树的插入
B+树插入:从最底层找到插入key,这个节点中的keys有富余的话则从中间裂开, 然后合到父节点那里,如果这一层也满了也裂开,层层往上。
B+树的删除
关于删除操作,还没很会。。。
B+树删除:借兄弟,借父,再不行就降低高度。