DATA STRUCTURES AND ALGORITHMS

22
DATA STRUCTURES AND ALGORITHMS Lecture Notes 9 Prepared by İnanç TAHRALI

description

DATA STRUCTURES AND ALGORITHMS. Lecture Notes 9 Prepared by İnanç TAHRALI. ROAD MAP. PRIORITY QUEUES (HEAPS) Model Simple Implementations Binary Heap Applications of Priority Queues d-Heaps Leftist Heaps. Review. insert worst case  O(logN) average case  O(1) deleteMin - PowerPoint PPT Presentation

Transcript of DATA STRUCTURES AND ALGORITHMS

Page 1: DATA STRUCTURES  AND ALGORITHMS

DATA STRUCTURES

ANDALGORITHMS

Lecture Notes 9

Prepared by İnanç TAHRALI

Page 2: DATA STRUCTURES  AND ALGORITHMS

2

ROAD MAP PRIORITY QUEUES (HEAPS)

Model Simple Implementations Binary Heap Applications of Priority Queues d-Heaps Leftist Heaps

Page 3: DATA STRUCTURES  AND ALGORITHMS

3

Review

insert worst case O(logN) average case O(1)

deleteMin worst case O(logN) average caseO(logN)

buildHeap worst case O(N) average case O(N)

decreaseKey worst case O(logN) average case O(1)

increaseKey worst case O(logN) average case O(logN)

remove worst case O(logN) average case O(logN)

Running Times of Operations for Binary Heaps

Page 4: DATA STRUCTURES  AND ALGORITHMS

4

d-Heaps Nodes have at most d children

Shallow

Insert one comparison for each percolate

DeleteMin d-1 comparisons

Good when number of insertions are much greater than deletion

Nh dlog

)(log NO d

)log( NdO d

Page 5: DATA STRUCTURES  AND ALGORITHMS

5

A 3-Heap

Page 6: DATA STRUCTURES  AND ALGORITHMS

6

Leftist Heaps Support merge operation efficiently

Merge O(N) for binary heaps Using arrays θ(N) Efficient implementation

Pointers makes others slower

Leftist heap has both a structural property and an ordering property

Its difference from binary heap is that, leftist heaps are not perfectly balanced

Page 7: DATA STRUCTURES  AND ALGORITHMS

7

Leftist Heaps Definition :

Null path length of a node x is the length of the shortest path from x to a node without two children.

Page 8: DATA STRUCTURES  AND ALGORITHMS

8

Leftist Heaps Definition :

For each node: NPL of left child is not less than NPL of right child

Only left tree is lefist heap

Page 9: DATA STRUCTURES  AND ALGORITHMS

9

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

Proof by induction:– if r = 1, there must be at least one tree node.– otherwise, suppose that the theorem is true for 1,2,…,r.– consider a leftist tree with r+1 noes on the right path– then the root has a right subtree with r nodes on the right path,

and a left subtree with at least r nodes on the right path (otherwise it would not be leftist)

– applying in the inductive hypothesis to these subtrees yields a minimum of 2r -1 nodes in each subtree

– this plus the root gives at least 2r+1 -1 nodes in the tree, prooving the theorem.

So, r = O(logN)

Page 10: DATA STRUCTURES  AND ALGORITHMS

10

Leftist Heap Operations Fundamental operation on leftist heaps is “merging”

What about insert and deleteMin

Algorithm

if H1 or H2 is NULLreturn the other one

compare the root values (assume H1 is smaller)H1->right = Merge(H1->right, H2)swap the left and right subtrees if necessary

if leftist heap property is violated

update the NPL of the root of H1return H1

Page 11: DATA STRUCTURES  AND ALGORITHMS

11

Leftist Heap Operations

Two leftist heaps H1 and H2

Page 12: DATA STRUCTURES  AND ALGORITHMS

12

Leftist Heap OperationsResult of merging H2 and H1’s right subheap

Page 13: DATA STRUCTURES  AND ALGORITHMS

13

Leftist Heap OperationsResult of attaching leftist heap of previous figure as H1’s right child

Page 14: DATA STRUCTURES  AND ALGORITHMS

14

Leftist Heap OperationsResult swapping children of H1’s root

Page 15: DATA STRUCTURES  AND ALGORITHMS

15

template <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 16: DATA STRUCTURES  AND ALGORITHMS

16

template <class Comparable>class LeftistHeap { public:

LeftistHeap( );LeftistHeap( const LeftistHeap & rhs );~LeftistHeap( );

bool isEmpty( ) const;bool isFull( ) const;const Comparable & findMin( ) const;

void insert( const Comparable & x );void deleteMin( );void deleteMin( Comparable & minItem );void makeEmpty( );void merge( LeftistHeap & rhs );

const LeftistHeap & operator=( const LeftistHeap & rhs );

Page 17: DATA STRUCTURES  AND ALGORITHMS

17

private:LeftistNode<Comparable> *root;

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;

LeftistNode<Comparable> * clone( LeftistNode<Comparable> *t ) const;

};

Page 18: DATA STRUCTURES  AND ALGORITHMS

18

/*Construct the leftist heap.template <class Comparable>LeftistHeap<Comparable>::LeftistHeap( ) {

root = NULL; }

/* Copy constructor.template <class Comparable>LeftistHeap<Comparable>::LeftistHeap( const

LeftistHeap<Comparable> & rhs ){

root = NULL;*this = rhs;

}

/*Destruct the leftist heap.template <class Comparable>LeftistHeap<Comparable>::~LeftistHeap( ){

makeEmpty( );}

Page 19: DATA STRUCTURES  AND ALGORITHMS

19

/*Merge rhs into the priority queue. rhs becomes empty. rhs must be different from this.

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

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

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

}

/*Internal method to merge two roots.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 );else return merge1( h2, h1 );

}

Page 20: DATA STRUCTURES  AND ALGORITHMS

20

/*Internal method to merge two roots. Assumes trees are not empty, and h1's root contains smallest item.

template <class Comparable>LeftistNode<Comparable> * LeftistHeap<Comparable>::

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

{if( h1->left == NULL ) // Single node

h1->left = h2; // Other fields in h1 already accurateelse

{ h1->right = merge( h1->right, h2 );

if( h1->left->npl < h1->right->npl ) swapChildren( h1 );

h1->npl = h1->right->npl + 1; } return h1;}

Page 21: DATA STRUCTURES  AND ALGORITHMS

21

/*Insert item x into the priority queue, maintaining heap order.

template <class Comparable>void LeftistHeap<Comparable>::insert( const Comparable & x ){

root = merge( new LeftistNode<Comparable>( x ), root );}

/* Remove the smallest item from the priority queue.template <class Comparable>void LeftistHeap<Comparable>::deleteMin( ){

if( isEmpty( ) )throw Underflow( );

LeftistNode<Comparable> *oldRoot = root;root = merge( root->left, root->right );delete oldRoot;

}

Page 22: DATA STRUCTURES  AND ALGORITHMS

22

Leftist Heap Operations

Result of merging right paths of H1 and H2