Published by Dimitris Kougioumtzis · Updated
Insert keys, delete a key, insert another key, then search. Resolve collisions with bucket chains.
Keep a linked sequence of keys at each bucket. New keys are prepended; deletion reconnects the predecessor to the next node. Insert initial keys, delete a key, insert one more key, then search the final set.
When to use Hash table separate chaining
Teaching collision resolution, duplicate suppression, deletion and reinsertion in bounded hash sets.
Choosing an alternative
Open addressing stores keys directly in slots and needs tombstones; chaining stores separate linked records.
Worked example: input and state changes
Example input
1,18,35,52,69 | 18,86,35
These checkpoints follow the actual example timeline. State details show the stored values and decisions; use the visualization to inspect every intervening step.
Step 0: Initialize 17 empty buckets.
State at this checkpoint
{ "values": [ "empty", "empty", "empty", "empty", "empty", "empty", "empty", "empty", "empty", "empty", "empty", "empty", "empty", "empty", "empty", "empty", "empty" ], "active": [], "kind": "matrix", "columns": 4 }Step 5: Insert 35: inspect bucket 1.
State at this checkpoint
{ "values": [ "empty", "18,1", "empty", "empty", "empty", "empty", "empty", "empty", "empty", "empty", "empty", "empty", "empty", "empty", "empty", "empty", "empty" ], "active": [ 1 ], "kind": "matrix", "columns": 4 }Step 10: Bucket 1: 69 > 52 > 35 > 18 > 1.
State at this checkpoint
{ "values": [ "empty", "69,52,35,18,1", "empty", "empty", "empty", "empty", "empty", "empty", "empty", "empty", "empty", "empty", "empty", "empty", "empty", "empty", "empty" ], "active": [ 1 ], "kind": "matrix", "columns": 4 }Step 14: Bucket 1: 86 > 69 > 52 > 35 > 1.
State at this checkpoint
{ "values": [ "empty", "86,69,52,35,1", "empty", "empty", "empty", "empty", "empty", "empty", "empty", "empty", "empty", "empty", "empty", "empty", "empty", "empty", "empty" ], "active": [ 1 ], "kind": "matrix", "columns": 4 }Step 19: 35 found.
State at this checkpoint
{ "values": [ "empty", "86,69,52,35,1", "empty", "empty", "empty", "empty", "empty", "empty", "empty", "empty", "empty", "empty", "empty", "empty", "empty", "empty", "empty" ], "active": [], "kind": "matrix", "columns": 4, "result": 1 }
Expected final state
35 found.
Complexity and input bounds
Family: Data structures. Time complexity: O(n) worst-case per operation.
Implementation detail: There are seventeen buckets or slots, keys 0..99, at most seven initial keys and one later insertion. Chains prepend keys and retain their order during deletion. Downloads return membership and serialize the table, sorted keys and comparison count. Individual operations are linear in the worst case; the reporting pass scans the bounded key universe.
Space complexity: O(n + table capacity + key universe) auxiliary storage, including bounded serialization helpers; animation frames use additional space.
Working-space bounds describe algorithm state and any explicitly noted output, excluding the visualization's saved timeline. Recording frames can use more memory than running the function alone. Here n is sequence length, V and E are vertex and edge counts, and other parameters refer to the named input dimensions.
1..7 initial keys | delete,insert,search keys. Keys 0..99; 17 buckets/slots; duplicate keys ignored.
The playground validates these limits before starting. Downloaded functions are callable implementations, with parameter and output-buffer contracts in their comments; they do not all repeat the playground's input validation.
Common mistakes and limitations
Do not disconnect the rest of a bucket when removing its head or an interior key.
Further reading
For background on algorithm analysis and the JavaScript function interface:
- MIT OpenCourseWare: algorithm design and analysis foundations
- MDN: JavaScript function parameters and return values
Editorial and testing approach · Browse all algorithm guides
Complete JavaScript implementation
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 | // Hash set: n 0..7 initial keys, all keys/deleteKey/insertKey/target 0..99. Insert initial keys, delete once, insert once, then return target membership. Duplicate keys ignored. 17 buckets/slots; at most 8 live keys. Separate chaining prepends nodes. Output capacity at least 132: [0..16] slot values/bucket heads; [17] count; [18..25] sorted keys; [26] search comparisons. Chaining additionally writes [32+key] next key (-1 chain end, -2 absent); these cells are zero for probing. Operations are O(n) worst case; bounded serialization scans the 100-key universe. Auxiliary space O(n + table capacity + key universe). Reference snippets assume validated playground inputs. Fixed scratch buffers have 256 slots. Sorting variants may use a different traversal strategy than the animation. function algorithm(a, n, deleteKey, insertKey, target, output) { let present = Array(256).fill(0); let head = Array(256).fill(0); let key = Array(256).fill(0); let next = Array(256).fill(0); let used = 0; let u = 0; let previous = 0; let bucket = 0; let value = 0; let deleting = 0; let found = 0; let length = 0; let comparisons = 0; for (let operation = 0; operation < n + 2; operation++) { deleting = 0; value = insertKey; if (operation < n) { value = a[operation]; } else if (operation == n) { value = deleteKey; deleting = 1; } bucket = value % 17; u = head[bucket]; previous = 0; while (u != 0 && key[u] != value) { previous = u; u = next[u]; } if (deleting != 0 && u != 0) { if (previous == 0) { head[bucket] = next[u]; } else { next[previous] = next[u]; } present[value] = 0; } else if (deleting == 0 && u == 0) { u = ++used; key[u] = value; next[u] = head[bucket]; head[bucket] = u; present[value] = 1; } } found = 0; value = target; u = head[value % 17]; while (u != 0) { comparisons++; if (key[u] == value) { found = 1; break; } u = next[u]; } for (let i = 0; i < 132; i++) { output[i] = 0; } for (let i = 0; i < 100; i++) { output[32 + i] = -2; } for (let i = 0; i < 17; i++) { output[i] = -1; u = head[i]; if (u != 0) { output[i] = key[u]; } while (u != 0) { output[32 + key[u]] = -1; if (next[u] != 0) { output[32 + key[u]] = key[next[u]]; } u = next[u]; } } for (let i = 0; i < 100; i++) { if (present[i] != 0) { output[18 + length++] = i; } } output[17] = length; output[26] = comparisons; return found; } |
Source
Authoritative authored book reference for hashing and collision-resolution methods. No original research paper for this exact bounded variant has been verified; the playground fixes capacity, hash formulas and operation order for reproducible comparison.
- The Art of Computer Programming, Volume 3: Sorting and Searching (2nd edition)
Donald E. Knuth (1998). Reference book.