0
0
Data Structures Theoryknowledge~10 mins

Collision handling (open addressing) in Data Structures Theory - Step-by-Step Execution

Choose your learning style9 modes available
Concept Flow - Collision handling (open addressing)
Start Insert
Compute Hash Index
Is Slot Empty?
NoProbe Next Slot
Wrap Around if Needed
Insert Key Here
End Insert
When inserting, compute the hash index. If the slot is taken, check the next slot until an empty one is found, wrapping around if needed.
Execution Sample
Data Structures Theory
hash_table = [None]*5
keys = [12, 22, 32]
for key in keys:
  index = key % 5
  while hash_table[index] is not None:
    index = (index + 1) % 5
  hash_table[index] = key
Insert keys 12, 22, 32 into a hash table of size 5 using linear probing to handle collisions.
Analysis Table
StepKeyComputed IndexSlot Empty?ActionHash Table State
1122YesInsert 12 at index 2[None, None, 12, None, None]
2222NoProbe next index 3[None, None, 12, None, None]
3223YesInsert 22 at index 3[None, None, 12, 22, None]
4322NoProbe next index 3[None, None, 12, 22, None]
5323NoProbe next index 4[None, None, 12, 22, None]
6324YesInsert 32 at index 4[None, None, 12, 22, 32]
7---All keys inserted[None, None, 12, 22, 32]
💡 All keys inserted; no empty slots needed beyond index 4.
State Tracker
VariableStartAfter Step 1After Step 2After Step 3After Step 4After Step 5After Step 6Final
hash_table[None, None, None, None, None][None, None, 12, None, None][None, None, 12, None, None][None, None, 12, 22, None][None, None, 12, 22, None][None, None, 12, 22, None][None, None, 12, 22, 32][None, None, 12, 22, 32]
index-233344-
Key Insights - 3 Insights
Why do we check the next slot when the computed index is already occupied?
Because in open addressing, if the slot is taken, we must find the next available slot by probing, as shown in steps 2 and 4 in the execution_table.
What happens if we reach the end of the table while probing?
We wrap around to the start of the table and continue probing, as indicated by the modulo operation in the code and the wrap-around step in the concept_flow.
Why do we stop probing once we find an empty slot?
Because the empty slot is where we can safely insert the new key without overwriting existing data, as shown in steps 1, 3, and 6.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution_table, what is the hash_table state after inserting key 22?
A[None, None, 22, None, None]
B[None, None, 12, None, None]
C[None, None, 12, 22, None]
D[None, None, 12, None, 22]
💡 Hint
Check the hash_table state in row 3 of the execution_table after step 3.
At which step does the condition 'Slot Empty?' become false for the first time?
AStep 1
BStep 2
CStep 4
DStep 5
💡 Hint
Look at the 'Slot Empty?' column in the execution_table to find the first 'No'.
If the table size was 3 instead of 5, what would happen when inserting key 32?
AIt would insert at index 0 after probing.
BIt would cause an infinite loop probing slots.
CIt would insert at index 2 without probing.
DIt would overwrite an existing key.
💡 Hint
Consider modulo operation with table size 3 and how probing wraps around.
Concept Snapshot
Open addressing handles collisions by probing for the next empty slot.
Compute hash index: index = key % table_size.
If slot occupied, check next slot (linear probing).
Wrap around to start if end reached.
Insert key at first empty slot found.
No extra memory needed for chaining.
Full Transcript
Collision handling with open addressing means when two keys hash to the same index, we look for the next empty slot to place the new key. We start by computing the hash index using modulo operation. If the slot is empty, we insert the key there. If not, we move to the next slot, wrapping around to the start if we reach the end of the table. This process continues until an empty slot is found. The example shows inserting keys 12, 22, and 32 into a table of size 5. Key 12 goes to index 2 directly. Key 22 also hashes to 2 but finds it occupied, so it moves to index 3. Key 32 probes indexes 2 and 3, both occupied, and inserts at index 4. This method avoids linked lists and keeps all keys in the table itself.