def test_push(): from binheap import BinHeap sample_list = [16, 14, 15, 9, 7, 6, 5, 1, 2, 3] bh = BinHeap(sample_list) sample_list.append(4) bh.push(4) assert bh._container == sample_list
class PriorityQ(object): """Class for implementing a priority queue.""" def __init__(self): self._heap = BinHeap(order='min') self._cumulative_idx = 0 def insert(self, pri, val): """Insert a value at the end of the priority queue.""" self._heap.push((pri, self._cumulative_idx, val)) self._cumulative_idx += 1 def pop(self): """Remove and return the value at the root of the priority queue.""" try: result = self._heap.pop() return result[2] except IndexError: raise IndexError("Cannot pop from empty queue.") def peek(self): """Return the value of at the root of the priority queue.""" try: return self._heap._heap_list[0][2] except IndexError: return None
class PriorityQ(object): """Class for implementing a priority queue.""" def __init__(self): self._heap = BinHeap(order='min') self._cumulative_idx = 0 def insert(self, pri, val): """Insert a value at the end of the priority queue.""" self._heap.push((pri, self._cumulative_idx, val)) self._cumulative_idx += 1 def pop(self): """Remove and return the value at the root of the priority queue.""" try: result = self._heap.pop() return result[2] except IndexError: raise IndexError("Cannot pop from empty queue.") def peek(self): """Return the value of at the root of the priority queue.""" try: return self._heap._heap_list[0][2] except IndexError: return None
def test_pull(full_heap, empty_heap): full_heap.pop() assert len(full_heap) == 11 assert helper(full_heap) is True full_heap.pop() assert len(full_heap) == 10 assert helper(full_heap) is True empty_heap = BinHeap() with pytest.raises(LookupError): empty_heap.pop() empty_heap.push(2) assert empty_heap.pop() == 2 with pytest.raises(LookupError): empty_heap.pop()
class Priorityq(object): def __init__(self): self.binheap = BinHeap() self._count = 0 def insert(self, priority, val): new_node = Node(priority, val, self._count) self.binheap.push(new_node) self._count += 1 def pop(self): return_node = self.binheap.pop() return return_node.val def peek(self): return self.binheap[0].val
def full_heap(): heap = BinHeap() heap.push(5) heap.push(4) heap.push(10) heap.push(7) heap.push(2) return heap
class PriorityQ(object): """Structure for values in a priorty queue. Items added to the priority queue are given a priority. If not set by the user, priority is set to be the lowest. When removing items, higher priority items are removed before lower priority items. """ def __init__(self): """Construct a new priority queue.""" self._values = BinHeap() self._min_priority = 0 def __len__(self): """Overwrite length to give us the amount of items in priority Q. Get all the items from all the buckets in the priority Q. """ return sum(len(b) for b in self._all_values[1:]) @property def _all_values(self): return self._values._values @property def _size(self): return self._values._size def insert(self, val, priority=None): """Add a new value into the priority queue. If no priority is given, uses current lowest priority. """ if priority is None: priority = self._min_priority if priority < self._min_priority: self._min_priority = priority try: i = self._all_values.index(priority) bucket = self._all_values[i] bucket._values.enqueue(val) except ValueError: self._values.push(Bucket(priority, [val])) def pop(self): """Remove the first value from priority Q. If no values in bucket then bucket is removed. """ if len(self._all_values) < 2: raise IndexError('Can not pop from empty priority Q.') bucket = self._all_values[1] result = bucket._values.dequeue() if len(bucket._values) == 0: self._values.pop() return result def peek(self): """Get the the most important item without removing it.""" if len(self._all_values) < 2: return bucket = self._all_values[1] return bucket._values.peek()
def test_pop_last(): heap = BinHeap() heap.push(5) assert heap.pop() == 5 assert heap.list == []
def test_pop_one_child(): heap = BinHeap() heap.push(5) heap.push(10) assert heap.pop() == 5
def test_push_more(): heap = BinHeap() heap.push(5) heap.push(4) assert heap.list[0] == 4 assert heap.list[1] == 5
def test_push(): heap = BinHeap() heap.push(5) assert heap.list[0] == 5