6.2-1 第一遍交换10和3 第二遍交换10和9
6.2-2
6.2-2
MIN-HEAPIFY(A, i)
1 l <- LEFT(i)
2 r <- RIGHT(i)
3 if l <= heap-size[A] and A[l] < A[i]
4 then smallest <- l
5 else smallest <- i
6 if r <= heap-size[A] and A[r] < [smallest]
7 then smallest <- r
8 if smallest != i
9 then exchange A[i] <-> A[smallest]
10
运行时间复杂度均为O(h)
6.2-3 程序 自动退出
6.2-4 所述情况i为叶子结点 左右孩子为空 自动退出
6.2-5
MAX-HEAPIFY(A,i)
while (i<=A.heap-size/2)//由于6.2-4知道,i>A.heap-size/2以后,最大堆不会有任何改变。
{ //这样也可以减少循环次数。
l=LEFT(i)
r=RIGHT(i)
if l<=A.heap-size and A[l]>A[i]
largest=l
else largest=i
if r<=A.heap-size and A[r]>A[largest]
largest=r
if largest!=i
exchange A[i]<->A[largest]
i=largest//把largest赋值给i,然后继续进行MAX-HEAPIFY(A,i)循环。
}
6.2-6 可以通过分析得出结果最坏情况从根节点到叶子节点一直都会调用MAX-HEAPIFY ,而堆的高度为h,因此最坏情况为Ω(lgn)。