Heaps and Priority Queues Motivation Situations where one has to choose the next most important from a collection. Examples: patients in an emergency room, scheduling programs in a multi-tasking OS. Need to choose the task with the highest priority A priority queue orders elements by their importance. A queue is insufficient, need an efficient solution. A binary search tree is also insufficient, especially worst case. Data Structures 1 Heaps and Priority Queues
Complete Binary Trees(Review) Tree of d levels, in which all levels are filled, except possibly d 1. Bottom most level has nodes filled in from the left. A B C E F H I J K L Data Structures 2 Heaps and Priority Queues
Why a Complete Binary Tree for a Heap No Pointers! Can use an array representation for the tree. Number node positions in level order. Nodes are positioned in an array in level order. Node Determination: Parent(r) = (r 1)/2, if 0 < r < n Left child(r) = 2r + 1 if 2r + 1 < n Right child(r) = 2r + 2 if 2r + 2 < n Left sibling(r) = r 1 if r is even and 0 < r < n Right sibling(r) = r + 1 if r is odd and r + 1 < n Data Structures 3 Heaps and Priority Queues
Heap Data Type Complete binary tree with the Heap Property. Heap has partial ordering. Min-heap: All nodes have values less than child values. Max-heap: All nodes have values greater than child values. The root stores the smallest value in a min-heap, largest value in a max-heap No relationship between node and its sibling in heaps. Applications: Heapsort uses the max-heap, replacement selection algorithm using the min-heap. Data Structures 4 Heaps and Priority Queues
Heap Data Type 28 7 20 6 20 14 17 11 3 5 28 22 16 35 3 8 29 33 MAX Heap MIN Heap Data Structures 5 Heaps and Priority Queues
Heap Operations Build the heap May insert one element at a time If all elements are available, can re-heap Insert Remove Data Structures 6 Heaps and Priority Queues
Heap Ops:Build Heap Use principle of induction: make subtrees to be heaps prior to tackling their root. Root may sift down. Work from the middle of the array (high end to low end). 1 7 2 3 5 6 4 5 6 7 4 2 1 3 Exchanges: (5 2), (7 3), (7 1), (6 1) Data Structures 7 Heaps and Priority Queues
Heap Ops:Insert Insert element to last position of the array. Reheap using exchanges (move up towards root). 28 Insert 30 28 20 6 20 6 17 11 3 5 17 11 3 5 3 8 3 8 30 Data Structures 8 Heaps and Priority Queues
Heap Ops:Remove (Max) Root element removed. Bring last element to the root. Reheap 28 Delete 28 8 20 6 20 6 17 11 3 5 17 11 3 5 3 8 3 Data Structures 9 Heaps and Priority Queues
Heap Ops:Remove (at position p) Exchange element to be deleted with last element. Compare p to its parent and move up as needed, until the root is reached. Sift down from position p. Data Structures 10 Heaps and Priority Queues
Max Heap Implementation public class MaxHeap<E extends Comparable<? super E>> { private E [ ] Heap ; / / P o i n t e r to the heap array private i n t size ; / / Maximum size of the heap private i n t n ; / / Number of t h i n g s i n heap public MaxHeap(E [ ] h, i n t num, i n t max) { Heap = h ; n = num ; size = max ; buildheap ( ) ; / / Return c u r r e n t size of the heap public i n t heapsize ( ) { return n ; / / I s pos a l e a f p o s i t i o n? public boolean i s L e a f ( i n t pos ) { return ( pos >= n / 2 ) && ( pos < n ) ; Data Structures 11 Heaps and Priority Queues
Max Heap Implementation:Access Functions / / Return p o s i t i o n f o r l e f t c h i l d of pos public i n t l e f t c h i l d ( i n t pos ) { a s s e r t pos < n /2 : P o s i t i o n has no l e f t c h i l d ; return 2 pos + 1; / / Return p o s i t i o n f o r r i g h t c h i l d of pos public i n t r i g h t c h i l d ( i n t pos ) { a s s e r t pos < ( n 1)/2 : P o s i t i o n has no r i g h t c h i l d ; return 2 pos + 2; / / Return p o s i t i o n f o r parent public i n t parent ( i n t pos ) { a s s e r t pos > 0 : P o s i t i o n has no parent ; return ( pos 1)/2; / / Heapify contents of Heap / public void buildheap ( ) { for ( i n t i =n/2 1; i >=0; i ) s i f t d o w n ( i ) ; Data Structures 12 Heaps and Priority Queues
Max Heap Implementation:Insert / / I n s e r t i n t o heap public void i n s e r t (E v a l ) { a s s e r t n < size : Heap i s f u l l ; i n t c u r r = n++; Heap [ c u r r ] = v a l ; / / S t a r t at end of heap / / Now s i f t up u n t i l c u r r s parent s key > c u r r s key while ( ( c u r r!= 0) && ( Heap [ c u r r ]. compareto ( Heap [ parent ( c u r r ) ] ) > 0 ) ) { D S u t i l. swap ( Heap, curr, parent ( c u r r ) ) ; c u r r = parent ( c u r r ) ; Data Structures 13 Heaps and Priority Queues
Max Heap Implementation:siftdown / / Put element i n i t s c o r r e c t place / private void s i f t d o w n ( i n t pos ) { a s s e r t ( pos >= 0) && ( pos < n ) : I l l e g a l heap p o s i t i o n ; while (! i s L e a f ( pos ) ) { i n t j = l e f t c h i l d ( pos ) ; i f ( ( j <(n 1)) && ( Heap [ j ]. compareto ( Heap [ j + 1 ] ) < 0 ) ) j ++; / / j i s now index of c h i l d with g r e a t e r value i f ( Heap [ pos ]. compareto ( Heap [ j ] ) >= 0) return ; D S u t i l. swap ( Heap, pos, j ) ; pos = j ; / / Move down Data Structures 14 Heaps and Priority Queues
Max Heap Implementation:remove public E removemax ( ) { / / Remove maximum value a s s e r t n > 0 : Removing from empty heap ; D S u t i l. swap ( Heap, 0, n ) ; / / Swap maximum with l a s t va i f ( n!= 0) / / Not on l a s t element s i f t d o w n ( 0 ) ; / / Put new heap r o o t v a l i n c o r r e c t pl return Heap [ n ] ; / / Remove element at s p e c i f i e d p o s i t i o n public E remove ( i n t pos ) { a s s e r t ( pos >= 0) && ( pos < n ) : I l l e g a l heap p o s i t i o n D S u t i l. swap ( Heap, pos, n ) ; / / Swap with l a s t value / / I f we j u s t swapped i n a big value, push i t up while ( Heap [ pos ]. compareto ( Heap [ parent ( pos ) ] ) > 0) { D S u t i l. swap ( Heap, pos, parent ( pos ) ) ; pos = parent ( pos ) ; i f ( n!= 0) s i f t d o w n ( pos ) ; / / I f i t i s l i t t l e, push down return Heap [ n ] ; Data Structures 15 Heaps and Priority Queues
Cost of Heap Construction Level n 1 of the heap has half the number of nodes at level n. Nodes move down from each level of the tree; node at level i: (i 1) moves. log n (i 1) n 2 Θ(n) i i=1 Data Structures 16 Heaps and Priority Queues