Professional Documents
Culture Documents
Heaps: Priority Queues
Heaps: Priority Queues
Priority Queues
Outline
Leftist heaps
9/2/2013
Binary Heaps
15
21 16
DeleteMin
Find the minimal element Return it Remove it from the queue
9/2/2013
Binary Heaps
A binary heap is an implementation of a priority queue. A binary heap is a complete binary tree with heap order property.
9/2/2013
12 34 43
9/2/2013
23 25 56 40 35 31 30
33
40
36
45
7
A
5
1 3
B
4 6
C
7
D
8 9 10
E
11 12
A B C D E F G H
1
9/2/2013
I
9 10
J
11
K
12
L
13 14 15
8
3 4
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 }
9/2/2013 Data Structure: Heaps 9
percolateUp
5 12 15 21 16 3 22 23 27 25 36 19 48
9/2/2013
10
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; }
9/2/2013
11
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; }
9/2/2013 Data Structure: Heaps 12
PercolateDown
32
12
34 43 40
23 25
56 40 35
31
30
33
36 45
9/2/2013
13
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; } }
9/2/2013 Data Structure: Heaps 14
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 }
9/2/2013 Data Structure: Heaps 15
Insertion
5 12 15 21 16 22 28 23 27 25 10 19 48
9/2/2013
16
Method insert
public void insert( Comparable x ) throws Overflow { if( isFull( ) ) throw new Overflow( ); array[++currentSize]=x; percolateUp(currentSize); }
9/2/2013
17
DeleteMin
5 12 15 21 16 22 28 23 27 19 25 10 48
9/2/2013
18
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; }
9/2/2013 Data Structure: Heaps 19
Method buildHeap
private void buildHeap( ) { for( int i = currentSize / 2; i > 0; i-- ) percolateDown( i ); }
32 12 34 43
9/2/2013
23 25 56 40 31 33 36 45
20
40
35
30
Method decreaseKey
public void decreaseKey(int p, Comparable d) throws outOfRange { if( p>currentSize ) throw new outOfRange(); array[ p ] = array[ p ] - d; percolateUp( p ); }
9/2/2013
21
Method increaseKey
public void increaseKey(int p, Comparable d) throws outOfRange { if( p>currentSize ) throw new outOfRange(); array[ p ] = array[ p ] + d; percolateDown( p ); }
9/2/2013
22
Binomial Queues
Binomial Tree
A binomial tree is defined recursively as follow: A binomial tree of height 0, denoted by B0, is a onenode tree. A binomial tree of height k, denoted by Bk, is formed by attaching a binomial tree Bk-1 to the root of another binomial tree Bk-1.
9/2/2013
24
1 3 3 1 1 4 6 4 1 1 5 10 10 5 1 1 6 15 20 15 6 1
1
3
1
Data Structure: Heaps 25
Structure
A binomial queue is a collection of heapordered trees, each of which is a binomial tree.
6 3
13
16 32 40
26 28 35 45 43 51 46 50
21
9/2/2013
26
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; }
9/2/2013 Data Structure: Heaps 27
Child nextSibling
26
28 35 45
26 28 35 45 46 50
28
43 51 46
50
9/2/2013 Data Structure: Heaps
43 51
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; } }
9/2/2013 Data Structure: Heaps 29
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 26 } 28 35 45 28 35 45 43 51 46 43 51 46
9/2/2013
50
50
30
9/2/2013
31
9/2/2013
34
Method insert
public void insert( Comparable x ) throws Overflow { BinomialQueue oneItem= new BinomialQueue( ); oneItem.currentSize = 1; oneItem.theTrees[0] = new BinomialNode( x ); merge( oneItem ); }
9/2/2013
35
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;
9/2/2013
37
Leftist Heaps
9/2/2013
39
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
0
0 0 0
40
1
0
9/2/2013
1
0
1
1 0 0
0
1
0 0 0
0 0 0 0
1 0 1 0 0
41
Leftist Heap
A leftist heap is a leftist tree with heap order property.
4
5 6 31 15 33
9/2/2013 Data Structure: Heaps 42
8 21 23 12
LeftHeapNode( Comparable theElement, LeftHeapNode lt, LeftHeapNode rt ) { element = theElement; npl = 0; left = lt; right = rt; Comparable element; LeftHeapNode left; }
9/2/2013 Data Structure: Heaps
31
15 33
9/2/2013
44
12 33 12 15 23 23
9/2/2013
45
31
33 12 23
8 15
9/2/2013
46
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; }
9/2/2013 Data Structure: Heaps 47
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 ); }
9/2/2013 Data Structure: Heaps 48
Applications
Event simulation Merge sort
9/2/2013
50
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.
9/2/2013
51
Heap Sort
5 12 34 25 30 23
5 12 23 34 25 30
9/2/2013 Data Structure: Heaps 52
Heap Sort
12 25 34 30 5 23
12 25 23 34 30 5
9/2/2013 Data Structure: Heaps 53
Heap Sort
23 25 34 12 5 30
23 25 30 34 12 5
9/2/2013 Data Structure: Heaps 54
Heap Sort
25 34 23 12 5 30
25 34 30 23 12 5
9/2/2013 Data Structure: Heaps 55
Heap Sort
34 30 23 12 5 25
34 30 25 23 12 5
9/2/2013 Data Structure: Heaps 56
9/2/2013
57