Home

Hash Tables


A hash table is a data structure that implements a map.

It uses an array to store key-value pairs, and a hash function that, given a key, computes an index into the array where the associated value can be found.

Operations

Performance

Hashing


Hashing is the process of mapping data of arbitrary size to fixed-size values using a hash function

A hash function:

A hash collision occurs when for two keys x and y, x $\neq$ y, but h(x) = h(y).

Collision Resolution


Important statistic: load factor (α)

Separate Chaining


Resolve collisions by having multiple items per array slot. Each array slot contains a linked list of items that are hashed to that index.

separatechaining

Cost analysis:

Average costs:

Linear Probing


Resolve collisions by finding a new slot for the item

linear probing

Two primary methods for deletion:

  1. Backshift
  1. Tombstone

Backshift method:

Tombstone method:

Problem with linear probing: clustering

Double Hashing


Double hashing improves on linear probing:

To generate relatively prime number:

Summary of Collision Resolution


Collision resolution approaches:

All approaches can be used to achieve O(1) performance on average, assuming

Implementation Details


How do we resize a hash table?