Ch 8: Hash Tables Flashcards
what is a hash table?
a data structure that stores unordered items by mapping (hashing) each item to a location in an array (or vector)
what is the main advantage of hash tables?
an item may require only O(1) insert, remove and search
what is a key?
a value used to map to an index
what is a bucket?
each hash table array element
what is a hash function?
computes a bucket index from the item’s key
what is collision?
occurs when an item is inserted into a hash table maps to the same bucket as an existing item in the hash table
what is chaining?
a collision resolution technique where each bucket has a list of items
what is open addressing?
a collision resolution technique where collisions are resolved by looking for an empty bucket elsewhere in the table
what is linear probing?
handles a collision by starting at the key’s mapped bucket, then linearly searches subsequent buckets until an empty bucket is found
what are two types of buckets? define them
empty-since-start: has been empty since the hash table was created
empty-after-removal: had an item removed that caused the bucket to now be empty
why does search stop at an empty-since-start but not an empty-after-removal?
stops because if previously inserting the item had reached that bucket, then it would have put it there. but since it has been empty since the start, it must not have been inserted
explain removal with linear probing
- determine the initial bucket
- probe each bucket until a matching item is found, an empty-since-start bucket is found, or all buckets have been probed
what is quadratic probing?
starting at the key’s mapped bucket, and then quadratically searches subsequent buckets until an empty bucket is found
what is the hash table function for quadratic probing
(H + c1 *i + c2&(i^2)) mod (tablesize)
H: item’s initial mapped bucket
c1 & c2: programmer definined constants for quadratic probing
i: increment
explain insert for quadratic probing
- use formula, starting with i = 0 to find bucket
- insert if bucket is empty
- otherwise, increment i and use the formula to find a new bucket, repeat