Fix hash_map_chaining.c and

hash_map_open_addressing.c
This commit is contained in:
krahets 2023-10-13 14:32:49 +08:00
parent fe01f77ff8
commit 84e2799f1b
6 changed files with 76 additions and 74 deletions

View File

@ -1,3 +1,4 @@
add_executable(array_hash_map array_hash_map.c) add_executable(array_hash_map array_hash_map.c)
add_executable(hash_map_chaining hash_map_chaining.c)
add_executable(hash_map_open_addressing hash_map_open_addressing.c)
add_executable(simple_hash simple_hash.c) add_executable(simple_hash simple_hash.c)

View File

@ -33,30 +33,30 @@ struct hashMapChaining {
typedef struct hashMapChaining hashMapChaining; typedef struct hashMapChaining hashMapChaining;
// 函数声明 // 函数声明
void extend(hashMapChaining *hashmap); void extend(hashMapChaining *hashMap);
/* 初始化桶数组 */ /* 初始化桶数组 */
hashMapChaining *newHashMapChaining() { hashMapChaining *newHashMapChaining() {
// 为哈希表分配空间 // 为哈希表分配空间
int tableSize = 4; int tableSize = 4;
hashMapChaining *hashmap = (hashMapChaining *)malloc(sizeof(hashMapChaining)); hashMapChaining *hashMap = (hashMapChaining *)malloc(sizeof(hashMapChaining));
// 初始化数组 // 初始化数组
hashmap->buckets = (Pair *)malloc(sizeof(Pair) * tableSize); hashMap->buckets = (Pair *)malloc(sizeof(Pair) * tableSize);
memset(hashmap->buckets, 0, sizeof(Pair) * tableSize); memset(hashMap->buckets, 0, sizeof(Pair) * tableSize);
hashmap->capacity = tableSize; hashMap->capacity = tableSize;
hashmap->size = 0; hashMap->size = 0;
hashmap->extendRatio = 2; hashMap->extendRatio = 2;
hashmap->loadThres = 2.0 / 3.0; hashMap->loadThres = 2.0 / 3.0;
return hashmap; return hashMap;
} }
/* 销毁哈希表 */ /* 销毁哈希表 */
void delHashMapChaining(hashMapChaining *hashmap) { void delHashMapChaining(hashMapChaining *hashMap) {
for (int i = 0; i < hashmap->capacity; i++) { for (int i = 0; i < hashMap->capacity; i++) {
Pair *pair = &hashmap->buckets[i]; Pair *pair = &hashMap->buckets[i];
Node *node = pair->node; Node *node = pair->node;
while (node != NULL) { while (node != NULL) {
Node *temp = node; Node *temp = node;
@ -65,24 +65,24 @@ void delHashMapChaining(hashMapChaining *hashmap) {
free(temp); free(temp);
} }
} }
free(hashmap->buckets); free(hashMap->buckets);
free(hashmap); free(hashMap);
} }
/* 哈希函数 */ /* 哈希函数 */
int hashFunc(hashMapChaining *hashmap, const int key) { int hashFunc(hashMapChaining *hashMap, const int key) {
return key % hashmap->capacity; return key % hashMap->capacity;
} }
/* 负载因子 */ /* 负载因子 */
double loadFactor(hashMapChaining *hashmap) { double loadFactor(hashMapChaining *hashMap) {
return (double)hashmap->size / (double)hashmap->capacity; return (double)hashMap->size / (double)hashMap->capacity;
} }
/* 查询操作 */ /* 查询操作 */
const char *get(hashMapChaining *hashmap, const int key) { char *get(hashMapChaining *hashMap, const int key) {
int index = hashFunc(hashmap, key); int index = hashFunc(hashMap, key);
Pair *pair = &hashmap->buckets[index]; Pair *pair = &hashMap->buckets[index];
Node *node = pair->node; Node *node = pair->node;
while (node != NULL) { while (node != NULL) {
if (node->key == key) { if (node->key == key) {
@ -94,11 +94,11 @@ const char *get(hashMapChaining *hashmap, const int key) {
} }
/* 添加操作 */ /* 添加操作 */
void put(hashMapChaining *hashmap, const int key, char *val) { void put(hashMapChaining *hashMap, const int key, char *val) {
if (loadFactor(hashmap) > hashmap->loadThres) { if (loadFactor(hashMap) > hashMap->loadThres) {
extend(hashmap); extend(hashMap);
} }
int index = hashFunc(hashmap, key); int index = hashFunc(hashMap, key);
// 先为新节点分配空间再赋值 // 先为新节点分配空间再赋值
Node *newNode = (Node *)malloc(sizeof(Node)); Node *newNode = (Node *)malloc(sizeof(Node));
@ -108,11 +108,11 @@ void put(hashMapChaining *hashmap, const int key, char *val) {
strcpy(newNode->val, val); strcpy(newNode->val, val);
newNode->val[strlen(val)] = '\0'; newNode->val[strlen(val)] = '\0';
Pair *pair = &hashmap->buckets[index]; Pair *pair = &hashMap->buckets[index];
Node *node = pair->node; Node *node = pair->node;
if (node == NULL) { if (node == NULL) {
hashmap->buckets[index].node = newNode; hashMap->buckets[index].node = newNode;
hashmap->size++; hashMap->size++;
return; return;
} }
while (node != NULL) { while (node != NULL) {
@ -131,13 +131,13 @@ void put(hashMapChaining *hashmap, const int key, char *val) {
node = node->next; node = node->next;
} }
node->next = newNode; node->next = newNode;
hashmap->size++; hashMap->size++;
} }
/* 删除操作 */ /* 删除操作 */
void removeItem(hashMapChaining *hashmap, int key) { void removeItem(hashMapChaining *hashMap, int key) {
int index = hashFunc(hashmap, key); int index = hashFunc(hashMap, key);
Pair *pair = &hashmap->buckets[index]; Pair *pair = &hashMap->buckets[index];
Node *node = pair->node; Node *node = pair->node;
// 保存后继的节点 // 保存后继的节点
Node *prev = NULL; Node *prev = NULL;
@ -152,7 +152,7 @@ void removeItem(hashMapChaining *hashmap, int key) {
// 释放内存 // 释放内存
free(node->val); free(node->val);
free(node); free(node);
hashmap->size--; hashMap->size--;
return; return;
} }
prev = node; prev = node;
@ -162,22 +162,22 @@ void removeItem(hashMapChaining *hashmap, int key) {
} }
/* 扩容哈希表 */ /* 扩容哈希表 */
void extend(hashMapChaining *hashmap) { void extend(hashMapChaining *hashMap) {
// 暂存原哈希表 // 暂存原哈希表
Pair *oldBuckets = hashmap->buckets; Pair *oldBuckets = hashMap->buckets;
int oldCapacity = hashmap->capacity; int oldCapacity = hashMap->capacity;
// 创建新的哈希表,重新分配一段空间 // 创建新的哈希表,重新分配一段空间
hashmap->capacity *= hashmap->extendRatio; hashMap->capacity *= hashMap->extendRatio;
hashmap->buckets = (Pair *)malloc(sizeof(Pair) * hashmap->capacity); hashMap->buckets = (Pair *)malloc(sizeof(Pair) * hashMap->capacity);
memset(hashmap->buckets, 0, sizeof(Pair) * hashmap->capacity); memset(hashMap->buckets, 0, sizeof(Pair) * hashMap->capacity);
hashmap->size = 0; hashMap->size = 0;
// 将原哈希表中的键值对重新哈希到新的哈希表中 // 将原哈希表中的键值对重新哈希到新的哈希表中
for (int i = 0; i < oldCapacity; i++) { for (int i = 0; i < oldCapacity; i++) {
Node *node = oldBuckets[i].node; Node *node = oldBuckets[i].node;
while (node != NULL) { while (node != NULL) {
put(hashmap, node->key, node->val); put(hashMap, node->key, node->val);
node = node->next; node = node->next;
} }
} }
@ -196,10 +196,10 @@ void extend(hashMapChaining *hashmap) {
} }
/* 打印哈希表 */ /* 打印哈希表 */
void print(hashMapChaining *hashmap) { void print(hashMapChaining *hashMap) {
for (int i = 0; i < hashmap->capacity; i++) { for (int i = 0; i < hashMap->capacity; i++) {
printf("["); printf("[");
Pair *pair = &hashmap->buckets[i]; Pair *pair = &hashMap->buckets[i];
Node *node = pair->node; Node *node = pair->node;
while (node != NULL) { while (node != NULL) {
if (node->val != NULL) { if (node->val != NULL) {

View File

@ -7,14 +7,15 @@
#include "../utils/common.h" #include "../utils/common.h"
/* 开放寻址哈希表 */ /* 开放寻址哈希表 */
struct Pair { struct pair {
int key; int key;
char *val; char *val;
}; };
typedef struct Pair Pair; typedef struct pair Pair;
struct HashMapOpenAddressing { /* 开放寻址哈希表 */
struct hashMapOpenAddressing {
int size; // 键值对数量 int size; // 键值对数量
int capacity; // 哈希表容量 int capacity; // 哈希表容量
double loadThres; // 触发扩容的负载因子阈值 double loadThres; // 触发扩容的负载因子阈值
@ -23,14 +24,14 @@ struct HashMapOpenAddressing {
Pair *TOMBSTONE; // 删除标记 Pair *TOMBSTONE; // 删除标记
}; };
typedef struct HashMapOpenAddressing HashMapOpenAddressing; typedef struct hashMapOpenAddressing hashMapOpenAddressing;
// 函数声明 // 函数声明
void extend(HashMapOpenAddressing *hashMap); void extend(hashMapOpenAddressing *hashMap);
/* 构造方法 */ /* 构造方法 */
HashMapOpenAddressing *newHashMapOpenAddressing() { hashMapOpenAddressing *newHashMapOpenAddressing() {
HashMapOpenAddressing *hashMap = (HashMapOpenAddressing *)malloc(sizeof(HashMapOpenAddressing)); hashMapOpenAddressing *hashMap = (hashMapOpenAddressing *)malloc(sizeof(hashMapOpenAddressing));
hashMap->size = 0; hashMap->size = 0;
hashMap->capacity = 4; hashMap->capacity = 4;
hashMap->loadThres = 2.0 / 3.0; hashMap->loadThres = 2.0 / 3.0;
@ -44,7 +45,7 @@ HashMapOpenAddressing *newHashMapOpenAddressing() {
} }
/* 析构方法 */ /* 析构方法 */
void delHashMapOpenAddressing(HashMapOpenAddressing *hashMap) { void delHashMapOpenAddressing(hashMapOpenAddressing *hashMap) {
for (int i = 0; i < hashMap->capacity; i++) { for (int i = 0; i < hashMap->capacity; i++) {
Pair *pair = hashMap->buckets[i]; Pair *pair = hashMap->buckets[i];
if (pair != NULL && pair != hashMap->TOMBSTONE) { if (pair != NULL && pair != hashMap->TOMBSTONE) {
@ -55,17 +56,17 @@ void delHashMapOpenAddressing(HashMapOpenAddressing *hashMap) {
} }
/* 哈希函数 */ /* 哈希函数 */
int hashFunc(HashMapOpenAddressing *hashMap, int key) { int hashFunc(hashMapOpenAddressing *hashMap, int key) {
return key % hashMap->capacity; return key % hashMap->capacity;
} }
/* 负载因子 */ /* 负载因子 */
double loadFactor(HashMapOpenAddressing *hashMap) { double loadFactor(hashMapOpenAddressing *hashMap) {
return (double)hashMap->size / (double)hashMap->capacity; return (double)hashMap->size / (double)hashMap->capacity;
} }
/* 搜索 key 对应的桶索引 */ /* 搜索 key 对应的桶索引 */
int findBucket(HashMapOpenAddressing *hashMap, int key) { int findBucket(hashMapOpenAddressing *hashMap, int key) {
int index = hashFunc(hashMap, key); int index = hashFunc(hashMap, key);
int firstTombstone = -1; int firstTombstone = -1;
// 线性探测,当遇到空桶时跳出 // 线性探测,当遇到空桶时跳出
@ -92,7 +93,7 @@ int findBucket(HashMapOpenAddressing *hashMap, int key) {
} }
/* 查询操作 */ /* 查询操作 */
char *get(HashMapOpenAddressing *hashMap, int key) { char *get(hashMapOpenAddressing *hashMap, int key) {
// 搜索 key 对应的桶索引 // 搜索 key 对应的桶索引
int index = findBucket(hashMap, key); int index = findBucket(hashMap, key);
// 若找到键值对,则返回对应 val // 若找到键值对,则返回对应 val
@ -104,7 +105,7 @@ char *get(HashMapOpenAddressing *hashMap, int key) {
} }
/* 添加操作 */ /* 添加操作 */
void put(HashMapOpenAddressing *hashMap, int key, char *val) { void put(hashMapOpenAddressing *hashMap, int key, char *val) {
// 当负载因子超过阈值时,执行扩容 // 当负载因子超过阈值时,执行扩容
if (loadFactor(hashMap) > hashMap->loadThres) { if (loadFactor(hashMap) > hashMap->loadThres) {
extend(hashMap); extend(hashMap);
@ -131,7 +132,7 @@ void put(HashMapOpenAddressing *hashMap, int key, char *val) {
} }
/* 删除操作 */ /* 删除操作 */
void removeItem(HashMapOpenAddressing *hashMap, int key) { void removeItem(hashMapOpenAddressing *hashMap, int key) {
// 搜索 key 对应的桶索引 // 搜索 key 对应的桶索引
int index = findBucket(hashMap, key); int index = findBucket(hashMap, key);
// 若找到键值对,则用删除标记覆盖它 // 若找到键值对,则用删除标记覆盖它
@ -145,7 +146,7 @@ void removeItem(HashMapOpenAddressing *hashMap, int key) {
} }
/* 扩容哈希表 */ /* 扩容哈希表 */
void extend(HashMapOpenAddressing *hashMap) { void extend(hashMapOpenAddressing *hashMap) {
// 暂存原哈希表 // 暂存原哈希表
Pair **bucketsTmp = hashMap->buckets; Pair **bucketsTmp = hashMap->buckets;
int oldCapacity = hashMap->capacity; int oldCapacity = hashMap->capacity;
@ -166,7 +167,7 @@ void extend(HashMapOpenAddressing *hashMap) {
} }
/* 打印哈希表 */ /* 打印哈希表 */
void print(HashMapOpenAddressing *hashMap) { void print(hashMapOpenAddressing *hashMap) {
for (int i = 0; i < hashMap->capacity; i++) { for (int i = 0; i < hashMap->capacity; i++) {
Pair *pair = hashMap->buckets[i]; Pair *pair = hashMap->buckets[i];
if (pair == NULL) { if (pair == NULL) {
@ -182,7 +183,7 @@ void print(HashMapOpenAddressing *hashMap) {
/* Driver Code */ /* Driver Code */
int main() { int main() {
// 初始化哈希表 // 初始化哈希表
HashMapOpenAddressing *hashmap = newHashMapOpenAddressing(); hashMapOpenAddressing *hashmap = newHashMapOpenAddressing();
// 添加操作 // 添加操作
// 在哈希表中添加键值对 (key, val) // 在哈希表中添加键值对 (key, val)

View File

@ -424,11 +424,11 @@
=== "JS" === "JS"
```javascript title="my_heap.js" ```javascript title="my_heap.js"
[class]{MaxHeap}-[func]{#left} [class]{MaxHeap}-[func]{left}
[class]{MaxHeap}-[func]{#right} [class]{MaxHeap}-[func]{right}
[class]{MaxHeap}-[func]{#parent} [class]{MaxHeap}-[func]{parent}
``` ```
=== "TS" === "TS"
@ -645,7 +645,7 @@
```javascript title="my_heap.js" ```javascript title="my_heap.js"
[class]{MaxHeap}-[func]{push} [class]{MaxHeap}-[func]{push}
[class]{MaxHeap}-[func]{#siftUp} [class]{MaxHeap}-[func]{siftUp}
``` ```
=== "TS" === "TS"
@ -783,7 +783,7 @@
```javascript title="my_heap.js" ```javascript title="my_heap.js"
[class]{MaxHeap}-[func]{pop} [class]{MaxHeap}-[func]{pop}
[class]{MaxHeap}-[func]{#siftDown} [class]{MaxHeap}-[func]{siftDown}
``` ```
=== "TS" === "TS"

View File

@ -264,7 +264,7 @@ AVL 树既是二叉搜索树也是平衡二叉树,同时满足这两类二叉
```javascript title="avl_tree.js" ```javascript title="avl_tree.js"
[class]{AVLTree}-[func]{height} [class]{AVLTree}-[func]{height}
[class]{AVLTree}-[func]{#updateHeight} [class]{AVLTree}-[func]{updateHeight}
``` ```
=== "TS" === "TS"
@ -454,7 +454,7 @@ AVL 树的特点在于“旋转”操作,它能够在不影响二叉树的中
=== "JS" === "JS"
```javascript title="avl_tree.js" ```javascript title="avl_tree.js"
[class]{AVLTree}-[func]{#rightRotate} [class]{AVLTree}-[func]{rightRotate}
``` ```
=== "TS" === "TS"
@ -538,7 +538,7 @@ AVL 树的特点在于“旋转”操作,它能够在不影响二叉树的中
=== "JS" === "JS"
```javascript title="avl_tree.js" ```javascript title="avl_tree.js"
[class]{AVLTree}-[func]{#leftRotate} [class]{AVLTree}-[func]{leftRotate}
``` ```
=== "TS" === "TS"
@ -641,7 +641,7 @@ AVL 树的特点在于“旋转”操作,它能够在不影响二叉树的中
=== "JS" === "JS"
```javascript title="avl_tree.js" ```javascript title="avl_tree.js"
[class]{AVLTree}-[func]{#rotate} [class]{AVLTree}-[func]{rotate}
``` ```
=== "TS" === "TS"
@ -733,7 +733,7 @@ AVL 树的节点插入操作与二叉搜索树在主体上类似。唯一的区
```javascript title="avl_tree.js" ```javascript title="avl_tree.js"
[class]{AVLTree}-[func]{insert} [class]{AVLTree}-[func]{insert}
[class]{AVLTree}-[func]{#insertHelper} [class]{AVLTree}-[func]{insertHelper}
``` ```
=== "TS" === "TS"
@ -833,7 +833,7 @@ AVL 树的节点插入操作与二叉搜索树在主体上类似。唯一的区
```javascript title="avl_tree.js" ```javascript title="avl_tree.js"
[class]{AVLTree}-[func]{remove} [class]{AVLTree}-[func]{remove}
[class]{AVLTree}-[func]{#removeHelper} [class]{AVLTree}-[func]{removeHelper}
``` ```
=== "TS" === "TS"

View File

@ -59,7 +59,7 @@ hide:
<h3 align="center"> 代码一键运行 </h3> <h3 align="center"> 代码一键运行 </h3>
<p align="center"> 提供各个算法与数据结构的简洁实现与测试样例,皆可直接运行</br>支持 Java, C++, Python, Go, JS, TS, C#, Swift, Zig 等语言 </p> <p align="center"> 提供各个算法与数据结构的简洁实现与测试样例,皆可直接运行</br>支持 Java、C++、Python、Go、JS、TS、C#、Swift、Rust、Dart、Zig 等语言 </p>
![running_code](index.assets/running_code.gif) ![running_code](index.assets/running_code.gif)