Presentation is loading. Please wait.

Presentation is loading. Please wait.

Priority Queues, Heaps CS3240, L. grewe 1. 2 Goals Describe a priority queue at the logical level and implement a priority queue as a list Describe the.

Similar presentations


Presentation on theme: "Priority Queues, Heaps CS3240, L. grewe 1. 2 Goals Describe a priority queue at the logical level and implement a priority queue as a list Describe the."— Presentation transcript:

1 Priority Queues, Heaps CS3240, L. grewe 1

2 2 Goals Describe a priority queue at the logical level and implement a priority queue as a list Describe the shape and order property of a heap and implement a heap in a nonlinked tree representation in an array Implement a priority queue as a heap Compare the implementations of a priority queue using a heap, a linked list, and a binary search tree

3 3 Priority Queue An ADT in which only the item with the highest priority can be accessed

4 4 Priority Queue Items on a priority queue are made up of pairs Can you think of how a priority queue might be implemented as An unsorted list? An array-based sorted list? A linked sorted list? A binary search tree?

5 5 Heaps Heap A complete binary tree, each of whose elements contains a value that is greater than or equal to the value of each of its children Remember? Complete tree

6 6 Heaps Heap with letters 'A'.. 'J'

7 7 Heaps Another heap with letters 'A'.. 'J'

8 8 Heaps C A T treePtr 50 20 18 30 10 Are these heaps? treePtr

9 9 Heaps 70 60 40 30 12 810 treePtr Is this a heap?

10 10 Heaps 70 60 40 30 12 8 treePtr Can we use this fact to implement an efficient PQ? Where is the largest element?

11 11 Heaps We have immediate access to highest priority item BUT if we remove it, the structure isn't a heap Can we "fix" the problem efficiently?

12 12 Heaps Shape property is restored. Can order property be restored?

13 13 Heaps How?

14 14 Heaps

15 15 Heaps template struct HeapType { void reheapDown(int root, int bottom); … ItemType* elements; // Dynamic array int numElements; }

16 16 Heaps 70 0 60 1 40 3 30 4 12 2 8 5 root Number nodes left to right by level

17 17 Heaps 70 0 60 1 40 3 30 4 12 2 8 5 tree [ 0 ] [ 1 ] [ 2 ] [ 3 ] [ 4 ] [ 5 ] [ 6 ] 70 60 12 40 30 8 elements Use number as index

18 18 Heaps ReapDown(heap, root, bottom) if heap.elements[root] is not a leaf Set maxChild to index of child with larger value if heap.elements[root] < heap.elements[maxChild] Swap(heap.elements[root], heap.elements[maxChild]) ReheapDown(heap, maxChild, bottom) What variables do we need? Where are a node's children?

19 19 Heaps // IMPLEMENTATION OF RECURSIVE HEAP MEMBER FUNCTIONS void HeapType ::ReheapDown(int root, int bottom) // Pre: ItemType overloads the relational operators // root is the index of the node that may violate the // heap order property // Post: Heap order property is restored between root and // bottom { int maxChild ; int rightChild ; int leftChild ; leftChild = root * 2 + 1 ; rightChild = root * 2 + 2 ;

20 20 Heaps if (leftChild <= bottom)// ReheapDown continued { if (leftChild == bottom) maxChild = leftChld; else { if (elements[leftChild] <= elements[rightChild]) maxChild = rightChild; else maxChild = leftChild; } if (elements[root] < elements[maxChild]) { Swap(elements[root], elements[maxChild]); ReheapDown(maxChild, bottom); }

21 21 Heaps What other operations might our HeapType need?

22 22 Heaps Add K: Where must the new node be put? Now what?

23 23 Heaps

24 24 void HeapType::ReheapUp(int root, int bottom) // Pre: ItemType overloads the relational operators // bottom is the index of the node that may violate // the heap order property. The order property is // satisfied from root to next-to-last node. // Post:Heap order property is restored between root and // bottom { int parent; if (bottom > root) { parent = ( bottom - 1 ) / 2; if (elements[parent] < elements[bottom]) { Swap(elements[parent], elements[bottom]); ReheapUp(root, parent); }

25 25 Heaps How can heaps be used to implement Priority Queues?

26 26 Priority Queues class FullPQ(){}; class EmptyPQ(){}; template class PQType { public: PQType(int); ~PQType(); void MakeEmpty(); bool IsEmpty() const; bool IsFull() const; void Enqueue(ItemType newItem); void Dequeue(ItemType& item); private: int length; HeapType items; int maxItems; };

27 27 Priority Queues template PQType ::PQType(int max) { maxItems = max; items.elements = new ItemType[max]; length = 0; } template void PQType ::MakeEmpty() { length = 0; } template PQType ::~PQType() { delete [] items.elements; }

28 28 Priority Queues Dequeue Set item to root element from queue Move last leaf element into root position Decrement length items.ReheapDown(0, length-1) Enqueue Increment length Put newItem in next available position items.ReheapUp(0, length-1)

29 29 Priority Queues template void PQType ::Dequeue(ItemType& item) { if (length == 0) throw EmptyPQ(); else { item = items.elements[0]; items.elements[0] = items.elements[length-1]; length--; items.ReheapDown(0, length-1); }

30 30 Priority Queues template void PQType ::Enqueue(ItemType newItem) { if (length == maxItems) throw FullPQ(); else { length++; items.elements[length-1] = newItem; items.ReheapUp(0, length-1); }

31 31 Comparison of Priority Queue Implementations EnqueueDequeue HeapO(log 2 N) Linked ListO(N) Binary Search Tree BalancedO(log 2 N) SkewedO(N)


Download ppt "Priority Queues, Heaps CS3240, L. grewe 1. 2 Goals Describe a priority queue at the logical level and implement a priority queue as a list Describe the."

Similar presentations


Ads by Google