Heaps Priority Queues Outline Binary heaps Binomial queues
Heaps Priority Queues
Outline Binary heaps Binomial queues Leftist heaps 10/21/2021 Data Structure: Heaps 2
Binary Heaps
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 21 16 19 22 28 25 23 48 27 The smallest value is in the root. 10/21/2021 Data Structure: Heaps 4
Operations on priority queues • Insert – Put an element into the queue • Delete. Min – Find the minimal element – Return it – Remove it from the queue 10/21/2021 Data Structure: Heaps 5
Binary Heaps • A binary heap is an implementation of a priority queue. • A binary heap is a complete binary tree with heap order property. 10/21/2021 Data Structure: Heaps 6
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. Height is in log n , 32 2 12 34 43 10/21/2021 40 where n is the number of nodes. 23 25 56 31 40 35 30 33 36 Data Structure: Heaps 45 7
Array implementation of complete binary tree 1 A 2 3 B C 4 5 6 7 D E F G 8 9 H I 11 12 10 L K J A B C D E F G H 1 15 2 10/21/2021 3 4 5 6 7 I 8 J 9 Data Structure: Heaps K 10 11 L 12 13 14 8
Class Binary. Heap public class Binary. Heap { public Binary. Heap( ) { this( DEFAULT_CAPACITY ); } public Binary. Heap( int capacity ) { current. Size = 0; array = new Comparable[ capacity + 1 ]; } … private static final int DEFAULT_CAPACITY = 100; private int current. Size; // Number of elements in heap private Comparable [ ] array; // The heap array } 10/21/2021 Data Structure: Heaps 9
percolate. Up 5 12 19 22 15 21 10/21/2021 16 3 25 23 27 48 36 Data Structure: Heaps 10
Method percolate. Up private void percolate. Up( int hole ) { Comparable x = array[hole]; while (hole > 1 && x. compare. To( array[ hole / 2 ] ) < 0) { array[ hole ] = array[ hole/2 ]; hole = hole/2; } array[ hole ] = x; } 10/21/2021 Data Structure: Heaps 11
Method percolate. Up private void percolate. Up( int hole ) { while (hole>1 && array[hole]. compare. To( array[ hole/2 ])<0) { swap(hole, hole/2); hole = hole/2; } } private void swap( int p 1, int p 2 ) { Comparable x = array[p 1]; array[p 1] = array[p 2]; array[p 2] = x; } 10/21/2021 Data Structure: Heaps 12
Percolate. Down 32 12 34 43 10/21/2021 40 23 25 56 31 40 35 30 Data Structure: Heaps 33 36 45 13
Method percolate. Down private void percolate. Down( int hole ) { int child; while( hole * 2 <= current. Size) { child = hole * 2; if(child != current. Size&&array[ child+1]. compare. To( array[child ])<0) child++; // choose the smaller child if( array[ child ]. compare. To( array[ hole ] ) < 0 ) swap( hole, child ); else break; hole = child; } } 10/21/2021 Data Structure: Heaps 14
Method percolate. Down private void percolate. Down( int hole ) { int child; Comparable tmp = array[ hole ]; // save the value of the node while ( hole * 2 <= current. Size ) { child = hole * 2; if(child != current. Size&&array[ child+1]. compare. To( array[child ])<0) child++; // choose the smaller child if( array[ child ]. compare. To( 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 } 10/21/2021 Data Structure: Heaps 15
Insertion 5 12 15 21 10/21/2021 16 19 22 28 25 23 27 48 10 Data Structure: Heaps 16
Method insert public void insert( Comparable x ) throws Overflow { if( is. Full( ) ) throw new Overflow( ); array[++current. Size]=x; percolate. Up(current. Size); } 10/21/2021 Data Structure: Heaps 17
Delete. Min 5 12 15 21 10/21/2021 16 10 22 28 19 23 27 48 25 Data Structure: Heaps 18
Method delete. Min public Comparable find. Min( ) { if( is. Empty( ) ) return null; return array[ 1 ]; } public Comparable delete. Min( ) { if( is. Empty( ) ) return null; Comparable min. Item = find. Min( ); array[ 1 ] = array[ current. Size--]; percolate. Down( 1 ); return min. Item; } 10/21/2021 Data Structure: Heaps 19
Method build. Heap private void build. Heap( ) { for( int i = current. Size / 2; i > 0; i-- ) percolate. Down( i ); } 32 12 34 43 10/21/2021 40 23 25 56 31 40 35 30 Data Structure: Heaps 33 36 45 20
Method decrease. Key public void decrease. Key(int p, Comparable d) throws out. Of. Range { if( p>current. Size ) throw new out. Of. Range(); array[ p ] = array[ p ] - d; percolate. Up( p ); } 10/21/2021 Data Structure: Heaps 21
Method increase. Key public void increase. Key(int p, Comparable d) throws out. Of. Range { if( p>current. Size ) throw new out. Of. Range(); array[ p ] = array[ p ] + d; percolate. Down( p ); } 10/21/2021 Data Structure: Heaps 22
Binomial Queues
Binomial Tree A binomial tree is defined recursively as follow: • A binomial tree of height 0, denoted by B 0, 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. 10/21/2021 Data Structure: Heaps 24
Property of Binomial Trees • A binomial tree of height k has 2 k nodes. • The number of nodes at depth d is the 1 binomial coefficient k 1 1 d. 1 2 1 1 1 2 1 4 1 1 6 3 4 3 10/21/2021 1 3 3 1 1 4 6 4 1 1 5 10 10 5 1 1 6 15 20 15 6 1 1 1 Data Structure: Heaps 25
Structure • A binomial queue is a collection of heapordered trees, each of which is a binomial tree. 6 3 21 26 13 28 35 45 16 32 40 43 51 46 50 10/21/2021 Data Structure: Heaps 26
Binomial Nodes class Binomial. Node { Binomial. Node( Comparable the. Element ) { this( the. Element, null ); } Binomial. Node( Comparable the. Element, Binomial. Node lt, Binomial. Node nt ) { element = the. Element; Child = lt; next. Sibling = nt; } Comparable element; Binomial. Node Child; Binomial. Node next. Sibling; } 10/21/2021 Data Structure: Heaps 27
Examples: binomial nodes 13 13 16 32 16 Child 32 40 40 next. Sibling 26 26 28 28 35 45 45 43 51 46 50 10/21/2021 35 Data Structure: Heaps 46 50 28
Binomial Queues public class Binomial. Queue { public Binomial. Queue( ) { the. Trees = new Binomial. Node[ MAX_TREES ]; make. Empty( ); }. . . public void make. Empty( ) { current. Size = 0; for( int i=0; i < the. Trees. length; i++ ) the. Trees[ i ] = null; } private static final int MAX_TREES = 14; private int current. Size; private Binomial. Node [ ] the. Trees; private int capacity( ) { return 2*the. Trees. length - 1; } } 10/21/2021 Data Structure: Heaps 29
Method combine. Trees private static Binomial. Node combine. Trees ( Binomial. Node t 1, Binomial. Node t 2 ) { if( t 1. element. compare. To( t 2. element ) > 0 ) return combine. Trees( t 2, t 1 ); t 2. next. Sibling = t 1. Child; t 1. left. Child = t 2; return t 1; 26 26 } 28 35 45 43 51 46 10/21/2021 50 Data Structure: Heaps 50 30
Method merge (1) public void merge( Binomial. Queue rhs ) throws Overflow { if( this == rhs ) return; if( current. Size+rhs. current. Size>capacity() ) throw new Overflow( ); current. Size += rhs. current. Size; Binomial. Node carry = null; for( int i=0, j=1; j<=current. Size; i++, j*=2 ) { Binomial. Node t 1 = the. Trees[ i ]; Binomial. Node t 2 = rhs. the. Trees[ i ]; 10/21/2021 Data Structure: Heaps 31
Method merge (2) // No trees if (t 1==null && t 2==null && carry==null) {} // Only this if (t 1!=null && t 2==null && carry==null) {} // Only rhs if (t 1==null && t 2!=null && carry==null) { the. Trees[i] = t 2; rhs. the. Trees[i] = null; } // Only carry if (t 1==null && t 2==null && carry!=null) { the. Trees[ i ] = carry; carry = null; } // this & rhs if (t 1!=null && t 2==null && carry!=null) { carry = combine. Trees( t 1, t 2 ); the. Trees[i]=rhs. the. Trees[i]=null; } 10/21/2021 Data Structure: Heaps 32
Method merge (3) // this and carry if (t 1!=null && t 2==null && { carry = combine. Trees( t 1, the. Trees[ i ] = null; // rhs and carry if (t 1==null && t 2!=null && { carry = combine. Trees( t 2, rhs. the. Trees[ i ] = null; // All three if (t 1!=null && t 2!=null && { the. Trees[ i ] = carry; carry = combine. Trees( t 1, rhs. the. Trees[ i ] = null; } 10/21/2021 carry!=null) carry ); } carry!=null) t 2 ); Data Structure: Heaps } 33
Method merge (4) for( int k=0; k < rhs. the. Trees. length; k++ ) rhs. the. Trees[ k ] = null; rhs. current. Size = 0; } 10/21/2021 Data Structure: Heaps 34
Method insert public void insert( Comparable x ) throws Overflow { Binomial. Queue one. Item= new Binomial. Queue( ); one. Item. current. Size = 1; one. Item. the. Trees[0] = new Binomial. Node( x ); merge( one. Item ); } 10/21/2021 Data Structure: Heaps 35
Method delete. Min (2) for( int j = min. Index - 1; j >= 0; j-- ) { deleted. Queue. the. Trees[ j ] = deleted. Tree; deleted. Tree = deleted. Tree. next. Sibling; deleted. Queue. the. Trees[ j ]. next. Sibling = null; } the. Trees[ min. Index ] = null; current. Size -= deleted. Queue. current. Size + 1; try { merge( deleted. Queue ); } catch( Overflow e ) { } return min. Item; } 10/21/2021 Data Structure: Heaps 36
Method delete. Min public Comparable delete. Min( ) { if( is. Empty( ) ) return null; int min. Index = find. Min. Index( ); Comparable min. Item = the. Trees[min. Index]. element; Binomial. Node deleted. Tree = the. Trees[ min. Index ]. child; Binomial. Queue deleted. Queue = new Binomial. Queue( ); deleted. Queue. current. Size =(1 << min. Index)-1; 10/21/2021 Data Structure: Heaps 37
Leftist Heaps
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. 10/21/2021 Data Structure: Heaps 1 1 0 0 0 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 10/21/2021 0 0 Data Structure: Heaps 40
Examples of leftist trees 1 1 1 0 0 0 0 1 0 Data Structure: Heaps 0 0 0 10/21/2021 1 0 41
Leftist Heap • A leftist heap is a leftist tree with heap order property. 4 5 21 6 31 8 15 12 23 33 10/21/2021 Data Structure: Heaps 42
Leftist Tree Nodes class Left. Heap. Node { Left. Heap. Node( Comparable the. Element ) { this( the. Element, null ); Left. Heap. Node( Comparable the. Element, Left. Heap. Node lt, Left. Heap. Node rt ) { element = the. Element; npl = 0; left = lt; right = rt; } } Comparable element; int npl; Left. Heap. Node left; Left. Heap. Node right; } 10/21/2021 Data Structure: Heaps 43
Merge Leftist Heaps 4 6 5 8 21 12 31 15 33 23 10/21/2021 Data Structure: Heaps 44
Merge Leftist Heaps 8 6 8 31 10/21/2021 12 15 8 12 33 12 15 23 23 15 23 Data Structure: Heaps 45
Merge Leftist Heaps 4 6 5 21 8 31 33 12 15 23 10/21/2021 Data Structure: Heaps 46
Method merge 1 private static Left. Heap. Node merge 1 ( Left. Heap. Node h 1, Left. Heap. Node h 2 ) { if( h 1. left == null ) // Single node h 1. left = h 2; // Other fields in h 1 OK else { h 1. right = merge( h 1. right, h 2 ); if( h 1. left. npl < h 1. right. npl ) swap. Children( h 1 ); h 1. npl = h 1. right. npl + 1; } return h 1; } 10/21/2021 Data Structure: Heaps 47
Method merge public void merge( Leftist. Heap rhs ) { if( this == rhs ) return; root = merge( root, rhs. root ); rhs. root = null; } private static Left. Heap. Node merge( Left. Heap. Node h 1, Left. Heap. Node h 2 ) { if( h 1 == null ) return h 2; if( h 2 == null ) return h 1; if( h 1. element. compare. To( h 2. element ) < 0 ) return merge 1( h 1, h 2 ); else return merge 1( h 2, h 1 ); } 10/21/2021 Data Structure: Heaps 48
Methods insert, delete. MIn public void insert( Comparable x ) { root=merge(new Left. Heap. Node(x), root); } public Comparable delete. Min( ) { if( is. Empty( ) ) return null; Comparable min. Item = root. element; root = merge( root. left, root. right ); return min. Item; } 10/21/2021 Data Structure: Heaps 49
Applications • Event simulation • Merge sort 10/21/2021 Data Structure: Heaps 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. 10/21/2021 Data Structure: Heaps 51
Heap Sort 5 12 34 23 25 30 5 12 23 34 25 30 10/21/2021 Data Structure: Heaps 52
Heap Sort 12 25 34 23 30 5 12 25 23 34 30 5 10/21/2021 Data Structure: Heaps 53
Heap Sort 23 25 34 30 12 5 23 25 30 34 12 5 10/21/2021 Data Structure: Heaps 54
Heap Sort 25 34 23 30 12 5 25 34 30 23 12 5 10/21/2021 Data Structure: Heaps 55
Heap Sort 34 30 23 25 12 5 34 30 25 23 12 5 10/21/2021 Data Structure: Heaps 56
10/21/2021 Data Structure: Heaps 57
- Slides: 57