In hash table implementations, collisions occur when two different keys hash to the same index in the array. To handle collisions, various strategies are employed to ensure efficient storage and retrieval of data. In this article, we'll explore some common methods to resolve collisions and provide working examples in C++.
Collision Resolution Methods in Hash Table.
There are multiple ways to resolve collision in Hash Table (Hash Map) but here we are going to learn about three popular collision resolution methods mentioned below:
- Chaining (Separate Chaining).
- Open Addressing.
- Robin Hood Hashing.
Now let's discuss each of these methods one by one in detail and use them in Hash Table Implementation.
Method 1: Chaining (Separate Chaining)
Separate Chaining is a popular collision resolution technique used in hash table implementations. It involves maintaining a linked list at each hash table index to handle collisions.
How does Separate Chaining Work?
It involves four important steps to implement the Chaining method to resolve collision:
1. Initialization: Create an array of linked lists, where each list represents a bucket in the hash table.
2. Insertion: When inserting a key-value pair, compute the hash of the key and determine the index in the array. Add the key-value pair to the linked list at that index.
3. Search: To retrieve a value for a given key, calculate the hash to find the index, and then search the linked list at that index for the key.
4. Collision Handling: When multiple keys hash to the same index (collision), they are simply added to the same linked list at that index.
Implementation of Separate Chaining in C++.
// C++ code for the implementation of Chaining in Hash Table #include <iostream> #include <list> #include <utility> class HashTable { private: static const int tableSize = 10; std::list<std::pair<int, int>> table[tableSize]; int hashFunction(int key) { return key % tableSize; } public: void insert(int key, int value) { int index = hashFunction(key); table[index].emplace_back(key, value); } int get(int key) { int index = hashFunction(key); for (const auto& pair : table[index]) { if (pair.first == key) { return pair.second; } } return -1; // Key not found } }; int main() { HashTable hashTable; hashTable.insert(5, 50); hashTable.insert(15, 150); hashTable.insert(25, 250); std::cout << "Value at key 15: " << hashTable.get(15) << std::endl; return 0; }
Value at key 15: 150
Method 1: Open Addressing.
How Open Addressing Works?
Implementation of Open Addressing in C++.
// C++ code to implement Open Addression in Hash Map #include <iostream> #include <vector> using namespace std; class HashTable { private: static const int tableSize = 10; vector<pair<int, int>> table; int hashFunction(int key) { return key % tableSize; } int linearProbing(int index, int attempt) { return (index + attempt) % tableSize; } public: HashTable() : table(tableSize, std::make_pair(-1, -1)) {} void insert(int key, int value) { int index = hashFunction(key); int attempt = 0; while (table[index].first != -1) { index = linearProbing(index, ++attempt); } table[index] = make_pair(key, value); } int get(int key) { int index = hashFunction(key); int attempt = 0; while (table[index].first != -1) { if (table[index].first == key) { return table[index].second; } index = linearProbing(index, ++attempt); } return -1; // Key not found } }; int main() { HashTable hashTable; hashTable.insert(5, 50); hashTable.insert(15, 150); hashTable.insert(25, 250); cout << "Value at key 15: " << hashTable.get(15) << endl; return 0; }
Value at key 15: 150
Method 3: Robin Hood Hashing.
How Robin Hood Hashing Works?
Implementation of Robin Hood Hashing in C++.
// C++ code to implement Robin Hood Hashing #include <iostream> #include <vector> using namespace std; class HashTable { private: static const int tableSize = 10; vector<pair<int, int>> table; int hashFunction(int key) { return key % tableSize; } public: HashTable() : table(tableSize, make_pair(-1, -1)) {} void insert(int key, int value) { int index = hashFunction(key); int distance = 0; while (table[index].first != -1) { if (distance > tableSize - 1) { break; // Avoid infinite loop } if (distance > index - hashFunction(table[index].first)) { swap(key, table[index].first); swap(value, table[index].second); distance = index - hashFunction(key); } index = (index + 1) % tableSize; distance++; } table[index] = make_pair(key, value); } int get(int key) { int index = hashFunction(key); int distance = 0; while (table[index].first != -1) { if (table[index].first == key) { return table[index].second; } index = (index + 1) % tableSize; distance++; } return -1; // Key not found } }; int main() { HashTable hashTable; hashTable.insert(5, 50); hashTable.insert(15, 150); hashTable.insert(25, 250); cout << "Value at key 15: " << hashTable.get(15) << endl; return 0; }
Value at key 15: 150
No comments:
Post a Comment