# 6.7

a.  In each pass of the loop in the method percolateDown we make two comparisons. Thus the total number of comparisons between elements in the method buildHeap is equal to 2 times the sum of heights of the nodes in a binary heap. Thus it suffices to show that the sum of heights of the nodes in a binary heap is at most N-1, where N is the number of nodes.

Let h be the height of a binary heap. Denote by Li the ith level, i.e. the set of nodes which distance from the root is i.

By Theorem 6.1 from the textbook, if our heap is a perfect binary tree of height $h-1\geq 0$ (denote this heap by H0) then it contains N0=2h-1 nodes and the sum of heights of the nodes is $2^h-1-h\leq N_0-1$ so we are done in this case.

Let now H be an arbitrary heap of height h. It can be obtained from H0 by inserting from left to right a certain number m of new nodes to a new bottom level Lh. Clearly, $0<m\leq 2^h$ and H has N=N0+m nodes. By inserting m elements to the level Lh we increased by 1 heights of some nodes on the levels $L_0,L_1,\cdots ,L_{h-1}$. Let us denote by ai the number of nodes on level Li which height was increased. Observe that $a_{h-1}=\lceil m/2\rceil$, a0=1 and, in general, $a_{i-1}=\lceil \frac{1}{2}a_i\rceil$, for $i=1,2,\cdots h-1$.

 It follows form the last equality (consider the cases of ai even and odd) that

\begin{displaymath}a_{i-1}\leq \frac{1}{2}a_i+\frac{1}{2}, \quad {\rm for}\ i=1,\cdots h-1.\end{displaymath}

Summing up these inequalities side by side we get

\begin{displaymath}\sum_{i=1}^{h-1}a_{i-1}\leq \frac{1}{2}\sum_{i=1}^{h-1}a_i+\frac{1}{2}(h-1).\end{displaymath}

Hence

\begin{displaymath}\sum_{i=0}^{h-1}a_i\leq \frac{1}{2}\sum_{i=0}^{h-1}a_i+\frac{1}{2}(h-1)+a_{h-1}-\frac{1}{2}a_0\end{displaymath}

so

\begin{displaymath}\frac{1}{2}\sum_{i=0}^{h-1}a_i\leq \frac{1}{2}(h-1)+a_{h-1}-\frac{1}{2}a_0=\frac{1}{2}(h-1)+\lceil m/2\rceil -\frac{1}{2}. \end{displaymath}

Consequently,

\begin{displaymath}\sum_{i=0}^{h-1}a_i\leq h-1 -1 + 2\lceil m/2\rceil .\end{displaymath}

Thus the sum of heights of nodes in our heap H is

\begin{displaymath}2^h-1-h+\sum_{i=0}^{h-1}a_i\leq 2^h-1-h+h-2+2\lceil m/2\rceil = N_0+m-2+2\lceil m/2\rceil -m \leq N-2+1=N-1\end{displaymath}

because $2\lceil m/2\rceil -m\leq 1$ (consider the cases of m even and odd).

b.  We proceed as in a play-off tournament. Let us partition the set of 8 elements into 4 pairs. We compare the elements in each pair (4 comparisons). Let W1 (W for winner) by the smaller element in the first pair and L1 (L for loser) the larger element in the first pair, i.e. W1 < L1. In general Wi, i=1,2,3,4, is the smaller element in the ith pair and Li is the larger so W2 < L2, W3 < L3, W4 < L4. Now, we compare W1 and W2 and suppose that W1 < W2. Next we compare W3 and W4 and, say, W3 < W4. Next we compare W1 with W3 (the winners), say W1 < W3 and finally we compare L1 with W2 (the losers in the first two rounds of the tournament against W1 - this comparison is not like in a play-off tournament). If W2 < L1 then the array [W1,W3,W2,W4,L3,L1,L2,L4] satisfies the heap-order property (because W1 < W3, W1 < W2, W3 < W4, W3 < L3, W2 < L1, W2 < L2 and W4 < L4). If L1 < W2 then [W1,W3,L1,W4,L3,W2,L2,L4] satisfies the heap-order property (because W1 < W3, W1 < L1, W3 < W4, W3 < L3, L1 < W2, L1 < W2 < L2 and W4 < L4).The total number of comparisons is 8.