def insert(heap_low, heap_high, num): hl = heap_low.copy() hh = heap_high.copy() if len(hl) == 0 or num < hl[0]: my_max_heap.heappush(hl, num) else: my_min_heap.heappush(hh, num) return hl, hh
def rebalance_heaps(heap_low, heap_high): rebalanced_heap_low = heap_low rebalanced_heap_high = heap_high if len(rebalanced_heap_low) - len(rebalanced_heap_high) > 1: element = my_max_heap.heappop(rebalanced_heap_low) my_min_heap.heappush(rebalanced_heap_high, element) if len(rebalanced_heap_high) - len(rebalanced_heap_low) > 1: element = my_min_heap.heappop(rebalanced_heap_high) my_max_heap.heappush(rebalanced_heap_low, element) return rebalanced_heap_low, rebalanced_heap_high