1 / 43

Priority Queue (Heap)

Priority Queue (Heap). Nattee Niparnan. Flash Back. Still recall BST? and AVL? What about Hash? What they can do?. The need of another ADT. Queue with priority Hospital Bank Can’t be realized by a normal queue Previous ADTs are not efficient enough. Priority Queue (Heap ).

kalila
Download Presentation

Priority Queue (Heap)

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Priority Queue (Heap) NatteeNiparnan

  2. Flash Back • Still recall BST? and AVL? • What about Hash? • What they can do?

  3. The need of another ADT • Queue with priority • Hospital • Bank • Can’t be realized by a normal queue • Previous ADTs are not efficient enough

  4. Priority Queue (Heap) • Excel in “find min”, “delete min”

  5. What is Heap? • Another kind of trees • Which is • A binary tree (not BST) • (Almost) Complete

  6. Still recall a Binary Tree? • A Binary Tree is either • An empty node • A node with at most two children • The children are also a Binary Tree

  7. Specialized Binary Tree • BST • Add • “all value in nodes of left tree are less than the value of the node” • “all value in nodes of right tree are more than the value of the node” • AVL • Add • The same as BST • The height of the left subtree and the right subtree must not be more than 1

  8. Specialized Binary Tree • Heap • Add • “all value in nodes of left tree and right tree are more than the value of the node” • Tree is “complete binary tree”

  9. What is Complete Binary Tree • A binary tree which is • Completely filled • Except the bottom level which must be filled from left to right

  10. What is Complete Binary Tree • A binary tree which is • Completely filled • Except the bottom level which must be filled from left to right

  11. Example of a Binary Heap 26 40 31 48 50 85 36 99 51 57 88 55

  12. Why we need such properties? • A.  because we need to find minimal • Helps us in the process • B. Also helps us in the process • But also allow us to store a tree on an array • Easy to identify children and parent

  13. 26 40 31 48 50 85 36 107 48 55 57 88 Binary Heap on Array 26 40 31 48 50 85 36 55 99 51 57 88

  14. Identifying child • Parent at heap[i] • Left child at heap[(i * 2)] • Right child at heap[(i * 2) + 1] 26 0 40 31 1 2 48 3 50 4 85 36 5 6 7 8 9 10 11 55 99 51 57 88

  15. Identifying parent • child at heap[i] • Parent at heap[(i – 1) / 2] 26 0 40 31 1 2 48 3 50 4 85 36 5 6 7 8 9 10 11 55 99 51 57 88

  16. Operation on Heap • FindMin • Simple, it is at the top • Insert • DeleteMin

  17. How to insert? • Where to put? • Have to maintain the property of the heap • Do we know the exact position? • Solution • Add to the bottom • Preserve B. but A. might fails • Fix it!!!

  18. Percolate Up (Bubble Up) • To bring smaller item up • How? While I am less than my parent swap myself with my parent

  19. Adding 30 (Bubble Up) 26 40 31 48 50 85 36 99 51 55 57 88 30

  20. Adding 30 (Bubble Up) 26 40 31 48 50 30 36 SWAP!! 99 51 55 57 88 85

  21. Adding 30 (Bubble Up) 26 DONE! 40 30 SWAP!! 48 50 31 36 99 51 55 57 88 85

  22. What is the Big O of bubble Up? • O (lg n) • But… assume that the value to be add more than the median of all value • The “bigger” half must be the leave of the tree • So, we usually bubble up only one time.

  23. How to Delete • We know where to delete • At the root • But…. What happen after that? • Fix it!! 40 Headless tree 31 48 50 85 36 99 51 55 57 88

  24. How to Delete • Bring someone else as the head • Who? • The easiest to moved one • The tail Headless tree 88 40 31 48 50 85 36 99 51 55 57 88

  25. How to Delete • Now, we fix B. • But A. fail • Fix it Headless tree 88 40 31 48 50 85 36 99 51 55 57

  26. Percolate Down (sieve down) • To bring bigger item down • How? While I am more than any of my children swap myself with “smaller” child

  27. Percolate Up (Bubble Up) • To bring smaller item up • How? While I am less than my parent swap myself with my parent

  28. See it in action 88 40 31 48 50 85 36 99 51 55 57

  29. See it in action More than? 88 40 Which is smaller? 31 48 50 85 36 99 51 55 57

  30. See it in action 31 SWAP!! 40 88 48 50 85 36 99 51 55 57

  31. See it in action 31 More than? 40 88 48 50 85 36 Which is smaller? 99 51 55 57

  32. See it in action 88 40 36 SWAP!! 48 50 85 88 Done!! 99 51 55 57

  33. What is the Big O of sieve down? • O (lg n) • Unlike the bubble up case • The value we put at the top is usually the bigger one • Hence, most of the time, we will have to actually do the O(lg n)

  34. That’s it • Now, we look at the code

  35. Java Code of Heap

  36. public class BinaryHeap<AnyType extends Comparable<? super AnyType>> { public BinaryHeap( ) public BinaryHeap( int capacity ) public BinaryHeap( AnyType [ ] items ) public void insert( AnyType x ) public AnyTypefindMin( ) public AnyTypedeleteMin( ) private static final int DEFAULT_CAPACITY = 10; private intcurrentSize; private AnyType [ ] array; private void percolateDown( int hole ) }

  37. Constructor /** * Construct the binary heap. * @param capacity the capacity of the binary heap. */ public BinaryHeap( int capacity ) { currentSize = 0; array = (AnyType[]) new Comparable[ capacity + 1 ]; }

  38. FindMin public AnyTypefindMin( ) { if( isEmpty( ) ) throw new UnderflowException( ); return array[ 1 ]; }

  39. Insert & Percolate Up public void insert( AnyType x ) { if( currentSize == array.length - 1 ) enlargeArray( array.length * 2 + 1 ); // Percolate up int hole = ++currentSize; for( ; hole > 1 && x.compareTo( array[ hole / 2 ] ) < 0; hole /= 2 ) array[ hole ] = array[ hole / 2 ]; array[ hole ] = x; } Father is larger? Loop until root

  40. Delete Min public AnyTypedeleteMin( ) { if( isEmpty( ) ) throw new UnderflowException( ); AnyTypeminItem = findMin( ); array[ 1 ] = array[ currentSize-- ]; percolateDown( 1 ); return minItem; }

  41. Percolate Down private void percolateDown( int hole ) { int child; AnyTypetmp = array[ hole ]; For( ; hole * 2 <= currentSize; hole = child ) { child = hole * 2; if( child != currentSize && array[ child + 1 ].compareTo( array[ child ] ) < 0 ) child++; if( array[ child ].compareTo( tmp ) < 0 ) array[ hole ] = array[ child ]; else break; } array[ hole ] = tmp; } Loop until the bottom We have two children? The right one is smaller? The smaller one is less than me?

  42. Main (testing) public static void main( String [ ] args ) { intnumItems = 10000; BinaryHeap<Integer> h = new BinaryHeap<Integer>( ); inti = 37; for( i = 37; i != 0; i = ( i + 37 ) % numItems ) h.insert( i ); for( i = 1; i < numItems; i++ ) if( h.deleteMin( ) != i ) System.out.println( "Oops! " + i ); }

  43. 26 40 31 48 50 85 36 99 51 55 57 88

More Related