哈希表(Hash Table)是一种非常重要且高效的数据结构,它通过哈希函数将元素映射到一个固定大小的数组中,以支持高效的插入、删除和查找操作。哈希表的主要优点是其接近常数时间的操作效率,因此在许多应用中,如数据库索引、缓存实现和符号表等,得到了广泛的应用。
哈希表是一种基于数组的数据结构,其中每个元素都通过哈希函数映射到数组的一个位置(即“桶”)。哈希表的核心思想就是通过某种哈希算法,利用数组下标的方式来加速数据的查找、插入和删除过程。
哈希函数(Hash Function)是哈希表中最为关键的部分,它的作用是将输入的元素转换为一个数组的索引值。好的哈希函数应当具有均匀分布的特点,以减少碰撞的发生。
常见的哈希函数设计:
hash(key) = key % table_size
,简单,但容易发生碰撞。hash(key) = floor(table_size * (key * A % 1))
,其中A是一个常数,通常选择A = (sqrt(5) - 1) / 2。哈希表常见的两种碰撞处理方法:
#include <iostream>
#include <vector>
#include <list>
#include <string>
using namespace std;
template <typename K, typename V>
class HashTable {
private:
// 哈希表的桶(每个桶是一个链表)
vector<list<pair<K, V>>> table;
int size; // 哈希表的大小
// 哈希函数:使用除法法
int hashFunction(K key) {
return key % size;
}
public:
// 构造函数
HashTable(int s) : size(s) {
table.resize(size);
}
// 插入操作
void insert(K key, V value) {
int index = hashFunction(key);
table[index].push_back(make_pair(key, value));
}
// 查找操作
bool search(K key, V& value) {
int index = hashFunction(key);
for (auto& pair : table[index]) {
if (pair.first == key) {
value = pair.second;
return true;
}
}
return false;
}
// 删除操作
bool remove(K key) {
int index = hashFunction(key);
auto& chain = table[index];
for (auto it = chain.begin(); it != chain.end(); ++it) {
if (it->first == key) {
chain.erase(it);
return true;
}
}
return false;
}
// 打印哈希表
void printTable() {
for (int i = 0; i < size; ++i) {
cout << "Bucket " << i << ": ";
for (auto& pair : table[i]) {
cout << "(" << pair.first << ", " << pair.second << ") ";
}
cout << endl;
}
}
};
int main() {
HashTable<int, string> hashTable(10);
// 插入元素
hashTable.insert(1, "One");
hashTable.insert(12, "Twelve");
hashTable.insert(23, "Twenty Three");
// 打印哈希表
hashTable.printTable();
// 查找元素
string value;
if (hashTable.search(12, value)) {
cout << "Found value for key 12: " << value << endl;
} else {
cout << "Key not found!" << endl;
}
// 删除元素
if (hashTable.remove(12)) {
cout << "Key 12 removed successfully!" << endl;
} else {
cout << "Key not found to remove!" << endl;
}
// 打印哈希表
hashTable.printTable();
return 0;
}
Bucket 0:
Bucket 1: (1, One)
Bucket 2:
Bucket 3:
Bucket 4:
Bucket 5:
Bucket 6:
Bucket 7:
Bucket 8: (12, Twelve)
Bucket 9: (23, Twenty Three)
Found value for key 12: Twelve
Key 12 removed successfully!
Bucket 0:
Bucket 1: (1, One)
Bucket 2:
Bucket 3:
Bucket 4:
Bucket 5:
Bucket 6:
Bucket 7:
Bucket 8:
Bucket 9: (23, Twenty Three)
因篇幅问题不能全部显示,请点此查看更多更全内容