尽管Python中没有独立的堆类型,但是包含了一些对操作函数的模块,这个模块叫heapq,主要的操作包含如下几个:
heappush(heap,x):x元素插入堆
heappop(heap):...弹出对中最小元素
heapify(heap):将heap属性强制应用到任意一个列表
hrapreplace(heap,x):将heap中最小元素弹出,同时x元素入堆
hlargest...=[]
>>> for i in x:
heappush(heap,i)
>>> heap
[1, 3, 2, 6, 4, 5]
>>>
注:shuffle函数将列表顺序打乱
堆元素的追加:...>>> heap
[1, 3, 2, 6, 4, 5]
>>> heappush(heap,0)
>>> heap
[0, 3, 1, 6, 4, 5, 2]
>>>
删除堆中元素:
>>> heap...[0, 3, 1, 6, 4, 5, 2]
>>> heappop(heap)
0
>>> heap
[1, 3, 2, 6, 4, 5]
>>> heappop(heap)
1
>>> heap
[