def test_long_mergeable_heap_invalid_second_heap(): h1 = create_addressable_pairing_heap(key_type=int) h2 = create_addressable_pairing_heap(key_type=int) h1.insert(1, 1) h1.insert(2, 1) h1.insert(3, 1) h1.insert(4, 1) h1.insert(5, 1) h2.insert(1, 1) handle1 = h2.insert(2, 2) h2.insert(3, 3) h2.insert(4, 4) h2.insert(5, 5) h1.meld(h2) assert len(h1) == 10 assert len(h2) == 0 assert handle1.key == 2 assert handle1.value == 2 # cannot insert after a meld with pytest.raises(ValueError): h2.insert(6, 6)
def test_double_mergeable_heap(): h1 = create_addressable_pairing_heap(key_type=float) h2 = create_addressable_pairing_heap(key_type=float) for i in range(100): h1.insert(i, i + 1) assert len(h1) == 100 for i in range(100, 200): h2.insert(i, i + 1) assert len(h2) == 100 h1.meld(h2) assert len(h1) == 200 assert len(h2) == 0 expected = 0 while len(h1) > 0: cur = h1.delete_min() assert cur.key == expected assert cur.value == expected + 1 expected += 1
def test_sort_with_heap(): rng = Random(17) numbers = [] for i in range(10000): numbers.append(rng.randint(0,1000)) heap = create_addressable_pairing_heap(key_type=int) for n in numbers: heap.insert(n, 0) sorted_numbers = [] while len(heap) != 0: sorted_numbers.append(heap.delete_min().key) assert all(sorted_numbers[i] <= sorted_numbers[i+1] for i in range(len(sorted_numbers)-1))
def test_long_heap(): h = create_addressable_pairing_heap(key_type=int) h1 = h.insert(5, 15) assert h1.key == 5 assert h1.value == 15 h1.value=10 assert h1.value == 10 h2 = h.insert(6, 20) h3 = h.insert(7, 20) h4 = h.insert(8, 20) h5 = h.find_min() assert h5.key == 5 assert h5.value == 10 h5.decrease_key(4) assert h5.key == 4 assert h5.value == 10 assert len(h) == 4 h5.delete() assert len(h) == 3 # check that handle is still valid even if # removed assert h5.key == 4 assert h5.value == 10 # But reusing should throw a value error with pytest.raises(ValueError): h5.delete() h.clear(); assert len(h) == 0
def test_double_any_value_heap(): h = create_addressable_pairing_heap(key_type=float, value_type=object) h1 = h.insert(5.5, "15") assert h1.key == 5.5 assert h1.value == "15" h1.value = "10" assert h1.value == "10" h.insert(6.5, "20") h.insert(7.5, "20") h.insert(8.5, "20") h5 = h.find_min() assert h5.key == 5.5 assert h5.value == "10" h5.decrease_key(4.5) assert h5.key == 4.5 assert h5.value == "10" assert len(h) == 4 h5.delete() assert len(h) == 3 # check that handle is still valid even if # removed assert h5.key == 4.5 assert h5.value == "10" # But reusing should throw a value error with pytest.raises(ValueError): h5.delete() h.clear() assert len(h) == 0
def test_any_any_addressable_heap(): h = create_addressable_pairing_heap(key_type=object, value_type=object) h1 = h.insert(MyKey(5.5), MyValue(15)) assert h1.key == MyKey(5.5) assert h1.value == MyValue(15) h1.value = MyValue(10) assert h1.value == MyValue(10) h.insert(MyKey(6.5), MyValue(20)) h.insert(MyKey(7.5), MyValue(20)) h.insert(MyKey(8.5), MyValue(20)) h5 = h.find_min() assert h5.key == MyKey(5.5) assert h5.value == MyValue(10) h5.decrease_key(MyKey(4.5)) assert h5.key == MyKey(4.5) assert h5.value == MyValue(10) assert len(h) == 4 h5.delete() assert len(h) == 3 # check that handle is still valid even if # removed assert h5.key == MyKey(4.5) assert h5.value == MyValue(10) # But reusing should throw a value error with pytest.raises(ValueError): h5.delete() h.clear() assert len(h) == 0
In this example we create a pairing heap. """ # %% # Start by importing the package. import jheaps # %% # Create the pairing heap using the following factory method. By default all # addressable heaps have `float` keys and `int` values. This can be adjusted by # parameters `key_type` and `value_type` in the factory method which can take # values `float`, `int`, `object`. heap = jheaps.create_addressable_pairing_heap() # %% # Adding elements can be performed using insert. We next add an element with key # equal to 3.14 and value 100. Moreover, we add a few more elements. heap.insert(3.14, 100) for i in range(1, 100): heap.insert(i, 1000 + i) # %% # Now our heap has 100 elements. print('Total elements so far: {}'.format(len(heap)))