5.散列表
5.1 概念
- 定义
- 散列表(哈希表 Hash Table)
是一种数据结构。
特点:可以根据数据元素的关键字计算出它在散列表中的存储地址。- 散列函数(哈希函数)
Addr=H(key)
建立了“关键字 ==》存储地址”的映射关系。- 冲突(碰撞)
在散列表中插入一个数据元素时,需要根据关键字的值确定其存储地址,若该地址已经存储其他元素,则这种情况为冲突。
减少冲突的方法:构造合适的散列函数
解决冲突的方法:拉链法、开放定址法- 同义词
不同关键字通过散列函数映射到同一个地址,则这两个关键字是在该散列函数下的同义词。
- 散列表的查找效率取决于三个因素:散列函数、处理冲突的方法、装填因子
装填因子=表中记录数n/散列表长度m
装填因子越大,装填记录越满,发生冲突可能性越大。
5.2 构造
- 设计散列函数需要注意什么?
1.定义域必须包含所有可能出现的关键字;
2.值域不能超过散列表的地址范围;
3.尽可能减少冲突。散列函数计算出来的地址尽可能均匀分布在整个地址空间;
4.散列函数尽量简单,能快速计算任一关键词的散列地址。
以下介绍几种常见构造方法:
- 除留余数法
散列表表长m,取一个不大于m但最接近或等于m的质数p。
质数:又称素数,除了1和自身外,不能被其他自然数整除
用质数的原因:不被公因子影响,可以更均匀,减少冲突。
适用场景:较为通用,关键字是整数即可。
- 直接定址法
或
其中,ab是常数。这种方法简单不会产生冲突。
但是如果关键字分布不连续,会浪费大量空间。
适用场景:关键字分布基本连续。
- 数字分析法
选取数码分布较为均匀的若干位作为散列地址。
如:以手机号后四位作为散列地址。
适用场景:关键字集合已知,且关键字的某几个数码位分布均匀。
- 平方取中法
取关键字的平方值的中间几位作为散列地址。
具体几位视情况而定。这种方法得到的散列地址与关键字的每位都有关,因此散列地址分布比较均匀。
适用场景:关键字的每位取值都不够均匀。
5.3 拉链法
- 定义
把所有同义词存储在一个链表中。
- 散列表的插入操作(拉链法解决冲突)
步骤:
1.结合散列表函数计算新元素的散列地址;
2.将新元素插入散列表对应的链表(可用头插法和尾插法)
- 头插法:
- 散列表的查找操作(拉链法解决冲突)
步骤:
1.根据散列函数计算目标的散列地址;
2.遍历查找链表所有元素。
- 散列表的删除操作(拉链法解决冲突)
1.根据散列函数计算目标的散列地址;
2.顺序查找散列表对应的链表,若查找成功,将目标元素从链表里删除。
- 完整代码
#include <stdio.h>
#include <stdlib.h>
#define SIZE 10
// 定义哈希表节点
typedef struct Node {
int key;
struct Node* next;
} Node;
// 创建节点
Node* createNode(int key) {
Node* newNode = (Node*)malloc(sizeof(Node));
if (newNode == NULL) {
printf("Memory allocation failed!\n");
exit(1);
}
newNode->key = key;
newNode->next = NULL;
return newNode;
}
// 哈希函数
int hashFunction(int key, int p) {
return key % p;
}
// 打印哈希表
void printHashtable(Node** hashtable) {
for (int i = 0; i < SIZE; i++) {
printf("%d: ", i);
Node* curr = hashtable[i];
while (curr != NULL) {
printf("%d ", curr->key);
curr = curr->next;
}
printf("\n");
}
printf("\n");
}
// 插入操作
void insert(Node** hashtable, int key, int p) {
int index = hashFunction(key, p);
// 创建新节点
Node* newNode = createNode(key);
// 若哈希表索引位置为空,则直接插入节点
if (hashtable[index] == NULL) {
hashtable[index] = newNode;
}
// 否则,插入到链表尾部
else {
Node* curr = hashtable[index];
while (curr->next != NULL) {
curr = curr->next;
}
curr->next = newNode;
}
printf("The hash table after insert key %d:\n", key);
printHashtable(hashtable);
}
// 删除操作
void deletehash(Node** hashtable, int key, int p) {
int index = hashFunction(key, p);
// 若哈希表索引位置为空,则直接返回
if (hashtable[index] == NULL) {
printf("Key %d is not in hash table!\n", key);
return;
}
Node* curr = hashtable[index];
Node* prev = NULL;
// 遍历链表,查找要删除的节点
while (curr != NULL && curr->key != key) {
prev = curr;
curr = curr->next;
}
// 若找到要删除的节点,则进行删除操作
if (curr != NULL) {
// 若要删除的节点是链表的第一个节点
if (prev == NULL) {
hashtable[index] = curr->next;
}
// 否则,修改前一个节点的指针跳过要删除的节点
else {
prev->next = curr->next;
}
free(curr);
printf("The hash table after delete key %d :\n", key);
printHashtable(hashtable);
}
else {
printf("Key %d is not in hash table!\n", key);
}
}
// 查询操作
void search(Node** hashtable, int key, int p) {
int index = hashFunction(key, p);
// 若哈希表索引位置为空,则直接返回
if (hashtable[index] == NULL) {
printf("Key %d is not in hash table!\n", key);
return;
}
Node* curr = hashtable[index];
// 遍历链表,查找要查询的节点
while (curr != NULL && curr->key != key) {
curr = curr->next;
}
// 若找到要查询的节点,则输出结果
if (curr != NULL) {
printf("Key %d is in hash table!\n", key);
} else {
printf("Key %d is not in hash table!\n", key);
}
}
int main() {
// 创建哈希表
Node* hashtable[SIZE] = { NULL };
// 插入操作
insert(hashtable, 5, SIZE);
insert(hashtable, 15, SIZE);
insert(hashtable, 25, SIZE);
insert(hashtable, 7, SIZE);
// 删除操作
deletehash(hashtable, 15, SIZE);
deletehash(hashtable, 10, SIZE);
// 查询操作
search(hashtable, 25, SIZE);
search(hashtable, 7, SIZE);
return 0;
}
5.4 开放定址法
- 定义
如果发生冲突,就给新元素另外找个空闲位置。
- 插入、查找操作
1.线性探测法
可设置散列函数:
2.平方探测法
用以下序列+原冲突位=新存储位
3.双散列法
设计第二个散列函数,新地址=原冲突地址×第二个散列函数。
4.伪随机序列法
根据题目中所给序列定。
- 删除
步骤
1.根据散列函数算出散列地址,对比关键字是否匹配,匹配则查找成功;
2.若不匹配,根据探测序列对比下一个地址的关键字,直到查找成功或失败;
3.若查找成功,则删除该元素。
- 注:删除元素不能简单的将被删元素的空间置为空,否则将截断在它之后的探测路径,可做个已删除的标记,进行逻辑删除。
带来的问题:散列表很空,则可以对散列表进行整理。
- 完整代码
#include <stdio.h>
#include <stdlib.h>
#define SIZE 10
// 定义哈希表节点
typedef struct Node {
int key;
int deleted; // 标记当前节点是否已被删除
} Node;
// 哈希函数
int hashFunction(int key, int p) {
return key % p;
}
// 插入操作
void insert(Node* hashtable, int key, int p) {
int index = hashFunction(key, p);
// 若哈希表索引位置为空,则直接插入节点
if (hashtable[index].key == -1 || hashtable[index].deleted == 1) {
hashtable[index].key = key;
hashtable[index].deleted = 0;
}
// 否则,使用线性探测法解决冲突
else {
int i = 1;
while (1) {
int newIndex = (index + i) % p;
if (hashtable[newIndex].key == -1 || hashtable[newIndex].deleted == 1) {
hashtable[newIndex].key = key;
hashtable[newIndex].deleted = 0;
break;
}
i++;
}
}
printf("The hash table after insert key %d :\n", key);
for (int i = 0; i < SIZE; i++) {
printf("%d ", hashtable[i].key);
}
printf("\n\n");
}
// 删除操作
void deletehash(Node* hashtable, int key, int p) {
int index = hashFunction(key, p);
// 若哈希表索引位置为空,则直接返回
if (hashtable[index].key == -1) {
printf("Key %d is not in the hash table!\n", key);
return;
}
// 遍历哈希表,查找要删除的节点
int i = 0;
while (1) {
int newIndex = (index + i) % p;
if (hashtable[newIndex].key == key) {
hashtable[newIndex].deleted = 1;
printf("The hash table after delete key %d :\n", key);
for (int i = 0; i < SIZE; i++) {
printf("%d ", hashtable[i].key);
}
printf("\n\n");
return;
}
// 若遍历到空节点或回到起始位置,则说明要删除的节点不存在于哈希表中
if (hashtable[newIndex].key == -1 || newIndex == index) {
printf("Key %d is not in the hash table!\n", key);
return;
}
i++;
}
}
// 查询操作
void search(Node* hashtable, int key, int p) {
int index = hashFunction(key, p);
// 若哈希表索引位置为空,则直接返回
if (hashtable[index].key == -1) {
printf("Key %d is not in the hash table!\n", key);
return;
}
// 遍历哈希表,查找要查询的节点
int i = 0;
while (1) {
int newIndex = (index + i) % p;
if (hashtable[newIndex].key == key) {
printf("Key %d is in the hash table!\n", key);
return;
}
// 若遍历到空节点或回到起始位置,则说明要查询的节点不存在于哈希表中
if (hashtable[newIndex].key == -1 || newIndex == index) {
printf("Key %d is not in the hash table!\n", key);
return;
}
i++;
}
}
int main() {
// 创建哈希表
Node hashtable[SIZE];
for (int i = 0; i < SIZE; i++) {
hashtable[i].key = -1;
hashtable[i].deleted = 0;
}
// 插入操作
insert(hashtable, 5, SIZE);
insert(hashtable, 15, SIZE);
insert(hashtable, 25, SIZE);
insert(hashtable, 7, SIZE);
// 删除操作
deletehash(hashtable, 15, SIZE);
deletehash(hashtable, 10, SIZE);
// 查询操作
search(hashtable, 25, SIZE);
search(hashtable, 10, SIZE);
return 0;
}