读书人

Python筑最小堆

发布时间: 2013-01-21 10:15:39 作者: rapoo

Python建最小堆
直接贴代码,比较好懂

#_*_coding:utf_8_'''建立一个最小堆'''import heapqimport randomclass 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()

读书人网 >perl python

热点推荐