CSE 214 Computer Science II Heaps and Priority Queues Spring 2018 Stony Brook University Instructor: Shebuti Rayana shebuti.rayana@stonybrook.edu http://www3.cs.stonybrook.edu/~cse214/sec02/
Introduction to Heaps In last few lectures you have learned about binary trees and binary search trees (BST) BST is a certain type of binary tree where the nodes in the left subtree have values less than the root and the nodes in the right subtree have values greater than the root. No duplicates are allowed in a BST Heap or Binary Heap is a certain kind of Complete Binary Tree Shebuti Rayana (CS, Stony Brook University) 2
Heaps Root A heap is a certain kind of complete binary tree. When a complete binary tree is built, its first node must be the root.
Heaps Complete binary tree. Left child of the root The second node is always the left child of the root.
Heaps Complete binary tree. Right child of the root The third node is always the right child of the root.
Heaps Complete binary tree. The next nodes always fill the next level from left-to-right.
Heaps Complete binary tree. The next nodes always fill the next level from left-to-right.
Heaps Complete binary tree. The next nodes always fill the next level from left-to-right.
Heaps Complete binary tree. The next nodes always fill the next level from left-to-right.
Heaps Complete binary tree.
Heap Property A Binary Heap is a complete binary tree which satisfies the heap ordering property. The ordering can be one of two types: the min-heap property: the value of each node is greater than or equal to the value of its parent, with the minimum-value element at the root. the max-heap property: the value of each node is less than or equal to the value of its parent, with the maximum-value element at the root. 39 95 53 72 61 83 Min-heap 61 95 83 53 39 72 Max-heap 11
Is this a Max-heap? 95 61 83 53 39 72 16 24 48 NOT Complete Shebuti Rayana (CS, Stony Brook University) 12
Is this a Max-heap? 95 NO 53 83 61 39 72 16 24 48 Shebuti Rayana (CS, Stony Brook University) 13
Is this a Max-heap? 95 61 83 53 39 72 16 24 48 YES Shebuti Rayana (CS, Stony Brook University) 14
Characteristics of Heap In a heap the highest (or lowest) priority element is always stored at the root, hence the name "heap". A heap is not a sorted structure and can be regarded as partially ordered. As you see from the picture, there is no particular relationship among nodes on any given level, even among the siblings. Min-heap Max-heap Since a heap is a complete binary tree, it has a smallest possible height - a heap with N nodes always has O(log N) height. A heap is useful data structure when you need to remove the object with the highest (or lowest) priority. A common use of a heap is to implement a priority queue. 15
Storage of a Heap A complete binary tree can be uniquely represented by storing its level order traversal in an array. As heap (Min or Max) is a complete binary tree, we can use an array to hold the data of the heap. Store the root in position 0. For any node in position i, its left child (if any) is in position 2i + 1 its right child (if any) is in position 2i + 2 its parent (if any) is in position (i-1)/2 (integer division) Shebuti Rayana (CS, Stony Brook University) 16
Storage of a Max-heap 95 61 83 Left child: 2i+1 Right child: 2i+2 Parent: (i-1)/2 53 39 72 16 24 48 0 1 2 3 4 5 6 7 8 95 61 83 53 39 72 16 24 48 Shebuti Rayana (CS, Stony Brook University) 17
Important point for Implementation The links between the tree's nodes are not actually stored as reference, or in any other way. The only way we "know" that "the array is a tree" is from the way we manipulate the data. Shebuti Rayana (CS, Stony Brook University) 18
Basic Operations on Heaps Create an empty heap (constructor). Insert a data element into a heap (Min or Max). Delete the maximum data element from the Maxheap. Delete the minimum data element from the Minheap Shebuti Rayana (CS, Stony Brook University) 19
Array Implementation of Heap public class Heap { private int[] data; /*storage array*/ private int heapsize; /*current size*/ private int maxsize; /*capacity*/ public Heap(int maximumsize) { /*constructor*/ if (maximumsize < 1) maxsize = 100; else maxsize = maximumsize; data = new int[maxsize]; heapsize = 0; } public boolean isempty(){ if(heapsize == 0)return true; else return false; } public boolean isfull(){ if(heapsize == maxsize) return true; else return false; } } Shebuti Rayana (CS, Stony Brook University) 20
Inserting new element in Max-heap Place the new element in the first available position in the array. Compare the new element with its parent. If the new element is greater, then swap it with its parent. Continue this process until either the new element s parent is greater than or equal to the new element, or the new element reaches the root Shebuti Rayana (CS, Stony Brook University) 21
Inserting into a Max-heap 95 Insert 38 61 83 53 39 72 16 24 48 38 Does it hold the max-heap property? YES Insert element to the first available position Shebuti Rayana (CS, Stony Brook University) 22
Inserting into a Max-heap (cont.) 95 Insert 70 61 83 53 39 72 16 24 48 70 Does it hold the max-heap property? NO Insert element to the first available position Shebuti Rayana (CS, Stony Brook University) 23
Inserting into a Max-heap (cont.) 95 Insert 70 61 83 53 70 72 16 24 48 39 Shebuti Rayana (CS, Stony Brook University) 24
Inserting into a Max-heap (cont.) 95 Insert 70 70 83 53 61 72 16 24 48 39 Does it hold the max-heap property now? YES Shebuti Rayana (CS, Stony Brook University) 25
Inserting into a Max-heap public void insert(int item) { int position; if (isfull()) throw new Exception(); heapsize++; data[heapsize-1] = item; position = heapsize 1; while (position > 0 && data[position] > data[(position-1)/2]){ swap(position, (position-1)/2); position = (position-1) / 2; } } Shebuti Rayana (CS, Stony Brook University) 26
Deleting maximum element from a Max-heap Maximum element is stored in the root, so place the root element in a variable to return later. Move the last element in the deepest level to the root (reduce the size of the heap by 1). While the moved element has a value lower than one of its children, swap this value with the highest-valued child. Return the original root that was saved. Shebuti Rayana (CS, Stony Brook University) 27
Deleting maximum element from a Max-heap (cont.) 95 Delete 95 70 83 53 61 72 16 24 48 39 Shebuti Rayana (CS, Stony Brook University) 28
Deleting maximum element from a Max-heap (cont.) 39 Delete 95 70 83 53 61 72 16 24 48 Does it hold the max-heap property now? NO Shebuti Rayana (CS, Stony Brook University) 29
Deleting maximum element from a Max-heap (cont.) 83 Delete 95 70 39 53 61 72 16 24 48 Does it hold the max-heap property now? NO Shebuti Rayana (CS, Stony Brook University) 30
Deleting maximum element from a Max-heap (cont.) 83 Delete 95 70 72 53 61 39 16 24 48 Does it hold the max-heap property now? YES Shebuti Rayana (CS, Stony Brook University) 31
Deleting maximum element from a Max-heap public int delete(){ } int answer; if (isempty()) throw new Exception(); answer = data[0]; data[0] = data[heapsize 1]; heapsize--; heapify(); return answer; Shebuti Rayana (CS, Stony Brook University) 32
Deleting maximum element from a Max-heap private void heapify() { } int position = 0; int childpos; while (position*2 + 1 < heapsize) { } childpos = position*2 + 1; if (childpos < heapsize-1 && data[childpos+1] > data[childpos]) childpos++; if (data[position]>= data[childpos]) return; swap(position, childpos); position = childpos; Shebuti Rayana (CS, Stony Brook University) 33
Complexity of Heap Assume the heap has N nodes. Then the heap has approximately log 2 N levels. Insert Since the insert swaps at most once per level, the order of complexity of insert is O(log N) Delete Since the delete swaps at most once per level, the order of complexity of delete is also O(log N) Shebuti Rayana (CS, Stony Brook University) 34
Min-heap Can you write the insert and delete methods for min-heap now? Shebuti Rayana (CS, Stony Brook University) 35
Priority Queues A priority queue PQ is like an ordinary queue except that we can only remove the maximum element at any given time (not the front element necessarily). If we use an ordinary array for the PQ, enqueue takes O(1) time but dequeue takes O(n) time. If we use a sorted array for the PQ, enqueue takes O(n) time, while dequeue takes O(1) time. We can use a heap to implement a priority queue, so that enqueue and dequeue take O(log N) time. Shebuti Rayana (CS, Stony Brook University) 36
Priority Queues If we use an ordinary array for the PQ, enqueue takes O(1) time but dequeue takes O(n) time. 0 1 2 3 4 5 6 7 8 25 61 83 95 39 72 48 Insert 48: O(1) PQ size 67 Shebuti Rayana (CS, Stony Brook University) 37
Priority Queues If we use an ordinary array for the PQ, enqueue takes O(1) time but dequeue takes O(n) time. 0 1 2 3 4 5 6 7 8 25 61 83 95 39 72 48 Delete Max (95): O(n) PQ size 7 25 61 83 39 72 48 PQ size 6 Shebuti Rayana (CS, Stony Brook University) 38
Applications of 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 Shebuti Rayana (CS, Stony Brook University) 39