【问题标题】:Understanding B+ tree insertion了解 B+ 树插入
【发布时间】:2013-04-10 13:42:14
【问题描述】:

我正在尝试使用以下序列创建 B+ 树,

10 20 30 40 50 60 70 80 90 100

所有索引节点应该有最少 2 个和最多 3 个键。我能够插入到 90,但是一旦插入 100,它就会将高度从 2 增加到 3。

问题是根的第二个孩子有一个节点,我无法修复它。它应该至少有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:我是手动进行的,以了解算法。没有代码!

【问题讨论】:

  • 在没有看到任何代码的情况下指导您?我们不可能知道您的代码做错了什么。您是否怀疑问题可能是什么?
  • @usr- 没有代码,我是手工做的!这就是为什么没有语言标签,它的算法。
  • 然后告诉我们你正在使用什么规则。
  • 你的 B+ 树的顺序是什么
  • @RBarryYoung- 添加了更多细节..

标签: database algorithm indexing b-tree


【解决方案1】:

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

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

【讨论】:

  • 如果只有两个孩子,那么只有一把钥匙可以吗?我认为它总是必须介于 ciel(m/2)m 之间
  • 孩子的数量范围从⌈m/2⌉到m。所以我相信键的范围应该是 ⌈m/2⌉-1 到 m-1。
  • 好吧,听起来不错。我会等待一段时间,然后接受。谢谢:)]
【解决方案2】:

要在插入值 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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2012-01-18
    • 2012-04-10
    • 2016-03-02
    • 2021-08-27
    • 2011-05-20
    • 2019-12-02
    • 2011-07-24
    相关资源
    最近更新 更多