了解B +树插入

时间:2013-04-17 17:10:13

标签: database algorithm indexing b-tree

我正在尝试按以下顺序创建B +树

10 20 30 40 50 60 70 80 90 100

所有索引节点应至少包含2个密钥和最多3个密钥。我能够插入到90,但是只要插入100,它就会将高度从2增加到3.

问题是root的第二个孩子有一个节点,我无法解决它。应该至少有2个,对吧?有人可以指导我吗?

更新:我正在关注此算法

If the bucket is not full (at most b - 1 entries after the insertion), add the record.
Otherwise, split the bucket.
Allocate new leaf and move half the bucket's elements to the new bucket.
Insert the new leaf's smallest key and address into the parent.
If the parent is full, split it too.
Add the middle key to the parent node.
Repeat until a parent is found that need not split.
If the root splits, create a new root which has one key and two pointers. (That is, the value that gets pushed to the new root gets removed from the original node)

P.S:我手动手动操作,以了解算法。没有代码!

2 个答案:

答案 0 :(得分:5)

我相信你的B +树没问题,假设你的B + Tree的顺序是3.如果订单 m ,每个内部节点都可以⌈m/2⌉ m 孩子。在您的情况下,每个内部节点可以有2到3个子节点。在B + Tree中,如果一个节点只有2个子节点,那么它只需要1个键,因此B + Tree不会违反任何约束。

如果您仍然感到困惑,请查看此B+ Tree Simulator。试试吧。

答案 1 :(得分:0)

要获取插入值10到100后绘制的树,树的顺序必须是4而不是3.否则给出的答案是正确的:顺序m允许每个叶子和每个节点中的m-1个键。之后,维基百科的描述变得有点混乱,因为它专注于儿童而不是键,并没有提到如何处理舍入。只处理密钥,规则是:

Max keys for all nodes = Order-1
Min keys for leaf nodes = floor(Order/2)
Min keys for internal nodes = floor(maxkeys/2)

所以你在节点中有一个键是正确的(order = 4,max = 3,minleaf = 2,minnode = 1)。您可能会发现此页面很有用,因为它具有进程的在线JavaScript版本以及插入和删除的文档:

http://goneill.co.nz/btree.php