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 == []
def test_pop(): abh = Binheap([1, 2, 3, 4, 5, 6]) for x in range(6, 0, -1): assert abh.pop() == x # onto an empty heap print abh.values with pytest.raises(IndexError): abh.pop()
def test_heap_handles_push_pops_with_correct_ordering(): """.""" from binheap import Binheap heap = Binheap([3, 5, 6, 2, 7, 8, 1, 0, 0, 10]) assert heap.__repr__() == '[0, 1, 2, 3, 7, 8, 6, 5, 10]' assert heap.pop() == 0 assert heap.__repr__() == '[1, 3, 2, 5, 7, 8, 6, 10]'
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
def test_pop(value, result): from binheap import Binheap new_heap = Binheap() new_heap.push(value) new_heap.pop() assert new_heap.binheap == result
def pop(self): """Return highest priority value in queue (with attached priority)""" return Binheap.pop(self)
def test_pop_max(init_list): raw, min_heap, max_heap = init_list heap = Binheap(raw, minmax="max") heap.pop() assert heap == [5, 1, -9, -3]
def test_pop_min(init_list): raw, min_heap, max_heap = init_list heap = Binheap(raw, minmax="min") heap.pop() assert heap == [-3, 1, 5, 7]