Largest Item in Heap

℡╲_俬逩灬. 提交于 2019-12-11 04:48:09

问题


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) cannot 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.


回答1:


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.



来源:https://stackoverflow.com/questions/6489302/largest-item-in-heap

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!