Data Structures
Hashing Techniques
Objective
To implement and compare four different hashing techniques by
reading words from a file and storing them using hash functions and
collision resolution strategies.
Project Requirements
1. File Input
- Read a list of words from a text file (words.txt), formatted as:
- One word per line or
- Space-separated words
- Preprocess the input by converting words to lowercase
2. Hashing Techniques
Use this hash function
where: b = 31, n is word length and m = 109 + 9 ( largest prime number)
Implement the following four hashing techniques:
2.1. Linear Probing
- Collision resolution by sequentially checking the next slot (open
addressing):
Indexᵢ = (h₁(s) + i) mod T
2.2. Quadratic Probing
- Collision resolution using a quadratic function:
Indexᵢ = (h₁(s) + c₁·i + c₂·i²) mod T
- Typically: c₁ = c₂ = 1
2.3. Double Hashing
- Uses two hash functions:
- Primary: h₁(s) = H(s) mod T
- Secondary: h₂(s) = 97 - (H(s) mod 97)
Indexᵢ = (h₁(s) + i·h₂(s)) mod T
2.4. Separate Chaining (Open Hashing)
- Collision resolution using linked lists or dynamic arrays at each index.
- All elements with the same hash index are stored in a chain.
Functionality
- Read and store words into four different hash tables, one per
technique
- Show:
- Hash values
- Inserted positions
- Collision counts for each value
Output
Print or log the contents of each hash table:
- Show all words inserted
- Indicate where collisions occurred
- For separate chaining, show linked lists at indices