You are on page 1of 11

Data Structures & algorithm analysis

Chapter 6: Heaps and priority queues

Heaps
Heap: Complete binary tree with the heap property:

Min-heap: All values less than child values. Max-heap: All values greater than child values.

The values are partially ordered.

Heap representation: Normally the arraybased complete binary tree representation.

Dr. Khalil Challita

Heap example

Parent of v[k] = v[(k-1)/2] Left child of v[k] = v[2*k+1] Right child of v[k] = v[2*k + 2]

A B D I J E F C G

Note: the first element (A) may be stored at position 0 or 1

A 0

B 1

C 2

D 3

E 4

F 5

G H 6 7
Dr. Khalil Challita

I 8

J 9

10 11 12 13
3

Building the Heap

(a) (4-2) (4-1) (2-1) (5-2) (5-4) (6-3) (6-5) (7-5) (7-6) (b) Steps of BuildHeap: (5-2), (7-3), (7-1), (6-1)
Dr. Khalil Challita 4

Priority Queues

When a collection of objects is organized by importance or priority, we call this a priority queue.
Example: Scheduling jobs in a multi-tasking operating system. The priority of a job may change, requiring some reordering of the jobs.

Implementation: Use a heap to store the priority queue, since a normal queue data structure will not implement a priority queue efficiently.

Dr. Khalil Challita

Binary Heap Class


#include <vector> template <class Comparable> class BinaryHeap { public: explicit BinaryHeap( int capacity = 100); bool isEmpty( ) const; bool isFull( ) const; const Comparable & findMin( ) const; void insert( const Comparable & x ); void deleteMin( Comparable & minItem ); void makeEmpty( );

Dr. Khalil Challita

Binary Heap Class


private: int currentSize;

// Number of elements in heap const Comparable HEAP_EMPTY; vector<Comparable> array; // The heap array void buildHeap( ); void percolateDown( int hole );

};

Dr. Khalil Challita

Binary Heap Implementation


template <class Comparable> BinaryHeap<Comparable>::BinaryHeap(int capacity, const comparable & empty): array( capacity + 1 ), HEAP_EMPTY (empty), currentSize( 0 ) { } template <class Comparable> void BinaryHeap<Comparable>::insert(const Comparable & x ) { if( isFull( ) ) cout << The heap is full ; // Percolate up int hole = ++currentSize; for( ; hole > 1 && x < array[ hole / 2 ]; hole /= 2 ) array[ hole ] = array[ hole / 2 ]; array[ hole ] = x; }
Dr. Khalil Challita 8

Binary Heap Implementation


template <class Comparable> const Comparable & BinaryHeap<Comparable>::findMin( ) const { if( isEmpty( ) ) ``return HEAP_EMPTY ; return array[ 1 ]; } template <class Comparable> void BinaryHeap<Comparable>::deleteMin( Comparable & minItem ) { if( isEmpty( ) ) return HEAP_EMPTY ; minItem = array[ 1 ]; array[ 1 ] = array[ currentSize-- ]; percolateDown( 1 ); }
Dr. Khalil Challita 9

Binary Heap Implementation


template <class Comparable> void BinaryHeap<Comparable>::buildHeap( ) { for( int i = currentSize / 2; i > 0; i-- ) percolateDown( i ); } template <class Comparable> bool BinaryHeap<Comparable>::isEmpty( ) const { return currentSize == 0;} template <class Comparable> bool BinaryHeap<Comparable>::isFull( ) const { return currentSize == array.size( ) - 1;} template <class Comparable> void BinaryHeap<Comparable>::makeEmpty( ) { currentSize = 0;}
Dr. Khalil Challita 10

Binary Heap Implementation


template <class Comparable> void BinaryHeap<Comparable>::percolateDown( int hole ) { /* 1*/ int child; /* 2*/ Comparable tmp = array[ hole ]; /* 3*/ for( ; hole * 2 <= currentSize; hole = child ) { /* 4*/ child = hole * 2; /* 5*/ if( child != currentSize && array[ child + 1] < array[ child ] ) /* 6*/ child++; /* 7*/ if( array[ child ] < tmp ) /* 8*/ array[ hole ] = array[ child ]; else /* 9*/ break; } /*10*/ array[ hole ] = tmp; }
Dr. Khalil Challita 11

You might also like