Hash Table Simulator
Insert, search and delete keys with four collision strategies, see the hash value and every probe explained, collisions and load factor, tombstones and automatic resizing.
Berjalan dalam pelayar anda. Fail anda tidak pernah meninggalkan peranti. Lihat cara menyemaknya
…
Cara melakukannya
- 1Set up the table
Choose the size, the collision method and an optional resize threshold.
- 2Insert keys
Type keys and press Insert all, or insert one key at a time.
- 3Read the log
Follow the hash value, probes and collisions in What happened.
- 4Search and delete
Type a key and press Search to see its probe path, or Delete to remove it.
- 5Watch the statistics
Check the load factor, collisions, average and expected probes.
- 6Practise and print
Answer hashing questions in Practice and print the final table with the log.
Tentang alat ini
A hash table stores keys so they can be found in about one step on average: a hash function turns each key into a slot number. When two keys land in the same slot, a collision, the table needs a strategy. Data structures courses ask students to insert a list of keys by hand under different strategies and show the final table. This simulator does exactly that with separate chaining, linear probing, quadratic probing and double hashing. Every insertion is explained: the hash value, each probe, each collision and where the key ends up. You can search for keys and see the probe path highlighted, delete keys with tombstones in open addressing, watch the load factor and average probes, and let the table resize automatically when it gets too full.
Hash functions
Whole-number keys use h(k) = k mod m, where m is the table size. Text keys are first turned into a number with a polynomial hash over their character codes. Prime table sizes spread keys more evenly, which is why the tool suggests the next prime when you choose a composite size.
Collision strategies
- **Separate chaining**: each slot holds a list, and colliding keys are added to it. The table never fills, but lists grow. - **Linear probing**: try slots h, h + 1, h + 2, … Simple, but keys form clusters that make later probes longer. - **Quadratic probing**: try h, h + 1, h + 4, h + 9, … which breaks up clusters, but can fail to find a free slot when the table is more than half full. - **Double hashing**: the step is a second hash, here h₂(k) = 1 + (k mod (m − 1)), so different keys follow different probe sequences.
Load factor and performance
The load factor α = n ÷ m is the number of keys per slot. For linear probing the expected number of probes for a successful search is about ½(1 + 1 ÷ (1 − α)), which rises sharply as α approaches 1; for chaining it is about 1 + α ÷ 2. The figures show the load factor, collisions and average probes so far next to this expected value. With a resize threshold such as 0.75, the table grows to the next prime at least twice its size and rehashes every key, which you can watch happen in the log.
Deleting in open addressing
Simply emptying a slot would break searches for keys that were placed after probing past it. The tool marks the slot as deleted (a tombstone): searches continue past it, and insertions can reuse it.
Worked example
Insert 10, 22, 31, 4, 15, 28, 17, 88, 59 into a table of size 11 with linear probing (a classic textbook exercise). 22 mod 11 = 0, 88 also hashes to 0 and moves to 1, 15 and 4 land at 4 and 5 after a collision, and so on. The final table is 22, 88, empty, empty, 4, 15, 28, 17, 59, 31, 10. With double hashing the same keys give 22, empty, 59, 17, 4, 15, 28, 88, empty, 31, 10.
Limits
The simulator teaches the classic strategies with simple hash functions. Production hash tables use stronger hash functions, different growth policies and techniques such as Robin Hood hashing or cuckoo hashing, which are not modelled. Table size is limited to 101 slots.
Contoh berselesaian
Size 11, linear probing: 10 22 31 4 15 28 17 88 59 → [22, 88, -, -, 4, 15, 28, 17, 59, 31, 10].
Ringkasnya
- Kategori: Pembangun & data
- Berjalan dalam pelayar, tanpa pendaftaran, tanpa muat naik.
- Maklumat yang anda masukkan kekal pada peranti anda.
- Tersedia dalam bahasa Inggeris.
- Kandungan disemak kali terakhir:
- Sumber rasmi: Cormen, Leiserson, Rivest and Stein, Introduction to Algorithms, 4th edition, chapter 11
Soalan lazim
What is a collision?
Two different keys hashing to the same slot. Every strategy in the tool is a different way of handling it.
Why use a prime table size?
With k mod m, a prime m avoids patterns in the keys (such as all even numbers) mapping to only some slots.
What is the load factor?
The number of stored keys divided by the number of slots. Open addressing tables are usually resized before it passes about 0.7.
Why can quadratic probing fail?
Its probe sequence only visits about half of the slots when the table size is prime, so it may not reach a free slot in a table that is more than half full.
What is a tombstone?
A marker left in a deleted slot so that searches keep probing past it; insertions may reuse it.
Can I use text keys?
Yes. Words are turned into numbers with a polynomial hash before taking the remainder.
