Download presentation
Presentation is loading. Please wait.
1
© 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
2
© 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.
3
© 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};
4
© 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
5
© 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.
6
© 2005 Pearson Education, Inc., Upper Saddle River, NJ. All rights reserved. The Comparator Interface (continued)
7
© 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.
8
© 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 }
9
© 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
10
© 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.
11
© 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); }
12
© 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); }
13
© 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.
14
© 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;
15
© 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; }
16
© 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 ≤.
17
© 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.
18
© 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
19
© 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.
20
© 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.
21
© 2005 Pearson Education, Inc., Upper Saddle River, NJ. All rights reserved. Inserting into a Heap (continued)
22
© 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;
23
© 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; }
24
© 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.
25
© 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)
26
© 2005 Pearson Education, Inc., Upper Saddle River, NJ. All rights reserved. Deleting from a Heap (continued)
27
© 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].
28
© 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;
29
© 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];
30
© 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; }
31
© 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)
32
© 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; }
33
© 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 ();
34
© 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);
35
© 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); }
36
© 2005 Pearson Education, Inc., Upper Saddle River, NJ. All rights reserved. Run of Program 22.1 Run: Display maximum heap: 52 21 39 15 17 29 8 Minimum value is 8
37
© 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)
38
© 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.
39
© 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)
40
© 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};
41
© 2005 Pearson Education, Inc., Upper Saddle River, NJ. All rights reserved. Building a Heap (continued)
42
© 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--; }
43
© 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.
44
© 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); }
45
© 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).
46
© 2005 Pearson Education, Inc., Upper Saddle River, NJ. All rights reserved. API for the Heaps Class
47
© 2005 Pearson Education, Inc., Upper Saddle River, NJ. All rights reserved. API for the Heaps Class (continued)
48
© 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.
49
© 2005 Pearson Education, Inc., Upper Saddle River, NJ. All rights reserved. Implementing a Priority Queue (continued)
50
© 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]; }... }
51
© 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]; }
52
© 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; }
53
© 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++; }
Similar presentations
© 2024 SlidePlayer.com. Inc.
All rights reserved.