Concept Flow - Collision Handling Using Chaining
Compute hash index
Check bucket at index
Empty
Insert
Add new node at end
Done
This flow shows how a key is hashed to an index, then inserted into a linked list at that index if a collision occurs.
class HashTable { constructor(size) { this.buckets = Array(size).fill(null); } insert(key, value) { const index = this.hash(key); if (!this.buckets[index]) { this.buckets[index] = { key, value, next: null }; } else { let current = this.buckets[index]; while (current.next) current = current.next; current.next = { key, value, next: null }; } } hash(key) { return key.length % this.buckets.length; } }
| Step | Operation | Index | Bucket State Before | Pointer Changes | Bucket State After | Visual State |
|---|---|---|---|---|---|---|
| 1 | Insert ('cat', 1) | 3 | [null, null, null, null, null] | buckets[3] = new node | [null, null, null, {cat:1, next:null}, null] | [null, null, null, cat(1) -> null, null] |
| 2 | Insert ('dog', 2) | 3 | [null, null, null, cat(1) -> null, null] | Traverse to end, add new node | [null, null, null, cat(1) -> dog(2) -> null, null] | [null, null, null, cat(1) -> dog(2) -> null, null] |
| 3 | Insert ('bird', 3) | 4 | [null, null, null, cat(1) -> dog(2) -> null, null] | buckets[4] = new node | [null, null, null, cat(1) -> dog(2) -> null, {bird:3, next:null}] | [null, null, null, cat(1) -> dog(2) -> null, bird(3) -> null] |
| 4 | Insert ('cow', 4) | 3 | [null, null, null, cat(1) -> dog(2) -> null, bird(3) -> null] | Traverse to end, add new node | [null, null, null, cat(1) -> dog(2) -> cow(4) -> null, bird(3) -> null] | [null, null, null, cat(1) -> dog(2) -> cow(4) -> null, bird(3) -> null] |
| 5 | Insert ('fish', 5) | 4 | [null, null, null, cat(1) -> dog(2) -> cow(4) -> null, bird(3) -> null] | Traverse to end, add new node | [null, null, null, cat(1) -> dog(2) -> cow(4) -> null, bird(3) -> fish(5) -> null] | [null, null, null, cat(1) -> dog(2) -> cow(4) -> null, bird(3) -> fish(5) -> null] |
| 6 | Stop | - | - | - | - | All keys inserted, chaining handled collisions |
| Variable | Start | After Step 1 | After Step 2 | After Step 3 | After Step 4 | After Step 5 | Final |
|---|---|---|---|---|---|---|---|
| buckets | [null, null, null, null, null] | [null, null, null, cat(1) -> null, null] | [null, null, null, cat(1) -> dog(2) -> null, null] | [null, null, null, cat(1) -> dog(2) -> null, bird(3) -> null] | [null, null, null, cat(1) -> dog(2) -> cow(4) -> null, bird(3) -> null] | [null, null, null, cat(1) -> dog(2) -> cow(4) -> null, bird(3) -> fish(5) -> null] | [null, null, null, cat(1) -> dog(2) -> cow(4) -> null, bird(3) -> fish(5) -> null] |
| index | - | 3 | 3 | 4 | 3 | 4 | - |
| current | - | cat(1) | dog(2) | bird(3) | cow(4) | fish(5) | - |
Collision Handling Using Chaining: - Use a linked list at each bucket to store multiple keys. - Hash key to index. - If bucket empty, insert node. - If not, traverse list and append node. - Handles collisions by chaining nodes in list.
collision handling using chaining in hash tables?fill([]) fills all indexes with the same array reference.Array.from with a function to create unique arraysArray.from({ length: 10 }, () => []) creates a new empty array at each index.const table = Array.from({ length: 5 }, () => []);
function hash(key) { return key % 5; }
// Insert keys
[7, 12, 17].forEach(key => {
const index = hash(key);
table[index].push(key);
});
console.log(table);const table = new Array(3).fill([]);
function hash(key) { return key % 3; }
function insert(key) {
const index = hash(key);
table[index].push(key);
}
insert(1);
insert(4);
insert(7);
console.log(table);Array.from({ length: 3 }, () => []) to create separate arrays.key % 4. After all insertions, which index has the longest chain and what is its content?Pass the test and this pattern moves unknown