• ↑↓ pour naviguer
  • pour ouvrir
  • pour sélectionner
  • ⌘ ⌥ ↵ pour ouvrir dans un panneau
  • ←→ pour naviguer
  • esc pour rejeter
⌘ '
raccourcis clavier

The Problem

When two keys hash to the same index, we need to store both. Arrays only hold one value per slot.

Core Idea

Each slot in the hash table holds a linked list (chain) of all key-value pairs that hashed to that index.

How It Works

  1. Hash function computes index for a key
  2. Key-value pair is appended to the linked list at that index
  3. To search: hash to index, then linearly search the chain
  4. To delete: find in chain and remove node
chaining T Hash Table (array) I0 Index 0 null T->I0 I1 Index 1 → (Alice, 100) → (Bob, 200) T->I1 I2 Index 2 → (Charlie, 300) T->I2

Key Properties

  • Simple to implement
  • Handles high load factors better than probing
  • Each chain should be short (ideally O(1) length)
  • Deletion is straightforward (just remove from linked list)

Connections

Edge Cases & Gotchas

  • Long chains → degrades to O(n) search time
  • Extra memory for linked list pointers
  • Poor hash function → all keys in one chain
  • Best when hash function distributes uniformly