0
0
Data Structures Theoryknowledge~6 mins

Collision handling (chaining) in Data Structures Theory - Full Explanation

Choose your learning style9 modes available
Introduction
Imagine you have a list of items to store, but sometimes two items want to go to the same spot. This problem happens in hash tables when two keys produce the same address. We need a way to handle these clashes so no data is lost or overwritten.
Explanation
Hash Table Collisions
When a hash function assigns the same index to two different keys, a collision occurs. This means the hash table cannot store both items in the same slot directly. Handling collisions is essential to keep the hash table efficient and accurate.
Collisions happen when different keys map to the same index in a hash table.
Chaining Method
Chaining solves collisions by storing all items that hash to the same index in a linked list or chain. Instead of one item per slot, each slot points to a list of items. This way, multiple keys can share the same index without overwriting each other.
Chaining uses linked lists at each slot to hold multiple items that collide.
Insertion in Chaining
To insert a key, the hash function finds the index. Then the key is added to the linked list at that index. If the list is empty, a new list starts; if not, the key is appended. This keeps all collided keys accessible.
Insertion adds the new key to the linked list at the hashed index.
Searching in Chaining
To find a key, the hash function locates the index. Then the linked list at that index is searched sequentially. If the key is found in the list, it is returned; otherwise, it is not in the table.
Searching checks the linked list at the hashed index for the key.
Advantages and Drawbacks
Chaining is simple and handles collisions well even when many keys collide. However, if many keys hash to the same index, the linked list grows long, slowing search and insertion. Good hash functions and table sizing help reduce this problem.
Chaining is easy to implement but can slow down if many keys collide at one index.
Real World Analogy

Imagine a mailbox where letters are sorted by street address. Sometimes, multiple letters go to the same mailbox slot. Instead of throwing letters away, the mailbox has a small basket to hold all letters for that address. You check the basket to find your letter.

Hash Table Collisions → Multiple letters addressed to the same mailbox slot
Chaining Method → A basket inside the mailbox slot holding all letters for that address
Insertion in Chaining → Putting a new letter into the basket at the mailbox slot
Searching in Chaining → Looking through the basket to find the correct letter
Advantages and Drawbacks → The basket keeps letters safe but can get crowded, making it harder to find one
Diagram
Diagram
┌───────────────┐
│ Hash Table    │
├───────────────┤
│ Index 0       │ → None
│ Index 1       │ → [Key A] → None
│ Index 2       │ → [Key B] → [Key C] → None
│ Index 3       │ → None
│ Index 4       │ → [Key D] → None
└───────────────┘
This diagram shows a hash table where some indexes point to linked lists (chains) holding multiple keys due to collisions.
Key Facts
CollisionWhen two different keys hash to the same index in a hash table.
ChainingA collision handling method that stores collided keys in a linked list at the same index.
Linked ListA sequence of nodes where each node points to the next, used to store multiple items in chaining.
Hash FunctionA function that converts a key into an index for the hash table.
Load FactorThe ratio of stored keys to the number of slots in the hash table, affecting performance.
Code Example
Data Structures Theory
class HashTable:
    def __init__(self, size=5):
        self.size = size
        self.table = [[] for _ in range(size)]

    def hash_function(self, key):
        return hash(key) % self.size

    def insert(self, key, value):
        index = self.hash_function(key)
        # Check if key exists and update
        for i, (k, v) in enumerate(self.table[index]):
            if k == key:
                self.table[index][i] = (key, value)
                return
        # Otherwise, append new key-value
        self.table[index].append((key, value))

    def search(self, key):
        index = self.hash_function(key)
        for k, v in self.table[index]:
            if k == key:
                return v
        return None

# Example usage
ht = HashTable()
ht.insert('apple', 10)
ht.insert('banana', 20)
ht.insert('grape', 30)  # Might collide with 'apple'
print(ht.search('apple'))
print(ht.search('banana'))
print(ht.search('grape'))
print(ht.search('orange'))
OutputSuccess
Common Confusions
Chaining stores all keys in one big list for the whole table.
Chaining stores all keys in one big list for the whole table. Chaining stores keys in separate linked lists at each index, not one global list.
Chaining removes the need for a good hash function.
Chaining removes the need for a good hash function. A good hash function is still important to distribute keys evenly and keep chains short.
Summary
Collisions happen when different keys map to the same hash table index and must be handled to avoid data loss.
Chaining handles collisions by storing collided keys in linked lists at each index, allowing multiple keys per slot.
Good hash functions and proper table size keep chains short and operations efficient.