430 likes | 632 Views
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 ).
E N D
Priority Queue (Heap) NatteeNiparnan
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) • Excel in “find min”, “delete min”
What is Heap? • Another kind of trees • Which is • A binary tree (not BST) • (Almost) Complete
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
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
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”
What is Complete Binary Tree • A binary tree which is • Completely filled • Except the bottom level which must be filled from left to right
What is Complete Binary Tree • A binary tree which is • Completely filled • Except the bottom level which must be filled from left to right
Example of a Binary Heap 26 40 31 48 50 85 36 99 51 57 88 55
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
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
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
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
Operation on Heap • FindMin • Simple, it is at the top • Insert • DeleteMin
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!!!
Percolate Up (Bubble Up) • To bring smaller item up • How? While I am less than my parent swap myself with my parent
Adding 30 (Bubble Up) 26 40 31 48 50 85 36 99 51 55 57 88 30
Adding 30 (Bubble Up) 26 40 31 48 50 30 36 SWAP!! 99 51 55 57 88 85
Adding 30 (Bubble Up) 26 DONE! 40 30 SWAP!! 48 50 31 36 99 51 55 57 88 85
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.
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
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
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
Percolate Down (sieve down) • To bring bigger item down • How? While I am more than any of my children swap myself with “smaller” child
Percolate Up (Bubble Up) • To bring smaller item up • How? While I am less than my parent swap myself with my parent
See it in action 88 40 31 48 50 85 36 99 51 55 57
See it in action More than? 88 40 Which is smaller? 31 48 50 85 36 99 51 55 57
See it in action 31 SWAP!! 40 88 48 50 85 36 99 51 55 57
See it in action 31 More than? 40 88 48 50 85 36 Which is smaller? 99 51 55 57
See it in action 88 40 36 SWAP!! 48 50 85 88 Done!! 99 51 55 57
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)
That’s it • Now, we look at the code
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 ) }
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 ]; }
FindMin public AnyTypefindMin( ) { if( isEmpty( ) ) throw new UnderflowException( ); return array[ 1 ]; }
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
Delete Min public AnyTypedeleteMin( ) { if( isEmpty( ) ) throw new UnderflowException( ); AnyTypeminItem = findMin( ); array[ 1 ] = array[ currentSize-- ]; percolateDown( 1 ); return minItem; }
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?
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 ); }
26 40 31 48 50 85 36 99 51 55 57 88