Hash Tables Flashcards
What is a dictionary?
A data structure that stores data by mapping keys to values
What are the methods that are implemented in dictionaries?
insert remove find size isEmpty keys values
Describe some possible implications of design decisions with dictionaries
Insert - overwrite existing keys?
Find - fail or return null for missing key?
What are the three possible implementations of associative arrays (dictionaries)?
List
Array
Binary search tree
Describe some features of list implementation of a dictionary?
Linear search complexity looking for keys
Simple
Describe some features of array implementation of a dictionary?
Same list of key/value pairs
Fixed size
O(n) search/add/remove
Describe some features of binary search tree implementation of a dictionary?
O(log n) search, addition and removal
Need an order on keys
What do hash tables provide?
Key-index mapping in a simple and uniform way
Describe a hash table
Data structure that implements a dictionary, by using a hash function to compute an index into an array of buckets from which the desired value can be found
What is a hash function?
Takes a key and produces an index/hash code
Describe the two stages in a hash function
- Hash key to its hash code l using the hash function h(k)
- Use the hash code as an index into a bucket containing the value associated with the key
What is a collision?
Two different keys will map to the same hash code
How is the has code further reduced to try and avoid collisions?
Using modulo arithmetic
Give some desirable properties of hash functions
Gives two far apart hash codes for values close together
Two keys are unlikely to have the same hash code
Maps the keys uniformly across the range of hash codes
What are two common methods of compression mapping?
Division method (modulo arithmetic) Multiple and Divide
When do compression mapping methods work best?
When m is prime