Podcast Ch22a Title: Array-based Binary Trees Description: Representing binary trees in an array; the Comparator interface; sorting using a comparator Participants: Barry Kurtz (instructor); John Helfert and Tobie Williams (students) Textbook: Data Structures for Java; William H. Ford and William R. Topp
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. 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.
Array-Based Binary Trees (continued) Integer[] arr = {5, 1, 3, 9, 6, 2, 4, 7, 0, 8};
Array-Based Binary Trees (concluded) For element arr[i] in an n-element array‑based binary tree, the following relationships hold: Left child of arr[i] is arr[2*i + 1] undefined if 2*i + 1 n Right child of arr[i] is arr[2*i + 2] undefined if 2*i + 2 n Parent of arr[i] is arr[(i-1)/2] undefined if i = 0
Which tree corresponds to the array arr? int arr[] = {3, 12, 15, 4, 67, 6, 55, 9};
The children of node arr[i] are found at nodes (i) arr[i+1], arr[i+2] (ii) arr[2*i + 1], arr[2*i + 2] (iii) arr[2*i], arr[2*i+1] (iv) arr[2*(i + 1)], arr[2*(i + 2)] The parent of node arr[i] is found at node (i) arr[i/2] (ii) arr[i % 2] (iii) arr[(i-1)/2] (iv) arr[(i + 1)/2] If an array contains 50 elements, the leaf nodes begin at index _________________
Assume that arr is an array-based tree with 95 members. What is the parent of arr[35]? (i) 22 (ii) 17 (iii) 18 (iv) 10 (b) What is the left child of arr[14]? (i) 15 (ii) 28 (iii) 29 (iv) 30 (c) What is the index of the first leaf node? (i) 46 (ii) 47 (iii) 50 (iv) 94 (d) What is the depth of the tree? (i) 5 (ii) 7 (iii) 6 (iv) 3 (e) How many leaf nodes does the tree have? (i) 47 (ii) 50 (iii) 49 (iv) 48
Complete the implementation of the recursive method preorderHeap() which outputs an array-based tree using a preorder scan? (int i is the root index) public static <T> void preorderHeap(T[] arr, int i){ if (i < arr.length){ System.out.print(arr[i] + " "); preorderHeap (arr, 2 * i + 1); preorderHeap (arr, 2 * i + 2); }
The Comparator Interface 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. 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.
The Comparator Interface (continued)
The Comparator Interface (continued) 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.
General Comparison Objects If a class implements the Comparable interface, we can develop generic Comparator classes that compare two objects for either the < or the > relation. The classes implement compare() by using the compareTo() ordering between objects.
The < Comparator import java.util.Comparator; // constructs the < Comparator public class Less<T> implements Comparator<T> { public int compare(T x, T y) return ((Comparable<T>)x).compareTo(y); }
The > Comparator // constructs the > Comparator public class Greater<T> implements Comparator<T> { public int compare(T x, T y) return -((Comparable<T>)x).compareTo(y); }
Complete the implementation of the string comparator class ShorterString which compares the length of the two strings. public class ShorterString implements Comparator<String> { public int compare(String strA, String strB) if (strA.length() < strB.length()) // less than case return -1; else if (strA.length() == strB.length()) // equals case return 0; else return 1; // greater than case }
Now write a version of the comparator class ShorterString that only has a single line return command. public class ShorterString implements Comparator<String> { public int compare(String strA, String strB) return strA.length() - strB.length(); }
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.
Selection Sort with Comparator // new version adds a Comparator parameter public static <T> void selectionSort( T[] arr, Comparator<? super T> 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;
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; }