def heapq_heappush(): # create a heap heap = [] print('random: ', data) print() for n in data: print('add {:>3}'.format(n)) heapq.heappush(heap, n) show_tree(heap)
def heapq_heappop(): print('random :', data) heapq.heapify(data) print('heapified:') show_tree(data) print() for _ in range(2): smallest = heapq.heappop(data) print('pop {:>3}:'.format(smallest)) show_tree(data)
def heapq_heapreplace(): data = [19, 9, 4, 10, 11] heapq.heapify(data) print('start:') show_tree(data) for n in [0, 13]: smallest = heapq.heapreplace(data, n) print('replace {:>2} with {:>2}:'.format(smallest, n)) show_tree(data)
def heapq_heapify(): print('random :', data) # heapq.heapify() is in-place modification heapq.heapify(data) print('heapified:') show_tree(data)