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
Describe the Multiple and Divide (MAD) method for compression mapping
c(l) = (al + b)mod m for some a and b
What is a cryptographic hash function?
Hash function that given a hash code l, it is very hard to generate a k such that h(k) = l
Also known as non-invertible function
What are cryptographic hash functions used for?
Tamper proof files
Pros of indexed storage classes
Very fast O(1) random access if you have an index, otherwise O(n)
Disadvantages of indexed storage classes
Generally not expandable without copying
Pros of sequential storage classes
Generally fast to expand by adding head or tail
Disadvantage of sequential storage classes
Everything built by traversal from end(s) so generally O(n)
Pros of associative storage classes
Use almost any value as key
Can address any key randomly