def test_insert_extract(): print("#test_insert_extract") for round in range(3): order = shuffled_list(50, round) A = [] for x in range(20): heap.heapInsert(A, order.pop()) heap.heapInsert(A, order.pop()) has_heap_property = is_min_heap(A) min_elem = min(A) extracted = heap.heapExtractMin(A) print("Correctly extracted min: {}, maintained heap property: {}".format( min_elem == extracted, has_heap_property and is_min_heap(A)))
def report_counts_on_basic_ops(A, expected_build_count, expected_extract_count, expected_insert_count): print("Counts for list of len: {}".format(len(A))) heap.reset_counts() heap.buildHeap(A) bh = heap.current_counts()['heapify_call_count'] print("buildHeap heapify calls within 20% of expected number of calls: {}".format(within_20_percent(expected_build_count, bh))) heap.reset_counts() m = heap.heapExtractMin(A) ex = heap.current_counts()['heapify_call_count'] print("heapExtractMin heapify calls within 20% of expected number of calls: {}".format(within_20_percent(expected_extract_count, ex))) heap.reset_counts() heap.heapInsert(A, m) ins = heap.current_counts()['swap_count'] print("heapInsert swap calls within 20% of expected number of calls: {}".format(within_20_percent(expected_insert_count, ins)))
import heap if __name__ == "__main__": A = heap.shuffled_list(20, 0) print("Complete Tree size 20:") heap.printCompleteTree(A) heap.buildHeap(A) print("After Build Heap") heap.printCompleteTree(A) print("After heapExtractMin") heap.heapExtractMin(A) heap.printCompleteTree(A) print("After heapInsert") heap.heapInsert(A, 9) heap.printCompleteTree(A)