priority queues (heaps) - cs.bilkent.edu.trkorpe/courses/cs202/lecture7.pdfa priority queue is a...

29
1 CS 202, Spring 2003 Fundamental Structures of Computer Science II Bilkent University 1 Priority Queues (Heaps) CS 202 – Fundamental Structures of Computer Science II Bilkent University Computer Engineering Department CS 202, Spring 2003 Fundamental Structures of Computer Science II Bilkent University 2 Need for priority based retrievel Some applications require different treatments for items Printer queue Items (files to be printed) can be prioritized depending on their size. OS scheduler It may be better to schedule smaller sized programs before larger sized programs. Priority queue structure can be used to model these kind of applications So that some operations (like finding the highest priority item) can be implemented very efficiently.

Upload: phungmien

Post on 14-Jul-2019

213 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: Priority Queues (Heaps) - cs.bilkent.edu.trkorpe/courses/cs202/lecture7.pdfA priority queue is a data structure that allows at the least the following two operations ... Binary Heap

1

CS 202, Spring 2003Fundamental Structures of Computer Science II

Bilkent University 1

Priority Queues (Heaps)

CS 202 – Fundamental Structures of Computer Science II

Bilkent UniversityComputer Engineering Department

CS 202, Spring 2003Fundamental Structures of Computer Science II

Bilkent University 2

Need for priority based retrievel

Some applications require different treatments for items

Printer queueItems (files to be printed) can be prioritized depending on their size.

OS schedulerIt may be better to schedule smaller sized programs before larger sized programs.

Priority queue structure can be used to model these kind of applications

So that some operations (like finding the highest priority item) can be implemented very efficiently.

Page 2: Priority Queues (Heaps) - cs.bilkent.edu.trkorpe/courses/cs202/lecture7.pdfA priority queue is a data structure that allows at the least the following two operations ... Binary Heap

2

CS 202, Spring 2003Fundamental Structures of Computer Science II

Bilkent University 3

Model

A priority queue is a data structure that allows at the least the following two operations

InsertInserts an element to a priority queue and maintains the priority queue properties after insertion.

deleteMinFinds, returns, and removes the minimum element in the priority queue.

Priority QueuePriority Queueinsert deleteMin

CS 202, Spring 2003Fundamental Structures of Computer Science II

Bilkent University 4

Simple Implementations of priority queues

Use a simple linked listInsert to the head of the list (O(1))Search for an item by traversing the list from start to end or until item found (O(N)).

Use a binary search treeInsert into the tree. (O(logN))Remove from the tree (O(logN)).

Page 3: Priority Queues (Heaps) - cs.bilkent.edu.trkorpe/courses/cs202/lecture7.pdfA priority queue is a data structure that allows at the least the following two operations ... Binary Heap

3

CS 202, Spring 2003Fundamental Structures of Computer Science II

Bilkent University 5

Binary Heap (or Heap)

It is a complete binary tree. A tree that is completely filled except the bottom level, which is strictly filled from left to right.

A complete binary tree of height h has between 2hand 2h+1 – 1 nodes. The height of a complete binary tree is: Nlog

CS 202, Spring 2003Fundamental Structures of Computer Science II

Bilkent University 6

Binary Heap

4 5 6 7

9

2

10 11 13 14 15

16 17

8

18 19

1

3

20 21 22 23 24 25 26

12

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 20

0 1 2 3

An element at position i will have children at positions 2i and 2i+1

An element at position i will have parent at position 2/i

Page 4: Priority Queues (Heaps) - cs.bilkent.edu.trkorpe/courses/cs202/lecture7.pdfA priority queue is a data structure that allows at the least the following two operations ... Binary Heap

4

CS 202, Spring 2003Fundamental Structures of Computer Science II

Bilkent University 7

4 5 6 7

9

2

10 11 13 14 15

16 17

8

18 19

1

3

20 21 22 23 24 25 26

12

28 29 30 3127

height = 4

When height = 4:Minimum number of nodes = 16 = 24

Maximum number of nodes = 31 = 25 - 1

CS 202, Spring 2003Fundamental Structures of Computer Science II

Bilkent University 8

Heap Order property

In a heap, for every node X, the key in the parent of X is smaller or equal to the key in X. Root has the smallest key.

13

35 16

40 31 19

13

35 16

40 45 19

A complete tree, but not a heap! A heap

Page 5: Priority Queues (Heaps) - cs.bilkent.edu.trkorpe/courses/cs202/lecture7.pdfA priority queue is a data structure that allows at the least the following two operations ... Binary Heap

5

CS 202, Spring 2003Fundamental Structures of Computer Science II

Bilkent University 9

Heap Classclass BinaryHeap

{public:explicit BinaryHeap( int capacity = 100 );bool isEmpty( ) const;bool isFull( ) const;const int & findMin( ) const;void insert( const int & x );void deleteMin( );void deleteMin( int & minItem );void makeEmpty( );

private:int currentSize; // Number of elements in heapvector <int> array; // The heap arrayvoid buildHeap( );void percolateDown( int hole );

};

CS 202, Spring 2003Fundamental Structures of Computer Science II

Bilkent University 10

Heap Insertvoid BinaryHeap::insert( const int & x )

{if( isFull( ) )

throw Overflow( );

// Percolate upint hole = ++currentSize;for( ; hole > 1 && x < array[ hole / 2 ]; hole /= 2 )

array[ hole ] = array[ hole / 2 ];array[ hole ] = x;

}

Page 6: Priority Queues (Heaps) - cs.bilkent.edu.trkorpe/courses/cs202/lecture7.pdfA priority queue is a data structure that allows at the least the following two operations ... Binary Heap

6

CS 202, Spring 2003Fundamental Structures of Computer Science II

Bilkent University 11

Heap Delete Minimumvoid BinaryHeap::deleteMin( )

{if( isEmpty( ) )

throw Underflow( );

array[ 1 ] = array[ currentSize-- ];percolateDown( 1 );

}

CS 202, Spring 2003Fundamental Structures of Computer Science II

Bilkent University 12

Heap Delete Minimumvoid BinaryHeap::percolateDown( int hole )

{int child;int tmp = array[ hole ];

for( ; hole * 2 <= currentSize; hole = child ){

child = hole * 2;if( child != currentSize && array[ child + 1 ] < array[ child ])child++;if( array[ child ] < tmp )

array[ hole ] = array[ child ];else

break;}array[ hole ] = tmp;

}

Page 7: Priority Queues (Heaps) - cs.bilkent.edu.trkorpe/courses/cs202/lecture7.pdfA priority queue is a data structure that allows at the least the following two operations ... Binary Heap

7

CS 202, Spring 2003Fundamental Structures of Computer Science II

Bilkent University 13

Other Heap Operations

Heap provides fast access to minimum element

O(longN) worst case

But heap does not store any other ordering information. Searching for an arbitrary item is not easy

Requires O(N)) time. An other data structure needs to be kept if we want to do other operations also.

CS 202, Spring 2003Fundamental Structures of Computer Science II

Bilkent University 14

Other Heap Operations

If we assume that the position of other elements (other than minimum) is also known by some other methods, several operations become cheap

decreaseKey (p. ∆)Lowers the value of the item at position p by ∆ amount.

Requires percolate upincreseKey (p, ∆)

Increases the value of the item at position p by ∆ amountRequires perculate down

remove (p)Removes the item at position p

decreaseKey(p, ∞) and deleteMin();BuildHeap

Take N items and place them into an empty heap.

Page 8: Priority Queues (Heaps) - cs.bilkent.edu.trkorpe/courses/cs202/lecture7.pdfA priority queue is a data structure that allows at the least the following two operations ... Binary Heap

8

CS 202, Spring 2003Fundamental Structures of Computer Science II

Bilkent University 15

buildHeap()

Takes N items and builds a heap. Simple method is: Insert N items successively.

A more efficient method exists. O(N) running time.

Sketch of Algorithm1. Position = lowerbound(heapSiz/2); // initial position2. perculateDown item at that position. 3. Decrement position by one. 4. Repeat steps 2,3,4 until position is 0.

CS 202, Spring 2003Fundamental Structures of Computer Science II

Bilkent University 16

buildHeap()template <class Comparable>

void BinaryHeap<Comparable>::buildHeap( ){

for( int i = currentSize / 2; i > 0; i-- )percolateDown( i );

}

30 10 70 110

20

80

90 60 120 140 130100

150

40

50

Last element

parent

Start from here to percolate down

Page 9: Priority Queues (Heaps) - cs.bilkent.edu.trkorpe/courses/cs202/lecture7.pdfA priority queue is a data structure that allows at the least the following two operations ... Binary Heap

9

CS 202, Spring 2003Fundamental Structures of Computer Science II

Bilkent University 17

30 10 70 110

20

80

90 60 120 140100

150

40

50 130

1

4

2 3

5 6 7

30 10 50 110

20

80

90 60 120 140100

150

40

70 130

1

4

2 3

5 6 7

After percolateDown(6)

After percolateDown(7)

CS 202, Spring 2003Fundamental Structures of Computer Science II

Bilkent University 18

30 10 70 110

20

80

90 60 120 140100

150

40

50 130

1

4

2 3

5 6 7

20 10 50 110

30

80

90 60 120 140100

150

40

70 130

1

4

2 3

5 6 7

After percolateDown(4)

After percolateDown(5)

Page 10: Priority Queues (Heaps) - cs.bilkent.edu.trkorpe/courses/cs202/lecture7.pdfA priority queue is a data structure that allows at the least the following two operations ... Binary Heap

10

CS 202, Spring 2003Fundamental Structures of Computer Science II

Bilkent University 19

20 10 50 110

30

80

90 60 120 140100

150

40

70 130

1

4

2 3

5 6 7

20 60 50 110

30

10

90 80 120 140100

150

40

70 130

1

4

2 3

5 6 7

After percolateDown(2)

After percolateDown(3)

CS 202, Spring 2003Fundamental Structures of Computer Science II

Bilkent University 20

30 60 50 110

150

20

90 80 120 140100

10

40

70 130

1

4

2 3

5 6 7

After percolateDown(1)

Page 11: Priority Queues (Heaps) - cs.bilkent.edu.trkorpe/courses/cs202/lecture7.pdfA priority queue is a data structure that allows at the least the following two operations ... Binary Heap

11

CS 202, Spring 2003Fundamental Structures of Computer Science II

Bilkent University 21

Analysis of BuildHeap

The cost of BuildHeap is bounded by the number of dashed red lines, which is bounded by the sum of heights of all nodes of the heap. We will show that this sum is O(N), where N is the number of nodes in the heap.

CS 202, Spring 2003Fundamental Structures of Computer Science II

Bilkent University 22

)i(h −==

++

∑=

+

h

0i

i

h

3

1).(h – 1

2S nodes all of heights of Sum

0. height at nodes 2......

3.-h height at nodes 22-h height at nodes 41-h height at nodes 2

h height at node 1

:containsbinary perfectA

–1h2 :is nodes the of heights the of sum the nodes, 1 – 1h2 containing h height of treebinary perfect the For

:Proof

:Theorem

height=2

height=1

height=0

1 node

2 nodes

4 nodes

Page 12: Priority Queues (Heaps) - cs.bilkent.edu.trkorpe/courses/cs202/lecture7.pdfA priority queue is a data structure that allows at the least the following two operations ... Binary Heap

12

CS 202, Spring 2003Fundamental Structures of Computer Science II

Bilkent University 23

)(2),1(12

)1(12121

22......168421122......168422

)1(2......)3(16)2(8)1(422)1(2......)4(16)3(8)2(4)1(2

)(2

1

11

11

10

NOSNhS

hhhhSS

hhhhShhhhhS

ihS

hh

hh

hhhh

hh

h

i

i

=⇒=+−−=

+−−=−+−−=

=++++++++−−=+++++++−=−

++−+−+−+=++−+−+−+−+=

−=

+

+

+

−=∑

where

CS 202, Spring 2003Fundamental Structures of Computer Science II

Bilkent University 24

A complete tree is not a perfect binary tree, but number of nodes in a complete tree of height h is:

2h <= N < 2h+1

The sum we came up is an upperbound on the sum of height of all nodes in a complete tree.

Page 13: Priority Queues (Heaps) - cs.bilkent.edu.trkorpe/courses/cs202/lecture7.pdfA priority queue is a data structure that allows at the least the following two operations ... Binary Heap

13

CS 202, Spring 2003Fundamental Structures of Computer Science II

Bilkent University 25

Application of Priority Queues

We have talked about application in Operating Systems – schedulingPrinter queue

Some more applicationsImplementation of several graph algorithms

Selection Problem…

Discrete event simulation

CS 202, Spring 2003Fundamental Structures of Computer Science II

Bilkent University 26

The Selection Problem

Given a list of N elements and an integer k (1 <= k <= N)

Find out the kth largest element in the list. Example:

List of elements: 4, 9, 0, 3, 5, 7, 10, 12, 2, 81st largest element is: 1210th largest element is: 06th largest element is: 6

12 10 9 8 7 5 4 3 2 0 (sorted order of items)

Page 14: Priority Queues (Heaps) - cs.bilkent.edu.trkorpe/courses/cs202/lecture7.pdfA priority queue is a data structure that allows at the least the following two operations ... Binary Heap

14

CS 202, Spring 2003Fundamental Structures of Computer Science II

Bilkent University 27

The Selection Problem

Some solutionsAlg-1)

Sort N elements: O(N2) (for a simple sort algorithm)Retrieve the kth largest element: O(1)

Alg-2) 1. Read k elements into an array: O(k)2. Sort the elements in the array: O(k2)3. For each of the remaining elements: (N-k)

3.1 Compare it to the last element in array, if it is larger than the last element in the array, replace it with the last element and put it into correct spot in the array: O(k)

Total Running time: O(k + k2 + (N-k) * k) = O(Nk).

CS 202, Spring 2003Fundamental Structures of Computer Science II

Bilkent University 28

The Selection Problem

New algorithmsThey will make use of heaps.

A1Assume we want to find the kth smallest element.

1. Read N elements into an array O(N)2. Apply BuildHeap to the array O(N)3. Perform k deleteMin operations. O(klogN)

Last operation will five us the kth smallest one.

The solution is symmetric for finding kth largest elementThe total running time is O(N + klogN)

Page 15: Priority Queues (Heaps) - cs.bilkent.edu.trkorpe/courses/cs202/lecture7.pdfA priority queue is a data structure that allows at the least the following two operations ... Binary Heap

15

CS 202, Spring 2003Fundamental Structures of Computer Science II

Bilkent University 29

The Selection Problem

A2Use the idea of Alg-2At any time maintain a set S of k largest element.We want to find the kth largest element.

1. Read k elements into a heap S (of size k). O(k)2. For each remaining element (N-k) elements

2.1 Compare it with the smallest element (root) in heap S 2.2 If element is larger than root, then put it into S instead of root. 2.3 Percolate down the root if necessary. O(logk)

Running time = O(k+(N-k)logk) = O(Nlogk).

CS 202, Spring 2003Fundamental Structures of Computer Science II

Bilkent University 30

d-Heaps

Exactly like a binary heap, except that all nodes have d children

A binary heap is a 2-heap.

1

2 4

6 7 98 10

3

1211 13

14 15

5

A 3-Heap

Page 16: Priority Queues (Heaps) - cs.bilkent.edu.trkorpe/courses/cs202/lecture7.pdfA priority queue is a data structure that allows at the least the following two operations ... Binary Heap

16

CS 202, Spring 2003Fundamental Structures of Computer Science II

Bilkent University 31

Other Heap Operations

MergeCombining two heaps into one. Is not a very simple operation

O(logN) cost

We will discuss three data structure that will support merge operation efficiently

Leftist HeapsSkew HeapsBinomial Queues

CS 202, Spring 2003Fundamental Structures of Computer Science II

Bilkent University 32

Leftist Heaps

If we use arrays to implement heaps: We will have two input arrays (two heaps). Combining them requires copying one array into an other: O(N) time for equal-sized heaps

Therefore, we use a linked data structure (like a binary) to perform merge operation more efficiently.

Page 17: Priority Queues (Heaps) - cs.bilkent.edu.trkorpe/courses/cs202/lecture7.pdfA priority queue is a data structure that allows at the least the following two operations ... Binary Heap

17

CS 202, Spring 2003Fundamental Structures of Computer Science II

Bilkent University 33

Leftist Heaps

A leftist heap hasA structural propertyAn order property

Order property is the same with ordinary binary heaps. Structural property is little bit different.

CS 202, Spring 2003Fundamental Structures of Computer Science II

Bilkent University 34

Leftist Heap Property

Definition: Null path length, npl(X), of any node X is defined as the length of the shortest path from X to a node without two children. Null path length of a node with zero or one child is 0. Null path length of a NULL node is defined to be -1.

Null path length of a node is 1 more than the minimum of the null path lengths of its children.

A

B C

FD E

G0

00

1

0

0

1

npl(A) = 1 + min{npl(B), npl(C)}= 1 + min{1,0}= 1 + 0 = 1

Page 18: Priority Queues (Heaps) - cs.bilkent.edu.trkorpe/courses/cs202/lecture7.pdfA priority queue is a data structure that allows at the least the following two operations ... Binary Heap

18

CS 202, Spring 2003Fundamental Structures of Computer Science II

Bilkent University 35

Leftist Heap Property

Leftist heap property is: for every node X, in the heap, the null path length of the left child is at least as large as that of the right child.

A

B C

D

0

00

1

1

E

D

0

A

B C

D

0

10

1

1

D

0

E 0

E

A Leftist Heap Not a Leftist Heap

CS 202, Spring 2003Fundamental Structures of Computer Science II

Bilkent University 36

TheoremTheorem:

A leftist tree with r nodes on the right path must have at least 2r-1 nodes.

A

B

D

0

00

1

2

E

H

F 0 G 0

1C

Right path

Right path: A, C, GLength of the right path = 2Nodes on the right path = 3

Minimum number of nodesof leftist tree: 23-1 = 7

npl(root) = legth_of_right_path

Page 19: Priority Queues (Heaps) - cs.bilkent.edu.trkorpe/courses/cs202/lecture7.pdfA priority queue is a data structure that allows at the least the following two operations ... Binary Heap

19

CS 202, Spring 2003Fundamental Structures of Computer Science II

Bilkent University 37

Theorem - ProofProof:

By inductionIf r = 1 then, there has to be at least one node

2r-1 =21-1 = 1 Assume theorem is true for 1,2,3,…r.A tree with r+1 right path nodes would look like the following:

R

r nodes>= r nodes

r+1 nodes

>= 2r-1 nodes

>= 2r-1 nodes

Total nodes = 2r-1 + 2r-1 + 1= 2 x 2r – 1= 2r+1 - 1

CS 202, Spring 2003Fundamental Structures of Computer Science II

Bilkent University 38

)1log( +N

From the previous theorem it follows that the maximum number of nodes in the right path of a leftist heap of size N nodes is:

General idea for leftist heap operations is to perform all the work on the right path, which is the shortest.

Since, inserts and merges on the right path could destroy theLeftist heap property, we need to restore the property.

Page 20: Priority Queues (Heaps) - cs.bilkent.edu.trkorpe/courses/cs202/lecture7.pdfA priority queue is a data structure that allows at the least the following two operations ... Binary Heap

20

CS 202, Spring 2003Fundamental Structures of Computer Science II

Bilkent University 39

Operations

We will see the following leftist heap operations

MergeMerges two leftist heaps into a single one.

InsertDeleteMin

CS 202, Spring 2003Fundamental Structures of Computer Science II

Bilkent University 40

Merge

Merge two heaps H1 and H2. A recursive algorithm

1. If either one is empty than it is trivial to merge: return the non-empty heap as the result of merge. 2. Else:

2. 1 Merge the heap with the larger root with the right sub-heap of the heap with the smaller root. (this is ca recursive call)2.2 Make the resulting heap as the right child of the heap withthe smaller root. Swap the children of the root of the new heap, if npl(right-child) is greater than npl(left-child). Update npl(root): npl(root) = npl(right-child) + 1;

Page 21: Priority Queues (Heaps) - cs.bilkent.edu.trkorpe/courses/cs202/lecture7.pdfA priority queue is a data structure that allows at the least the following two operations ... Binary Heap

21

CS 202, Spring 2003Fundamental Structures of Computer Science II

Bilkent University 41

LH1 RH1

R1

LH2 RH2

R2

LH1

R1

RH1

LH2 RH2

R2

H1 H2

If R2 >= R1

CS 202, Spring 2003Fundamental Structures of Computer Science II

Bilkent University 42

LH1 RH1

R1

LH2 RH2

R2

H1 H2

If R2 < R1

LH1 RH1

R1

LH2

R2

RH2

Page 22: Priority Queues (Heaps) - cs.bilkent.edu.trkorpe/courses/cs202/lecture7.pdfA priority queue is a data structure that allows at the least the following two operations ... Binary Heap

22

CS 202, Spring 2003Fundamental Structures of Computer Science II

Bilkent University 43

Example – Merge the following heaps

3

10

21

0

00

1

1

14

23

0

8

26

17

6

12

18

0

00

1

2

24

33

037 18

7

0

1

0

0

H1 H2

Both are leftist heaps

merge (H1, H2)

CS 202, Spring 2003Fundamental Structures of Computer Science II

Bilkent University 44

Since 6 is greater than 3, we should recursively merge H2 with Right subheap of H1

merge (H1->right, H2)

Level 1

3

10

21 14

23

8

26

17

6

12

18 24

33

37 18

7

Page 23: Priority Queues (Heaps) - cs.bilkent.edu.trkorpe/courses/cs202/lecture7.pdfA priority queue is a data structure that allows at the least the following two operations ... Binary Heap

23

CS 202, Spring 2003Fundamental Structures of Computer Science II

Bilkent University 45

Level 2

8

26

17

6

12

18 24

33

37 18

7

CS 202, Spring 2003Fundamental Structures of Computer Science II

Bilkent University 46

Level 3

8

26

17 37 18

7

Page 24: Priority Queues (Heaps) - cs.bilkent.edu.trkorpe/courses/cs202/lecture7.pdfA priority queue is a data structure that allows at the least the following two operations ... Binary Heap

24

CS 202, Spring 2003Fundamental Structures of Computer Science II

Bilkent University 47

Level 4

26

17

18

NULL

8

CS 202, Spring 2003Fundamental Structures of Computer Science II

Bilkent University 48

Level 5

18NULL

merge

18

Now, start backtracking

Page 25: Priority Queues (Heaps) - cs.bilkent.edu.trkorpe/courses/cs202/lecture7.pdfA priority queue is a data structure that allows at the least the following two operations ... Binary Heap

25

CS 202, Spring 2003Fundamental Structures of Computer Science II

Bilkent University 49

Back to Level 4

26

17 18

8

0

0 0

1

CS 202, Spring 2003Fundamental Structures of Computer Science II

Bilkent University 50

Back to level 3

37

7

26

17 18

8

0

0 0

10 Swap 37 and 8 rooted subtrees.

7

26

17 18

8

0

0 0

1 37

0

0

1

Page 26: Priority Queues (Heaps) - cs.bilkent.edu.trkorpe/courses/cs202/lecture7.pdfA priority queue is a data structure that allows at the least the following two operations ... Binary Heap

26

CS 202, Spring 2003Fundamental Structures of Computer Science II

Bilkent University 51

Back to level 2

6

12

18 24

33

7

26

17 18

8

0 0

1 37

0

0

1

0

00

1

2

No need for swapping, sincenpl(12) = npl(7)

CS 202, Spring 2003Fundamental Structures of Computer Science II

Bilkent University 52

Back to level 13

10

21 14

230

00

1

2

Swap 10 and 6

6

12

18 24

33

7

26

17 18

8

0 0

1 37

0

0

1

0

00

1

2

Page 27: Priority Queues (Heaps) - cs.bilkent.edu.trkorpe/courses/cs202/lecture7.pdfA priority queue is a data structure that allows at the least the following two operations ... Binary Heap

27

CS 202, Spring 2003Fundamental Structures of Computer Science II

Bilkent University 53

Back to level 1

This is the new heap after merge!

32

10

21 14

23

01

1

0

6

18 24

33

7

26

17 18

8

0 0

1 37

0

0

0

00

1 012

2

CS 202, Spring 2003Fundamental Structures of Computer Science II

Bilkent University 54

Implementationtemplate <class Comparable>

class LeftistNode{

Comparable element;LeftistNode *left;LeftistNode *right;int npl;

LeftistNode( const Comparable & theElement, LeftistNode *lt = NULL,LeftistNode *rt = NULL, int np = 0 )

: element( theElement ), left( lt ), right( rt ), npl( np ) { }friend class LeftistHeap<Comparable>;

};

Page 28: Priority Queues (Heaps) - cs.bilkent.edu.trkorpe/courses/cs202/lecture7.pdfA priority queue is a data structure that allows at the least the following two operations ... Binary Heap

28

CS 202, Spring 2003Fundamental Structures of Computer Science II

Bilkent University 55

template <class Comparable>class LeftistHeap{public:LeftistHeap( );LeftistHeap( const LeftistHeap & rhs );~LeftistHeap( );

bool isEmpty( ) const;bool isFull( ) const;void insert( const Comparable & x );void deleteMin( );void deleteMin( Comparable & minItem );void merge( LeftistHeap & rhs );

private:LeftistNode<Comparable> *root; /* data member */LeftistNode<Comparable> * merge( LeftistNode<Comparable> *h1,

LeftistNode<Comparable> *h2 ) const;LeftistNode<Comparable> * merge1( LeftistNode<Comparable> *h1,

LeftistNode<Comparable> *h2 ) const;void swapChildren( LeftistNode<Comparable> * t ) const;

};

CS 202, Spring 2003Fundamental Structures of Computer Science II

Bilkent University 56

// Merge right handside heap into this heap. rhs becomes empty.

template <class Comparable>void LeftistHeap<Comparable>::merge( LeftistHeap & rhs ){

if( this == &rhs ) // Avoid aliasing problemsreturn;

root = merge( root, rhs.root );rhs.root = NULL;

}

Page 29: Priority Queues (Heaps) - cs.bilkent.edu.trkorpe/courses/cs202/lecture7.pdfA priority queue is a data structure that allows at the least the following two operations ... Binary Heap

29

CS 202, Spring 2003Fundamental Structures of Computer Science II

Bilkent University 57

// calls merge1 to to actual merge. Makes sure that when merge1 is called, // h1 is the node that has smaller root than h2.

template <class Comparable>LeftistNode<Comparable> *LeftistHeap<Comparable>::merge( LeftistNode<Comparable> * h1,

LeftistNode<Comparable> * h2 ) const{

if( h1 == NULL )return h2;

if( h2 == NULL )return h1;

if( h1->element < h2->element )return merge1( h1, h2 );

elsereturn merge1( h2, h1 );

}

CS 202, Spring 2003Fundamental Structures of Computer Science II

Bilkent University 58

// assumes heaps are not empty// assumes that h1 has smaller root.

template <class Comparable>LeftistNode<Comparable> *LeftistHeap<Comparable>::merge1( LeftistNode<Comparable> * h1,

LeftistNode<Comparable> * h2 ) const{

if( h1->left == NULL ) // Single nodeh1->left = h2; // Other fields in h1 already accurate

else{

h1->right = merge( h1->right, h2 );if( h1->left->npl < h1->right->npl )

swapChildren( h1 );h1->npl = h1->right->npl + 1;

}return h1;

}