堆数据结构
尝试考虑最大堆中第 n 个最大键的位置的下限。假设堆按数组排列。我认为上限是 min(2^n-2, 数组大小 -1),但它的下限总是 0 吗?
Trying to think of a lower bound to the position of say, the nth largest key in a max-heap. Assuming the heap's laid out in array. The upper bound's min(2^n-2, array size -1) i think, but is it always lower bounded by 0?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
对问题的初步调查揭示了 n 与下限和上限之间的以下关系(假设堆中有 14 个元素)
要确定可能大于堆数组特定位置中的元素的元素数,请计算以该位置为根的子树的大小。然后,这两个数字通过公式编辑相关
:
请注意,计算是向后执行的。给定数组/堆中的位置,如果堆已排序,则可以确定该值将位于哪个位置。给定节点,堆可以分为三个分区:
如果我们查看一个包含 14 个元素的示例堆,并想要确定第 6 个位置中可能值的范围,则组如下: 第
因此下限为 3(第 1 组中的元素数量 + 1),而上限为 11(第一组中的元素数量 + 第三组中的元素数量 + 1)。
Initial investigation of the problem reveals the following relation between n and the lower and upper bounds (assumption there are 14 elements in the heap)
To determine the number of elements that are possible larger than the element in a specific location of the heap array, calculate the size of the subtree rooted at that location. These two numbers are then related by the formula
EDIT:
Note that the calculation is performed backwards. Given a position in the array / heap, it is possible to determine in which position the value will be if the heap were sorted. Given the node the heap can be divided into three partitions:
If we look at an example heap with 14 elements and want to determine the range of possible values in the 6th location, the groups are as follows:
The lower bound is therefore 3 (# of elements in group one + 1) while the upper bound is 11 (# of elements in group one + # of elements in group three + 1).