Largest Item in Heap
The largest item in a heap must appear in position 1, and the second largest must be in position 2 or position 3. Give the list of positions in a heap of size 31 where the kth largest (i) can appear, and (ii) c开发者_Python百科annot appear, for k=2, 3, 4 (assuming the values to be distinct).
I am trying to study this for my midterm but its 3AM and I am stuck on this problem in the book as it does not provide a solution. Any help would be appreciated.
If you look at the Heap Implementation example on Wikipedia, you will see that the third largest can be in position 2 or 3, whichever one the second largest is not, as well as positions 4+5 or 6+7, depending on where the second largest is. Thus, it can be in 2-7.
The fourth largest must then be in any position the third largest can be, plus any position which is a direct child of the third largest. This means it can be anywhere from 2-15.
The following picture is 0-based, as it is an array implementation, so add one for the position.
精彩评论