CS211, Lecture 20 Priority queues and HeapsPriority queuePriority-queue interfacePriority-queue implementationsSlide 5Definition of a heapBecause a heap is a complete binary tree, it goes nicely in an array bWhere is the maximum value of a heap?Removing the max from an n-node tree takes time O(log n)Slide 10PowerPoint PresentationSlide 12Inserting a value into a heap takes time O(log n)Heap sort1CS211, Lecture 20 Priority queues and HeapsReadings: Weiss,sec. 6.9,secs. 21.1--21.5.When they've got two queues going, there's never any queue! P.J. Heaps.(The only quote I could find that had both queues and heaps in it.)2In various contexts, one needs a listof items, each with a priority.Operations:1. Add an item (with some priority).2. Find an item with maximum priority.3. Remove a maximum-priority item.That is a priority queue.Example: files waiting to be printed, print in order of size (smaller the size, the higher the priority).Example: Job scheduler. Many processes waiting to be executed. Those with higher priority numbers are most important and should be give time before others.Priority queue3/** An instance is a priority queue */public interface PriorityQueue { void insert(Comparable x); /** Insert x into the priority queue */void makeEmpty(); /** Make the queue empty */boolean isEmpty(); /** = “queue is empty” */int size(); /** = the size of the queue *//** = largest item in this queue --throw exception if queue is empty*/ Comparable findMax();/** = delete and return largest item --throw exception if queue is empty*/ Comparable removeMax();}Priority-queue interfacex.compareTo(y) is used to see which has higher priority, x or y. Objects x and y could have many fields.Weiss also allows the possibility of changing the priority of an item in the queue. We don’t discuss that feature.4Possible implementations. Assume queue has n items. We look at average-case times. O(1) means constant time.1.Unordered array segment b[0..n-1]. Insert: O(1), findMax: O(n), removeMax: O(n).2. Ordered array segment b[0..n-1].Insert: O(n), findMax:O(1), removeMax: O(n)3. Ordered array segments b[0..n-1], from largest to smallest.Insert: O(n), findMax:O(1), removeMax: O(1)4.binary search tree (if depth of tree is a minimum).Insert: O(log n), findMax:O(log n), removeMax: O(log n) But how do we keep the tree nicely balanced? Priority-queue implementations5Possible implementations. Assume queue has n items. We look at average-case times. O(1) means constant time.5. Special case. Suppose the possible priorities are 0..n-1. Keep an array priority[0..n-1] in which priority[p] is a linked list of items with priority p. Variable highestp: the highest p for which priority[p] is not empty (-1 if none)Insert, worst case: O(1), findMax:O(1), removeMax: O(n)Priority-queue implementations0 1 null2…6Definition of a heapFirst, number nodes in breadth-first order.A complete binary tree is one in which: if node number n is present, so are nodes 0..n-1.A heap is a complete binary tree in which: the value in any node is at least the values in its children.54390518233762719Caution: Weiss numbers nodes 1..n instead of 0..n-1.7Because a heap is a complete binary tree,it goes nicely in an array bPlace node k in b[k]!The parent of node k is in b[(k-1)/2]. The parent of node 9 is in b[(9-1)/2], which is b[4] The parent of node 8 is in b[(8-1)/2], which is b[3]54390518233762719The children of node k are in b[k*2 + 1] b[k*2 + 2]Children of 3 are in b[7] and b[8]8Where is the maximum value of a heap?Max value of a heap is in node 0.Node 0 is in b[0] in our array implementation.Therefore, retrieving the max takes time O(1) (constant time).543905182337627199Removing the max from an n-node tree takes time O(log n)/** Precondition: n > 0. Remove max value from heap. */7439051823356272974320518233562729743805122335627292438051723356272910Removing the max from an n-node tree takes time O(log n)/** Remove max value from heap. Precondition: n > 0. */n= n – 1; b[0]= b[n];// Bubble b[0] down to its proper place in b[0..n-1]int k= 0;// inv: the only possible offender of the heap property is b[k]while (k has a child that is larger) {Let h be the larger of k’s childs;Swap b[h] and b[k];k= h;}7439051823356272974320518233562729Whenever some computation is messy, write a function for it11/** k is a node in b[0..n-1]. If k has a larger child, return the larger of its children; otherwise, return k */public static int f(int[] b, int n, int k) { int h= 2*k+1; // first child; will be the larger child if (h >= n) return k; // k has no children // Set h to index of the larger of k’s childs. if (h+1 < n || b[h+1].compareTo(b[h]) > 0)h= h+1; if (b[k].compareTo(b[h] <= 0) return h; return k;}74320518233562729The children of node k are in b[k*2 + 1], b[k*2 + 2]12Removing the max from an n-node tree takes time O(log n)/** Remove max value from heap. Precondition: n > 0. */n= n – 1; b[0]= b[n];// Bubble b[0] down to its proper place in b[0..n-1]int k= 0; h= f(k);// inv: the only possible offender of the heap property is b[k] if k offends, h is its larger child; otherwise, h = kwhile (h != k) {Swap b[h] and b[k];k= h; h= f(k);}743905182335627297432051823356272913Inserting a value into a heap takes time O(log n)/** Insert ob into heap b[0..n-1]. */b[n]= ob; n= n+1;Bubble b[n-1] up76390518233562729104743905182335627291067539061823356272910414/** Sort b[0..n-1] */ // Make b[0..n-1] into a heap invariant: b[0..k-1] is a heap (P1 below) for (int k= 0; k != n; k= k+1){ Bubble b[k] up } // Sort the heap b[0..n-1] invariant: Picture P2 below int h= n; while (h != 0) { h= h-1; Swap b[0] and b[h]; Bubble b[0] down in b[0..h-1] }Heap sort this is a heap sorted, >= b[0..h-1] 0 h nEach part time O(n log n)Total time is O(n log n) this is a heap 0 k nP1: bP2:
View Full Document