Priority Queue (Heap)
description
Transcript of 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)
Insert Find Delete Find Min
Delete Min
List O(1) O(n) O(n) O(n) O(n)BST,AVL O(lg n) O(lg n) O(lg n) O(lg n) O(lg n)
Ordered List
O( n ) O( n ) O( n ) O( 1 ) O( 1 )
Hash O(1) O(1) O(1) - -Heap O(lg n) - - O(1) O(lg n)
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
A. “all value in nodes of left tree and right tree are more than the value of the node”
B. 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
4031
48 50 85 36
99 51 55 57 88
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
Binary Heap on Array26 40 31 48 50 85 36 107 48 55 57 88
26
4031
48 50 85 36
99 51 55 57 88
Identifying child
• Parent at heap[i]– Left child at heap[(i * 2)]– Right child at heap[(i * 2) + 1]
26
40 31
48 50 85 36
99 51 55 57 88
3
7 8
0
1 2
4 5 6
9 10 11
Identifying parent
• child at heap[i]– Parent at heap[(i – 1) / 2]
26
40 31
48 50 85 36
99 51 55 57 88
3
7 8
0
1 2
4 5 6
9 10 11
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 parentswap myself with my parent
Adding 30 (Bubble Up)
26
4031
48 50 85 36
99 51 55 57 88 30
Adding 30 (Bubble Up)
26
4031
48 50 30 36
99 51 55 57 88 85
SWAP!!
Adding 30 (Bubble Up)
26
4030
48 50 31 36
99 51 55 57 88 85
DONE!
SWAP!!
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 31
48 50 85 36
99 51 55 57 88
Headless tree
How to Delete
• Bring someone else as the head– Who?
• The easiest to moved one• The tail
40 31
48 50 85 36
99 51 55 57 88
Headless tree88
How to Delete
• Now, we fix B.– But A. fail
• Fix it
40 31
48 50 85 36
99 51 55 57
Headless tree88
Percolate Down (sieve down)
• To bring bigger item down• How?
While I am more than any of my childrenswap myself with “smaller” child
Percolate Up (Bubble Up)
• To bring smaller item up• How?
While I am less than my parentswap myself with my parent
See it in action
88
4031
48 50 85 36
99 51 55 57
See it in action
88
4031
48 50 85 36
99 51 55 57
More than?
Which is smaller?
See it in action
31
4088
48 50 85 36
99 51 55 57
SWAP!!
See it in action
31
4088
48 50 85 36
99 51 55 57
More than?
Which is smaller?
See it in action
88
4036
48 50 85 88
99 51 55 57Done!!
SWAP!!
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
Java Code of Heap
public class BinaryHeap<AnyType extends Comparable<? super AnyType>>{ public BinaryHeap( ) public BinaryHeap( int capacity ) public BinaryHeap( AnyType [ ] items )
public void insert( AnyType x )
public AnyType findMin( )
public AnyType deleteMin( )
private static final int DEFAULT_CAPACITY = 10; private int currentSize; private AnyType [ ] array;
private void percolateDown( int hole )}
/** * Construct the binary heap. * @param capacity the capacity of the binary heap. */ public BinaryHeap( int capacity ) { currentSize = 0; array = (AnyType[]) new Comparable[ capacity + 1 ]; }
Constructor
public AnyType findMin( ){ if( isEmpty( ) ) throw new UnderflowException( ); return array[ 1 ];}
FindMin
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;}
Insert & Percolate Up
Loop until root
Father is larger?
Delete Min
public AnyType deleteMin( ){ if( isEmpty( ) ) throw new UnderflowException( );
AnyType minItem = findMin( ); array[ 1 ] = array[ currentSize-- ]; percolateDown( 1 );
return minItem;}
Percolate Downprivate void percolateDown( int hole ){ int child; AnyType tmp = 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;}
We have two children?
The right one is smaller?
The smaller one is less than me?
Loop until the bottom
Main (testing)
public static void main( String [ ] args ) { int numItems = 10000; BinaryHeap<Integer> h = new BinaryHeap<Integer>( ); int i = 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
4031
48 50 85 36
99 51 55 57 88