Пример #1
0
def test_binheap_push_one_value_pop_one_value():
    """Tests push on binary heap"""
    from binheap import Binheap
    new_binheap = Binheap()
    new_binheap.push(2)
    new_binheap.pop()
    assert new_binheap._container == []
Пример #2
0
def test_binheap_push_two_values_second_less_than():
    """Tests push on binary heap"""
    from binheap import Binheap
    new_binheap = Binheap()
    new_binheap.push(2)
    new_binheap.push(1)
    assert new_binheap._container == [2, 1]
Пример #3
0
 def insert(self, item):
     """Make sure it's a tuple and try to insert it"""
     try:
         assert isinstance(item, tuple)
         assert len(item) == 2
     except AssertionError:
         raise ValueError("Only tuples of (priority, data) are valid")
     Binheap.push(self, item)
Пример #4
0
 def dijkstra(self, start, end):
     """Awasome dijkstra using min heap."""
     if start not in self.graph or end not in self.graph:
         raise ValueError('no such node exist')
     heap, visited = Binheap(), set()
     heap.push([0, start, []])
     while heap:
         curdist, node, path = heap.pop()
         path = path + [node]
         if node == end:
             break
         if node not in visited:
             visited.add(node)
             for neighbor, weight in self.neighbors(node).items():
                 heap.push((curdist + weight, neighbor, path))
     else:
         return [], None
     return curdist, path
Пример #5
0
def test_binheap_push_multiple_unordered():
    """Tests push on binary heap"""
    from binheap import Binheap
    new_binheap = Binheap()
    new_binheap.push(5)
    new_binheap.push(4)
    new_binheap.push(6)
    new_binheap.push(3)
    new_binheap.push(7)
    new_binheap.push(2)
    new_binheap.push(9)
    assert new_binheap._container == [9, 6, 7, 3, 4, 2, 5]
Пример #6
0
def test_binheap_push_single_value_container_head():
    """Tests push on binary heap"""
    from binheap import Binheap
    new_binheap = Binheap()
    new_binheap.push(1)
    assert new_binheap._container == [1]
Пример #7
0
def test_push():
    ebh = Binheap()
    assert isinstance(ebh, Binheap)
    # onto an empty heap
    ebh.push(5)
    assert ebh.values[0] == 5

    ebh.push(1)
    assert ebh.values[1] == 1
    ebh.push(5)
    assert ebh.values[2] == 5

    ebh.push(2)
    assert ebh.values[1] == 2
    assert ebh.values[3] == 1
    ebh.push(3)
    assert ebh.values[4] == 2
    assert ebh.values[1] == 3

    ebh.push(7)
    assert ebh.values[0] == 7
    assert ebh.values[2] == 5
    assert ebh.values[5] == 5
    ebh.push(6)
    assert ebh.values[0] == 7
    assert ebh.values[2] == 6
    assert ebh.values[5] == 5
    assert ebh.values[6] == 5
Пример #8
0
def test_pop(value, result):
    from binheap import Binheap
    new_heap = Binheap()
    new_heap.push(value)
    new_heap.pop()
    assert new_heap.binheap == result
Пример #9
0
def test_push_max_large(init_list):
    raw, min_heap, max_heap = init_list
    heap = Binheap(max_heap, minmax="max")
    heap.push(10)
    assert heap == [10, 1, 7, -3, -9, 5]
Пример #10
0
def test_push_max_med(init_list):
    raw, min_heap, max_heap = init_list
    heap = Binheap(max_heap, minmax="max")
    heap.push(6)
    assert heap == [7, 1, 6, -3, -9, 5]
Пример #11
0
def test_push_max_small(init_list):
    raw, min_heap, max_heap = init_list
    heap = Binheap(max_heap, minmax="max")
    heap.push(3)
    assert heap == [7, 1, 5, -3, -9, 3]
Пример #12
0
def test_push_min_small(init_list):
    raw, min_heap, max_heap = init_list
    heap = Binheap(min_heap, minmax="min")
    heap.push(3)
    assert heap == [-9, -3, 3, 7, 1, 5]
Пример #13
0
def test_push_min_large(init_list):
    raw, min_heap, max_heap = init_list
    heap = Binheap(min_heap, minmax="min")
    heap.push(10)
    assert heap == [-9, -3, 5, 7, 1, 10]
Пример #14
0
def binheap_20():
    """Making one heap instance with len of 20 per test."""
    heap = Binheap()
    for num in range(20):
        heap.push(num)
    return heap