CSC297.Java, Class 36: AVL Trees, Dictionaries, and More Admin: * No new BishopD homework. * Your new homework (due next Monday; ask questions on Wednesday and Friday): Implement AVL trees. * When should we have your final? * Official: Monday at 9 a.m. http://www.grinnell.edu/office/registrar/ * Let's use Monday * Closed book, closed notes (maybe one page of notes) * Topics: Design an ADT; Compute runnign time of an algorithm, etc. * Homework due. Overview: * AVL trees * Key balancing operations * Sketch of implementation * Dictionaries, revisited. * Implementing dictionaries with association lists. * Implementing dictionaries with BSTs. * Hash tables. Balancing operations for AVL trees after insertion. * See the real whiteboard. What about deletion? * Same thing, except ... you have to check every parent node to see if another rotation is required. Implementation: * Use a variant of the Nodes we used for BSTs. * Each node should keep track of the height of its subree. * Each node might keep track of whether it is balanced, imbalanced to the left, or imbalanced to the right. Review: What is a dictionary? * Stuff that is referenced by string. How do you implement a dictionary? * As a vector of key/value pairs. * put: O(n), because you have to first check if it's already there. * get: O(n), because you have to look at each thing. * replace: O(n), because you have to look at each thing. * delete: O(n) * As a *sorted* vector of key/value pairs * get: O(log_2(n)), because you can use binary search * replace: O(log_2(n)), because you can use binary search * put: O(n), because you have to "shove stuff over" to make room * delete: O(n), because you have to "shove stuff back" * As a BST of key/value pairs, using "compare the keys" as your mayPrecede operation. * put: O(depth) O(log_2(n)) in balanced trees. * get: O(depth) O(log_2(n)) in balanced trees. * replace: O(depth) O(log_2(n)) in balanced trees. * delete: O(depth) O(log_2(n)) in balanced trees. * In a balanced tree: * number of elements = 2^(number of levels) * number of levels = log_2(number of elements) * Can we do better? * If our keys are numbers in the range [O .. k), can we get O(1) for all the operations? * Sure, use a vector of size k. * Numbers+Vectors give O(1), strings+BSTs give O(log_2(n)) * Can we go from strings to numbers? Hash table: * Implement a dictionary using a vector. * To put something into the dictionary, compute a number (the hashValue) for its key, and shove it in that place in the vector. * To look up something by key, compuate a number for the key and look in the vector. * Replace and delete are similar. Sounds good in the abstract. * How do we compute that number? * What happens if two keys have the same number? To compute the number (in Java), use String.hashCode() * In Java, all classes are expected to provide such a function. * String.hashCode uses something like "sum of char[i]*32^(length-i) mod some big number" * Prevents averages from being such a strong factor. Implementation: * Need helper class, Pair.java, to store key/value pairs. What should happen if two key/value pairs end up in the same position? * Option one: Find another position (usually the next blank cell) * To search, you now need to look in not just the current cell, but also subsequent cells, until you find the element or an empty cell. * To delete, you need to do something painfully similar. * Option two: Put more than one thing in each cell. (Make each cell a list or Vector.) * Searching is a little harder. * Deleting is a little harder. * Option three: Expand the size of the array and cross your fingers that you wont' get another conflict. * Expensive * Lots of "cross your fingers" Have a good day. Start your AVL tree implementation.