def main(): # Task number 1 print("Task #1") r1 = build_tree_lst() print("\t", r1) print() # Task number 2 print("Task #2") r2 = build_tree_oop() print() # Task number 3 print("Task #3") print("\tThe height of the tree of Task #2 is:", r2.height(0)) print() # Task number 4 print("Task #4") lst = [5, 2, 44, 15, 10, 9, 56, 77, 1, 14, 34, 95, 115, 6, 260] binary_heap = BinaryHeap() binary_heap.heapify(lst) print("\tThe list: ", lst) print("\tMax Binary Heap: ", binary_heap) print() # Task number 5 print("Task #5") binary_heap_max = BinaryHeapMax() binary_heap_max.heapify(lst) print("\tThe list: ", lst) print("\tMax Binary Heap of 8 elements: ", binary_heap_max)
def test_swap_list(): """Test the list after swap.""" from bin_heap import BinaryHeap bin_hp = BinaryHeap([3, 1.5]) assert bin_hp._list == [1.5, 3] bin_hp._swap(0, 1) assert bin_hp._list == [3, 1.5]
def test_swap_parent_child(): """Test the parent and child values after swap.""" from bin_heap import BinaryHeap bin_hp = BinaryHeap([3, 1.5]) assert bin_hp._list[0] == 1.5 and bin_hp._list[1] == 3 bin_hp._swap(0, 1) assert bin_hp._list[1] == 1.5 and bin_hp._list[0] == 3
def test_valid_heap_non_basic(): """Test heap from more complex heap list.""" from bin_heap import BinaryHeap bin_hp = BinaryHeap([1, 1.5, 3, 4, 2, 2.5, 6, 2, 8, 5.5, 3.5]) assert bin_hp._list == [1, 1.5, 2.5, 2, 2, 3, 6, 4, 8, 5.5, 3.5]
def test_is_empty(): bin_list = BinaryHeap() assert bin_list.heaplist == [0]
def test_random_input(): h = BinaryHeap() for i in random.sample(range(100), 100): h.push(i) assert all_items(h) == range(0, 100)
def test_load_up_the_list(): heap = BinaryHeap() for i in range(10000, 0, -1): heap.push(i) assert all_items(heap) == range(1, 10001)
def test_reverse_range(): heap = BinaryHeap() for i in range(5, 0, -1): heap.push(i) assert all_items(heap) == range(1, 6)
def test_one_push_pop(): bin_list = BinaryHeap() bin_list.push(10) assert bin_list.pop() == 10
def test_push_heap(): """Test heap function orders value correctly after pop.""" from bin_heap import BinaryHeap bin_hp = BinaryHeap([1.5, 1, 3, 4, 2, 2.5, 6, 2, 8, 5.5]) bin_hp.push(3.5) assert bin_hp._list == [1, 1.5, 2.5, 2, 2, 3, 6, 4, 8, 5.5, 3.5]
def empty_bin_heap(): """Return empty binary heap.""" from bin_heap import BinaryHeap return BinaryHeap()
def test_max_option(): """Test BinaryHeap with max heap option.""" from bin_heap import BinaryHeap maxheap = BinaryHeap([1, 2, 3], 'max') assert maxheap._list == [3, 1, 2]
def test_init_with_invalid_iterable(): """Test Binary Heap init with invalid iterable.""" from bin_heap import BinaryHeap with pytest.raises(TypeError, message="Optional binary heap argument must be iterable."): BinaryHeap(42)
def test_init_no_minmax(): """Test for when optional paramater is not 'min' or 'max'.""" from bin_heap import BinaryHeap with pytest.raises(TypeError, message="min/max optional parameter must be 'min' or 'max'"): BinaryHeap(1.5, "apple")
def test_organize_right(): """Test organize right after pop.""" from bin_heap import BinaryHeap bin_hp = BinaryHeap([12, 13, 9, 15, 16, 3, 5]) bin_hp.pop() bin_hp._list == [5, 13, 3, 15, 16, 9]
def non_empty_bin_heap(): """Return empty binary heap [1, 2, 4, 5, 3].""" from bin_heap import BinaryHeap return BinaryHeap([5, 1, 4, 2, 3])
def test_max_pop(): """Test pop on max heap.""" from bin_heap import BinaryHeap maxheap = BinaryHeap([1, 2, 3], 'max') assert maxheap.pop() == 3