Chapter 6
Exercise 6.1-1
Available in the latest revision of the IM.
Exercise 6.1-2
Available in the latest revision of the IM.
Exercise 6.1-3
Available in the latest revision of the IM.
Exercise 6.1-4
Available in the latest revision of the IM.
Exercise 6.1-5
Available in the latest revision of the IM.
Exercise 6.1-6
Available in the latest revision of the IM.
Exercise 6.1-7
Available in the latest revision of the IM.
Exercise 6.1-8
Available in the latest revision of the IM.
Exercise 6.2-1
Instead of presenting the solution directly, I recommend a marvelous website for visualizing algorithms called VisuAlgo. Select the option to visualize a binary heap. Choose in the bottom left corner the menu item Create(A) - O(N) and enter the following content for =27,17,3,16,13,10,1,5,7,12,4,8,9,0. Press Go. You will see that nodes at indices 3 and 6 are swapped and later nodes at indices 6 and 13 are exchanged. In essence, this shows the effect of calling Max-Heapify(A,3).
Exercise 6.2-2
Available in the latest revision of the IM.
Exercise 6.2-3
The procedure Min-Heapify(A,i) differs from Max-Heapify(A,i) in the following ways:
The variable
largestshould be renamed tosmallest.At line 3, the second comparison operator should be reversed from
>to<.At line 6, the second comparison operator should be reversed from
>to<.Line 10 should be changed to call
Min-Heapify(A,smallest).
The running times of both procedures are asymptotically the same.
Exercise 6.2-4
Available in the latest revision of the IM.
Exercise 6.2-5
Available in the latest revision of the IM.
Exercise 6.2-6
Available in the latest revision of the IM.
Exercise 6.2-7
Available in the latest revision of the IM.
Exercise 6.3-1
Follow the instructions from Exercise 6.2-1 this time entering for =5,3,17,10,84,19,6,22,9 . Press Go and watch how the transformations are happening. You may also choose to monitor the process step by step.
Exercise 6.3-2
The text in the book referencing this exercise, the formulation of this exercise and the solution provided in the IM are nonsense. For any positive real , it is by definition true that . What we need to prove here is that for . We have
Exercise 6.3-3
Available in the latest revision of the IM.
Exercise 6.3-4
Available in the latest revision of the IM.
Although the proof in the IM is correct, the 4th "fact" is not true in general. It says
For nonnegative reals and , we have .
A simple counterexample is and . In the main proof is a positive integer and is a positive integer divided by two to the power of some positive integer.
Furthermore, "facts" 1 and 5 are not trivial, thus they must be formally proven.
Simpler inductive proof
We prove the statement by induction on .
The base case is true, since based on Exercise 6.1-8 an element heap has leaves.
For the inductive step, assume that and denote by the number of nodes at height . We know that these nodes can produce at most children, whose quantity we can upper bound using the inductive hypothesis. Therefore,
which entails
We have used equation (3.5) to get rid off the inner ceil expression. This concludes the proof.
Exercise 6.4-1
Available in the latest revision of the IM.
Exercise 6.4-2
Available in the latest revision of the IM.
Exercise 6.4-3
Even if the array is sorted in increasing order, after the first step, it will be turned into a max-heap. In both cases, assuming all elements of are distinct, the extraction phase dominates, since each extraction involves swapping the root with the last element and restoring the heap property. Therefore, the asymptotic running time will remain the same , irrespectively whether the input array is sorted in increasing or decreasing order (see also Exercise 6.4-5).
On the other hand, for special situations, like all numbers being equal, the best-case running time is . Observe that in this case the input is sorted in both directions. Since none of the children is greater than its parent all nodes demand extraction time (no recursive calls will happen in Max-Heapify).
Exercise 6.4-4
We build upon Exercise 6.2-7. Suppose we have a sufficiently large array of length with zeros and ones. In step 1, this array is turned into a max-heap of height , with zeros as leaves. During the extraction phase, internal nodes will become roots before any leaf. Let us just focus on internal nodes at level . Since and , the total number of data movements is at least .
Exercise 6.4-5
See Theorem 1 in section 4 in the attached PDF document (downloaded from here on September 2025).
Comments on the paper
The paper uses a slightly different nomenclature than the book. The number of elements is denoted by whilst denotes the number of levels, so the height is .
Let us derive the average internal path length for the top elements, knowing that the root is one of them. At level we must have at least large keys to "pick up" those excluded at level . We have
Therefore, the average internal path length is greater than , as stated in the paper. The summation (lines 1 -> 2) is explained here.
Exercise 6.5-1
Follow the instructions from Exercise 6.2-1 this time entering for =15,13,9,5,12,8,7,4,0,6,2,1. Select the ExtractMax() menu item and press 1x (Once).
Exercise 6.5-2
Available in the latest revision of the IM.
Exercise 6.5-3
See also Exercise 6.2-3.
Min-Heap-Minimum(A)
if A.heap-size < 1
error "heap underflow"
return A[1]
Min-Heap-Extract-Min(A)
min = Min-Heap-Minimum(A)
A[1] = A[A.heap-size]
A.heap-size = A.heap-size - 1
Min-Heapify(A, 1)
return min
Min-Heap-Decrease-Key(A, x, k)
if k > x.key
error "new key is greater than current key"
x.key = k
find the index i in array A where object x occurs
while i > 1 and A[Parent(i)].key > A[i].key
exchange A[i] with A[Parent(i)], updating the information that maps
priority queue objects to array indices
i = Parent(i)
Min-Heap-Insert(A, x, n)
if A.heap-size == n
error "heap overflow"
A.heap-size = A.heap-size + 1
k = x.key
A[A.heap-size] = x
map x to index heap-size in the array
Min-Heap-Decrease-Key(A, x, k)Exercise 6.5-4
The solution in the IM is wrong. Here is the correct version.
Max-Heap-Decrease-Key(A, x, k)
if k > x.key
error "new key is greater than current key"
x.key = k
find the index i in array A where object x occurs
Max-Heapify(A, i)The running time is plus the overhead for mapping priority queue objects to array indices.
Exercise 6.5-5
The solution in the IM is wrong. Line 5 is totally redundant. This error stems from needlessly overcomplicating the book with that satellite data story. In the 3rd edition, where only keys were stored, setting the newly allocated memory cell to was mandatory to avoid the "new key is smaller than current key” error.
Exercise 6.5-6
Increasing the key of a node in a max-heap cannot violate the max-heap property between and its children, but can lead to the situation where the key of the parent of is smaller than the key of . Thus, we need to traverse the path upward in a tree and float the node up, so it lands in a correct position. This task is realized by the while loop in Max-Heap-Increase-Key. Replacing the loop by a call to Max-Heapify will not work, because it can only traverse a path downward in a tree.
Exercise 6.5-7
Available in the latest revision of the IM.
Exercise 6.5-8
Available in the latest revision of the IM.
There is a bug in the IM's pseudo-code. Instead of A[i].key inside the while condition there should be x.key.
Exercise 6.5-9
Available in the latest revision of the IM.
This is a questionable objective, since both stack and queue should have a guaranteed performance for push/pop and enqueue/dequeue operations, respectively. Imagine using a stack where push/pop, on average, demands time.
Exercise 6.5-10
Available in the latest revision of the IM.
Exercise 6.5-11
Available in the latest revision of the IM.
A concrete example is available here.
Problem 6-1
Available in the latest revision of the IM.
Problem 6-2
Available in the latest revision of the IM.
Problem 6-3
Available in the latest revision of the IM.
There is a bug in the IM's FLOAT procedure in part (d). The following two changes fix the issue:
if above > leftshould be altered toif above > Y[i, j]the unconditional
elseshould be altered toelse if left > Y[i, j]
The point is that a newly inserted element may float upward and to the left until it hits a smaller or equal element.
A concrete example for part (f) is available here.
Last updated