CMSC 341 Data Structures Priority Queues Binary Heaps
CMSC 341: Data Structures Priority Queues – Binary Heaps 1
Recall Queues • FIFO: First-In, First-Out • Some contexts where this seems right? • Some contexts where some things should be allowed to skip ahead in the line? 2
Queues that Allow Line Jumping • Need a new ADT • Operations: Insert an Item, Remove the “Best” Item insert 6 15 2 23 18 12 45 3 delete. Min 7 3
Priority Queue ADT 1. PQueue data : collection of data with priority 2. PQueue operations – insert – delete. Min 3. PQueue property: for two elements in the queue, x and y, if x has a lower priority value than y, x will be deleted before y 4
Applications of the Priority Queue • Select print jobs in order of decreasing length • Forward packets on routers in order of urgency • Select most frequent symbols for compression • Sort numbers, picking minimum first • Anything greedy 5
Potential Implementations insert delete. Min Unsorted list (Array) O(1) O(n) Unsorted list (Linked-List) O(1) O(n) Sorted list (Array) O(n) O(1)* Sorted list (Linked-List) O(n) O(1) 6
Recall From Lists, Queues, Stacks • Use an ADT that corresponds to your needs • The right ADT is efficient, while an overly general ADT provides functionality you aren’t using, but are paying for anyways • Heaps provide O(log n) worst case for both insert and delete. Min, O(1) average insert 7
Binary Heap Properties 1. Structure Property 2. Ordering Property 8
Brief interlude: Some Definitions: A Perfect binary tree – A binary tree with all leaf nodes at the same depth. All internal nodes have 2 children. height h 2 h+1 – 1 nodes 2 h – 1 non-leaves 2 h leaves 11 5 21 2 1 16 9 3 7 10 13 25 19 22 30 9
Heap Structure Property • A binary heap is a complete binary tree. Complete binary tree – binary tree that is completely filled, with the possible exception of the bottom level, which is filled left to right. Examples: 10
Representing Complete Binary Trees in an Array 1 2 4 8 H From node i: 3 B 5 D 9 A 10 I 6 E J 11 K 12 F C 7 G L left child: right child: parent: implicit (array) implementation: 0 A B C D E F G H I J K L 1 2 3 4 5 6 7 8 9 10 11 12 13 11
Why this approach to storage? 12
Heap Order Property Heap order property: For every non-root node X, the value in the parent of X is less than the value in X. 10 10 20 30 20 80 15 40 50 80 60 85 99 700 not a heap 13
Heap Operations • find. Min: • insert(val): percolate up. • delete. Min: percolate down. 10 20 40 50 700 80 60 85 99 65 14
Heap – Insert(val) Basic Idea: 1. Put val at “next” leaf position 2. Percolate up by repeatedly exchanging node until no longer needed 15
Inserting a Value 4 12 5 26 29 i 0 1 array __ 4 25 45 2 35 3 4 14 31 5 6 21 7 15 3 8 insert here to keep tree complete 9 10 11 12 13 14 15 5 12 26 25 14 15 29 45 35 31 21 currentsize = 13 Insert 3 3 __ __
Inserting a Value 4 12 5 26 29 25 45 35 14 31 15 21 save new value in a temporary location: tmp 3 Insert 3
Inserting a Value 4 12 5 26 29 25 45 35 14 31 21 15 14 tmp copy 14 down because 14 > 3 3 Insert 3
Inserting a Value 4 12 5 26 29 25 45 35 12 31 21 15 14 tmp copy 12 down because 12 > 3 3 Insert 3
Inserting a Value 4 4 5 26 29 25 45 35 12 31 21 15 14 tmp copy 4 down because 4 > 3 3 Insert 3
Inserting a Value 3 insert 3 4 5 26 29 25 45 35 12 31 21 15 14 Insert 3
Heap After Insert 3 4 5 26 29 25 45 35 12 31 21 15 14
Deleting a Value (note new tree!) 3 7 5 26 29 25 45 35 12 31 21 15 14 Delete 3
Deleting a Value 7 5 26 29 25 45 35 12 31 21 15 14 save root value … tmp 3 Delete 3
Deleting a Value copy value of last node in complete tree into temporary location; 14 decrement currentsize 7 5 26 29 25 45 35 12 31 21 15 X 14 tmp 3 Delete 3
Deleting a Value 14 push down root … 5 compare children select smaller 26 29 25 45 35 7 12 31 15 21 tmp 3 Delete 3
Deleting a Value 5 copy smaller value into parent 26 29 25 45 35 7 12 31 14 push down root … 15 21 tmp 3 Delete 3
Deleting a Value 5 14 push down root … 7 26 29 25 45 35 compare children select smaller (25) 12 31 15 21 tmp 3 Delete 3
Deleting a Value 5 push down root … 7 14 26 29 25 45 35 12 31 copy 14 into parent because 14 < smaller child 15 21 tmp 3 Delete 3
Deleting a Value 5 7 14 26 29 25 45 35 12 31 15 21 return 3 Delete 3
Heap – Deletemin Basic Idea: 1. Remove root (that is always the min!) 2. Put “last” leaf node at root 3. Find smallest child of node 4. Swap node with its smallest child if needed. 5. Repeat steps 3 & 4 until no swaps needed. 31
Insert: 16, 32, 4, 69, 105, 43, 2 0 1 2 3 4 5 6 7 8 32
Building a Heap 12 5 11 3 10 6 9 4 8 1 7 2 33
Building a Heap • Adding the items one at a time is O(n log n) in the worst case • Can we do this in O(n) 34
Working on Heaps • What are the two properties of a heap? – Structure Property – Order Property • How do we work on heaps? – Fix the structure – Fix the order 35
Build. Heap: Floyd’s Method 12 5 11 3 10 6 9 4 8 1 7 2 Add elements arbitrarily to form a complete tree. Pretend it’s a heap and fix the heap-order property! 12 5 11 3 4 10 8 1 6 7 2 9 36
Buildheap pseudocode private void build. Heap() { for ( int i = current. Size/2; i > 0; i-- ) percolate. Down( i ); } runtime: 37
Build. Heap: Floyd’s Method 12 5 11 3 4 10 8 1 6 7 9 2 38
Build. Heap: Floyd’s Method 12 5 11 3 4 10 8 1 2 7 9 6 39
Build. Heap: Floyd’s Method 12 12 5 11 3 4 10 8 1 2 7 6 5 9 3 4 11 1 8 10 7 2 9 6 40
Build. Heap: Floyd’s Method 12 12 5 11 3 4 10 8 1 2 7 5 9 3 6 4 11 1 8 10 7 2 9 6 12 5 3 4 2 1 8 10 7 6 11 9 41
Build. Heap: Floyd’s Method 12 12 5 11 3 4 10 8 1 2 7 5 9 11 3 6 4 1 8 2 10 7 6 12 12 5 3 4 2 1 8 10 7 6 11 9 3 4 2 5 8 10 7 6 11 9 42
Finally… 1 3 2 4 5 12 8 10 7 6 9 11 runtime: 43
Running time for this? 1 3 2 4 5 12 8 10 7 6 9 11 runtime: 44
Can you implement this percolate method 45
Heap Sort • Given n values we can sort them in place in O(n log n) time – Insert values into array -- O(n) – heapify -- O(n) – repeatedly delete min -- O(lg n), n times • Using a min heap, this code sorts in reverse (high down to low) order. • With a max heap, it sorts in normal (low up to high) order. • Given an unsorted array A[ ] of size n for (i = n-1; i >= 1; i--) { x = find. Min( ); delete. Min( ); A[i+1] = x; }
- Slides: 46