1 / 96

241-423 Advanced Data Structures and Algorithms

241-423 Advanced Data Structures and Algorithms. Objective s implement heaps (a kind of array-based complete binary tree), heap sort, priority queues. Semester 2, 2012-2013. 11. Heaps. Contents. 1. Tree Terminology 2. Binary Trees 3. The Comparator Interface

teal
Download Presentation

241-423 Advanced Data Structures and Algorithms

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. 241-423 Advanced Data Structures and Algorithms Objectives implement heaps (a kind of array-based complete binary tree), heap sort, priority queues Semester 2, 2012-2013 11. Heaps

  2. Contents 1. Tree Terminology 2. Binary Trees 3. The Comparator Interface 4. Generalized Array Sorting 5. Heaps 6. Sorting with a Max Heap 7. API for the Heaps Class 8. Priority Queue Collection

  3. 1. Tree Terminology continued

  4. A path between a parent node X0 anda subtree node N is a sequence of nodes P = X0, X1, . . ., (Xk is N) • k is the length of the path • each node Xi is the parent of Xi+1 for 0  i  k-1 continued

  5. The level of a node is the length of the path from root to that node. • The height of a tree is the maximum level in the tree. continued

  6. 2. Binary Trees • In a binary tree, each node has at most two children. continued

  7. Each node of a binary tree defines a left and a right subtree. Each subtree is a tree. Right child of T Left child of T

  8. { -1 if TN is empty 1+max( height(TL), height(TR)) if TN not empty height(N) = height(TN) = Height of a Binary Tree Node • Node N starts a subtree TN , with TL and TR the left and right subtrees. Then: continued

  9. degenerate binary tree (a list)

  10. A Complete Binary Tree • A complete binary tree of height h has all its nodes filled through level h-1, and the nodes at depth h run left to right with no gaps. continued

  11. An Array-based Complete Binary Tree • An array arr[] can be viewed as a complete binary tree if: • the root is stored in arr[0] • the level 1 children are in arr[1], arr[2] • the level 2 children are in arr[3], arr[4], arr[5], arr[6] • etc. continued

  12. Integer[] arr = {5, 1, 3, 9, 6, 2, 4, 7, 0, 8}; continued

  13. For arr[i] in an n-element array‑based complete binary tree: Left child of arr[i] is arr[2*i + 1]; or undefined if (2*i + 1)  n Right child of arr[i] is arr[2*i + 2]; or undefined if (2*i + 2)  n Parent of arr[i] is arr[(i-1)/2]; or undefined if i = 0

  14. 3. The Comparator Interface continued

  15. 3.1. Comparator vs. Comparable • Comparator.compare(T x, T y) • comparison is done between two objects, x and y • the method is implemented outside the T class • Comparable.compareTo(T y) • comparison is done between 'this' object and object y • the method is implemented by the T class

  16. 3.2. Benefits of Comparator • The comparison code is in a separate place from the other code for a class. • 1. This means that the same objects can be compared in different ways by defining different Comparators • see the two comparators for circles

  17. 2. A Comparator can be passed to a sort method as an argument to control the ordering of objects. • this means that the same sort method can sort in different ways depending on its Comparator argument. • e.g. sorting an array into ascending or descending order • e.g. sorting data into max heaps or min heaps

  18. 3.3. Comparing Circles public class Circle { private double xCenter, yCenter; private double radius; public Circle(double x, double y, double r) { xCenter = x; yCenter = y; radius = r; } public double getX() { return xCenter; } :

  19. public double getY() { return yCenter; } public double getRadius() { return radius; } public double getArea() { return Math.PI * radius * radius; } : // more methods, but no comparison function } // end of Circle class

  20. Comparing Circle Radii public class RadiiComp implements Comparator<Circle> { public int compare(Circle c1, Circle c2) { double radC1 = c1.getRadius(); double radC2 = c2.getRadius(); // returns < 0 if radC1 < radC2, // 0 if radC1 == radC2, // > 0 if radC1 > radC2 return (int)(radC1 – radC2); } // end of compare() // equals() is inherited from Object superclass } // end of RadiiComp class

  21. Comparing Circle Positions public class PosComp implements Comparator<Circle> { public int compare(Circle c1, Circle c2) { double c1Dist = (c1.getX() * c1.getX()) + (c1.getY() * c1.getY()); double c2Dist = (c2.getX() * c2.getX()) + (c2.getY() * c2.getY()); // returns < 0 if c1Dist < c2Dist, // 0 if c1Dist == c2Dist, // > 0 if c1Dist > c2Dist return (int)(c1Dist - c2Dist); } // end of compare() // equals() is inherited from Object superclass } // end of PosComp class

  22. Comparing Circles in Two Ways Circle c1 = new Circle(0, 0, 5); Circle c2 = new Circle(3, 2, 7); RadiiComp rComp = new RadiiComp(); if (rComp.compare(c1, c2) < 0) System.out.println("Circle 1 is smaller than circle 2"); PosComp pComp = new PosComp(); if (pComp.compare(c1, c2) < 0) System.out.println("Circle 1 is nearer to origin than circle 2"); Circle 1 is smaller than circle 2 Circle 1 is nearer to origin than circle 2

  23. 3.4. Comparators for Sorting • The Less and Greater Comparator classes make sorting and searching functions more flexible • see selectionSort() in the next few slides • Less and Greater will also be used to create min and max heaps.

  24. The Less Comparator import java.util.Comparator; // the < Comparator public class Less<T> implements Comparator<T> { public int compare(T x, T y) { return ((Comparable<T>)x).compareTo(y); } } uses T's compareTo() to compare x and y x Less y == x.compareTo y == x < y

  25. Using Less Comparator<Integer> lessInt = new Less<Integer>(); Integer a = 3, b = 5; if (lessInt.compare(a, b) < 0) System.out.println(a + " < " + b); 3 < 5

  26. The Greater Comparator // the > Comparator public class Greater<T> implements Comparator<T> { public int compare(T x, T y) { return -((Comparable<T>)x).compareTo(y); } } uses T's compareTo() to compare x and y x Greater y == -(x.compareTo y) == -(x < y) == x > y

  27. Using Greater Comparator<Integer> greaterInt = new Greater<Integer>(); Integer a = 9, b = 5; if (greaterInt.compare(a, b) < 0) System.out.println(a + " > " + b); 9 > 5

  28. 4. Generalized Array Sorting • A single selectionSort() method can sort array in different ways by being passed different Comparator arguments. • The Less or Greater Comparators let the method sort an array of objects into ascending or descending order.

  29. Selection Sort with Comparator // new version adds a Comparator parameter public static <T> void selectionSort(T[] arr, Comparator<? super T> comp) { int smallIndex; int n = arr.length; for (int pass = 0; pass < n-1; pass++) { // scan the sublist starting at index smallIndex = pass; : Compare with selectionSort() in Part 2 which uses Comparable.

  30. // j traverses the sublist arr[pass+1] to arr[n-1] for (int j = pass+1; j < n; j++) // if smaller element found, assign posn to smallIndex if (comp.compare(arr[j], arr[smallIndex]) < 0) smallIndex = j; // swap smallest element into arr[pass] T temp = arr[pass]; arr[pass] = arr[smallIndex]; arr[smallIndex] = temp; } } // end of selectionSort()

  31. Example String[] arr = {"red", "green", "blue", "yellow", "teal", "orange"}; Less<String> lessComp = new Less<String>(); Greater<String> gtComp = new Greater<String>(); Arrays.selectionSort(arr, lessComp); System.out.println("Less sort: " + Arrays.toString(arr)); Arrays.selectionSort(arr, gtComp); System.out.println("Greater sort: " + Arrays.toString(arr)); // ascending Less sort: [blue, green, orange, red, teal, yellow] Greater sort: [yellow, teal, red, orange, green, blue] // descending

  32. 5. Heaps • A maximum heap is an array‑based complete binary tree in which the value of a parent is ≥ the value of both its children. lvl 0 2 3 1 5 55 50 52 25 10 11 20 22 0 1 2 3 4 5 6 7 8 there’s no ordering within a level(between siblings) continued

  33. lvl 0 2 1 40 15 30 10 0 1 2 3

  34. A minimum heap uses the relation ≤. lvl 0 2 3 1 55 5 10 50 11 20 52 25 22 0 1 2 3 4 5 6 7 8 continued

  35. lvl 0 2 1 10 15 30 40 0 1 2 3

  36. A max heap will be ordered using the Greater comparator. • I’ll focus on max heaps in these notes • A min heap will be ordered using the Less comparator.

  37. Heap Uses • Heapsort • one of the best sorting methods • in-place; no quadratic worst-case • Selection algorithms • finding the min, max, median, k-th element in sublinear time • Graph algorithms • Prim's minimal spanning tree; • Dijkstra's shortest path

  38. Max Heap Operations • Inserting an element: pushHeap() • Deleting an element: popHeap() • most of the work is done by calling adjustHeap() • Array --> heap conversion: makeHeap() • most of the work is done by calling adjustHeap() • Heap sorting: heapSort() • utilizes makeHeap() then popHeap()

  39. 5.1. Inserting into a Max Heap pushHeap() • Assume that the array is a maximum heap. • a new item will enter the array at index last with the heap expanding by one element continued

  40. Insert an item by moving the nodes on the path of parents down one level until the item is correctly placed as a parent in the heap. insert 50 path of parents continued

  41. continued

  42. At each step, compare item with parent • if item is larger, move parent down one level • arr[currPos] = parent; • currPos = the parent index; • Stop when parent is larger than item • assign item to the currPos position • arr[currPos] = item;

  43. pushHeap() Depending on the comparator, pushHeap() can work with max or min heaps. public static <T> void pushHeap(T[] arr, int last, T item, Comparator<? super T> comp) { // insert item into the heap arr[] // assume that arr[0] to arr[last-1] // are in heap order // currPos is the index that moves up the // path of parents int currPos = last; int parentPos = (currPos-1)/2; // see slide 16 :

  44. // move up parents path to the root while (currPos != 0) { // compare target and parent value if (comp.compare(item,arr[parentPos]) < 0) { arr[currPos] = arr[parentPos]; // move data from parent --> current currPos = parentPos; parentPos = (currPos-1)/2; // get next parent } else // heap condition is ok break; } arr[currPos] = item; // put item in right location } // end of pushHeap()

  45. 5.2. Deleting from a Heap popHeap() • Deletion is normally restricted to the root only • remove the maximum element (in a max heap) • To erase the root of an n‑element heap: • exchange the root with the last element (the one at index n‑1); delete the moved root • filter (sift) the new root down to its correct position in the heap call adjustHeap()

  46. Deletion Example for a Max Heap • Delete 63 • exchange with 18; remove 63 • filter down 18 to correct position continued

  47. (63) removed continued

  48. Move 18 down: • smaller than 30 and 40; swap with 40 18 18 38 continued

More Related