Announcements – Exam 2: 03/11, PA2______,HW3______. Today: Loose ends Associative Arrays Hash Tables Intro to Hash Functions
Loose ends… Classic balanced BST structures: • Red-Black trees – max ht 2log 2 n. Constant # of rotations for insert, remove, find. • AVL trees – max ht 1.44log 2 n. O(log n) rotations upon remove. Balanced BSTs, pros and cons: - Insert, Remove, and Find are always O(log n) - An improvement over: - Range finding & nearest neighbor - Possible to search for single keys faster - If data is so big that it doesn’t fit in memory it must be stored on disk and we require a different structure.
Hashing - using “hash tables” to implement _________ Structure of a dictionary: Associative Array: Key -> Value Dictionary with a particular interface Locker # -> student Overloads operator[] for insert and find Course Number -> Schedule info myDict[“Miguel”] = 22; Color -> BMP int d = myDict[“Miguel”]; Vertex -> Set of incident edges Flight number -> arrival information Hash Table: URL -> html page One way of implementing dictionaries dice roll -> payoff amt Commonly appears as an associative array
Hashing: (defn) Keyspace – a (math) description of the keys for a set of data. Goal of hashing: use a function to map the keyspace into a small set of integers. What’s fuzzy about this goal? Problem: Keyspaces are often large…
Overview: class dict client code (implemented with a hash table) declares an object of ADT dictionary array dict<ktype, vtype> d; ex: insert is d[k] = v; hash function k A Hash Table consists of: • • •
A perfect hash function: (Ann, black cat) 0 (Ben, HP) 1 (Cory, spy) 2 (David, bball player) 3 (Ellen, butterfly) 4 (Finn, cereal killer) 5 (Gus, ghost) 6 (Harmony, bee) 7 A contrived example: these keys have a fabulous hash fn. a. each key hashes to a different int b. collection of keys hash to a seq of ints
0 A perfect hash function? 1 2 3 Roll 5 dice: 4 5 6 7 8 9 10 11 12 13 14 15 Is this a good hash function? 16 17 18 19
What characterizes a good hash function? • • • General Purpose Hash Functions KeySpace Easy, if N-1 0 1 … |KeySpace| ~ N KeySpace keys used: N-1 0 1 …
Recommend
More recommend