How is the order of items managed by Python's heapq library determined?

…衆ロ難τιáo~ 提交于 2020-01-23 19:46:48

问题


I was under the impression that the first value was what determined a values position in the heap, however that doesn't seem to be the case.

from __future__ import print_function
import heapq

q = []
heapq.heappush(q, (10, 11))
heapq.heappush(q, (11, 12))
heapq.heappush(q, (9, 10))
print(q)

This gives me an output of

[(9, 10), (11, 12), (10, 11)]

However I was expecting an output like

[(9, 10), (10, 11), (11, 12)]

回答1:


The condition on heapq is not a "sort guarantee" over the provided list. Instead, it guarantees q[k] <= q[2*k+1] and q[k] <= q[2*k+2] (using q as in your example).

This is due that it is managed internally as a binary tree.

If you simply expect to use the sorted list, you can use the heappop as shown here. In your specific example you could:

sorted_q = [heappop(q) for i in range(len(q))

and the result, as you expected, will be:

>>> print sorted_q
[(9, 10), (10, 11), (11, 12)]

The theory is explained here in the docs. Relevant is the following line:

The interesting property of a heap is that a[0] is always its smallest element.

Which is a direct result of the condition q[k] <= q[2*k+1] and q[k] <= q[2*k+2], which is a condition of the heap.

However, there are no further guarantees about the order on the rest of the array. And, in fact, both following trees are valid heaps:

    0
 1     2
2 5   3 4

and

    0
 2     1
5 3   4 2

Which are stored, respectively, as

[0, 1, 2, 2, 5, 3, 4]

and

[0, 2, 1, 5, 3, 4, 2]


来源:https://stackoverflow.com/questions/36479149/how-is-the-order-of-items-managed-by-pythons-heapq-library-determined

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