Пример #1
0
 def put(self, val):
     # put should push the element to the heap (heappush)
     # raise an IndexError if called after max_size is reached. Nothing should be returned
     if len(self) < self.max_size:
         minheap = P2.MinHeap(self.elements)
         minheap.heappush(val)
     else:
         raise IndexError(
             'Queue is already at maximum length, cannot put another element.'
         )
Пример #2
0
 def get(self):
     # get should remove and return the root element of the heap (heappop)
     # raise an IndexError if called on an empty priority queue
     if len(self) > 0:
         minheap = P2.MinHeap(self.elements)
         min_elem = minheap.heappop()
         return (min_elem)
     else:
         raise IndexError(
             'Queue is empty, cannot perform get on an empty queue.')
Пример #3
0
 def peek(self):
     # peek should return the smallest value in the queue.
     # raise an IndexError if called on an empty priority queue
     if len(self) > 0:
         minheap = P2.MinHeap(self.elements)
         min_elem = minheap.heappop()
         minheap.heappush(min_elem)
         return (min_elem)
     else:
         raise IndexError(
             'Queue is empty, cannot perform peek from an empty queue.')
Пример #4
0
 def __init__(self, max_size):
     super().__init__(max_size)
     self.heap = myHeap.MinHeap(self.elements)