Download presentation
Presentation is loading. Please wait.
Published byChrystal Houston Modified over 9 years ago
1
Problem of the Day At low tide, a ship docks in the harbor Ship is 9’ above the water line when it docks Over the side hangs a rope ladder w/ rungs 1’ apart Tide rises at a rate of 9” per hour After 6 hours what length of ladder will still be above water?
2
Problem of the Day At low tide, a ship docks in the harbor Ship is 9’ above the water line when it docks Over the side hangs a rope ladder w/ rungs 1’ apart Tide rises at a rate of 9” per hour After 6 hours what length of ladder will still be above water? 9’ – the ladder will rise with the boat!
3
CSC 212 – Data Structures
4
Queue ADT Creates fair & equal system to handle data in order added Structure for element processing in order added
5
Queue ADT Creates fair & equal system to handle data in order added Structure for element processing in order added From each according to his ability, to each according to his needs Karl Marx
6
Deque ADT Limited inequality when processing elements process either end of structure Add and remove to process either end of structure Can’t use middle elements, however
7
Deque ADT Limited inequality when processing elements process either end of structure Add and remove to process either end of structure Can’t use middle elements, however Four legs good, two legs better From Animal Farm George Orwell
8
Priority Queue ADT Priority queue uses strict ordering of data Values assigned priority when added to the queue completely biased order Priorities used to process in completely biased order
9
Priority Queue ADT Priority queue uses strict ordering of data Values assigned priority when added to the queue completely biased order Priorities used to process in completely biased order First you get the sugar, then you get the power, then you get the women
10
Priority Queue ADT PriorityQueue yet another Collection Prioritize each datum contained in the collection PQ is organized from lowest to highest priority Access smallest priority only sort of like Queue min() & removeMin() return priority & value Implementation not defined: this is still an ADT Remember that organization & order is theoretical only
11
PriorityQueue yet another Collection Prioritize each datum contained in the collection PQ is organized from lowest to highest priority Access smallest priority only sort of like Queue min() & removeMin() return priority & value Implementation not defined: this is still an ADT Remember that organization & order is theoretical only Priority Queue ADT
12
Entry s in a PriorityQueue PriorityQueues use Entry to hold data As with Position, implementations may differ Entry has 2 items that define how it gets used PQ will only use key – the priority given to the Entry Value is important data to be processed by program
13
P RIORITY Q UEUE Operations D EQUE Q UEUE P RIORITY Q UEUE addFront() addLast() enqueue()insert() getFront() getLast() front()min() removeFront() removeLast() dequeue()removeMin()
14
Sequence -based Priority Queue
15
Using Sorted Sequence Entry s stored in increasing order of priority insert() checks each index to find where to add Once found, Position s may shift to make space 5 Sequence
16
Using Sorted Sequence Entry s stored in increasing order of priority insert() checks each index to find where to add Once found, Position s may shift to make space 5
17
Sequence Using Sorted Sequence Entry s stored in increasing order of priority insert() checks each index to find where to add Once found, Position s may shift to make space 1 5
18
Sequence 1 Using Sorted Sequence Entry s stored in increasing order of priority insert() checks each index to find where to add Once found, Position s may shift to make space 5
19
Sequence 1 Using Sorted Sequence Entry s stored in increasing order of priority insert() checks each index to find where to add Once found, Position s may shift to make space 3 5
20
Using Sorted Sequence Entry s stored in increasing order of priority insert() checks each index to find where to add Once found, Position s may shift to make space Sequence 1 35
21
Using Sorted Sequence Entry s stored in increasing order of priority insert() checks each index to find where to add Once found, Position s may shift to make space 9 Sequence 135
22
Using Sorted Sequence Entry s stored in increasing order of priority insert() checks each index to find where to add Once found, Position s may shift to make space Sequence 1 359
23
Using Sorted Sequence Entry s stored in increasing order of priority insert() checks each index to find where to add Once found, Position s may shift to make space 0 Sequence 1359
24
Using Sorted Sequence Entry s stored in increasing order of priority insert() checks each index to find where to add Once found, Position s may shift to make space 0 1359
25
Using Sorted Sequence Entry s stored in increasing order of priority min() returns 0 th element in the Sequence Planned well, removeMin() does not need to shift 0 1359
26
Using Sorted Sequence Entry s stored in increasing order of priority min() returns 0 th element in the Sequence Planned well, removeMin() does not need to shift 0 1359
27
1 Using Sorted Sequence Entry s stored in increasing order of priority min() returns 0 th element in the Sequence Planned well, removeMin() does not need to shift 359
28
Using Sorted Sequence Entry s stored in increasing order of priority min() returns 0 th element in the Sequence Planned well, removeMin() does not need to shift 59 3
29
5 Using Sorted Sequence Entry s stored in increasing order of priority min() returns 0 th element in the Sequence Planned well, removeMin() does not need to shift 9
30
Using Sorted Sequence Entry s stored in increasing order of priority min() returns 0 th element in the Sequence Planned well, removeMin() does not need to shift Sequence 9
31
Using Sorted Sequence Entry s stored in increasing order of priority No shifting ever required if planned well. How? What is big-Oh complexity of this approach? insert(): removeMin(): min():
32
Unsorted Sequence s ♥ Clutter Entry s can be stored in any order Add Entry to Sequence end during insert() Guarantees that this is fast with no shifting needed 5 Sequence
33
Unsorted Sequence s ♥ Clutter Entry s can be stored in any order Add Entry to Sequence end during insert() Guarantees that this is fast with no shifting needed Sequence 5
34
Unsorted Sequence s ♥ Clutter Entry s can be stored in any order Add Entry to Sequence end during insert() Guarantees that this is fast with no shifting needed Sequence 1 5
35
Unsorted Sequence s ♥ Clutter Entry s can be stored in any order Add Entry to Sequence end during insert() Guarantees that this is fast with no shifting needed Sequence 5 1
36
Unsorted Sequence s ♥ Clutter Entry s can be stored in any order Add Entry to Sequence end during insert() Guarantees that this is fast with no shifting needed Sequence 5 3 1
37
Unsorted Sequence s ♥ Clutter Entry s can be stored in any order Add Entry to Sequence end during insert() Guarantees that this is fast with no shifting needed Sequence 5 13
38
Unsorted Sequence s ♥ Clutter Entry s can be stored in any order Add Entry to Sequence end during insert() Guarantees that this is fast with no shifting needed Sequence 5 13 9
39
Unsorted Sequence s ♥ Clutter Entry s can be stored in any order Add Entry to Sequence end during insert() Guarantees that this is fast with no shifting needed Sequence 5 139
40
Unsorted Does It… Later Put off work until min() & removeMin() Scan Sequence to find Entry with smallest priority Still should not shift when removing from Sequence Sequence 5 139
41
Unsorted Does It… Later Put off work until min() & removeMin() Scan Sequence to find Entry with smallest priority Still should not shift when removing from Sequence Sequence 5 139
42
Unsorted Does It… Later Put off work until min() & removeMin() Scan Sequence to find Entry with smallest priority Still should not shift when removing from Sequence Sequence 5 139
43
Unsorted Does It… Later Put off work until min() & removeMin() Scan Sequence to find Entry with smallest priority Still should not shift when removing from Sequence Sequence 5 139
44
Unsorted Does It… Later Put off work until min() & removeMin() Scan Sequence to find Entry with smallest priority Still should not shift when removing from Sequence Sequence 5 139
45
Unsorted Does It… Later Put off work until min() & removeMin() Scan Sequence to find Entry with smallest priority Still should not shift when removing from Sequence Sequence 5 139
46
Unsorted Does It… Later Put off work until min() & removeMin() Scan Sequence to find Entry with smallest priority Still should not shift when removing from Sequence Sequence 5 39
47
Unsorted Sequence Complexity What is big-Oh complexity of this approach? insert(): removeMin(): min():
48
Why Use Sorted Sequence ? Cheap to check & remove data with this approach But it is relatively expensive to add data When insert() rare relative to calling min() Good trade-off here, so sorted Sequence worth it
49
Why Use Sorted Sequence ? Cheap to check & remove data with this approach But it is relatively expensive to add data When insert() rare relative to calling min() Good trade-off here, so sorted Sequence worth it
50
Why Use Unsorted Sequence ? Cheap to add data with this approach But it is relatively expensive to remove or check it When would this trade-off work?
51
Why Use Unsorted Sequence ? Cheap to add data with this approach But it is relatively expensive to remove or check it When would this trade-off work? Examples?
52
For Next Lecture Complete your (last) weekly assignment Read GT 8.3 before next Monday's lecture Why did we discuss binary trees before this? Does order really matter? Reasons using this over Sequence-based approaches? Programming project #3 due soon
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.