Tables, Priority Queues, Heaps
- Table ADT
– purpose, implementations
- Priority Queue ADT
– variation on Table ADT
- Heaps
– purpose, implementation – heapsort
EECS 268 Programming II 1
Tables, Priority Queues, Heaps Table ADT purpose, implementations - - PowerPoint PPT Presentation
Tables, Priority Queues, Heaps Table ADT purpose, implementations Priority Queue ADT variation on Table ADT Heaps purpose, implementation heapsort EECS 268 Programming II 1 Table ADT A table in generic terms has M
EECS 268 Programming II 1
EECS 268 Programming II 2
3
EECS 268 Programming II 4
5
– store item under its key
– delete item with key == key_value, if present
– return pointer to item with key==key_value
– Functor: a function-object, much like a fn pointer – visitor is executed for each node in table
EECS 268 Programming II 6
EECS 268 Programming II 7
Figure 11-3 The data members for two sorted linear implementations of the ADT table for the data in Figure 11-1: (a) array based; (b) pointer based
8
Figure 11-4 The data members for a binary search tree implementation of the ADT table for the data in Figure 11-1
implementations
EECS 268 Programming II 9
10
Figure 11-5a Insertion for unsorted linear implementations: array based
11
Figure 11-6a Insertion for sorted linear implementations: array based
12
Figure 11-5b Insertion for unsorted linear implementations: pointer based
13
Figure 11-6b Insertion for sorted linear implementations: pointer based
EECS 268 Programming II 14
EECS 268 Programming II 15
Figure 11-7 The average-case order of the ADT table operations for various implementations
EECS 268 Programming II 16
EECS 268 Programming II 17
#include <string> using namespace std; typedef string KeyType; class KeyedItem { public: KeyedItem() {} KeyedItem(const KeyType& keyValue) : searchKey(keyValue){} KeyType getKey() const { return searchKey; } private: KeyType searchKey; };
EECS 268 Programming II 18
class City : public KeyedItem { public: City() : KeyedItem() {} City(const string& name, const string& ctry, const int& num) : KeyedItem(name), country(ctry), pop(num) {} string cityName() const; int getPopulation() const; void setPopulation(int newPop); private: // city's name is search-key value string country; int pop; };
EECS 268 Programming II 19
EECS 268 Programming II 20
EECS 268 Programming II 21
searchMin, sort
EECS 268 Programming II 22
EECS 268 Programming II 23
EECS 268 Programming II 24
25
Figure 11-8 UML diagram for the class PriorityQueue
EECS 268 Programming II 26
Figure 11-9a An array-based implementation of the ADT priority queue
EECS 268 Programming II 27
Figure 11-9b A pointer-based implementation of the ADT priority queue
28
Figure 11-9c A binary search tree implementation of the ADT priority queue
EECS 268 Programming II 29
EECS 268 Programming II 30
EECS 268 Programming II 31
EECS 268 Programming II 32
Figure 11-10 UML diagram for the class Heap
EECS 268 Programming II 33
EECS 268 Programming II 34
EECS 268 Programming II 35
EECS 268 Programming II 36
EECS 268 Programming II 37
EECS 268 Programming II 38
– need to know the heap’s maximum size
– items: an array of heap items – size: an integer equal to the current number of items in the heap
EECS 268 Programming II 39
EECS 268 Programming II 40
Figure 11-12a Disjoint heaps
EECS 268 Programming II 41
Figure 11-12b A semiheap
EECS 268 Programming II 42
EECS 268 Programming II 43
EECS 268 Programming II 44
EECS 268 Programming II 45
EECS 268 Programming II 46
Figure 11-17 Transforming the array anArray into a heap
EECS 268 Programming II 47
EECS 268 Programming II 48
EECS 268 Programming II 49
EECS 268 Programming II 50