![]() Unlike with the Stack and Queue ADTs, it is not clear if we can use a """ class EmptyPriorityQueueError( Exception): """Exception raised when calling pop on an empty priority queue.""" def _str_( self) -> str: """Return a string representation of this error.""" return 'You called dequeue on an empty priority queue.' List-based Raise an EmptyPriorityQueueError when the priority queue is empty. """ def dequeue( self) -> Any: """Remove and return the item with the highest priority. ![]() """ def enqueue( self, priority: int, item: Any) -> None: """Add the given item with the given priority to this priority queue. > pq = PriorityQueue() > pq.is_empty() True > pq.enqueue(1, 'hello') > pq.is_empty() False > pq.enqueue(5, 'goodbye') > pq.enqueue(2, 'hi') > pq.dequeue() 'goodbye' """ def _init_( self) -> None: """Initialize a new and empty priority queue.""" def is_empty( self) -> bool: """Return whether this priority queue contains no items. When removing an item from the queue, the highest-priority item is the one that is removed. Here is the public interface of a PriorityQueueįrom typing import Any class PriorityQueue: """A collection items that are be removed in priority order. Next chapter, we’ll study an application of priority queues that uses aĭifferent way of representing priorities. ![]() Integers, with larger integers representing higher priorities. For this section, we’ll simply represent priorities as One subtlety with our definition of this ADT is in how we represent Item with a priority ( enqueue), remove the highest priority
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |