CSC153 2004S, Class 51: The Dictionary ADT Admin: * Interesting convo yesterday; Almost all the students there were there to have their name announced. * Questions on HW 8? * Hmmm ... there are 13 students preregistered for each of my two classes. (CSC152-CSII and CSC223-Software Design.) * The picnic may move to the science elbow. Announcement at 1ish. Overview: * Dictionary Basics * Applying Dictionaries * Dictionary Methods * Dictionaries vs. Databases * Implementation 1: Lists * Implementation 2: BSTs The Dictionary ADT * Philosophy * Applications * Methods Philosophy of the Dictionary ADT * Like arrays, but indexed by arbitrary objects rather than integers * Typically, strings are used as the indices * The indices are called "keys" Applications: * Build "real" dictionaries (associate a definition with a word) * Indexing collections of stuff * Counting words * Sets Design questions: * Can you have more than one value associated with an index? Typically no. * However, you can make a list the associated value. * Are the keys homogenous or heterogeneous? All objects * Are the associated values homogenous or hextexroxgenyqeous? All objects Methods: * Object get(Object key) * If the key is not in the dictionary, could throw an exception or *return null* * Returning null: A lot like arrays of objects: If you don't put something in a space, null is there. * Object set(Object key, Object value) * If the key is already in the dictionary, overwrite and return the old object * Object getKey(Object value) - Returns *a* key of the object. * Some iteration method * Iterate key/value Pairs * Iterate keys * Iterate values ['Eh] * Constructors * Object remove(Object key) * Vs. setting to null * void clear() * void dump(OutputStream pen) -> Better implemented by making it serializable? * String toString() Implementation (Data Structures): * Philosophy * Efficiency * Details Philsophy 1: Use an unordered list or an array. Put key/value pairs in the list or array. * Efficiency: * set: O(1): Replace the head and change your pointer; Whoops ... if it has to check for an existing key/value pair, O(n) * get: O(n) Philosophy 2: Use an ordered array (assumes keys can be compared) * Efficiency: * set: O(n), since we may have to "shove everything over" * get: O(logn) Philosophy 3: Use a tree (Binary Search Trees: BST) (assumes keys can be compared) * Binary trees with key/value pairs in which the keys of left descendants of a node are less than the key of the node and keys of right descendants are greater * Efficiency (Assume the tree is close to complete) * set: O(log(n)) * get: O(log(n)) * Problem: If the tree is not balanced (and it's not likely to be), these can both be as bad as O(n) * Solution: Balance the tree * How: Take 301 or wait until next week (if we have time) Philosophy 4: Hash Tables: Convert each key to a number and use an array * Problems: * The numbers may be really big. Solution: Mod by the size of the array you've chosen. * There are more objects than integers (particularly if you mod): Solution: Associate a list or BST with each cell. Questions: * Tell me more about the Iowa guy [Andy] * Why don't we use theta or little-o? [Saul] Observation: Iowa Frat Boys may be vampires.