hashing
hashing
Hash Table is a data structure which stores data in an associative manner. In a hash table, data is
stored in an array format, where each data value has its own unique index value. Access of data
becomes very fast if we know the index of the desired data.
Thus, it becomes a data structure in which insertion and search operations are very fast
irrespective of the size of the data. Hash Table uses an array as a storage medium and uses hash
technique to generate an index where an element is to be inserted or is to be located from.
Hashing
Hashing is a technique to convert a range of key values into a range of indexes of an array. We're
going to use modulo operator to get a range of key values. Consider an example of hash table of
size 20, and the following items are to be stored. Item are in the (key,value) format.
• (1,20)
• (2,70)
• (42,80)
• (4,25)
• (12,44)
• (14,32)
• (17,11)
• (13,78)
• (37,98)
Sr.No. Key Hash Array Index
1 1 1 % 20 = 1 1
2 2 2 % 20 = 2 2
3 42 42 % 20 = 2 2
4 4 4 % 20 = 4 4
5 12 12 % 20 = 12 12
6 14 14 % 20 = 14 14
7 17 17 % 20 = 17 17
8 13 13 % 20 = 13 13
9 37 37 % 20 = 17 17
class HashTable {
private:
vector<int> table;
vector<bool> isOccupied;
// Hash Function
int hashFunction(int key) {
return key % TABLE_SIZE;
}
public:
// Constructor
HashTable() {
table.resize(TABLE_SIZE, -1); // Initialize table with -1 (empty)
isOccupied.resize(TABLE_SIZE, false); // Track occupied slots
}
int main() {
HashTable ht;
// Insert keys into the hash table
ht.insert(10);
ht.insert(20);
ht.insert(30);
ht.insert(25);
// Delete a key
ht.remove(25);
cout << "\nHash Table after deletion:" << endl;
ht.display();
return 0;
}
Hash Collision
When the hash function generates the same index for multiple keys, there will be a conflict (what
value to be stored in that index). This is called a hash collision.
If j is the slot for multiple elements, it contains a pointer to the head of the list of elements.
If no element is present, j contains NIL.
Code
#include <iostream>
using namespace std;
const int TABLE_SIZE = 10; // Define the size of the hash table
// Hash Function
int hashFunction(int key) {
return key % TABLE_SIZE; // Modulo operation to determine index
}
public:
// Constructor to initialize the hash table
HashTable() {
for (int i = 0; i < TABLE_SIZE; i++) {
table[i] = nullptr; // Initialize all slots to null
}
}
cout << "Inserted " << key << " at index " << index << endl;
}
while (current) {
if (current->key == key) {
return true; // Key found
}
current = current->next; // Move to the next node
}
return false; // Key not found
}
while (current) {
if (current->key == key) { // If key is found
if (prev) {
prev->next = current->next; // Bypass the node
} else {
table[index] = current->next; // Remove head node
}
delete current; // Free memory
cout << "Removed " << key << " from index " << index << endl;
return;
}
prev = current;
current = current->next;
}
cout << "Key " << key << " not found!" << endl;
}
int main() {
HashTable ht;
// Delete a key
ht.remove(25);
cout << "\nHash Table after deletion:" << endl;
ht.display();
return 0;
}
i. Linear Probing As we can see, it may happen that the hashing technique is used to
create an already used index of the array. In such a case, we can search the next empty
location in the array by looking into the next cell until we find an empty cell. This
technique is called linear probing.
(hash(x) + 1) % S.
Array
Sr.No. Key Hash After Linear Probing, Array Index
Index
1 1 1 % 20 = 1 1 1
2 2 2 % 20 = 2 2 2
3 42 42 % 20 = 2 2 3
4 4 4 % 20 = 4 4 4
5 12 12 % 20 = 12 12 12
6 14 14 % 20 = 14 14 14
7 17 17 % 20 = 17 17 17
8 13 13 % 20 = 13 13 13
9 37 37 % 20 = 17 17 18
ii. Quadratic Probing It works similar to linear probing but the spacing between the slots
is increased (greater than one) by using the following relation.
where,
class HashTable {
private:
vector<int> table;
vector<bool> isOccupied;
CollisionResolution method;
// Hash Functions
int hashFunction(int key) {
return key % TABLE_SIZE;
}
public:
// Constructor
HashTable(CollisionResolution methodType) {
table.resize(TABLE_SIZE, -1);
isOccupied.resize(TABLE_SIZE, false);
method = methodType;
}
// Delete a key
void remove(int key) {
int index = search(key);
if (index != -1) {
table[index] = -1;
isOccupied[index] = false;
cout << "Removed " << key << " from index " << index << endl;
} else {
cout << "Key " << key << " not found!" << endl;
}
}
int main() {
cout << "Choose Collision Resolution Method:\n";
cout << "1. Linear Probing\n2. Quadratic Probing\n3. Double Hashing\n";
int choice;
cin >> choice;
CollisionResolution method;
switch (choice) {
case 1:
method = LINEAR;
break;
case 2:
method = QUADRATIC;
break;
case 3:
method = DOUBLE_HASHING;
break;
default:
cout << "Invalid choice!" << endl;
return 0;
}
HashTable ht(method);
ht.insert(10);
ht.insert(20);
ht.insert(30);
ht.insert(25); // Collision resolved based on selected method
cout << "\nHash Table after insertions:" << endl;
ht.display();
ht.remove(25);
cout << "\nHash Table after deletion:" << endl;
ht.display();
return 0;
}
Lab Tasks
After going through Lab tasks, submit a separate Word document which should
include all the results of the tasks Code should be in word and output should be
screenshot.
First page should contain your Full name, Registration #, Course name and Date
Objective:
To provide students with a hands-on learning experience to explore the concepts of hashing, including hash
functions, hash tables, and collision resolution techniques (chaining). Students will apply these concepts to
solve a real-world problem, analyze their effectiveness, and reflect on their findings.
Scenario:
Imagine you are part of a team developing a new e-commerce platform. The platform needs a fast and
efficient system to store and retrieve product data using unique product IDs. Each product ID is an integer
representing the product in the catalog. Due to a large number of products, collisions in the hash table are
unavoidable. Your task is to design and implement a hash table to handle these collisions effectively.
Tasks:
1. Create a hash table with a fixed size (e.g., 10 slots) to store integers (product IDs).
o Chaining
Part 3: Operations
o Search: Find if a product ID exists in the hash table and return its index.
5. Test your hash table by inserting the following product IDs in order: 10, 20, 30, 25, 35, 45, 50.