© 2005 Pearson Education, Inc., Upper Saddle River, NJ. All rights reserved. Data Structures for Java William H. Ford William R. Topp Chapter 22 Heaps.

Slides:



Advertisements
Similar presentations
Chapter 15 Heaps. Copyright © 2005 Pearson Addison-Wesley. All rights reserved Chapter Objectives Define a heap abstract data structure Demonstrate.
Advertisements

1 HeapSort CS 3358 Data Structures. 2 Heapsort: Basic Idea Problem: Arrange an array of items into sorted order. 1) Transform the array of items into.
1 CSC 427: Data Structures and Algorithm Analysis Fall 2004 Heaps and heap sort  complete tree, heap property  min-heaps & max-heaps  heap operations:
Sorting Chapter Sorting Consider list x 1, x 2, x 3, … x n We seek to arrange the elements of the list in order –Ascending or descending Some O(n.
© 2005 Pearson Education, Inc., Upper Saddle River, NJ. All rights reserved. Data Structures for Java William H. Ford William R. Topp Chapter 7 Sorting.
© 2006 Pearson Addison-Wesley. All rights reserved12 B-1 Chapter 12 (continued) Tables and Priority Queues.
CMPT 225 Priority Queues and Heaps. Priority Queues Items in a priority queue have a priority The priority is usually numerical value Could be lowest.
Analysis of Algorithms CS 477/677 Instructor: Monica Nicolescu.
Chapter 10 Heaps Anshuman Razdan Div of Computing Studies
©TheMcGraw-Hill Companies, Inc. Permission required for reproduction or display. Chapter 11 Sorting and Searching.
© 2006 Pearson Addison-Wesley. All rights reserved12 A-1 Chapter 12 Heaps.
Main Index Contents 11 Main Index Contents Selection Sort Selection SortSelection Sort Selection Sort (3 slides) Selection Sort Alg. Selection Sort Alg.Selection.
Chapter 12 B Priority Queues. © 2004 Pearson Addison-Wesley. All rights reserved 12 B-2 The ADT Priority Queue: A Variation of the ADT Table The ADT priority.
Week 10: Heap and Priority queue. Any feature here?
Heaps and heapsort COMP171 Fall 2005 Part 2. Sorting III / Slide 2 Heap: array implementation Is it a good idea to store arbitrary.
1 Chapter 8 Priority Queues. 2 Implementations Heaps Priority queues and heaps Vector based implementation of heaps Skew heaps Outline.
1 CSC 427: Data Structures and Algorithm Analysis Fall 2010 transform & conquer  transform-and-conquer approach  balanced search trees o AVL, 2-3 trees,
Maps A map is an object that maps keys to values Each key can map to at most one value, and a map cannot contain duplicate keys KeyValue Map Examples Dictionaries:
© 2005 Pearson Education, Inc., Upper Saddle River, NJ. All rights reserved. Data Structures for Java William H. Ford William R. Topp Chapter 18 Binary.
Binary Trees Michael R. Wick
ADSA: Heaps/ Advanced Data Structures and Algorithms Objectives – –implement heaps (a kind of array-based complete binary tree), heap sort,
© 2011 Pearson Addison-Wesley. All rights reserved 11 B-1 Chapter 11 (continued) Trees.
 2006 Pearson Education, Inc. All rights reserved Searching and Sorting.
The Binary Heap. Binary Heap Looks similar to a binary search tree BUT all the values stored in the subtree rooted at a node are greater than or equal.
data ordered along paths from root to leaf
Chapter 11 Heap. Overview ● The heap is a special type of binary tree. ● It may be used either as a priority queue or as a tool for sorting.
© 2005 Pearson Education, Inc., Upper Saddle River, NJ. All rights reserved. Data Structures for Java William H. Ford William R. Topp Chapter 16 Binary.
Chapter 21 Priority Queue: Binary Heap Saurav Karmakar.
1 Heaps and Priority Queues Starring: Min Heap Co-Starring: Max Heap.
Data Structures Using C++ 2E Chapter 10 Sorting Algorithms.
© 2005 Pearson Education, Inc., Upper Saddle River, NJ. All rights reserved. Data Structures for Java William H. Ford William R. Topp Chapter 8 Collection.
© 2005 Pearson Education, Inc., Upper Saddle River, NJ. All rights reserved. Data Structures for Java William H. Ford William R. Topp Chapter 5 Generic.
© 2005 Pearson Education, Inc., Upper Saddle River, NJ. All rights reserved. Data Structures for Java William H. Ford William R. Topp Chapter 13 Implementing.
Priority Queues and Heaps. October 2004John Edgar2  A queue should implement at least the first two of these operations:  insert – insert item at the.
The ADT Table The ADT table, or dictionary Uses a search key to identify its items Its items are records that contain several pieces of data 2 Figure.
Liang, Introduction to Java Programming, Tenth Edition, (c) 2013 Pearson Education, Inc. All rights reserved. 1 Chapter 20 Lists, Stacks, Queues, and Priority.
Queues, Stacks and Heaps. Queue List structure using the FIFO process Nodes are removed form the front and added to the back ABDC FrontBack.
Main Index Contents 11 Main Index Contents Complete Binary Tree Example Complete Binary Tree Example Maximum and Minimum Heaps Example Maximum and Minimum.
Lecture 15 Jianjun Hu Department of Computer Science and Engineering University of South Carolina CSCE350 Algorithms and Data Structure.
Chapter 12 Heaps & HeapSort © John Urrutia 2014, All Rights Reserved1.
Heaps and basic data structures David Kauchak cs161 Summer 2009.
Prof. Amr Goneid, AUC1 Analysis & Design of Algorithms (CSCE 321) Prof. Amr Goneid Department of Computer Science, AUC Part R3. Priority Queues.
HEAPS. Review: what are the requirements of the abstract data type: priority queue? Quick removal of item with highest priority (highest or lowest key.
© 2005 Pearson Education, Inc., Upper Saddle River, NJ. All rights reserved. Data Structures for Java William H. Ford William R. Topp Chapter 4 Introduction.
Chapter 9: Sorting1 Sorting & Searching Ch. # 9. Chapter 9: Sorting2 Chapter Outline  What is sorting and complexity of sorting  Different types of.
© 2005 Pearson Education, Inc., Upper Saddle River, NJ. All rights reserved. Data Structures for Java William H. Ford William R. Topp Chapter 20 Ordered.
Course: Programming II - Abstract Data Types HeapsSlide Number 1 The ADT Heap So far we have seen the following sorting types : 1) Linked List sort by.
Liang, Introduction to Java Programming, Tenth Edition, (c) 2013 Pearson Education, Inc. All rights reserved. 1 Chapter 23 Sorting.
Priority Queues and Heaps. John Edgar  Define the ADT priority queue  Define the partially ordered property  Define a heap  Implement a heap using.
"Teachers open the door, but you must enter by yourself. "
Data Structures for Java William H. Ford William R. Topp
Chapter 11 Heap.
Design and Analysis of Algorithms
ADT Heap data structure
Heapsort Heap & Priority Queue.
CS Data Structures Chapter 17 Heaps Mehmet H Gunes
"Teachers open the door, but you must enter by yourself. "
Data Structures for Java William H. Ford William R. Topp
Podcast Ch22c Title: Deleting from a Heap
Lecture 9 CS2013.
Heaps and Priority Queues
Algorithms: Design and Analysis
Podcast Ch22b Title: Inserting into a Heap
1 Lecture 10 CS2013.
Heaps By JJ Shepherd.
Podcast Ch22a Title: Array-based Binary Trees
Chapter 12 Heap ADT © 2011 Pearson Addison-Wesley. All rights reserved.
The Heap ADT A heap is a complete binary tree where each node’s datum is greater than or equal to the data of all of the nodes in the left and right.
CMPT 225 Lecture 16 – Heap Sort.
Presentation transcript:

© 2005 Pearson Education, Inc., Upper Saddle River, NJ. All rights reserved. Data Structures for Java William H. Ford William R. Topp Chapter 22 Heaps Bret Ford © 2005, Prentice Hall

© 2005 Pearson Education, Inc., Upper Saddle River, NJ. All rights reserved. Array-Based Binary Trees A complete binary tree of depth d contains all possible nodes through level d-1 and nodes at level d occupy the leftmost positions in the tree. A complete binary tree of depth d contains all possible nodes through level d-1 and nodes at level d occupy the leftmost positions in the tree. An array arr can be viewed as a complete binary tree. The root is arr[0], the first- level children are arr[1] and arr[2], the second-level children are arr[3], arr[4], arr[5], arr[6], and so forth. An array arr can be viewed as a complete binary tree. The root is arr[0], the first- level children are arr[1] and arr[2], the second-level children are arr[3], arr[4], arr[5], arr[6], and so forth.

© 2005 Pearson Education, Inc., Upper Saddle River, NJ. All rights reserved. Array-Based Binary Trees (continued) Integer[] arr = {5, 1, 3, 9, 6, 2, 4, 7, 0, 8};

© 2005 Pearson Education, Inc., Upper Saddle River, NJ. All rights reserved. Array-Based Binary Trees (concluded) For element arr[i] in an n-element array ‑ based binary tree, the following relationships hold: For element arr[i] in an n-element array ‑ based binary tree, the following relationships hold: Left child of arr[i] isarr[2*i + 1] undefined if 2*i + 1  n Right child of arr[i] isarr[2*i + 2] undefined if 2*i + 2  n Parent of arr[i] isarr[(i-1)/2] undefined if i = 0

© 2005 Pearson Education, Inc., Upper Saddle River, NJ. All rights reserved. The Comparator Interface The ordering determined when a class implements the Comparable interface is termed the class's natural ordering. The ordering determined when a class implements the Comparable interface is termed the class's natural ordering. The Comparator interface defines objects containing a comparison function which imposes a total ordering on some collection of objects. The Comparator interface defines objects containing a comparison function which imposes a total ordering on some collection of objects. Comparators can be passed to a sort method to allow precise control over the sort order. and can also be used to control the order of certain data structures such as a heap. Comparators can be passed to a sort method to allow precise control over the sort order. and can also be used to control the order of certain data structures such as a heap.

© 2005 Pearson Education, Inc., Upper Saddle River, NJ. All rights reserved. The Comparator Interface (continued)

© 2005 Pearson Education, Inc., Upper Saddle River, NJ. All rights reserved. The Comparator Interface (continued) A Comparator object implements the compare() method and acts like a function that compares two objects. A Comparator object implements the compare() method and acts like a function that compares two objects. Comparator objects are external to the classes that use them and must access data in those classes using public methods. Comparator objects are external to the classes that use them and must access data in those classes using public methods.

© 2005 Pearson Education, Inc., Upper Saddle River, NJ. All rights reserved. CircleLess Class public class CircleLess implements Comparator { public int compare(Circle x, Circle y) { double radX = x.getRadius(), radY = y.getRadius(); // returns < 0 if radX < radY, // 0 if radX == radY, and > 0 // if radX > radY return (int)(radX - radY); } // The method equals(Object obj) is // inherited from the Object superclass }

© 2005 Pearson Education, Inc., Upper Saddle River, NJ. All rights reserved. CircleLess Class Example Circle circA = new Circle(5), circB = new Circle(7.5); CircleLess circComp = new CircleLess();// comparing object if (circComp.compare(circA, circB) < 0) System.out.println("Circle A is less than circle B"); Output: Circle A is less than circle B

© 2005 Pearson Education, Inc., Upper Saddle River, NJ. All rights reserved. General Comparison Objects If a class implements the Comparable interface, we can develop generic Comparator classes that compare two objects for either the relation. The classes implement compare() by using the compareTo() ordering between objects. If a class implements the Comparable interface, we can develop generic Comparator classes that compare two objects for either the relation. The classes implement compare() by using the compareTo() ordering between objects.

© 2005 Pearson Education, Inc., Upper Saddle River, NJ. All rights reserved. The < Comparator import java.util.Comparator; // constructs the < Comparator public class Less implements Comparator { public int compare(T x, T y) { return ((Comparable )x).compareTo(y); }

© 2005 Pearson Education, Inc., Upper Saddle River, NJ. All rights reserved. The > Comparator // constructs the > Comparator public class Greater implements Comparator { public int compare(T x, T y) { return -((Comparable )x).compareTo(y); }

© 2005 Pearson Education, Inc., Upper Saddle River, NJ. All rights reserved. Generalized Array Sorting By using a Less or Greater parameter in a sorting method, the method can place an array in ascending or descending order. By using a Less or Greater parameter in a sorting method, the method can place an array in ascending or descending order.

© 2005 Pearson Education, Inc., Upper Saddle River, NJ. All rights reserved. Selection Sort with Comparator // new version adds a Comparator parameter public static void selectionSort( T[] arr, Comparator comp) { // index of smallest element in the sublist int smallIndex; int pass, j, n = arr.length; T temp; // pass has the range 0 to n-2 for (pass = 0; pass < n-1; pass++) { // scan the sublist starting at index pass smallIndex = pass;

© 2005 Pearson Education, Inc., Upper Saddle River, NJ. All rights reserved. Selection Sort with Comparator (concluded) // j traverses the sublist arr[pass+1] // to arr[n-1] for (j = pass+1; j < n; j++) // if smaller element found, assign // smallIndex to that position if (comp.compare(arr[j], arr[smallIndex]) < 0) smallIndex = j; // swap the next smallest // element into arr[pass] temp = arr[pass]; arr[pass] = arr[smallIndex]; arr[smallIndex] = temp; }

© 2005 Pearson Education, Inc., Upper Saddle River, NJ. All rights reserved. Heaps A maximum heap is an array ‑ based tree in which the value of a parent is ≥ the value of its children. A minimum heap uses the relation ≤. A maximum heap is an array ‑ based tree in which the value of a parent is ≥ the value of its children. A minimum heap uses the relation ≤.

© 2005 Pearson Education, Inc., Upper Saddle River, NJ. All rights reserved. Inserting into a Heap Assume that the array the elements in the index range 0  i < last < n form a heap. Assume that the array the elements in the index range 0  i < last < n form a heap. The new element will enter the array at index last with the heap expanding by one element. The new element will enter the array at index last with the heap expanding by one element.

© 2005 Pearson Education, Inc., Upper Saddle River, NJ. All rights reserved. Inserting into a Heap (continued) Insert item into a heap by moving nodes on the path of parents down one level until the item is assigned as a parent that has heap ordering. Insert item into a heap by moving nodes on the path of parents down one level until the item is assigned as a parent that has heap ordering. Path of parents for insertion of item = 50

© 2005 Pearson Education, Inc., Upper Saddle River, NJ. All rights reserved. Inserting into a Heap (continued) The static method pushHeap() of the class Heaps inserts a new value in the heap. The parameter list includes the array arr, the index last, the new value item, and a Comparator object of type Greater or Less indicating whether the heap is a maximum or minimum heap. The static method pushHeap() of the class Heaps inserts a new value in the heap. The parameter list includes the array arr, the index last, the new value item, and a Comparator object of type Greater or Less indicating whether the heap is a maximum or minimum heap.

© 2005 Pearson Education, Inc., Upper Saddle River, NJ. All rights reserved. Inserting into a Heap (continued) The algorithm uses an iterative scan with variable currPos initially set to last. At each step, compare the value item with the value of the parent and if item is larger, copy the parent value to the element at index currPos and assign the parent index as the new value for currPos. The effect is to move the parent down one level. Stop when the parent is larger and assign item to the position currPos. The algorithm uses an iterative scan with variable currPos initially set to last. At each step, compare the value item with the value of the parent and if item is larger, copy the parent value to the element at index currPos and assign the parent index as the new value for currPos. The effect is to move the parent down one level. Stop when the parent is larger and assign item to the position currPos.

© 2005 Pearson Education, Inc., Upper Saddle River, NJ. All rights reserved. Inserting into a Heap (continued)

© 2005 Pearson Education, Inc., Upper Saddle River, NJ. All rights reserved.pushHeap() // the array elements in the range // (0, last) are a heap; insert item // into the heap so that the range // (0, last+1) is a heap; use the Comparator // comp to perform comparisons public static void pushHeap(T[] arr, int last, T item, Comparator comp) { // assume the new item is at location // arr[last] and that the elements arr[0] // to arr[last-1] are in heap order int currPos, parentPos; // currPos is an index that traverses // path of parents; item is assigned // in the path currPos = last; parentPos = (currPos-1)/2;

© 2005 Pearson Education, Inc., Upper Saddle River, NJ. All rights reserved. pushHeap() (concluded) // traverse path of parents up to the root while (currPos != 0) { // compare target and parent value if (comp.compare(item,arr[parentPos]) < 0) { // move data from parent position to current // position; update current position to parent // position; compute next parent arr[currPos] = arr[parentPos]; currPos = parentPos; parentPos = (currPos-1)/2; } else // heap condition is ok; break break; } // the correct location has been // discovered; assign target arr[currPos] = item; }

© 2005 Pearson Education, Inc., Upper Saddle River, NJ. All rights reserved. Deleting from a Heap Deletion from a heap is normally restricted to the root only. Hence, the operation removes the maximum (or minimum) element. Deletion from a heap is normally restricted to the root only. Hence, the operation removes the maximum (or minimum) element. To erase the root of an n ‑ element heap, exchange the element at index n ‑ 1 and the root and filter the root down into its correct position in the tree. To erase the root of an n ‑ element heap, exchange the element at index n ‑ 1 and the root and filter the root down into its correct position in the tree.

© 2005 Pearson Education, Inc., Upper Saddle River, NJ. All rights reserved. Deleting from a Heap (continued) // filter the array element arr[first] down the heap with index // range [first, last) private static void adjustHeap(T[] arr, int first, int last, Comparator comp)

© 2005 Pearson Education, Inc., Upper Saddle River, NJ. All rights reserved. Deleting from a Heap (continued)

© 2005 Pearson Education, Inc., Upper Saddle River, NJ. All rights reserved.adjustHeap() The implementation of adjustHeap() uses the integer variables currPos and childPos to scan the path of children. The implementation of adjustHeap() uses the integer variables currPos and childPos to scan the path of children. Let currPos = first and target = arr[first]. The iterative scan proceeds until we reach a leaf node or target is ≥ to the values of the children at the current position. Let currPos = first and target = arr[first]. The iterative scan proceeds until we reach a leaf node or target is ≥ to the values of the children at the current position. Move currPos and childPos down the path of children in tandem. Set childPos = index of the largest (smallest) of arr[2*currPos + 1] and arr[2*currPos + 2]. Move currPos and childPos down the path of children in tandem. Set childPos = index of the largest (smallest) of arr[2*currPos + 1] and arr[2*currPos + 2].

© 2005 Pearson Education, Inc., Upper Saddle River, NJ. All rights reserved. adjustHeap() (continued) // filter the array element arr[first] down the // heap with index range (first, last) public static void adjustHeap(T[] arr, int first, int last, Comparator comp) { int currentPos, childPos; T target; // start at first and filter target down the heap currentPos = first; target = arr[first]; // compute the left child index and begin // a scan down path of children, stopping // at end of list (last) or when we find a // place for target childPos = 2 * currentPos + 1;

© 2005 Pearson Education, Inc., Upper Saddle River, NJ. All rights reserved. adjustHeap() (continued) while (childPos < last) { // index of right child is childPos+1; // compare the two children; change // childPos if comp.compare(arr[childPos+1], // arr[childPos]) < 0 if ((childPos+1 < last) && comp.compare(arr[childPos+1], arr[childPos]) < 0) childPos = childPos + 1; // compare selected child to target if (comp.compare(arr[childPos],target) < 0) { // comp.compare(selected child, target) < 0; // move selected child to the parent; // position of selected child is now vacated arr[currentPos] = arr[childPos];

© 2005 Pearson Education, Inc., Upper Saddle River, NJ. All rights reserved. adjustHeap() (concluded) // update indices to continue the scan currentPos = childPos; childPos = 2 * currentPos + 1; } else // target belongs at currentPos break; } arr[currentPos] = target; }

© 2005 Pearson Education, Inc., Upper Saddle River, NJ. All rights reserved. Implementing popHeap() The implementation first captures the root and then exchanges it with the last value in the heap (arr[last-1]). A call to adjustHeap() reestablishes heap order in a heap which now has index range [0, last-1). Method popHeap() concludes by returning the original root value. The implementation first captures the root and then exchanges it with the last value in the heap (arr[last-1]). A call to adjustHeap() reestablishes heap order in a heap which now has index range [0, last-1). Method popHeap() concludes by returning the original root value. // delete the maximum (minimum) element in the heap and // return its value public static T popHeap(T[] arr, int last, Comparator comp)

© 2005 Pearson Education, Inc., Upper Saddle River, NJ. All rights reserved. Implementing popHeap() (concluded) // the array elements in the range (0, last) // are a heap; swap the first and last elements // of the heap and then make the elements in the // index range (0, last-1) a heap; use the Comparator // comp to perform comparisons public static T popHeap(T[] arr, int last, Comparator comp) { // element that is popped from the heap T temp = arr[0]; // exchange last element in the heap with the deleted // (root) element arr[0] = arr[last-1]; arr[last-1] = temp; // filter down the root over the range (0, last-1) adjustHeap(arr, 0, last-1, comp); return temp; }

© 2005 Pearson Education, Inc., Upper Saddle River, NJ. All rights reserved. Program 22.1 import ds.util.Heaps; import ds.util.Greater; import ds.util.Less; public class Program22_1 { public static void main(String[] args) { // integer array used to create heaps arrA and arrB Integer[] intArr = {15, 29, 52, 17, 21, 39, 8}, heapArrA = new Integer[intArr.length], heapArrB = new Integer[intArr.length]; int i; // comparators to specify maximum or minimum heap Greater greater = new Greater (); Less less = new Less ();

© 2005 Pearson Education, Inc., Upper Saddle River, NJ. All rights reserved. Program 22.1 (continued) // load elements from intArr into heapArrA to form // a maximum heap and into heapArrB to form a // minimum heap for (i = 0; i < intArr.length; i++) { Heaps.pushHeap(heapArrA, i, intArr[i], greater); Heaps.pushHeap(heapArrB, i, intArr[i], less); } // display the heapArrA System.out.println("Display maximum heap:"); System.out.println(Heaps.displayHeap(heapArrA, heapArrA.length, 2)); // graphically display heapArrB before and // after popHeap() Heaps.drawHeaps(heapArrB, heapArrB.length, 2);

© 2005 Pearson Education, Inc., Upper Saddle River, NJ. All rights reserved. Program 22.1 (concluded) Integer minObj = Heaps.popHeap(heapArrB, heapArrB.length, less); System.out.println("\nMinimum value is " + minObj); // the index range is 0 to heapArrB.length-1 Heaps.drawHeap(heapArrB, heapArrB.length-1, 2); }

© 2005 Pearson Education, Inc., Upper Saddle River, NJ. All rights reserved. Run of Program 22.1 Run: Display maximum heap: Minimum value is 8

© 2005 Pearson Education, Inc., Upper Saddle River, NJ. All rights reserved. Complexity of Heap Operations A heap stores elements in an array-based tree that is a complete tree. The pushHeap() and adjustHeap() operations reorder elements in the tree by move up the path of parents for push() and down the path of largest (smallest) children for pop(). Assuming the heap has n elements, the maximum length for a path between a leaf node and the root is log 2 n, so the runtime efficiency of the algorithms is O(log 2 n) A heap stores elements in an array-based tree that is a complete tree. The pushHeap() and adjustHeap() operations reorder elements in the tree by move up the path of parents for push() and down the path of largest (smallest) children for pop(). Assuming the heap has n elements, the maximum length for a path between a leaf node and the root is log 2 n, so the runtime efficiency of the algorithms is O(log 2 n)

© 2005 Pearson Education, Inc., Upper Saddle River, NJ. All rights reserved. Sorting with a Heap If the original array is a maximum heap, an efficient sorting algorithm can be devised. If the original array is a maximum heap, an efficient sorting algorithm can be devised. For each iteration i, the largest element is arr[0]. Exchange arr[0] with arr[i] and then reorder the array so that elements in the index range [0, i) are a heap. This is precisely the action of popHeap(), which is an O(log 2 n) algorithm. For each iteration i, the largest element is arr[0]. Exchange arr[0] with arr[i] and then reorder the array so that elements in the index range [0, i) are a heap. This is precisely the action of popHeap(), which is an O(log 2 n) algorithm. By transforming an arbitrary array into a heap, this algorithm will sort the array. By transforming an arbitrary array into a heap, this algorithm will sort the array.

© 2005 Pearson Education, Inc., Upper Saddle River, NJ. All rights reserved. Building a Heap Transforming an arbitrary array into a heap is called "heapifying" the array. Transforming an arbitrary array into a heap is called "heapifying" the array. The method makeHeap() in the Heaps class performs this transformation. The method makeHeap() in the Heaps class performs this transformation. Turn an n ‑ element array into a heap by filtering down each parent in the tree beginning with the last parent at index (n-2)/2 and ending with the root node at index 0 Turn an n ‑ element array into a heap by filtering down each parent in the tree beginning with the last parent at index (n-2)/2 and ending with the root node at index 0 public static void makeHeap(T[] arr, Comparator comp)

© 2005 Pearson Education, Inc., Upper Saddle River, NJ. All rights reserved. Building a Heap (continued) Integer[] arr = {9, 12, 17, 30, 50, 20, 60, 65, 4, 19};

© 2005 Pearson Education, Inc., Upper Saddle River, NJ. All rights reserved. Building a Heap (continued)

© 2005 Pearson Education, Inc., Upper Saddle River, NJ. All rights reserved. Building a Heap (concluded) // arrange the array elements into a heap; use the // Comparator comp to perform comparisons public static void makeHeap(T[] arr, Comparator comp) { int heapPos, lastPos; // compute the size of the heap and // the index of the last parent lastPos = arr.length; heapPos = (lastPos - 2)/2; // filter down every parent in order // from last parent down to root while (heapPos >= 0) { adjustHeap(arr, heapPos, lastPos, comp); heapPos--; }

© 2005 Pearson Education, Inc., Upper Saddle River, NJ. All rights reserved. Heapsort The heap sort is a modified version of the selection sort for an array arr that is a heap. The heap sort is a modified version of the selection sort for an array arr that is a heap. For each i = n, n-1,..., 2, call popHeap() which pops arr[0] from the heap and assigns it at index i-1. For each i = n, n-1,..., 2, call popHeap() which pops arr[0] from the heap and assigns it at index i-1. With a maximum heap, the array is assorted in ascending order. A minimum heap sorts the array in descending order. With a maximum heap, the array is assorted in ascending order. A minimum heap sorts the array in descending order.

© 2005 Pearson Education, Inc., Upper Saddle River, NJ. All rights reserved. Heapsort (continued) public static void heapSort(T[] arr, Comparator comp) { // "heapify" the array arr Heaps.makeHeap(arr, comp); int i, n = arr.length; // iteration that determines elements // arr[n-1]... arr[1] for (i = n; i > 1; i--) { // call popHeap() to move next // largest to arr[n-1] Heaps.popHeap(arr, i, comp); }

© 2005 Pearson Education, Inc., Upper Saddle River, NJ. All rights reserved. Heapsort (concluded) A mathematical analysis shows that the worst case running time of makeHeap() is O(n). A mathematical analysis shows that the worst case running time of makeHeap() is O(n). During the second phase of the heap sort, popHeap() executes n - 1 times. Each operation has efficiency O(log 2 n). During the second phase of the heap sort, popHeap() executes n - 1 times. Each operation has efficiency O(log 2 n). The worst-case complexity of the heap sort is O(n) + O(n log 2 n) = O(n log 2 n). The worst-case complexity of the heap sort is O(n) + O(n log 2 n) = O(n log 2 n).

© 2005 Pearson Education, Inc., Upper Saddle River, NJ. All rights reserved. API for the Heaps Class

© 2005 Pearson Education, Inc., Upper Saddle River, NJ. All rights reserved. API for the Heaps Class (continued)

© 2005 Pearson Education, Inc., Upper Saddle River, NJ. All rights reserved. Implementing a Priority Queue Recall that the HeapPQueue class implements the PQueue interface. Recall that the HeapPQueue class implements the PQueue interface. The class uses a heap as the underlying storage structure. The class uses a heap as the underlying storage structure. The user is free to specify either a Less or Greater comparator which dictates whether a deletion removes the minimum or the maximum element from the collection. The user is free to specify either a Less or Greater comparator which dictates whether a deletion removes the minimum or the maximum element from the collection.

© 2005 Pearson Education, Inc., Upper Saddle River, NJ. All rights reserved. Implementing a Priority Queue (continued)

© 2005 Pearson Education, Inc., Upper Saddle River, NJ. All rights reserved. The HeapPQueue Class public class HeapPQueue implements PQueue { // heapElt holds the priority queue elements private T[] heapElt; // number of elements in the priority queue private int numElts; // Comparator used for comparisons private Comparator comp; // create an empty maximum priority queue public HeapPQueue() { comp = new Less (); numElts = 0; heapElt = (T[]) new Object[10]; }... }

© 2005 Pearson Education, Inc., Upper Saddle River, NJ. All rights reserved. HeapPQueue Class peek() // return the highest priority item // Precondition: the priority queue is not empty; // if it is empty, throws NoSuchElementException public T peek() { // check for an empty heap if (numElts == 0) throw new NoSuchElementException( "HeapPQueue peek(): empty queue"); // return the root of the heap return heapElt[0]; }

© 2005 Pearson Education, Inc., Upper Saddle River, NJ. All rights reserved. HeapPQueue Class pop() // erase the highest priority item and return it // Precondition: the priority queue is not empty; // if it is empty, throws NoSuchElementException public T pop() { // check for an empty priority queue if (numElts == 0) throw new NoSuchElementException( "HeapPQueue pop(): empty queue"); // pop the heap and save the return value in top T top = Heaps.popHeap(heapElt, numElts, comp); // heap has one less element numElts--; return top; }

© 2005 Pearson Education, Inc., Upper Saddle River, NJ. All rights reserved. HeapPQueue Class push() // insert item into the priority queue public void push(T item) { // if the current capacity is used up, reallocate // with double the capacity if (numElts == heapElt.length) enlargeCapacity(); // insert item into the heap Heaps.pushHeap(heapElt, numElts, item, comp); numElts++; }