Python建最小堆
直接贴代码,比较好懂[python]#_*_coding:utf_8_'''''建立一个最小堆'''import heapqimport randomclass MyHeap():def __init__(self):self.n = 0self.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()
补充:Web开发 , Python ,