直接贴代码,比较好懂
#_*_coding:utf_8_
'''
建立一个最小堆
'''
import heapq
import random
class MyHeap():
def __init__(self):
self.n = 0
self.list = []
def buildHeap(self):
self.list = [random.randint(1, 100) for i in range(30)]
heapq.heapify(self.list) #建立最小堆
for i in range(len(self.list)):
print heapq.heappop(self.list) , #在堆中取出一个元素
print '\n'
myheap = MyHeap()
myheap.buildHeap()
http://demon.blog.ustc.edu.cn/
http://demon.blog.ustc.edu.cn/
http://demon.blog.ustc.edu.cn/
http://demon.blog.ustc.edu.cn/