def test_binary_heap_push(self): bh = BinaryHeap() self.assertEqual(bh.size(), 0) bh.push(1) bh.push(2) bh.push(3) self.assertEqual(bh.size(), 3)
def test_unique_values_raises_error_if_1_value_in_list(): """If a value is pushed into heap that already exists, error is raised.""" from binary_heap import BinaryHeap b = BinaryHeap() b.push(1) with pytest.raises(ValueError): b.push(1)
def test_binary_heap_max(self): bh = BinaryHeap() bh.push(1) bh.push(3) bh.push(2) self.assertEqual(bh.max(), 3) self.assertEqual(bh.size(), 3)
def test_heap(): test_heap = B_heap() test_heap.push(7) test_heap.push(8) test_heap.push(9) test_heap.push(3) return test_heap
def test_multipop_maxheap(): maxheap = BinaryHeap([7, 9, 18, 1, 38, 5.4, 6, 200], minheap=False) length = len(maxheap) maxheap.pop() maxheap.pop() maxheap.push(400) maxheap.pop() maxheap.pop() assert maxheap.pop() == 9 assert len(maxheap) == length - 4
def test_multipop_minheap(): minheap = BinaryHeap([7, 9, 18, 1, 38, 5.4, 6, 200]) length = len(minheap) minheap.pop() minheap.pop() minheap.push(0) minheap.pop() minheap.pop() assert minheap.pop() == 7 assert len(minheap) == length - 4
def test_push(): for x in range(50): a = BinaryHeap() for i in range(47): a.push(random.randint(0,100)) assert max(a._list) == a._list[0] index = a._size - 1 while index : assert a._list[index] <= a._list[(index-1)//2] index -= 1
def test_pop(): bt = BinaryHeap() for i in range(50): bt.push(random.randint(0,1000)) for i in range(49): tmp = max(bt._list[1:]) bt.pop() assert tmp == bt._list[0] bt.pop() assert 0 == len(bt._list) # testing for empty list pop a = BinaryHeap() with pytest.raises(IndexError): assert a.pop()
class PriorityQueue(object): def __init__(self): self.heap = BinaryHeap() self.seniority = 0 def insert(self, priority, val): self.heap.push((priority, self.seniority, val)) self.seniority -= 1 def pop(self): try: return self.heap.pop()[2] except IndexError: raise IndexError("The queue is empty") def peek(self): try: return self.heap._list[0][2] except IndexError: raise IndexError("The queue is empty")
def test_binary_heap(): _heap = BinaryHeap() with raises(IndexError): _heap.pop() _heap.push(2) _heap.push(20) _heap.push(-12) _heap.push(223) _heap.push(12) _heap.push(-2) sortedList = _heap.values assert _heap.values == [223, 20, -2, 2, 12, -12] _heap.pop() _heap.pop() assert _heap.values == [12, 2, -2, -12]
def test_binary_heap(): _heap = BinaryHeap() with raises(IndexError): _heap.pop() _heap.push(2) _heap.push(20) _heap.push(-12) _heap.push(223) _heap.push(12) _heap.push(-2) sortedList = _heap.values assert _heap.values == [223, 20, -2, 2, 12, -12] _heap.pop() _heap.pop() assert _heap.values == [12, 2, -2, -12]
def test_push_raises_type_error(): """If anything besides int, float or string given to push, raise error.""" from binary_heap import BinaryHeap b = BinaryHeap() with pytest.raises(TypeError): b.push([1, 2, 3])
def test_pop_maxheap(): maxheap = BinaryHeap([7, 9, 18, 1, 38, 5.4, 6], minheap=False) maxheap.push(400) length = len(maxheap) assert maxheap.pop() == 400 assert len(maxheap) == length - 1
class PriorityQ(object): """A class for a priority queue.""" def __init__(self, iterable=()): """Initialize a priority queue, optionally with items from iterable. The items in the priority queue are stored in a binary minheap. Items are first sorted by priority, then queue insertion order. Priority is expressed as an integer with 0 being the most important. args: iterable: an optional iterable to add to the priority queue. Items added this way will be given a priority of None. each item inside iterable can be either: * A QNode object * A container with value, priority * A non-iterable value """ self.heap = BinaryHeap(iterable=()) for item in iterable: try: is_container = len(item) == 2 except TypeError: # Case of QNode or non-iterable item self.insert(item) else: if is_container: # Case of value, iterable self.insert(item[0], item[1]) else: raise TypeError( "More than two args: instantiation supports\ non-iter value or iter of value, priority") def __repr__(self): return repr(self.heap) def __len__(self): return len(self.heap) def __iter__(self): return iter(self.heap) def __getitem__(self, index): return self.heap[index] def __setitem__(self, index, value): self.heap[index] = value def insert(self, item, priority=None): """Insert an item into the priority queue. If the item is a QNode object, it will be added tracking queue order. If not, a new QNode object is created to hold the item with queue order and optional priority assigned. args: item: the item to add (QNode or other value) priority: the optional integer priority (0 is most important) """ if isinstance(item, QNode): item.order = len(self) self.heap.push(item) else: self.heap.push(QNode(item, priority=priority, order=len(self))) def pop(self): """Remove and return the most important item from the queue.""" return self.heap.pop().val def peek(self): """Return the most important item from queue without removal.""" return self.heap[0].val
def test_push_value_is_inserted(): test_heap = B_heap() test_heap.push(100) assert test_heap._list[0] == 100
def test_pop_minheap(): minheap = BinaryHeap([7, 9, 18, 1, 38, 5.4, 6]) minheap.push(0) length = len(minheap) assert minheap.pop() == 0 assert len(minheap) == length - 1
class PriorityQ(object): """A class for a priority queue.""" def __init__(self, iterable=()): """Initialize a priority queue, optionally with items from iterable. The items in the priority queue are stored in a binary minheap. Items are first sorted by priority, then queue insertion order. Priority is expressed as an integer with 0 being the most important. args: iterable: an optional iterable to add to the priority queue. Items added this way will be given a priority of None. each item inside iterable can be either: * A QNode object * A container with value, priority * A non-iterable value """ self.heap = BinaryHeap(iterable=()) for item in iterable: try: is_container = len(item) == 2 except TypeError: # Case of QNode or non-iterable item self.insert(item) else: if is_container: # Case of value, iterable self.insert(item[0], item[1]) else: raise TypeError("More than two args: instantiation supports\ non-iter value or iter of value, priority") def __repr__(self): return repr(self.heap) def __len__(self): return len(self.heap) def __iter__(self): return iter(self.heap) def __getitem__(self, index): return self.heap[index] def __setitem__(self, index, value): self.heap[index] = value def insert(self, item, priority=None): """Insert an item into the priority queue. If the item is a QNode object, it will be added tracking queue order. If not, a new QNode object is created to hold the item with queue order and optional priority assigned. args: item: the item to add (QNode or other value) priority: the optional integer priority (0 is most important) """ if isinstance(item, QNode): item.order = len(self) self.heap.push(item) else: self.heap.push(QNode(item, priority=priority, order=len(self))) def pop(self): """Remove and return the most important item from the queue.""" return self.heap.pop().val def peek(self): """Return the most important item from queue without removal.""" return self.heap[0].val