Presentation is loading. Please wait.

Presentation is loading. Please wait.

Chapter 9 Heaps and Priority Queues. 9-2 What is a Heap? A heap is a binary tree that satisfies these special SHAPE and ORDER properties: –Its shape must.

Similar presentations


Presentation on theme: "Chapter 9 Heaps and Priority Queues. 9-2 What is a Heap? A heap is a binary tree that satisfies these special SHAPE and ORDER properties: –Its shape must."— Presentation transcript:

1 Chapter 9 Heaps and Priority Queues

2 9-2 What is a Heap? A heap is a binary tree that satisfies these special SHAPE and ORDER properties: –Its shape must be a complete binary tree. –For each node in the heap, the value stored in that node is greater than or equal to the value in each of its children.

3 9-3 Are these Both Heaps? C A T treePtr 50 20 18 30 10

4 9-4 Is this a Heap? 70 60 40 30 12 810 tree

5 9-5 Where is the Largest Element in a Heap Always Found? 70 60 40 30 12 8 tree

6 9-6 We Can Number the Nodes Left to Right by Level This Way 70 0 60 1 40 3 30 4 12 2 8 5 tree

7 9-7 And use the Numbers as Array Indexes to Store the Trees 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 tree.nodes

8 9-8 // HEAP SPECIFICATION struct HeapType { void ReheapDown ( int root, int bottom ) ; void ReheapUp ( int root, int top ) ; int* elements; //ARRAY to be allocated dynamically int numElements ; };

9 9-9 Maintaining Heap When we delete an element from the heap, we have to copy a node from the bottom in its place to maintain the shape But the heap loses its order property We must swap elements until we fix this problem Consider the problem shown in the next slides when the root node is deleted

10 9-10

11 9-11

12 9-12 ReHeapDown We call the function ReHeapDown that pushes down the element that violates the heap order property until the problem is fixed It is a recursive function that takes the index of the current node in violation and the index of the bottom element The Heap is to be fixed between the current node and the bottom element

13 9-13 ReHeapDown Each time ReHeapDown is called, we go down one level thus we satisfy the smaller caller criterea for recursion There are two base cases where we stop: –If elements[root] is a leaf –If heap order property is satisfied

14 9-14 ReheapDown // IMPLEMENTATION OF RECURSIVE HEAP MEMBER FUNCTIONS void HeapType::ReheapDown ( int root, int bottom ) // Pre: 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 ;

15 9-15 ReheapDown (cont) 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 ) ; }

16 9-16 At the End of the Second Iteration of the Loop

17 9-17 ReHeapUp This function is converse of ReHeapDown It takes a leaf node that violates the order property and moves it up until its correct position is found Each node’s value should be compared to the value of its parent and then it should be moved up if parent is smaller

18 // IMPLEMENTATIONcontinued void HeapType::ReheapUp ( int root, int top ) // Pre: root is the index of the node that may violate the heap // order property. The order property is satisfied from top to // next-to-last node. // Post: Heap order property is restored between root and top //Comment: The parameters passed to this function are now correct { int parent ; if ( root > top ) //If we have not reached top yet { parent = ( root - 1 ) / 2; if ( elements [ parent ] < elements [ root ] ) { Swap ( elements [ parent ], elements [ root ] ) ; ReheapUp ( parent,top ) ; //climb up } 18

19 9-19 Priority Queue A priority queue is an ADT with the property that only the highest-priority element can be accessed at any time.

20 9-20 ADT Priority Queue Operations Transformers –MakeEmpty –Enqueue –Dequeue Observers –IsEmpty –IsFull change state observe state

21 9-21 Implementation Level There are many ways to implement a priority queue –An unsorted List- dequeuing would require searching through the entire list –An Array-Based Sorted List- Enqueuing is expensive –A Reference-Based Sorted List- Enqueuing again is 0(N) –A Binary Search Tree- On average, 0(log 2 N) steps for both enqueue and dequeue –A Heap- guarantees 0(log 2 N) steps, even in the worst case

22 9-22 PQType ~PQType Enqueue Dequeue. [0] [1] [2] [3] [4] [5] [6] [7] [8] [9] ‘X’ ‘C’ ‘J’ Private Data: numItems 3 maxItems 10 items.elements.numElements class PQType

23 9-23 Class PQType Declaration class FullPQ(){}; class EmptyPQ(){}; class PQType { public: PQType(int); ~PQType(); void MakeEmpty(); bool IsEmpty() const; bool IsFull() const; void Enqueue(int newItem); void Dequeue(int& item); private: int length; HeapType items; int maxItems; };

24 9-24 Class PQType Function Definitions PQType::PQType(int max) { maxItems = max; items.elements = new ItemType[max]; length = 0; } void PQType::MakeEmpty() { length = 0; } PQType::~PQType() { delete [] items.elements; }

25 9-25 Class PQType Function Definitions 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)

26 9-26 Code for Dequeue void PQType::Dequeue(int& item) { if (length == 0) throw EmptyPQ(); else { item = items.elements[0]; items.elements[0] = items.elements[length-1]; length--; items.ReheapDown(0, length-1); }

27 9-27 Code for Enqueue void PQType::Enqueue(int newItem) { if (length == maxItems) throw FullPQ(); else { length++; items.elements[length-1] = newItem; items.ReheapUp(0, length-1); }

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


Download ppt "Chapter 9 Heaps and Priority Queues. 9-2 What is a Heap? A heap is a binary tree that satisfies these special SHAPE and ORDER properties: –Its shape must."

Similar presentations


Ads by Google