1 / 19

Main Index

Chapter 8 – Queues and Priority Queues. 1. Main Index. Contents. Model for a Queue The Queue Queue ADT (3 slides) Radix Sort (2 slides) miniQueue() Bounded Queue Priority Queue Priority Queue ADT (2 slides). Removing Items from a Heap Summary Slides (5 slides). 2. Main Index.

Download Presentation

Main Index

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Chapter 8 – Queues and Priority Queues 1 Main Index Contents • Model for a Queue • The Queue • Queue ADT (3 slides) • Radix Sort (2 slides) • miniQueue() • Bounded Queue • Priority Queue • Priority Queue ADT (2 slides) • Removing Items from a Heap • Summary Slides (5 slides)

  2. 2 Main Index Contents Grocery Store Checkout: A Model for a Queue

  3. 3 Main Index Contents The Queue A Queue is a FIFO (First in First Out) Data Structure. Elements are inserted in the Rear of the queue and are removed at the Front.

  4. queue(); Create an empty queue. CLASS queue CLASS queue <queue> <queue> Constructor Operations bool empty() const; Check whether the queue is empty. Return true if it is empty and false otherwise. T&l front(); Return a reference to the value of the item at the font of the queue. Precondition: The queue is not empty. 4 Main Index Contents

  5. const T& front() const; Constant version of front(). CLASS queue <queue> Operations void pop(); Remove the item from the front of the queue. Precondition: The queue is not empty. Postcondition: The element at the front of the queue is the element that was added immediately after the element just popped or the queue is empty. 5 Main Index Contents

  6. void push(const T& item); Insert the argument item at the back of the queue. Postcondition: The queue has a new item at the back CLASS queue <queue> Operations int size() const; Return the number of elements in the queue. 6 Main Index Contents

  7. 7 Main Index Contents The Radix Sort Order ten 2 digit numbers in 10 bins from smallest number to largest number. Requires 2 calls to the sort Algorithm. Initial Sequence: 91 6 85 15 92 35 30 22 39 Pass 0: Distribute the cards into bins according to the 1's digit (100).

  8. 8 Main Index Contents The Radix Sort Final Sequence: 91 6 85 15 92 35 30 22 39 Pass 1: Take the new sequence and distribute the cards into bins determined by the 10's digit (101).

  9. 9 Main Index Contents

  10. 10 Main Index Contents The Bounded queue

  11. Priority Queue A Special form of queue from which items are removed according to their designated priority and not the order in which they entered. Items entered the queue in sequential order but will be removed in the order #2, #1, #4, #3.

  12. priority_queue(); Create an empty priority queue. Type T must implement the operator <. CLASS priority_queue CLASS priority_queue <queue> <queue> Constructor Operations bool empty() const; Check whether the priority queue is empty. Return true if it is empty, and false otherwise. Create void pop(); Remove the item of highest priority from the queue. Precondition: The priority queue is not empty. Postcondition: The priority queue has 1 less element 12 Main Index Contents

  13. CLASS priority_queue <queue> Operations 13 Main Index Contents void push(const T& item); Insert the argument item into the priority queue. Postcondition: The priority queue contains a new element. int size() const; Return the number of items in the priority queue. T& top(); Return a reference to the item having the highest priority. Precondition: The priority queue is not empty. const T& top(); Constant version of top().

  14. Removing Elements From a Heap

  15. 15 Main Index Contents Summary Slide 1 §- Queue - A first-come-first-served data structure. §- Insertion operations (push()) occur at the back of the sequence §- deletion operations (pop()) occur at the front of the sequence.

  16. 16 Main Index Contents Summary Slide 2 §- The radix sort algorithm - Orders an integer vector by using queues (bins). - This sorting technique has running time O(n) but has only specialized applications. - The more general in-place O(n log2n) sorting algorithms are preferable in most cases.

  17. 17 Main Index Contents Summary Slide 3 §- The miniQueue class - Provides a class with STL queue class interface. - Uses the list class by object composition.

  18. 18 Main Index Contents Summary Slide 4 §- Implementing a queue with a fixed-size array - Indices qfront and qback move circularly around the array. - Gives O(1) time push() and pop() operations with no wasted space in the array.

  19. s 19 Main Index Contents Summary Slide 5 §- Priority queue - Pop() returns the highest priority item (largest or smallest). - Normally implemented by a heap, which is discussed in Chapter 14. - The push() and pop() operations have running time O(log2n)

More Related