Heap 重设数组的最大比较次数是多少?

Heap 重设数组的最大比较次数是多少?,heap,Heap,是否有一个通用公式来计算与heapify n元素的最大比较次数 如果不是,那么13是对8个元素的数组进行heapify的最大比较次数吗 我的理由是: at h = 0, 1 node, 0 comparisons, 1* 0 = 0 comparisons at h = 1, 2 nodes, 1 comparison each, 2*1 = 2 comparisons at h = 2, 4 nodes, 2 comparisons each, 4*2 = 8 comparisons

是否有一个通用公式来计算与heapify n元素的最大比较次数

如果不是,那么13是对8个元素的数组进行heapify的最大比较次数吗

我的理由是:

at h = 0, 1 node, 0 comparisons, 1* 0 = 0 comparisons 

at h = 1, 2 nodes, 1 comparison each, 2*1 = 2 comparisons

at h = 2, 4 nodes, 2 comparisons each, 4*2 = 8 comparisons

at h = 3, 1 node, 3 comparisons each, 1*3 = 3 comparisons

Total = 0 + 2 + 8 + 3 =13

公认的理论是构建堆最多需要(2N-2)个比较。因此,所需的最大比较次数应为14次。通过检查由8个元素组成的堆,我们可以很容易地确认这一点:

             7
           /   \
          3     1
         / \   / \
        5   4 8   2
       /
      6
在这里,4个叶节点永远不会向下移动。节点5和1可以向下移动1级。3可以向下移动两层。和7可以向下移动3级。因此,关卡移动的最大数量为:

(0*4)+(1*2)+(2*1)+(3*1) = 7
每个关卡移动都需要2次比较,因此最大比较次数为14次。

请参阅。