def __init__(self): self.queue = BinHeap()
def enqueue(self, item): BinHeap.insert(self.queue, item)
from pythonds.trees.binheap import BinHeap bh = BinHeap() bh.insert(5) bh.insert(7) bh.insert(3) bh.insert(11) print(bh.delMin()) print(bh.delMin()) print(bh.delMin()) print(bh.delMin())
def dequeue(self): return BinHeap.delMin(self.queue)
def postorder(tree): if tree: postorder(tree.get_left_child()) postorder(tree.get_right_child()) print(tree.get_root_val()) def midorder(tree): if tree: midorder(tree.get_left_child()) print(tree.get_root_val()) midorder(tree.get_right_child()) if __name__ == '__main__': bh = BinHeap() arr = [9, 5, 6, 2, 3] bh.buildHelp(arr) i = len(arr) - 1 while i >= 0: arr_min = bh.delmin() arr[i] = arr_min print(arr) ''' r = BinaryTree('a') print(r.get_root_val()) print(r.get_left_child()) r.insert_left('b') print(r.get_left_child()) print(r.get_left_child().get_root_val()) r.insert_right('c')
## Using PriorityQueue ## #pq = PriorityQueue({1: "a", 2: "b", 3: "c"}) pq = PriorityQueue() pq.put((2, 'code')) pq.put((1, 'sleep')) pq.put((3, 'eat')) while not pq.empty(): print(pq.get()) ## Using Binary Heap to implement Priority queue ## p1 = [(2, "code"), (1, "eat"), (3, "sleep")] heapq.heapify(p1) p2 = [] heapq.heappush(p2, (1, "code")) heapq.heappush(p2, (2, "eat")) print("p1", p1) while p1: print(heapq.heappop(p1)) print("p2", p2) while p2: print(heapq.heappop(p2)) ## Using pythonds module ## p3 = BinHeap() p3.insert(1) p3.insert(2) p3.insert(4) print("p3", p3) print(p3.delMin())
from pythonds.trees.binheap import BinHeap bh = BinHeap() bh.insert(5) bh.insert(55) bh.insert(10) bh.insert(2) bh.insert(8) print(bh.delMin()) print(bh.delMin()) print(bh.delMin()) print(bh.delMin()) print(bh.delMin())
""" 交换节点 """ #首先判断i*2+1是否在超出列表长度 if i * 2 + 1 > self.currentSize: return i * 2 else: #若没有超出,则比较左右孩子的数据项的大小 if self.heapList[i * 2] < self.heapList[i * 2 + 1]: return i * 2 else: return i * 2 + 1 def delMin(self): """ 删除最小的数据项 """ #获取最小项,即根节点 reval = self.heapList[1] self.heapList[1] = self.heapList[self.currentSize] self.currentSize -= 1 # self.heapList.pop() self.percDown(1) return reval if __name__ == '__main__': alist = BinHeap() alist.BuildHeap([5, 9, 11, 14, 18, 19, 21, 33, 17, 27]) print(alist.delMin())
# http://interactivepython.org/runestone/static/pythonds/Trees/BinaryHeapOperations.html # BinaryHeap() creates a new, empty, binary heap. # insert(k) adds a new item to the heap. # findMin() returns the item with the minimum key value, leaving item in the heap. # delMin() returns the item with the minimum key value, removing the item from the heap. # isEmpty() returns true if the heap is empty, false otherwise. # size() returns the number of items in the heap. # buildHeap(list) builds a new heap from a list of keys. print("\nBinary Heap Operations") from pythonds.trees.binheap import BinHeap # ---------------------------------- print("Sample 1") bh = BinHeap() bh.insert(5) bh.insert(7) bh.insert(3) bh.insert(11) print(bh.delMin()) print(bh.delMin()) print(bh.delMin()) print(bh.delMin()) # ---------------------------------- class BinHeap: def __init__(self): self.heapList = [0]