Priority Queues Priority queues have the following characteristics –Each item placed into a priority queue has a priority value associated with it –When a remove is requested from a priority queue, we remove the highest priority item –We should also have a way to determine if the priority queue is empty –We can also have a peek to see what item has the highest priority without removing it It is best to limit priority values to be integers (reason to be seen shortly).
Priority Queues It is also a wise thing to maintain the following values for a priority queue –current size (how many items are in the queue) –the priority of the highest priority item in the queue to make remove more efficient however it causes (some) additional work –after the remove happens we need to possibly change this value –after an add we just need to check if the one we added is higher than the highest, if so, change it.
Priority Queues Implementation of a priority queue could be an array of queues. The index of the array is the priority value (see how we want our priority values to be integers?) The range of priority values (all integers) determines how many queues we're storing --- that is, how many elements of the array there will be. Let's implement a priority queue in this way now.
Priority Queues If we don't want to limit the priority values to integers or the range of priorities is large --- an array of queues is impractical. What alternate implementation(s) could handle a large range of floating point priorities?
Priority Queues What alternate implementation(s) could handle a large range of floating point priorities? –Store a linked list of nodes where each node contains a priority value and the item stored in the priority queue –This will require more work to insert into the priority queue, but will remove the need to keep track of largest priority, etc. Let's implement a priority queue in this way.
Deques Deque (or Dequeue) stands for double-ended queue. It is pronounced “deck”. Allows adding to and removing from both the front and rear. –Since this allows adding and removing from both ends what kind of linked list would be best to implement this data structure?