CSC297.Java, Class 35: Dictionaries Admin: * How did the homework go? * Alex tried and couldn't do it * Yvonne and three upper-level CS students couldn't do it. * Weekly Bishop check (No new assignments) * Thoughts on a final * Homework: * Look up AVL trees and find out how to insert and delete into them. Overview: * Review of homework * Detour: Balanced trees * The dictionary ADT. * Implementing dictionaries with sorted lists. * Implementing dictionaries with search trees. * Looking ahead: Hash tables Morals from attempting to implement delete and find * Think carefully about the strategy you're using. * If your strategy involves you doing two things at once (e.g., deleting and finding a value), you're likely to screw up one of the two and it will be hard to figure out which. * We don't always have to balance binary trees. (Of course, if they're not balanced, they won't be efficient. However, they can't ever be less efficient than sorted lists.) * Complete trees are almost impossible to maintain. Instead, we look for "balanced" trees (e.g., not more than a 2:1 ratio of subtree sizes) * Balanced subtrees can stil have O(log_2(n)) depth. Techniques for balancing binary search trees: * AVL trees * 2-3 trees * Red-black trees * B-trees * "Splaying" We'll try AVL trees (named after their designers, Adelson-Velskii and Landis) Key idea: Allow each node to have different depth subtrees, but allow the subtrees to differ in height by only one. * How unbalanced can this be? Probably about a factor of 2 in terms of nodes. Problem: How do we keep a tree balanced during addition and deletion? Solution: Keep track of balance and "rotate" key nodes in the tree. The Dictionary ADT. (Also called "Keyed Table") * Dictionaries are collections of values indexed by string. * Like vectors (collections of values indexed by number), except that the index is a string rather than a number. * Quite similar to binary search trees, when you assume that the key is the name and the comparator is a string. * Similar to hash in Perl. * Similar to association lists in Scheme. How do we implement dictionaries? Wait until next week to find out.