View
215
Download
0
Embed Size (px)
Citation preview
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 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.
9-3
Are these Both Heaps?
C
A T
treePtr
50
20
18
30
10
9-4
Is this a Heap?
70
60
40 30
12
8 10
tree
9-5
Where is the Largest Element in a Heap Always Found?
70
60
40 30
12
8
tree
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
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
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
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
9-10
9-11
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
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
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 ;
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 ) ;}
}}
9-16
At the End of the Second Iteration of the Loop
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
// IMPLEMENTATION continued
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
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.
9-20
ADT Priority Queue Operations
Transformers – MakeEmpty – Enqueue– Dequeue
Observers – IsEmpty
– IsFull
change state
observe state
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(log2N) steps for both enqueue and dequeue
– A Heap- guarantees 0(log2N) steps, even in the worst case
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<char>
9-23
Class PQType Declarationclass 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;};
9-24
Class PQType Function DefinitionsPQType::PQType(int max){ maxItems = max; items.elements = new ItemType[max]; length = 0;}void PQType::MakeEmpty(){ length = 0;}PQType::~PQType(){ delete [] items.elements;}
9-25
Class PQType Function Definitions
DequeueSet item to root element from queueMove last leaf element into root positionDecrement lengthitems.ReheapDown(0, length-1)
EnqueueIncrement lengthPut newItem in next available position items.ReheapUp(0, length-1)
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); }}
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); }}
9-28
Comparison of Priority Queue Implementations
Enqueue Dequeue
Heap O(log2N) O(log2N)
Linked List O(N) O(N)
Binary Search Tree
Balanced O(log2N) O(log2N)
Skewed O(N) O(N)