B 树中键的最大和最小数量

发布于 2024-10-30 05:08:19 字数 358 浏览 7 评论 0原文

阶数为 128、高度为 3 的 B 树中可以存储的键的最大和最小数量是多少?

为了最大化,这就是我所做的: 您有一个根节点。根节点可以拥有的最大子节点为 m(阶),即 128。这 128 个子节点中的每一个都有 128 个子节点,因此总共有 1+128+16384=16512 个节点。根据 Wikipedia,n 个节点的 B 树可以存储 n-1 个键,因此最多可以有 16511 个键。

对于分钟: 您有一个根节点,它可以拥有的最小子节点数为 2,这 2 个子节点可以拥有的最小子节点数为 m/2,其中 m 是阶数,因此每个子节点有 64 个。这样我们总共有 1+2+64+64=131 个子节点和 131-1=130 个键。

我在这里所做的正确吗?

What is the maximum and minimum number of keys that can be stored in a B-tree of order 128 and height 3?

For the maximum, here's what I did:
You have a single root node. The maximum children a root node can have is m (order), so that's 128. And each of those 128 children have 128 children, so that gives us a total of 1+128+16384=16512 total nodes. According to Wikipedia, a B-tree of n nodes can store n-1 keys, so that leaves us with a maximum of 16511 keys.

For min:
You have a single root node, and the minimum number of children this can have is 2, and the minimum number of children these 2 children can have are m/2, where m is the order, so 64 children each. That leaves us with 1+2+64+64=131 total children and 131-1=130 keys.

Is what I have done here correct?

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

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

发布评论

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

评论(4

小苏打饼 2024-11-06 05:08:19

根据维基百科,如果一个节点有 n 个子节点,则它最多可以有 n-1 个键。
所以 ,

                           root [127keys]
                 128 childnodes [each having 127 keys]
             128*128 childnodes [each having 127 keys]

127+128*127+128*128*127 = total no of maximum allowed keys

according to wikipedia , a node can have at most n-1 keys if it has n child nodes.
therefore ,

                           root [127keys]
                 128 childnodes [each having 127 keys]
             128*128 childnodes [each having 127 keys]

127+128*127+128*128*127 = total no of maximum allowed keys
请远离我 2024-11-06 05:08:19

节点可以包含的键数量有下限和上限。这些界限用固定整数 t >= 2 表示,称为 B 树的最小度数。

  • 除根之外的每个节点都必须至少有 t - 1 个键。
    除根之外的每个内部节点至少有 t 个子节点。
    如果树非空,则根必须至少有一个键。

  • 每个节点最多可以包含 2t - 1 个键。
    因此,一个内部节点最多可以有 2t 个子节点。
    如果一个节点正好包含 2t - 1 个键,我们就说它是满的。

高度 3 的最大键数 = (2t-1) + (2t-1) * 2t + (2t-1) * 2t * 2t
高度 3 的最小键数 = (t-1) + (t-1)*t + (t-1) * t * t

There are lower and upper bounds on the number of keys a node can contain. These bounds are expressed in terms of a fixed integer t >= 2 called the minimum degree of the B-tree.

  • Every node other than the root must have at least t - 1 keys.
    Every internal node other than the root has at least t children.
    If the tree is nonempty, the root must have at least one key.

  • Every node can contain at most 2t - 1 keys.
    Therefore, an internal node can have at most 2t children.
    We say that a node is full if it contains exactly 2t - 1 keys.

Max no of keys for height 3 = (2t-1) + (2t-1) * 2t + (2t-1) * 2t * 2t
Min no of keys for height 3 = (t-1) + (t-1)*t + (t-1) * t * t

护你周全 2024-11-06 05:08:19

这实际上取决于您如何定义顺序。根据 Knuth 的说法,b 树的阶数是子节点的最大数量,这意味着最大答案是 129。如果阶数的定义是非根节点的最小键数,则答案为最大值未知。

使用 this 定义,您的最小值计算是正确的,但最大值是不是,因为每个节点(包括叶子节点)都包含 m-1 个键。这也与Cormen中B-Tree的定义一致。如果n是16512,并且每个n存储127个密钥,那么答案肯定不是16511。

It really depends on how you define order. According to Knuth, the order of a b-tree is the maximum number of children, which would mean that the max answer is 129. If the definition of order is the minimum number of keys of a non-root node, then the answer to the max is unknown.

Using this definition, your calculation of minimum is correct, but your maximum is not, because every node, including the leaves, contains m-1 keys. This is also consistent with the definition of B-Tree in Cormen. if n is 16512, and each n stores 127 keys, the answer is definitely not 16511.

惯饮孤独 2024-11-06 05:08:19

下面的 C# 代码用于计算给定级别数和节点可以拥有的最大子节点数的任何 B 树的最大键数。

        /// <summary>
        /// Given number of levels in the tree, computes the maximum number of keys the tree can hold. 
        /// </summary>
        /// <param name="levelCount">Is the number of levels in the tree. </param>
        /// <param name="MaxBranchingDegree ">Is the maximum number of children a node in the tree can have. </param>
        /// <returns>Maximum number of keys a tree with <paramref name="levelCount"> levels can hold. </returns>
        public int ComputeMaxKeyCount(int levelCount, int MaxBranchingDegree )
        {
           int maxKeys = 0;
           for (int l = 0; l < levelCount; l++)
           {
              maxKeys += (MaxBranchingDegree - 1) * (int)Math.Pow(MaxBranchingDegree, l);
           }
           return maxKeys;
        }

Here is the C# code to compute maximum number of keys for any B-Tree given number of levels and maximum number of children a node can have.

        /// <summary>
        /// Given number of levels in the tree, computes the maximum number of keys the tree can hold. 
        /// </summary>
        /// <param name="levelCount">Is the number of levels in the tree. </param>
        /// <param name="MaxBranchingDegree ">Is the maximum number of children a node in the tree can have. </param>
        /// <returns>Maximum number of keys a tree with <paramref name="levelCount"> levels can hold. </returns>
        public int ComputeMaxKeyCount(int levelCount, int MaxBranchingDegree )
        {
           int maxKeys = 0;
           for (int l = 0; l < levelCount; l++)
           {
              maxKeys += (MaxBranchingDegree - 1) * (int)Math.Pow(MaxBranchingDegree, l);
           }
           return maxKeys;
        }
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文