Definition

A data structure used to implement a Map ADT (dictionary) (keys to values). It uses a hash function to map keys to corresponding indices in an Array

Mathematical definition

A hash table for a given key type consists of:

  • Hash function
  • An array (aka table) of size Ideally, an item will therefore be stored at

Hash function

A hash function should:

  • Give the same result for any particular key
  • Be efficient to compute -
Designing hash functions

Usually, an efficient hash function is composed of two functions (noting that keys can be of different types):

  • Hash code:
    • Disperses the keys in an approximately random way to integers, attempting to avoid collisions
  • Compression function:
    • Compresses back to ‘allowed range’ for the final array index

Thus the final function would be in the form:

Approaches

There are two general approaches that one can take:

  • View the key as a tuple of integers with each being an integer in the range for some
  • View the key as (possibly very large) nonnegative integer

Performance

AlgorithmAverageWorst case
SpaceΘ(n)O(n)
Search getΘ(1)O(n)
Insert putΘ(1)O(n)
Delete removeΘ(1)O(n)

  • task Complete section on designing hash functions and collisions