平衡 B 树有多平衡

发布于 2024-09-27 18:27:10 字数 141 浏览 4 评论 0原文

假设我有一个 B 树,其节点采用 3-4 配置(3 个元素和 4 个指针)。假设我按照规则合法地建立了我的树,我是否有可能达到一层有两个节点,一个节点有4个退出指针,另一个节点只有两个退出指针的情况?

一般来说,对于正确使用的 B 树的平衡性有什么保证

Say I have a B-Tree with nodes in a 3-4 configuration (3 elements and 4 pointers). Assuming I build up my tree legally according to the rules, is it possible for me to reach a situation where there are two nodes in a layer and one node has 4 exiting pointers and the other has only two exiting pointers?

In general, what guarantees do I have as to the balancedness of a properly used B-Tree

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

無心 2024-10-04 18:27:10

平衡(在一般平衡树数据结构中)背后的想法是任何两个子树之间的深度差异为零或一(取决于树)。换句话说,用于查找叶节点的比较次数总是相似的。

所以是的,你最终可能会遇到你所描述的情况,仅仅因为深度是相同的。每个节点中的元素数量与平衡无关(但见下文)。

这是完全合法的,即使左节点中的项目比右节点多(未显示空指针):

                 +---+---+---+
                 | 8 |   |   |
                 +---+---+---+
        ________/    |
       /             |
      |              |
+---+---+---+  +---+---+---+
| 1 | 2 | 3 |  | 9 |   |   |
+---+---+---+  +---+---+---+

但是,拥有 3-4 BTree 是非常不寻常的(有些人实际上会说这不是根本就是 BTree,而是其他一些数据结构)。

对于 BTree,每个节点中的键数通常为偶数(例如 4-5 树),这样拆分和组合就更容易。对于 4-5 树,当节点填满时决定提升哪个键很容易 - 它是五个键中的中间一个。对于 3-4 树来说,这不是一个明确的问题,因为它可能是两个之一(四个元素没有明确的中间位置)。

它还允许您遵循节点应包含在 n2n 元素之间的规则。此外(对于“正确的”BTree),叶节点都处于相同深度,而不仅仅是彼此在其中。

如果将以下值添加到空 BTree,则最终可能会出现您所描述的情况:

Add           Tree Structure
---          ----------------
 1                  1

 2                 1,2

 5                1,2,5

 6               1,2,5,6

 7                  5
                   / \
                1,2   6,7

 8                  5
                   / \
                1,2   6,7,8

 9                  5
                   / \
                1,2   6,7,8,9

The idea behind balance (in general balanced tree data structures) is that the difference in depths between any two sub-trees is zero or one (depending on the tree). In other words, the number of comparisons used to find a leaf node is always similar.

So yes, you can end up in the situation you describe, simply because the depths are the same. The number of elements in each node is not a concern to the balance (but see below).

This is perfectly legal even though there's more items in the left node than the right (null pointers are not shown):

                 +---+---+---+
                 | 8 |   |   |
                 +---+---+---+
        ________/    |
       /             |
      |              |
+---+---+---+  +---+---+---+
| 1 | 2 | 3 |  | 9 |   |   |
+---+---+---+  +---+---+---+

However, it's very unusual to have a 3-4 BTree (some would actually say that's not a BTree at all, but some other data structure).

With BTrees, you usually have an even number of keys as maximum in each node (e.g., a 4-5 tree) so that the splitting and combining is easier. With a 4-5 tree, the decision as to which key is promoted when a node fills up is easy - it's the middle one of the five. That's not such a clear-cut matter with a 3-4 tree since it could be one of two (there is no definite middle for four elements).

It also allows you to follow the rule that your nodes should contain between n and 2n elements. In addition (for "proper" BTrees), the leaf nodes are all at the same depth, not just within one of each other.

If you added the following values to an empty BTree, you could end up with the situation you describe:

Add           Tree Structure
---          ----------------
 1                  1

 2                 1,2

 5                1,2,5

 6               1,2,5,6

 7                  5
                   / \
                1,2   6,7

 8                  5
                   / \
                1,2   6,7,8

 9                  5
                   / \
                1,2   6,7,8,9
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文