Presentation is loading. Please wait.

Presentation is loading. Please wait.

Heaps Priority Queues. 1/6/2016Data Structure: Heaps2 Outline Binary heaps Binomial queues Leftist heaps.

Similar presentations


Presentation on theme: "Heaps Priority Queues. 1/6/2016Data Structure: Heaps2 Outline Binary heaps Binomial queues Leftist heaps."— Presentation transcript:

1 Heaps Priority Queues

2 1/6/2016Data Structure: Heaps2 Outline Binary heaps Binomial queues Leftist heaps

3 Binary Heaps

4 1/6/2016Data Structure: Heaps4 Heap order property For every root node N, the key in the parent of N is smaller than or equal to the key in N. 5 12 15 22 16 28 23 21 19 2548 27 The smallest value is in the root.

5 1/6/2016Data Structure: Heaps5 Operations on priority queues Insert –Put an element into the queue DeleteMin –Find the minimal element –Return it –Remove it from the queue

6 1/6/2016Data Structure: Heaps6 Binary Heaps A binary heap is an implementation of a priority queue. A binary heap is a complete binary tree with heap order property.

7 1/6/2016Data Structure: Heaps7 Complete Binary Tree A binary tree is a complete binary tree if every level in the tree is completely filled, except the leaf level, which is filled from left to right. 32 12 34 25 40 56 40 43 23 3133 30 3645 35 Height is in  log 2 n , where n is the number of nodes.

8 1/6/2016Data Structure: Heaps8 Array implementation of complete binary tree A B D E I J K H C FG L ABCDIHGFEJ LK 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 1 2 4 3 567 89 12 11 10

9 1/6/2016Data Structure: Heaps9 Class BinaryHeap public class BinaryHeap {public BinaryHeap( ) {this( DEFAULT_CAPACITY ); } public BinaryHeap( int capacity ) {currentSize = 0; array = new Comparable[ capacity + 1 ]; } … private static final int DEFAULT_CAPACITY = 100; private int currentSize; // Number of elements in heap private Comparable [ ] array; // The heap array }

10 1/6/2016Data Structure: Heaps10 percolateUp 5 12 15 22 16 3 23 21 19 25 48 27 36

11 1/6/2016Data Structure: Heaps11 Method percolateUp private void percolateUp( int hole ) {Comparable x = array[hole]; while (hole > 1 && x.compareTo( array[ hole / 2 ] ) < 0) {array[ hole ] = array[ hole/2 ]; hole = hole/2; } array[ hole ] = x; }

12 1/6/2016Data Structure: Heaps12 Method percolateUp private void percolateUp( int hole ) {while (hole>1 && array[hole].compareTo( array[ hole/2 ])<0) {swap(hole, hole/2); hole = hole/2; } private void swap( int p1, int p2 ) {Comparable x = array[p1]; array[p1] = array[p2]; array[p2] = x; }

13 1/6/2016Data Structure: Heaps13 PercolateDown 32 12 34 25 4056 40 43 23 3133 30 3645 35

14 1/6/2016Data Structure: Heaps14 Method percolateDown private void percolateDown( int hole ) {int child; while( hole * 2 <= currentSize) {child = hole * 2; if(child != currentSize&&array[ child+1].compareTo( array[child ])<0) child++;// choose the smaller child if( array[ child ].compareTo( array[ hole ] ) < 0 ) swap( hole, child ); else break; hole = child; }

15 1/6/2016Data Structure: Heaps15 Method percolateDown private void percolateDown( int hole ) {int child; Comparable tmp = array[ hole ]; // save the value of the node while ( hole * 2 <= currentSize ) {child = hole * 2; if(child != currentSize&&array[ child+1].compareTo( array[child ])<0) child++;// choose the smaller child if( array[ child ].compareTo( tmp ) < 0 ) {array[ hole ] = array[ child ];// move child up hole = child; // move hole down } else break; } array[ hole ] = tmp;// put the value in the hole }

16 1/6/2016Data Structure: Heaps16 Insertion 5 12 15 22 16 28 23 21 19 25 48 27 10

17 1/6/2016Data Structure: Heaps17 Method insert public void insert( Comparable x ) throws Overflow {if( isFull( ) )throw new Overflow( ); array[++currentSize]=x; percolateUp(currentSize); }

18 1/6/2016Data Structure: Heaps18 DeleteMin 5 12 15 22 16 28 23 21 10 1948 27 25

19 1/6/2016Data Structure: Heaps19 Method deleteMin public Comparable findMin( ) {if( isEmpty( ) )return null; return array[ 1 ]; } public Comparable deleteMin( ) {if( isEmpty( ) )return null; Comparable minItem = findMin( ); array[ 1 ] = array[ currentSize--]; percolateDown( 1 ); return minItem; }

20 1/6/2016Data Structure: Heaps20 Method buildHeap private void buildHeap( ) {for( int i = currentSize / 2; i > 0; i-- ) percolateDown( i ); } 32 12 34 25 4056 40 43 23 3133 30 3645 35

21 1/6/2016Data Structure: Heaps21 Method decreaseKey public void decreaseKey(int p, Comparable d) throws outOfRange {if( p>currentSize )throw new outOfRange(); array[ p ] = array[ p ] - d; percolateUp( p ); }

22 1/6/2016Data Structure: Heaps22 Method increaseKey public void increaseKey(int p, Comparable d) throws outOfRange {if( p>currentSize )throw new outOfRange(); array[ p ] = array[ p ] + d; percolateDown( p ); }

23 Binomial Queues

24 1/6/2016Data Structure: Heaps24 Binomial Tree A binomial tree is defined recursively as follow: A binomial tree of height 0, denoted by B 0, is a one- node tree. A binomial tree of height k, denoted by B k, is formed by attaching a binomial tree B k-1 to the root of another binomial tree B k-1.

25 1/6/2016Data Structure: Heaps25 Property of Binomial Trees A binomial tree of height k has 2 k nodes. The number of nodes at depth d is the binomial coefficient  k   d . 1 1 2 1 3 1 4 6 4 1 1 1 1 2 1 1 3 3 1 1 4 6 4 1 1 5 10 10 5 1 1 6 15 20 15 6 1

26 1/6/2016Data Structure: Heaps26 Structure A binomial queue is a collection of heap- ordered trees, each of which is a binomial tree. 6 3 21 13 16 32 40 26 28 35 43 45 51 46 50

27 1/6/2016Data Structure: Heaps27 Binomial Nodes class BinomialNode {BinomialNode( Comparable theElement ) { this( theElement, null, null ); } BinomialNode( Comparable theElement, BinomialNode lt, BinomialNode nt ) {element = theElement; Child = lt;nextSibling = nt; } Comparable element; BinomialNode Child; BinomialNode nextSibling; }

28 1/6/2016Data Structure: Heaps28 Examples: binomial nodes 13 16 32 40 26 2835 43 45 51 46 50 13 16 32 40 26 28 35 43 45 51 46 50 Child nextSibling

29 1/6/2016Data Structure: Heaps29 Binomial Queues public class BinomialQueue {public BinomialQueue( ) {theTrees = new BinomialNode[ MAX_TREES ]; makeEmpty( );}... public void makeEmpty( ) {currentSize = 0; for( int i=0; i < theTrees.length; i++ ) theTrees[ i ] = null;} private static final int MAX_TREES = 14; private int currentSize; private BinomialNode [ ] theTrees; private int capacity( ) {return 2*theTrees.length - 1;} }

30 1/6/2016Data Structure: Heaps30 Method combineTrees private static BinomialNode combineTrees ( BinomialNode t1,BinomialNode t2 ) {if( t1.element.compareTo( t2.element ) > 0 ) return combineTrees( t2, t1 ); t2.nextSibling = t1.Child; t1.leftChild = t2; return t1; } 26 2835 43 45 51 46 50 26 28 35 43 45 51 46 50

31 1/6/2016Data Structure: Heaps31 Method merge (1) public void merge( BinomialQueue rhs ) throws Overflow {if( this == rhs ) return; if( currentSize+rhs.currentSize>capacity() ) throw new Overflow( ); currentSize += rhs.currentSize; BinomialNode carry = null; for( int i=0,j=1; j<=currentSize; i++,j*=2 ) {BinomialNode t1 = theTrees[ i ]; BinomialNode t2 = rhs.theTrees[ i ];

32 1/6/2016Data Structure: Heaps32 Method merge (2) // No trees if (t1==null && t2==null && carry==null) {} // Only this if (t1!=null && t2==null && carry==null) {} // Only rhs if (t1==null && t2!=null && carry==null) {theTrees[i] = t2; rhs.theTrees[i] = null;} // Only carry if (t1==null && t2==null && carry!=null) {theTrees[ i ] = carry; carry = null;} // this & rhs if (t1!=null && t2==null && carry!=null) {carry = combineTrees( t1, t2 ); theTrees[i]=rhs.theTrees[i]=null;}

33 1/6/2016Data Structure: Heaps33 Method merge (3) // this and carry if (t1!=null && t2==null && carry!=null) {carry = combineTrees( t1, carry ); theTrees[ i ] = null;} // rhs and carry if (t1==null && t2!=null && carry!=null) {carry = combineTrees( t2, carry ); rhs.theTrees[ i ] = null;} // All three if (t1!=null && t2!=null && carry!=null) {theTrees[ i ] = carry; carry = combineTrees( t1, t2 ); rhs.theTrees[ i ] = null;} }

34 1/6/2016Data Structure: Heaps34 Method merge (4) for( int k=0; k < rhs.theTrees.length; k++ ) rhs.theTrees[ k ] = null; rhs.currentSize = 0; }

35 1/6/2016Data Structure: Heaps35 Method insert public void insert( Comparable x ) throws Overflow {BinomialQueue oneItem= new BinomialQueue( ); oneItem.currentSize = 1; oneItem.theTrees[0] = new BinomialNode( x ); merge( oneItem ); }

36 1/6/2016Data Structure: Heaps36 Method deleteMin (2) for( int j = minIndex - 1; j >= 0; j-- ) {deletedQueue.theTrees[ j ] = deletedTree; deletedTree = deletedTree.nextSibling; deletedQueue.theTrees[ j ].nextSibling = null; } theTrees[ minIndex ] = null; currentSize -= deletedQueue.currentSize + 1; try { merge( deletedQueue ); } catch( Overflow e ) { } return minItem; }

37 1/6/2016Data Structure: Heaps37 Method deleteMin public Comparable deleteMin( ) {if( isEmpty( ) )return null; int minIndex = findMinIndex( ); Comparable minItem = theTrees[minIndex].element; BinomialNode deletedTree = theTrees[ minIndex ].child; BinomialQueue deletedQueue = new BinomialQueue( ); deletedQueue.currentSize =(1 << minIndex)-1;

38 Leftist Heaps

39 1/6/2016Data Structure: Heaps39 Null Path Length The null path length of the node X, npl(X), is the length of the shortest path from X to a node without 2 children. npl(X) = 1 + min (npl(Y 1 ), npl(Y 2 )), where Y 1 and Y 2 are children of X. 1 1 1 0 00 0 0 0

40 1/6/2016Data Structure: Heaps40 Leftist Trees A leftist tree is a binary tree such that for every node X in the tree npl(left)  npl(right), where left and right are left child and right child of X. 1 1 1 0 00 0 0 0

41 1/6/2016Data Structure: Heaps41 Examples of leftist trees 1 1 1 0 01 0 0 0 1 1 10 00 0 0 0 0 1 1 0 00 1 0 0 0

42 1/6/2016Data Structure: Heaps42 Leftist Heap A leftist heap is a leftist tree with heap order property. 4 5 6 21 1531 8 12 23 33

43 1/6/2016Data Structure: Heaps43 Leftist Tree Nodes class LeftHeapNode {LeftHeapNode( Comparable theElement ) {this( theElement, null, null );} LeftHeapNode( Comparable theElement, LeftHeapNode lt, LeftHeapNode rt ) {element = theElement; npl = 0; left = lt;right = rt;} Comparable element; int npl; LeftHeapNode left;LeftHeapNode right; }

44 1/6/2016Data Structure: Heaps44 Merge Leftist Heaps 4 5 6 21 1531 8 12 23 33

45 1/6/2016Data Structure: Heaps45 Merge Leftist Heaps 6 1531 8 12 23 33 8 12 23 15 8 12 23 15

46 1/6/2016Data Structure: Heaps46 Merge Leftist Heaps 4 5 21 6 31 33 8 12 23 15

47 1/6/2016Data Structure: Heaps47 Method merge1 private static LeftHeapNode merge1 ( LeftHeapNode h1, LeftHeapNode h2 ) {if( h1.left == null ) // Single node h1.left = h2; // Other fields in h1 OK else {h1.right = merge( h1.right, h2 ); if( h1.left.npl < h1.right.npl ) swapChildren( h1 ); h1.npl = h1.right.npl + 1; } return h1; }

48 1/6/2016Data Structure: Heaps48 Method merge public void merge( LeftistHeap rhs ) {if( this == rhs ) return; root = merge( root, rhs.root ); rhs.root = null; } private static LeftHeapNode merge( LeftHeapNode h1, LeftHeapNode h2 ) {if( h1 == null ) return h2; if( h2 == null ) return h1; if( h1.element.compareTo( h2.element ) < 0 ) return merge1( h1, h2 ); else return merge1( h2, h1 ); }

49 1/6/2016Data Structure: Heaps49 Methods insert, deleteMIn public void insert( Comparable x ) {root=merge(new LeftHeapNode(x),root); } public Comparable deleteMin( ) {if( isEmpty( ) )return null; Comparable minItem = root.element; root = merge( root.left, root.right ); return minItem; }

50 1/6/2016Data Structure: Heaps50 Applications Event simulation Merge sort

51 1/6/2016Data Structure: Heaps51 Event Simulation Events have the scheduled time to happen. Advancing time by clock ticks is too slow. We need to find the “next” event. So, events are put into heaps with time as the element. We choose the next event from the root of the heapp.

52 1/6/2016Data Structure: Heaps52 Heap Sort 5 12 34 25 23 30 51234232530

53 1/6/2016Data Structure: Heaps53 Heap Sort 12 25 34 30 23 5 1225342330 5

54 1/6/2016Data Structure: Heaps54 Heap Sort 23 25 34 12 30 5 2325343012 5

55 1/6/2016Data Structure: Heaps55 Heap Sort 25 34 23 12 30 5 2534233012 5

56 1/6/2016Data Structure: Heaps56 Heap Sort 34 30 23 12 25 5 3430232512 5

57 1/6/2016Data Structure: Heaps57


Download ppt "Heaps Priority Queues. 1/6/2016Data Structure: Heaps2 Outline Binary heaps Binomial queues Leftist heaps."

Similar presentations


Ads by Google