An array of n real numbers, H[1,…,n], is called a heap if H[⌊i/2⌋]≥H[i] for 1<i≤n, where ⌊i/2⌋ is the quotient of integer i divided by 2 (e.g., ⌊4/2⌋=⌊5/2⌋=2). A heap can be treated as a binary tree such that H[1] is the root node and H[⌊i/2⌋] is the parent node of H[i]. Answer the following questions.
(1) Explain whether array [6,3,4,1,0,3,4,2] is a heap or not.
(2) Show that if H[1,…,n] is a heap, H[1,…,j] is also a heap for any j(1≤j<n).
(3) Suppose that H[1,…,n] is a heap but H[1,…,n+1] is not. Describe a procedure that exchanges elements in H[1,…,n+1] to make it a heap in O(logn) worst-case time.
(4) Suppose that H[1,…,n] is a heap. Prove that H[1] is maximum among all elements in H[1,…,n]. Suppose that after replacing H[1] with H[n], H[1,…,n−1] is not a heap. Describe a procedure that exchanges elements in H[1,…,n−1] to make it a heap in O(logn) worst-case time.
(5) Describe an algorithm that exchanges elements in H[1,…,n] to make it a heap in O(n) worst-case time.
(6) Using the procedures defined above, describe an algorithm that sorts array H[1,…,n] in O(nlogn) worst-case time.
一个包含 n 个实数的数组 H[1,…,n],如果满足 H[⌊i/2⌋]≥H[i] 对于 1<i≤n,其中 ⌊i/2⌋ 是整数 i 除以 2 的商(例如,⌊4/2⌋=⌊5/2⌋=2),则称其为堆。堆可以被看作是一个二叉树,其中 H[1] 是根节点,H[⌊i/2⌋] 是 H[i] 的父节点。回答以下问题。
Statement: If H[1,…,n] is a heap, then H[1,…,j] is also a heap for any j(1≤j<n).
Proof:
Let H[1,…,n] be a heap, meaning H[⌊i/2⌋]≥H[i] for all 1<i≤n. Consider any j<n. Since the heap property holds for all elements in H[1,…,n], it will also hold for all elements in H[1,…,j] because the parent-child relationships in the heap are preserved up to index j. Therefore, H[1,…,j] is also a heap.
Suppose H[1,…,n] is a heap, but after inserting an element at position n+1, the array H[1,…,n+1] is not a heap. The violation of the heap property could only occur if the newly inserted element at position n+1 is greater than its parent. To fix this, we perform the heapify-up operation.
Heapify-up Procedure:
Let i=n+1.
While i>1 and H[i]>H[⌊i/2⌋]:
Swap H[i] and H[⌊i/2⌋].
Set i=⌊i/2⌋.
Time Complexity: The worst-case time complexity of this procedure is O(logn), as we may need to traverse up the height of the heap, which is logarithmic in terms of the number of elements.
Proof that H[1] is the maximum element in H[1,…,n]:
By the definition of a max-heap, the root node (i.e., H[1]) must be greater than or equal to all its children. By induction, it can be proven that H[1] is greater than or equal to all elements in the heap. Hence, H[1] is the maximum element.
Heapify-down Procedure:
After replacing H[1] with H[n], if H[1,…,n−1] is not a heap, we need to restore the heap property by performing the heapify-down operation.
Let i=1.
While 2i≤n−1 (i.e., while i has at least one child):
Let j=2i (left child).
If j<n−1 and H[j+1]>H[j], set j=j+1 (right child is larger).
If H[i]≥H[j], break the loop.
Swap H[i] and H[j].
Set i=j.
Time Complexity: The worst-case time complexity of this procedure is O(logn), as we may need to traverse down the height of the heap.