Main Index
-
Upload
stephanie-gutierrez -
Category
Documents
-
view
42 -
download
1
description
Transcript of Main Index
1 Main IndexMain Index ContentsContents1 Main IndexMain Index ContentsContents
Model for a Queue
The Queue
Queue ADT (3 slides) (3 slides)
Radix Sort (2 slides) (2 slides)
miniQueue()
Bounded Queue
Priority Queue
Priority Queue ADT (2 slides) (2 slides)
Chapter 8 Chapter 8 – – Queues and Priority QueuesQueues and Priority Queues
Removing Items from a Heap
Summary Slides (5 slides)
2 Main IndexMain Index ContentsContents2 Main IndexMain Index ContentsContents
Grocery Store Checkout: A Grocery Store Checkout: A Model for a QueueModel for a Queue
3 Main IndexMain Index ContentsContents3 Main IndexMain Index ContentsContents
The QueueThe 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.
C
B C
A B C
A
bac kf ro nt
p u s h A
A Bf ro nt bac k
p u s h B
f ro nt bac kp u s h C
f ro nt bac kp o p A
f ro ntbac k
p o p B
4 Main IndexMain Index ContentsContents4 Main IndexMain Index ContentsContents
CLASS queue Constructor <queue>
queue();Create an empty queue.
CLASS queue Operations <queue>
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.
5 Main IndexMain Index ContentsContents5 Main IndexMain Index ContentsContents
CLASS queue Operations <queue>
const T& front() const;Constant version of front().
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.
6 Main IndexMain Index ContentsContents6 Main IndexMain Index ContentsContents
CLASS queue Operations <queue>
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
int size() const;Return the number of elements in the queue.
7 Main IndexMain Index ContentsContents7 Main IndexMain Index ContentsContents
The Radix SortThe Radix SortOrder 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 39Pass 0: Distribute the cards into bins according
to the 1's digit (100).
9 130
0
39
987
6
6
3 51 58 5
543
2 29 2
21
8 Main IndexMain Index ContentsContents8 Main IndexMain Index ContentsContents
The Radix SortThe Radix Sort
Final Sequence: 91 6 85 15 92 35 30 22 39Pass 1: Take the new sequence and distribute
the cards into bins determined by the 10's digit (101).
6
0
9 29 1
9
8 5
87654
3 93 53 0
3
2 2
2
1 5
1
9 Main IndexMain Index ContentsContents9 Main IndexMain Index ContentsContents
m iniQ .pus h(1 0)
m iniQ .pus h(2 5 )
m iniQ .pus h(5 0 )
n = m iniQ .fro nt() // n = 1 0
m iniQ .po p()
Q u e u e S ta te m e n t L is tL is t S ta te m e n tQ u e u e1 0
ba c kfro ntqlis t.pus h_ ba c k (1 0 ) 1 0
ba c kfro nt
1 0 2 5
ba c kfro ntqlis t.pus h_ ba c k (2 5 ) 1 0 2 5
ba c kfro nt
5 01 0 2 5
ba c kfro nt
q list.push _ b a ck (5 0 )1 0 2 5
fro nt
5 0
ba c k
re turn qlis t.fro nt() // re turn 1 0
2 5 5 0
ba c kfro ntqlis t.po p_ fro nt() 2 5 5 0
m in iQ u e u e < in t> m in iQ ; // d e c la re a n e m p ty q u e u e
10 Main IndexMain Index ContentsContents10 Main IndexMain Index ContentsContents
The Bounded queueThe Bounded queue
A
BC
qf ro ntqbac k
Ins e r t e le m e nts A,B , C
BC qf ro nt
qbac k
R e m o ve e le m e nt A
D
Cqf ro nt
qbac k
Ins e r t e le m e nt D ,
Cqf ro nt
qbac k
R e m o ve e le m e nt B
D
Cqfront
qba c k
Ins e r t e le m e nt D ,
Cqfront qba c k
Ins e r t e le m e nt E
C ircu la r V iewArra y V iew
D E
Ins e r t e le m e nt D , Ins e r t e le m e nt E
C D
qfrontqba c k
E C D
qfrontqba c k
11 Main IndexMain Index ContentsContents
Priority QueuePriority Queue
J o b # 3C lerk
J o b # 4Su p erv is o r
J o b # 2P res id en t
J o b # 1M an ager
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 Main IndexMain Index ContentsContents12 Main IndexMain Index ContentsContents
CLASS priority_queue Constructor <queue>
priority_queue();Create an empty priority queue. Type T must
implement the operator <.
CLASS priority_queue Operations <queue>
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
13 Main IndexMain Index ContentsContents13 Main IndexMain Index ContentsContents
CLASS priority_queue Operations <queue>
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 Main IndexMain Index ContentsContents
Removing Elements From a Removing Elements From a HeapHeap
45
1511
35102520
4228
H e a p a fte r e ra s ing 5 0
50
101511
35422520
4528
H e a p b e fo re e ra s ing 5 0
15 Main IndexMain Index ContentsContents15 Main IndexMain Index ContentsContents
Summary Slide 1Summary 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 Main IndexMain Index ContentsContents16 Main IndexMain Index ContentsContents
Summary Slide 2Summary 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 Main IndexMain Index ContentsContents17 Main IndexMain Index ContentsContents
Summary Slide 3Summary Slide 3
§- The miniQueue class- Provides a class with STL queue class interface.
- Uses the list class by object composition.
18 Main IndexMain Index ContentsContents18 Main IndexMain Index ContentsContents
Summary Slide 4Summary 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 Main IndexMain Index ContentsContents
s
19 Main IndexMain Index ContentsContents
Summary Slide 5Summary 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)