parent
d3e597af94
commit
9563965a20
@ -37,7 +37,7 @@
|
|||||||
- 源代码可一键运行,帮助读者在实践练习中提升编程技能,了解算法工作原理和数据结构底层实现;
|
- 源代码可一键运行,帮助读者在实践练习中提升编程技能,了解算法工作原理和数据结构底层实现;
|
||||||
- 鼓励读者互助学习,提问与评论通常可在两日内得到回复;
|
- 鼓励读者互助学习,提问与评论通常可在两日内得到回复;
|
||||||
|
|
||||||
若本书对您有所帮助,请点个 Star :star: 支持一下,谢谢!
|
若本书对您有所帮助,请在页面右上角点个 Star :star: 支持一下,谢谢!
|
||||||
|
|
||||||
## 推荐语
|
## 推荐语
|
||||||
|
|
||||||
|
@ -10,24 +10,24 @@
|
|||||||
# define HASH_MAP_DEFAULT_SIZE 100
|
# define HASH_MAP_DEFAULT_SIZE 100
|
||||||
|
|
||||||
/* 键值对 int->string */
|
/* 键值对 int->string */
|
||||||
struct Entry {
|
struct pair {
|
||||||
int key;
|
int key;
|
||||||
char *val;
|
char *val;
|
||||||
};
|
};
|
||||||
|
|
||||||
typedef struct Entry Entry;
|
typedef struct pair pair;
|
||||||
|
|
||||||
/* 用于表示键值对、键、值的集合 */
|
/* 用于表示键值对、键、值的集合 */
|
||||||
struct MapSet {
|
struct mapSet {
|
||||||
void *set;
|
void *set;
|
||||||
int len;
|
int len;
|
||||||
};
|
};
|
||||||
|
|
||||||
typedef struct MapSet MapSet;
|
typedef struct mapSet mapSet;
|
||||||
|
|
||||||
/* 基于数组简易实现的哈希表 */
|
/* 基于数组简易实现的哈希表 */
|
||||||
struct ArrayHashMap {
|
struct ArrayHashMap {
|
||||||
Entry *buckets[HASH_MAP_DEFAULT_SIZE];
|
pair *buckets[HASH_MAP_DEFAULT_SIZE];
|
||||||
};
|
};
|
||||||
|
|
||||||
typedef struct ArrayHashMap ArrayHashMap;
|
typedef struct ArrayHashMap ArrayHashMap;
|
||||||
@ -47,7 +47,7 @@ int hashFunc(int key) {
|
|||||||
/* 查询操作 */
|
/* 查询操作 */
|
||||||
const char *get(const ArrayHashMap *d, const int key) {
|
const char *get(const ArrayHashMap *d, const int key) {
|
||||||
int index = hashFunc(key);
|
int index = hashFunc(key);
|
||||||
const Entry *pair = d->buckets[index];
|
const pair *pair = d->buckets[index];
|
||||||
if (pair == NULL)
|
if (pair == NULL)
|
||||||
return NULL;
|
return NULL;
|
||||||
return pair->val;
|
return pair->val;
|
||||||
@ -55,7 +55,7 @@ const char *get(const ArrayHashMap *d, const int key) {
|
|||||||
|
|
||||||
/* 添加操作 */
|
/* 添加操作 */
|
||||||
void put(ArrayHashMap *d, const int key, const char *val) {
|
void put(ArrayHashMap *d, const int key, const char *val) {
|
||||||
Entry *pair = malloc(sizeof(Entry));
|
pair *pair = malloc(sizeof(pair));
|
||||||
pair->key = key;
|
pair->key = key;
|
||||||
pair->val = malloc(strlen(val) + 1);
|
pair->val = malloc(strlen(val) + 1);
|
||||||
strcpy(pair->val, val);
|
strcpy(pair->val, val);
|
||||||
@ -73,19 +73,19 @@ void removeItem(ArrayHashMap *d, const int key) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
/* 获取所有键值对 */
|
/* 获取所有键值对 */
|
||||||
void entrySet(ArrayHashMap *d, MapSet *set) {
|
void pairSet(ArrayHashMap *d, mapSet *set) {
|
||||||
Entry *entries;
|
pair *entries;
|
||||||
int i = 0, index = 0;
|
int i = 0, index = 0;
|
||||||
int total = 0;
|
int total = 0;
|
||||||
|
|
||||||
/* 统计有效 entry 数量 */
|
/* 统计有效键值对数量 */
|
||||||
for (i = 0; i < HASH_MAP_DEFAULT_SIZE; i++) {
|
for (i = 0; i < HASH_MAP_DEFAULT_SIZE; i++) {
|
||||||
if (d->buckets[i] != NULL) {
|
if (d->buckets[i] != NULL) {
|
||||||
total++;
|
total++;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
entries = malloc(sizeof(Entry) * total);
|
entries = malloc(sizeof(pair) * total);
|
||||||
for (i = 0; i < HASH_MAP_DEFAULT_SIZE; i++) {
|
for (i = 0; i < HASH_MAP_DEFAULT_SIZE; i++) {
|
||||||
if (d->buckets[i] != NULL) {
|
if (d->buckets[i] != NULL) {
|
||||||
entries[index].key = d->buckets[i]->key;
|
entries[index].key = d->buckets[i]->key;
|
||||||
@ -100,12 +100,12 @@ void entrySet(ArrayHashMap *d, MapSet *set) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
/* 获取所有键 */
|
/* 获取所有键 */
|
||||||
void keySet(ArrayHashMap *d, MapSet *set) {
|
void keySet(ArrayHashMap *d, mapSet *set) {
|
||||||
int *keys;
|
int *keys;
|
||||||
int i = 0, index = 0;
|
int i = 0, index = 0;
|
||||||
int total = 0;
|
int total = 0;
|
||||||
|
|
||||||
/* 统计有效 entry 数量 */
|
/* 统计有效键值对数量 */
|
||||||
for (i = 0; i < HASH_MAP_DEFAULT_SIZE; i++) {
|
for (i = 0; i < HASH_MAP_DEFAULT_SIZE; i++) {
|
||||||
if (d->buckets[i] != NULL) {
|
if (d->buckets[i] != NULL) {
|
||||||
total++;
|
total++;
|
||||||
@ -125,12 +125,12 @@ void keySet(ArrayHashMap *d, MapSet *set) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
/* 获取所有值 */
|
/* 获取所有值 */
|
||||||
void valueSet(ArrayHashMap *d, MapSet *set) {
|
void valueSet(ArrayHashMap *d, mapSet *set) {
|
||||||
char **vals;
|
char **vals;
|
||||||
int i = 0, index = 0;
|
int i = 0, index = 0;
|
||||||
int total = 0;
|
int total = 0;
|
||||||
|
|
||||||
/* 统计有效 entry 数量 */
|
/* 统计有效键值对数量 */
|
||||||
for (i = 0; i < HASH_MAP_DEFAULT_SIZE; i++) {
|
for (i = 0; i < HASH_MAP_DEFAULT_SIZE; i++) {
|
||||||
if (d->buckets[i] != NULL) {
|
if (d->buckets[i] != NULL) {
|
||||||
total++;
|
total++;
|
||||||
@ -152,9 +152,9 @@ void valueSet(ArrayHashMap *d, MapSet *set) {
|
|||||||
/* 打印哈希表 */
|
/* 打印哈希表 */
|
||||||
void print(ArrayHashMap *d) {
|
void print(ArrayHashMap *d) {
|
||||||
int i;
|
int i;
|
||||||
MapSet set;
|
mapSet set;
|
||||||
entrySet(d, &set);
|
pairSet(d, &set);
|
||||||
Entry *entries = (Entry*) set.set;
|
pair *entries = (pair*) set.set;
|
||||||
for (i = 0; i < set.len; i++) {
|
for (i = 0; i < set.len; i++) {
|
||||||
printf("%d -> %s\n", entries[i].key, entries[i].val);
|
printf("%d -> %s\n", entries[i].key, entries[i].val);
|
||||||
}
|
}
|
||||||
@ -193,7 +193,7 @@ int main() {
|
|||||||
printf("\n遍历键值对 Key->Value\n");
|
printf("\n遍历键值对 Key->Value\n");
|
||||||
print(map);
|
print(map);
|
||||||
|
|
||||||
MapSet set;
|
mapSet set;
|
||||||
|
|
||||||
keySet(map, &set);
|
keySet(map, &set);
|
||||||
int *keys = (int*) set.set;
|
int *keys = (int*) set.set;
|
||||||
|
@ -6,12 +6,12 @@
|
|||||||
|
|
||||||
#include "../utils/common.hpp"
|
#include "../utils/common.hpp"
|
||||||
|
|
||||||
/* 键值对 int->String */
|
/* 键值对 */
|
||||||
struct Entry {
|
struct Pair {
|
||||||
public:
|
public:
|
||||||
int key;
|
int key;
|
||||||
string val;
|
string val;
|
||||||
Entry(int key, string val) {
|
Pair(int key, string val) {
|
||||||
this->key = key;
|
this->key = key;
|
||||||
this->val = val;
|
this->val = val;
|
||||||
}
|
}
|
||||||
@ -20,12 +20,12 @@ struct Entry {
|
|||||||
/* 基于数组简易实现的哈希表 */
|
/* 基于数组简易实现的哈希表 */
|
||||||
class ArrayHashMap {
|
class ArrayHashMap {
|
||||||
private:
|
private:
|
||||||
vector<Entry *> buckets;
|
vector<Pair *> buckets;
|
||||||
|
|
||||||
public:
|
public:
|
||||||
ArrayHashMap() {
|
ArrayHashMap() {
|
||||||
// 初始化数组,包含 100 个桶
|
// 初始化数组,包含 100 个桶
|
||||||
buckets = vector<Entry *>(100);
|
buckets = vector<Pair *>(100);
|
||||||
}
|
}
|
||||||
|
|
||||||
~ArrayHashMap() {
|
~ArrayHashMap() {
|
||||||
@ -45,7 +45,7 @@ class ArrayHashMap {
|
|||||||
/* 查询操作 */
|
/* 查询操作 */
|
||||||
string get(int key) {
|
string get(int key) {
|
||||||
int index = hashFunc(key);
|
int index = hashFunc(key);
|
||||||
Entry *pair = buckets[index];
|
Pair *pair = buckets[index];
|
||||||
if (pair == nullptr)
|
if (pair == nullptr)
|
||||||
return nullptr;
|
return nullptr;
|
||||||
return pair->val;
|
return pair->val;
|
||||||
@ -53,7 +53,7 @@ class ArrayHashMap {
|
|||||||
|
|
||||||
/* 添加操作 */
|
/* 添加操作 */
|
||||||
void put(int key, string val) {
|
void put(int key, string val) {
|
||||||
Entry *pair = new Entry(key, val);
|
Pair *pair = new Pair(key, val);
|
||||||
int index = hashFunc(key);
|
int index = hashFunc(key);
|
||||||
buckets[index] = pair;
|
buckets[index] = pair;
|
||||||
}
|
}
|
||||||
@ -67,20 +67,20 @@ class ArrayHashMap {
|
|||||||
}
|
}
|
||||||
|
|
||||||
/* 获取所有键值对 */
|
/* 获取所有键值对 */
|
||||||
vector<Entry *> entrySet() {
|
vector<Pair *> pairSet() {
|
||||||
vector<Entry *> entrySet;
|
vector<Pair *> pairSet;
|
||||||
for (Entry *pair : buckets) {
|
for (Pair *pair : buckets) {
|
||||||
if (pair != nullptr) {
|
if (pair != nullptr) {
|
||||||
entrySet.push_back(pair);
|
pairSet.push_back(pair);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
return entrySet;
|
return pairSet;
|
||||||
}
|
}
|
||||||
|
|
||||||
/* 获取所有键 */
|
/* 获取所有键 */
|
||||||
vector<int> keySet() {
|
vector<int> keySet() {
|
||||||
vector<int> keySet;
|
vector<int> keySet;
|
||||||
for (Entry *pair : buckets) {
|
for (Pair *pair : buckets) {
|
||||||
if (pair != nullptr) {
|
if (pair != nullptr) {
|
||||||
keySet.push_back(pair->key);
|
keySet.push_back(pair->key);
|
||||||
}
|
}
|
||||||
@ -91,7 +91,7 @@ class ArrayHashMap {
|
|||||||
/* 获取所有值 */
|
/* 获取所有值 */
|
||||||
vector<string> valueSet() {
|
vector<string> valueSet() {
|
||||||
vector<string> valueSet;
|
vector<string> valueSet;
|
||||||
for (Entry *pair : buckets) {
|
for (Pair *pair : buckets) {
|
||||||
if (pair != nullptr) {
|
if (pair != nullptr) {
|
||||||
valueSet.push_back(pair->val);
|
valueSet.push_back(pair->val);
|
||||||
}
|
}
|
||||||
@ -101,7 +101,7 @@ class ArrayHashMap {
|
|||||||
|
|
||||||
/* 打印哈希表 */
|
/* 打印哈希表 */
|
||||||
void print() {
|
void print() {
|
||||||
for (Entry *kv : entrySet()) {
|
for (Pair *kv : pairSet()) {
|
||||||
cout << kv->key << " -> " << kv->val << endl;
|
cout << kv->key << " -> " << kv->val << endl;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -135,7 +135,7 @@ int main() {
|
|||||||
|
|
||||||
/* 遍历哈希表 */
|
/* 遍历哈希表 */
|
||||||
cout << "\n遍历键值对 Key->Value" << endl;
|
cout << "\n遍历键值对 Key->Value" << endl;
|
||||||
for (auto kv : map.entrySet()) {
|
for (auto kv : map.pairSet()) {
|
||||||
cout << kv->key << " -> " << kv->val << endl;
|
cout << kv->key << " -> " << kv->val << endl;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
149
codes/cpp/chapter_hashing/hash_map_chaining.cpp
Normal file
149
codes/cpp/chapter_hashing/hash_map_chaining.cpp
Normal file
@ -0,0 +1,149 @@
|
|||||||
|
/**
|
||||||
|
* File: hash_map_chaining.cpp
|
||||||
|
* Created Time: 2023-06-13
|
||||||
|
* Author: Krahets (krahets@163.com)
|
||||||
|
*/
|
||||||
|
|
||||||
|
#include "../utils/common.hpp"
|
||||||
|
|
||||||
|
/* 键值对 */
|
||||||
|
struct Pair {
|
||||||
|
public:
|
||||||
|
int key;
|
||||||
|
string val;
|
||||||
|
Pair(int key, string val) {
|
||||||
|
this->key = key;
|
||||||
|
this->val = val;
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
/* 链式地址哈希表 */
|
||||||
|
class HashMapChaining {
|
||||||
|
private:
|
||||||
|
int size; // 键值对数量
|
||||||
|
int capacity; // 哈希表容量
|
||||||
|
double loadThres; // 触发扩容的负载因子阈值
|
||||||
|
int extendRatio; // 扩容倍数
|
||||||
|
vector<vector<Pair *>> buckets; // 桶数组
|
||||||
|
|
||||||
|
public:
|
||||||
|
/* 构造方法 */
|
||||||
|
HashMapChaining() : size(0), capacity(4), loadThres(2.0 / 3), extendRatio(2) {
|
||||||
|
buckets.resize(capacity);
|
||||||
|
}
|
||||||
|
|
||||||
|
/* 哈希函数 */
|
||||||
|
int hashFunc(int key) {
|
||||||
|
return key % capacity;
|
||||||
|
}
|
||||||
|
|
||||||
|
/* 负载因子 */
|
||||||
|
double loadFactor() {
|
||||||
|
return (double)size / (double)capacity;
|
||||||
|
}
|
||||||
|
|
||||||
|
/* 查询操作 */
|
||||||
|
string get(int key) {
|
||||||
|
int index = hashFunc(key);
|
||||||
|
// 遍历桶,若找到 key 则返回对应 val
|
||||||
|
for (Pair *pair : buckets[index]) {
|
||||||
|
if (pair->key == key) {
|
||||||
|
return pair->val;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
// 若未找到 key 则返回 nullptr
|
||||||
|
return nullptr;
|
||||||
|
}
|
||||||
|
|
||||||
|
/* 添加操作 */
|
||||||
|
void put(int key, string val) {
|
||||||
|
// 当负载因子超过阈值时,执行扩容
|
||||||
|
if (loadFactor() > loadThres) {
|
||||||
|
extend();
|
||||||
|
}
|
||||||
|
int index = hashFunc(key);
|
||||||
|
// 遍历桶,若遇到指定 key ,则更新对应 val 并返回
|
||||||
|
for (Pair *pair : buckets[index]) {
|
||||||
|
if (pair->key == key) {
|
||||||
|
pair->val = val;
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
// 若无该 key ,则将键值对添加至尾部
|
||||||
|
buckets[index].push_back(new Pair(key, val));
|
||||||
|
size++;
|
||||||
|
}
|
||||||
|
|
||||||
|
/* 删除操作 */
|
||||||
|
void remove(int key) {
|
||||||
|
int index = hashFunc(key);
|
||||||
|
auto &bucket = buckets[index];
|
||||||
|
// 遍历桶,从中删除键值对
|
||||||
|
for (int i = 0; i < bucket.size(); i++) {
|
||||||
|
if (bucket[i]->key == key) {
|
||||||
|
Pair *tmp = bucket[i];
|
||||||
|
bucket.erase(bucket.begin() + i); // 从中删除键值对
|
||||||
|
delete tmp; // 释放内存
|
||||||
|
size--;
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/* 扩容哈希表 */
|
||||||
|
void extend() {
|
||||||
|
// 暂存原哈希表
|
||||||
|
vector<vector<Pair *>> bucketsTmp = buckets;
|
||||||
|
// 初始化扩容后的新哈希表
|
||||||
|
capacity *= extendRatio;
|
||||||
|
buckets.clear();
|
||||||
|
buckets.resize(capacity);
|
||||||
|
size = 0;
|
||||||
|
// 将键值对从原哈希表搬运至新哈希表
|
||||||
|
for (auto &bucket : bucketsTmp) {
|
||||||
|
for (Pair *pair : bucket) {
|
||||||
|
put(pair->key, pair->val);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/* 打印哈希表 */
|
||||||
|
void print() {
|
||||||
|
for (auto &bucket : buckets) {
|
||||||
|
cout << "[";
|
||||||
|
for (Pair *pair : bucket) {
|
||||||
|
cout << pair->key << " -> " << pair->val << ", ";
|
||||||
|
}
|
||||||
|
cout << "]\n";
|
||||||
|
}
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
/* Driver Code */
|
||||||
|
int main() {
|
||||||
|
/* 初始化哈希表 */
|
||||||
|
HashMapChaining map = HashMapChaining();
|
||||||
|
|
||||||
|
/* 添加操作 */
|
||||||
|
// 在哈希表中添加键值对 (key, value)
|
||||||
|
map.put(12836, "小哈");
|
||||||
|
map.put(15937, "小啰");
|
||||||
|
map.put(16750, "小算");
|
||||||
|
map.put(13276, "小法");
|
||||||
|
map.put(10583, "小鸭");
|
||||||
|
cout << "\n添加完成后,哈希表为\nKey -> Value" << endl;
|
||||||
|
map.print();
|
||||||
|
|
||||||
|
/* 查询操作 */
|
||||||
|
// 向哈希表输入键 key ,得到值 value
|
||||||
|
string name = map.get(13276);
|
||||||
|
cout << "\n输入学号 13276 ,查询到姓名 " << name << endl;
|
||||||
|
|
||||||
|
/* 删除操作 */
|
||||||
|
// 在哈希表中删除键值对 (key, value)
|
||||||
|
map.remove(12836);
|
||||||
|
cout << "\n删除 12836 后,哈希表为\nKey -> Value" << endl;
|
||||||
|
map.print();
|
||||||
|
|
||||||
|
return 0;
|
||||||
|
}
|
166
codes/cpp/chapter_hashing/hash_map_open_addressing.cpp
Normal file
166
codes/cpp/chapter_hashing/hash_map_open_addressing.cpp
Normal file
@ -0,0 +1,166 @@
|
|||||||
|
/**
|
||||||
|
* File: hash_map_open_addressing.cpp
|
||||||
|
* Created Time: 2023-06-13
|
||||||
|
* Author: Krahets (krahets@163.com)
|
||||||
|
*/
|
||||||
|
|
||||||
|
#include "../utils/common.hpp"
|
||||||
|
|
||||||
|
/* 键值对 */
|
||||||
|
struct Pair {
|
||||||
|
int key;
|
||||||
|
string val;
|
||||||
|
|
||||||
|
Pair(int k, string v) : key(k), val(v) {
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
/* 开放寻址哈希表 */
|
||||||
|
class HashMapOpenAddressing {
|
||||||
|
private:
|
||||||
|
int size; // 键值对数量
|
||||||
|
int capacity; // 哈希表容量
|
||||||
|
double loadThres; // 触发扩容的负载因子阈值
|
||||||
|
int extendRatio; // 扩容倍数
|
||||||
|
vector<Pair *> buckets; // 桶数组
|
||||||
|
Pair *removed; // 删除标记
|
||||||
|
|
||||||
|
public:
|
||||||
|
/* 构造方法 */
|
||||||
|
HashMapOpenAddressing() {
|
||||||
|
// 构造方法
|
||||||
|
size = 0;
|
||||||
|
capacity = 4;
|
||||||
|
loadThres = 2.0 / 3.0;
|
||||||
|
extendRatio = 2;
|
||||||
|
buckets = vector<Pair *>(capacity, nullptr);
|
||||||
|
removed = new Pair(-1, "-1");
|
||||||
|
}
|
||||||
|
|
||||||
|
/* 哈希函数 */
|
||||||
|
int hashFunc(int key) {
|
||||||
|
return key % capacity;
|
||||||
|
}
|
||||||
|
|
||||||
|
/* 负载因子 */
|
||||||
|
double loadFactor() {
|
||||||
|
return static_cast<double>(size) / capacity;
|
||||||
|
}
|
||||||
|
|
||||||
|
/* 查询操作 */
|
||||||
|
string get(int key) {
|
||||||
|
int index = hashFunc(key);
|
||||||
|
// 线性探测,从 index 开始向后遍历
|
||||||
|
for (int i = 0; i < capacity; i++) {
|
||||||
|
// 计算桶索引,越过尾部返回头部
|
||||||
|
int j = (index + i) % capacity;
|
||||||
|
// 若遇到空桶,说明无此 key ,则返回 nullptr
|
||||||
|
if (buckets[j] == nullptr)
|
||||||
|
return nullptr;
|
||||||
|
// 若遇到指定 key ,则返回对应 val
|
||||||
|
if (buckets[j]->key == key && buckets[j] != removed)
|
||||||
|
return buckets[j]->val;
|
||||||
|
}
|
||||||
|
return nullptr;
|
||||||
|
}
|
||||||
|
|
||||||
|
/* 添加操作 */
|
||||||
|
void put(int key, string val) {
|
||||||
|
// 当负载因子超过阈值时,执行扩容
|
||||||
|
if (loadFactor() > loadThres)
|
||||||
|
extend();
|
||||||
|
int index = hashFunc(key);
|
||||||
|
// 线性探测,从 index 开始向后遍历
|
||||||
|
for (int i = 0; i < capacity; i++) {
|
||||||
|
// 计算桶索引,越过尾部返回头部
|
||||||
|
int j = (index + i) % capacity;
|
||||||
|
// 若遇到空桶、或带有删除标记的桶,则将键值对放入该桶
|
||||||
|
if (buckets[j] == nullptr || buckets[j] == removed) {
|
||||||
|
buckets[j] = new Pair(key, val);
|
||||||
|
size += 1;
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
// 若遇到指定 key ,则更新对应 val
|
||||||
|
if (buckets[j]->key == key) {
|
||||||
|
buckets[j]->val = val;
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/* 删除操作 */
|
||||||
|
void remove(int key) {
|
||||||
|
int index = hashFunc(key);
|
||||||
|
// 线性探测,从 index 开始向后遍历
|
||||||
|
for (int i = 0; i < capacity; i++) {
|
||||||
|
// 计算桶索引,越过尾部返回头部
|
||||||
|
int j = (index + i) % capacity;
|
||||||
|
// 若遇到空桶,说明无此 key ,则直接返回
|
||||||
|
if (buckets[j] == nullptr)
|
||||||
|
return;
|
||||||
|
// 若遇到指定 key ,则标记删除并返回
|
||||||
|
if (buckets[j]->key == key) {
|
||||||
|
delete buckets[j]; // 释放内存
|
||||||
|
buckets[j] = removed;
|
||||||
|
size -= 1;
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/* 扩容哈希表 */
|
||||||
|
void extend() {
|
||||||
|
// 暂存原哈希表
|
||||||
|
vector<Pair *> bucketsTmp = buckets;
|
||||||
|
// 初始化扩容后的新哈希表
|
||||||
|
capacity *= extendRatio;
|
||||||
|
buckets = vector<Pair *>(capacity, nullptr);
|
||||||
|
size = 0;
|
||||||
|
// 将键值对从原哈希表搬运至新哈希表
|
||||||
|
for (Pair *pair : bucketsTmp) {
|
||||||
|
if (pair != nullptr && pair != removed) {
|
||||||
|
put(pair->key, pair->val);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/* 打印哈希表 */
|
||||||
|
void print() {
|
||||||
|
for (auto &pair : buckets) {
|
||||||
|
if (pair != nullptr) {
|
||||||
|
cout << pair->key << " -> " << pair->val << endl;
|
||||||
|
} else {
|
||||||
|
cout << "nullptr" << endl;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
/* Driver Code */
|
||||||
|
int main() {
|
||||||
|
/* 初始化哈希表 */
|
||||||
|
HashMapOpenAddressing map = HashMapOpenAddressing();
|
||||||
|
|
||||||
|
/* 添加操作 */
|
||||||
|
// 在哈希表中添加键值对 (key, value)
|
||||||
|
map.put(12836, "小哈");
|
||||||
|
map.put(15937, "小啰");
|
||||||
|
map.put(16750, "小算");
|
||||||
|
map.put(13276, "小法");
|
||||||
|
map.put(10583, "小鸭");
|
||||||
|
cout << "\n添加完成后,哈希表为\nKey -> Value" << endl;
|
||||||
|
map.print();
|
||||||
|
|
||||||
|
/* 查询操作 */
|
||||||
|
// 向哈希表输入键 key ,得到值 value
|
||||||
|
string name = map.get(13276);
|
||||||
|
cout << "\n输入学号 13276 ,查询到姓名 " << name << endl;
|
||||||
|
|
||||||
|
/* 删除操作 */
|
||||||
|
// 在哈希表中删除键值对 (key, value)
|
||||||
|
map.remove(16750);
|
||||||
|
cout << "\n删除 16750 后,哈希表为\nKey -> Value" << endl;
|
||||||
|
map.print();
|
||||||
|
|
||||||
|
return 0;
|
||||||
|
}
|
@ -68,11 +68,11 @@ public class GraphAdjList {
|
|||||||
/* 打印邻接表 */
|
/* 打印邻接表 */
|
||||||
public void print() {
|
public void print() {
|
||||||
Console.WriteLine("邻接表 =");
|
Console.WriteLine("邻接表 =");
|
||||||
foreach (KeyValuePair<Vertex, List<Vertex>> entry in adjList) {
|
foreach (KeyValuePair<Vertex, List<Vertex>> pair in adjList) {
|
||||||
List<int> tmp = new List<int>();
|
List<int> tmp = new List<int>();
|
||||||
foreach (Vertex vertex in entry.Value)
|
foreach (Vertex vertex in pair.Value)
|
||||||
tmp.Add(vertex.val);
|
tmp.Add(vertex.val);
|
||||||
Console.WriteLine(entry.Key.val + ": [" + string.Join(", ", tmp) + "],");
|
Console.WriteLine(pair.Key.val + ": [" + string.Join(", ", tmp) + "],");
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -7,10 +7,10 @@
|
|||||||
namespace hello_algo.chapter_hashing;
|
namespace hello_algo.chapter_hashing;
|
||||||
|
|
||||||
/* 键值对 int->string */
|
/* 键值对 int->string */
|
||||||
class Entry {
|
class Pair {
|
||||||
public int key;
|
public int key;
|
||||||
public string val;
|
public string val;
|
||||||
public Entry(int key, string val) {
|
public Pair(int key, string val) {
|
||||||
this.key = key;
|
this.key = key;
|
||||||
this.val = val;
|
this.val = val;
|
||||||
}
|
}
|
||||||
@ -18,7 +18,7 @@ class Entry {
|
|||||||
|
|
||||||
/* 基于数组简易实现的哈希表 */
|
/* 基于数组简易实现的哈希表 */
|
||||||
class ArrayHashMap {
|
class ArrayHashMap {
|
||||||
private List<Entry?> buckets;
|
private List<Pair?> buckets;
|
||||||
public ArrayHashMap() {
|
public ArrayHashMap() {
|
||||||
// 初始化数组,包含 100 个桶
|
// 初始化数组,包含 100 个桶
|
||||||
buckets = new();
|
buckets = new();
|
||||||
@ -36,14 +36,14 @@ class ArrayHashMap {
|
|||||||
/* 查询操作 */
|
/* 查询操作 */
|
||||||
public string? get(int key) {
|
public string? get(int key) {
|
||||||
int index = hashFunc(key);
|
int index = hashFunc(key);
|
||||||
Entry? pair = buckets[index];
|
Pair? pair = buckets[index];
|
||||||
if (pair == null) return null;
|
if (pair == null) return null;
|
||||||
return pair.val;
|
return pair.val;
|
||||||
}
|
}
|
||||||
|
|
||||||
/* 添加操作 */
|
/* 添加操作 */
|
||||||
public void put(int key, string val) {
|
public void put(int key, string val) {
|
||||||
Entry pair = new Entry(key, val);
|
Pair pair = new Pair(key, val);
|
||||||
int index = hashFunc(key);
|
int index = hashFunc(key);
|
||||||
buckets[index] = pair;
|
buckets[index] = pair;
|
||||||
}
|
}
|
||||||
@ -56,19 +56,19 @@ class ArrayHashMap {
|
|||||||
}
|
}
|
||||||
|
|
||||||
/* 获取所有键值对 */
|
/* 获取所有键值对 */
|
||||||
public List<Entry> entrySet() {
|
public List<Pair> pairSet() {
|
||||||
List<Entry> entrySet = new();
|
List<Pair> pairSet = new();
|
||||||
foreach (Entry? pair in buckets) {
|
foreach (Pair? pair in buckets) {
|
||||||
if (pair != null)
|
if (pair != null)
|
||||||
entrySet.Add(pair);
|
pairSet.Add(pair);
|
||||||
}
|
}
|
||||||
return entrySet;
|
return pairSet;
|
||||||
}
|
}
|
||||||
|
|
||||||
/* 获取所有键 */
|
/* 获取所有键 */
|
||||||
public List<int> keySet() {
|
public List<int> keySet() {
|
||||||
List<int> keySet = new();
|
List<int> keySet = new();
|
||||||
foreach (Entry? pair in buckets) {
|
foreach (Pair? pair in buckets) {
|
||||||
if (pair != null)
|
if (pair != null)
|
||||||
keySet.Add(pair.key);
|
keySet.Add(pair.key);
|
||||||
}
|
}
|
||||||
@ -78,7 +78,7 @@ class ArrayHashMap {
|
|||||||
/* 获取所有值 */
|
/* 获取所有值 */
|
||||||
public List<string> valueSet() {
|
public List<string> valueSet() {
|
||||||
List<string> valueSet = new();
|
List<string> valueSet = new();
|
||||||
foreach (Entry? pair in buckets) {
|
foreach (Pair? pair in buckets) {
|
||||||
if (pair != null)
|
if (pair != null)
|
||||||
valueSet.Add(pair.val);
|
valueSet.Add(pair.val);
|
||||||
}
|
}
|
||||||
@ -87,7 +87,7 @@ class ArrayHashMap {
|
|||||||
|
|
||||||
/* 打印哈希表 */
|
/* 打印哈希表 */
|
||||||
public void print() {
|
public void print() {
|
||||||
foreach (Entry kv in entrySet()) {
|
foreach (Pair kv in pairSet()) {
|
||||||
Console.WriteLine(kv.key + " -> " + kv.val);
|
Console.WriteLine(kv.key + " -> " + kv.val);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -123,7 +123,7 @@ public class array_hash_map {
|
|||||||
|
|
||||||
/* 遍历哈希表 */
|
/* 遍历哈希表 */
|
||||||
Console.WriteLine("\n遍历键值对 Key->Value");
|
Console.WriteLine("\n遍历键值对 Key->Value");
|
||||||
foreach (Entry kv in map.entrySet()) {
|
foreach (Pair kv in map.pairSet()) {
|
||||||
Console.WriteLine(kv.key + " -> " + kv.val);
|
Console.WriteLine(kv.key + " -> " + kv.val);
|
||||||
}
|
}
|
||||||
Console.WriteLine("\n单独遍历键 Key");
|
Console.WriteLine("\n单独遍历键 Key");
|
||||||
|
@ -4,16 +4,16 @@
|
|||||||
* Author: liuyuxin (gvenusleo@gmail.com)
|
* Author: liuyuxin (gvenusleo@gmail.com)
|
||||||
*/
|
*/
|
||||||
|
|
||||||
/* 键值对 int -> String */
|
/* 键值对 */
|
||||||
class Entry {
|
class Pair {
|
||||||
int key;
|
int key;
|
||||||
String val;
|
String val;
|
||||||
Entry(this.key, this.val);
|
Pair(this.key, this.val);
|
||||||
}
|
}
|
||||||
|
|
||||||
/* 基于数组简易实现的哈希表 */
|
/* 基于数组简易实现的哈希表 */
|
||||||
class ArrayHashMap {
|
class ArrayHashMap {
|
||||||
late List<Entry?> _buckets;
|
late List<Pair?> _buckets;
|
||||||
|
|
||||||
ArrayHashMap() {
|
ArrayHashMap() {
|
||||||
// 初始化数组,包含 100 个桶
|
// 初始化数组,包含 100 个桶
|
||||||
@ -29,7 +29,7 @@ class ArrayHashMap {
|
|||||||
/* 查询操作 */
|
/* 查询操作 */
|
||||||
String? get(int key) {
|
String? get(int key) {
|
||||||
final int index = _hashFunc(key);
|
final int index = _hashFunc(key);
|
||||||
final Entry? pair = _buckets[index];
|
final Pair? pair = _buckets[index];
|
||||||
if (pair == null) {
|
if (pair == null) {
|
||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
@ -38,7 +38,7 @@ class ArrayHashMap {
|
|||||||
|
|
||||||
/* 添加操作 */
|
/* 添加操作 */
|
||||||
void put(int key, String val) {
|
void put(int key, String val) {
|
||||||
final Entry pair = Entry(key, val);
|
final Pair pair = Pair(key, val);
|
||||||
final int index = _hashFunc(key);
|
final int index = _hashFunc(key);
|
||||||
_buckets[index] = pair;
|
_buckets[index] = pair;
|
||||||
}
|
}
|
||||||
@ -50,20 +50,20 @@ class ArrayHashMap {
|
|||||||
}
|
}
|
||||||
|
|
||||||
/* 获取所有键值对 */
|
/* 获取所有键值对 */
|
||||||
List<Entry> entrySet() {
|
List<Pair> pairSet() {
|
||||||
List<Entry> entrySet = [];
|
List<Pair> pairSet = [];
|
||||||
for (final Entry? pair in _buckets) {
|
for (final Pair? pair in _buckets) {
|
||||||
if (pair != null) {
|
if (pair != null) {
|
||||||
entrySet.add(pair);
|
pairSet.add(pair);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
return entrySet;
|
return pairSet;
|
||||||
}
|
}
|
||||||
|
|
||||||
/* 获取所有键 */
|
/* 获取所有键 */
|
||||||
List<int> keySet() {
|
List<int> keySet() {
|
||||||
List<int> keySet = [];
|
List<int> keySet = [];
|
||||||
for (final Entry? pair in _buckets) {
|
for (final Pair? pair in _buckets) {
|
||||||
if (pair != null) {
|
if (pair != null) {
|
||||||
keySet.add(pair.key);
|
keySet.add(pair.key);
|
||||||
}
|
}
|
||||||
@ -74,7 +74,7 @@ class ArrayHashMap {
|
|||||||
/* 获取所有值 */
|
/* 获取所有值 */
|
||||||
List<String> values() {
|
List<String> values() {
|
||||||
List<String> valueSet = [];
|
List<String> valueSet = [];
|
||||||
for (final Entry? pair in _buckets) {
|
for (final Pair? pair in _buckets) {
|
||||||
if (pair != null) {
|
if (pair != null) {
|
||||||
valueSet.add(pair.val);
|
valueSet.add(pair.val);
|
||||||
}
|
}
|
||||||
@ -84,7 +84,7 @@ class ArrayHashMap {
|
|||||||
|
|
||||||
/* 打印哈希表 */
|
/* 打印哈希表 */
|
||||||
void printHashMap() {
|
void printHashMap() {
|
||||||
for (final Entry kv in entrySet()) {
|
for (final Pair kv in pairSet()) {
|
||||||
print("${kv.key} -> ${kv.val}");
|
print("${kv.key} -> ${kv.val}");
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -118,7 +118,7 @@ void main() {
|
|||||||
|
|
||||||
/* 遍历哈希表 */
|
/* 遍历哈希表 */
|
||||||
print("\n遍历键值对 Key->Value");
|
print("\n遍历键值对 Key->Value");
|
||||||
map.entrySet().forEach((kv) => print("${kv.key} -> ${kv.val}"));
|
map.pairSet().forEach((kv) => print("${kv.key} -> ${kv.val}"));
|
||||||
print("\n单独遍历键 Key");
|
print("\n单独遍历键 Key");
|
||||||
map.keySet().forEach((key) => print("$key"));
|
map.keySet().forEach((key) => print("$key"));
|
||||||
print("\n单独遍历值 Value");
|
print("\n单独遍历值 Value");
|
||||||
|
@ -6,21 +6,21 @@ package chapter_hashing
|
|||||||
|
|
||||||
import "fmt"
|
import "fmt"
|
||||||
|
|
||||||
/* 键值对 int->String */
|
/* 键值对 */
|
||||||
type entry struct {
|
type pair struct {
|
||||||
key int
|
key int
|
||||||
val string
|
val string
|
||||||
}
|
}
|
||||||
|
|
||||||
/* 基于数组简易实现的哈希表 */
|
/* 基于数组简易实现的哈希表 */
|
||||||
type arrayHashMap struct {
|
type arrayHashMap struct {
|
||||||
buckets []*entry
|
buckets []*pair
|
||||||
}
|
}
|
||||||
|
|
||||||
/* 初始化哈希表 */
|
/* 初始化哈希表 */
|
||||||
func newArrayHashMap() *arrayHashMap {
|
func newArrayHashMap() *arrayHashMap {
|
||||||
// 初始化数组,包含 100 个桶
|
// 初始化数组,包含 100 个桶
|
||||||
buckets := make([]*entry, 100)
|
buckets := make([]*pair, 100)
|
||||||
return &arrayHashMap{buckets: buckets}
|
return &arrayHashMap{buckets: buckets}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -42,7 +42,7 @@ func (a *arrayHashMap) get(key int) string {
|
|||||||
|
|
||||||
/* 添加操作 */
|
/* 添加操作 */
|
||||||
func (a *arrayHashMap) put(key int, val string) {
|
func (a *arrayHashMap) put(key int, val string) {
|
||||||
pair := &entry{key: key, val: val}
|
pair := &pair{key: key, val: val}
|
||||||
index := a.hashFunc(key)
|
index := a.hashFunc(key)
|
||||||
a.buckets[index] = pair
|
a.buckets[index] = pair
|
||||||
}
|
}
|
||||||
@ -55,8 +55,8 @@ func (a *arrayHashMap) remove(key int) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
/* 获取所有键对 */
|
/* 获取所有键对 */
|
||||||
func (a *arrayHashMap) entrySet() []*entry {
|
func (a *arrayHashMap) pairSet() []*pair {
|
||||||
var pairs []*entry
|
var pairs []*pair
|
||||||
for _, pair := range a.buckets {
|
for _, pair := range a.buckets {
|
||||||
if pair != nil {
|
if pair != nil {
|
||||||
pairs = append(pairs, pair)
|
pairs = append(pairs, pair)
|
||||||
|
@ -36,7 +36,7 @@ func TestArrayHashMap(t *testing.T) {
|
|||||||
|
|
||||||
/* 遍历哈希表 */
|
/* 遍历哈希表 */
|
||||||
fmt.Println("\n遍历键值对 Key->Value")
|
fmt.Println("\n遍历键值对 Key->Value")
|
||||||
for _, kv := range mapp.entrySet() {
|
for _, kv := range mapp.pairSet() {
|
||||||
fmt.Println(kv.key, " -> ", kv.val)
|
fmt.Println(kv.key, " -> ", kv.val)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -71,11 +71,11 @@ class GraphAdjList {
|
|||||||
/* 打印邻接表 */
|
/* 打印邻接表 */
|
||||||
public void print() {
|
public void print() {
|
||||||
System.out.println("邻接表 =");
|
System.out.println("邻接表 =");
|
||||||
for (Map.Entry<Vertex, List<Vertex>> entry : adjList.entrySet()) {
|
for (Map.Entry<Vertex, List<Vertex>> pair : adjList.entrySet()) {
|
||||||
List<Integer> tmp = new ArrayList<>();
|
List<Integer> tmp = new ArrayList<>();
|
||||||
for (Vertex vertex : entry.getValue())
|
for (Vertex vertex : pair.getValue())
|
||||||
tmp.add(vertex.val);
|
tmp.add(vertex.val);
|
||||||
System.out.println(entry.getKey().val + ": " + tmp + ",");
|
System.out.println(pair.getKey().val + ": " + tmp + ",");
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -8,12 +8,12 @@ package chapter_hashing;
|
|||||||
|
|
||||||
import java.util.*;
|
import java.util.*;
|
||||||
|
|
||||||
/* 键值对 int->String */
|
/* 键值对 */
|
||||||
class Entry {
|
class Pair {
|
||||||
public int key;
|
public int key;
|
||||||
public String val;
|
public String val;
|
||||||
|
|
||||||
public Entry(int key, String val) {
|
public Pair(int key, String val) {
|
||||||
this.key = key;
|
this.key = key;
|
||||||
this.val = val;
|
this.val = val;
|
||||||
}
|
}
|
||||||
@ -21,7 +21,7 @@ class Entry {
|
|||||||
|
|
||||||
/* 基于数组简易实现的哈希表 */
|
/* 基于数组简易实现的哈希表 */
|
||||||
class ArrayHashMap {
|
class ArrayHashMap {
|
||||||
private List<Entry> buckets;
|
private List<Pair> buckets;
|
||||||
|
|
||||||
public ArrayHashMap() {
|
public ArrayHashMap() {
|
||||||
// 初始化数组,包含 100 个桶
|
// 初始化数组,包含 100 个桶
|
||||||
@ -40,7 +40,7 @@ class ArrayHashMap {
|
|||||||
/* 查询操作 */
|
/* 查询操作 */
|
||||||
public String get(int key) {
|
public String get(int key) {
|
||||||
int index = hashFunc(key);
|
int index = hashFunc(key);
|
||||||
Entry pair = buckets.get(index);
|
Pair pair = buckets.get(index);
|
||||||
if (pair == null)
|
if (pair == null)
|
||||||
return null;
|
return null;
|
||||||
return pair.val;
|
return pair.val;
|
||||||
@ -48,7 +48,7 @@ class ArrayHashMap {
|
|||||||
|
|
||||||
/* 添加操作 */
|
/* 添加操作 */
|
||||||
public void put(int key, String val) {
|
public void put(int key, String val) {
|
||||||
Entry pair = new Entry(key, val);
|
Pair pair = new Pair(key, val);
|
||||||
int index = hashFunc(key);
|
int index = hashFunc(key);
|
||||||
buckets.set(index, pair);
|
buckets.set(index, pair);
|
||||||
}
|
}
|
||||||
@ -61,19 +61,19 @@ class ArrayHashMap {
|
|||||||
}
|
}
|
||||||
|
|
||||||
/* 获取所有键值对 */
|
/* 获取所有键值对 */
|
||||||
public List<Entry> entrySet() {
|
public List<Pair> pairSet() {
|
||||||
List<Entry> entrySet = new ArrayList<>();
|
List<Pair> pairSet = new ArrayList<>();
|
||||||
for (Entry pair : buckets) {
|
for (Pair pair : buckets) {
|
||||||
if (pair != null)
|
if (pair != null)
|
||||||
entrySet.add(pair);
|
pairSet.add(pair);
|
||||||
}
|
}
|
||||||
return entrySet;
|
return pairSet;
|
||||||
}
|
}
|
||||||
|
|
||||||
/* 获取所有键 */
|
/* 获取所有键 */
|
||||||
public List<Integer> keySet() {
|
public List<Integer> keySet() {
|
||||||
List<Integer> keySet = new ArrayList<>();
|
List<Integer> keySet = new ArrayList<>();
|
||||||
for (Entry pair : buckets) {
|
for (Pair pair : buckets) {
|
||||||
if (pair != null)
|
if (pair != null)
|
||||||
keySet.add(pair.key);
|
keySet.add(pair.key);
|
||||||
}
|
}
|
||||||
@ -83,7 +83,7 @@ class ArrayHashMap {
|
|||||||
/* 获取所有值 */
|
/* 获取所有值 */
|
||||||
public List<String> valueSet() {
|
public List<String> valueSet() {
|
||||||
List<String> valueSet = new ArrayList<>();
|
List<String> valueSet = new ArrayList<>();
|
||||||
for (Entry pair : buckets) {
|
for (Pair pair : buckets) {
|
||||||
if (pair != null)
|
if (pair != null)
|
||||||
valueSet.add(pair.val);
|
valueSet.add(pair.val);
|
||||||
}
|
}
|
||||||
@ -92,7 +92,7 @@ class ArrayHashMap {
|
|||||||
|
|
||||||
/* 打印哈希表 */
|
/* 打印哈希表 */
|
||||||
public void print() {
|
public void print() {
|
||||||
for (Entry kv : entrySet()) {
|
for (Pair kv : pairSet()) {
|
||||||
System.out.println(kv.key + " -> " + kv.val);
|
System.out.println(kv.key + " -> " + kv.val);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -126,7 +126,7 @@ public class array_hash_map {
|
|||||||
|
|
||||||
/* 遍历哈希表 */
|
/* 遍历哈希表 */
|
||||||
System.out.println("\n遍历键值对 Key->Value");
|
System.out.println("\n遍历键值对 Key->Value");
|
||||||
for (Entry kv : map.entrySet()) {
|
for (Pair kv : map.pairSet()) {
|
||||||
System.out.println(kv.key + " -> " + kv.val);
|
System.out.println(kv.key + " -> " + kv.val);
|
||||||
}
|
}
|
||||||
System.out.println("\n单独遍历键 Key");
|
System.out.println("\n单独遍历键 Key");
|
||||||
|
157
codes/java/chapter_hashing/hash_map_chaining.java
Normal file
157
codes/java/chapter_hashing/hash_map_chaining.java
Normal file
@ -0,0 +1,157 @@
|
|||||||
|
/**
|
||||||
|
* File: hash_map_chaining.java
|
||||||
|
* Created Time: 2023-06-13
|
||||||
|
* Author: Krahets (krahets@163.com)
|
||||||
|
*/
|
||||||
|
|
||||||
|
package chapter_hashing;
|
||||||
|
|
||||||
|
import java.util.ArrayList;
|
||||||
|
import java.util.List;
|
||||||
|
|
||||||
|
/* 键值对 */
|
||||||
|
class Pair {
|
||||||
|
public int key;
|
||||||
|
public String val;
|
||||||
|
|
||||||
|
public Pair(int key, String val) {
|
||||||
|
this.key = key;
|
||||||
|
this.val = val;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/* 链式地址哈希表 */
|
||||||
|
class HashMapChaining {
|
||||||
|
int size; // 键值对数量
|
||||||
|
int capacity; // 哈希表容量
|
||||||
|
double loadThres; // 触发扩容的负载因子阈值
|
||||||
|
int extendRatio; // 扩容倍数
|
||||||
|
List<List<Pair>> buckets; // 桶数组
|
||||||
|
|
||||||
|
/* 构造方法 */
|
||||||
|
public HashMapChaining() {
|
||||||
|
size = 0;
|
||||||
|
capacity = 4;
|
||||||
|
loadThres = 2 / 3.0;
|
||||||
|
extendRatio = 2;
|
||||||
|
buckets = new ArrayList<>(capacity);
|
||||||
|
for (int i = 0; i < capacity; i++) {
|
||||||
|
buckets.add(new ArrayList<>());
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/* 哈希函数 */
|
||||||
|
int hashFunc(int key) {
|
||||||
|
return key % capacity;
|
||||||
|
}
|
||||||
|
|
||||||
|
/* 负载因子 */
|
||||||
|
double loadFactor() {
|
||||||
|
return (double) size / capacity;
|
||||||
|
}
|
||||||
|
|
||||||
|
/* 查询操作 */
|
||||||
|
String get(int key) {
|
||||||
|
int index = hashFunc(key);
|
||||||
|
List<Pair> bucket = buckets.get(index);
|
||||||
|
// 遍历桶,若找到 key 则返回对应 val
|
||||||
|
for (Pair pair : bucket) {
|
||||||
|
if (pair.key == key) {
|
||||||
|
return pair.val;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
// 若未找到 key 则返回 null
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
|
||||||
|
/* 添加操作 */
|
||||||
|
void put(int key, String val) {
|
||||||
|
// 当负载因子超过阈值时,执行扩容
|
||||||
|
if (loadFactor() > loadThres) {
|
||||||
|
extend();
|
||||||
|
}
|
||||||
|
int index = hashFunc(key);
|
||||||
|
List<Pair> bucket = buckets.get(index);
|
||||||
|
// 遍历桶,若遇到指定 key ,则更新对应 val 并返回
|
||||||
|
for (Pair pair : bucket) {
|
||||||
|
if (pair.key == key) {
|
||||||
|
pair.val = val;
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
// 若无该 key ,则将键值对添加至尾部
|
||||||
|
Pair pair = new Pair(key, val);
|
||||||
|
bucket.add(pair);
|
||||||
|
size++;
|
||||||
|
}
|
||||||
|
|
||||||
|
/* 删除操作 */
|
||||||
|
void remove(int key) {
|
||||||
|
int index = hashFunc(key);
|
||||||
|
List<Pair> bucket = buckets.get(index);
|
||||||
|
// 遍历桶,从中删除键值对
|
||||||
|
for (Pair pair : bucket) {
|
||||||
|
if (pair.key == key)
|
||||||
|
bucket.remove(pair);
|
||||||
|
}
|
||||||
|
size--;
|
||||||
|
}
|
||||||
|
|
||||||
|
/* 扩容哈希表 */
|
||||||
|
void extend() {
|
||||||
|
// 暂存原哈希表
|
||||||
|
List<List<Pair>> bucketsTmp = buckets;
|
||||||
|
// 初始化扩容后的新哈希表
|
||||||
|
capacity *= extendRatio;
|
||||||
|
buckets = new ArrayList<>(capacity);
|
||||||
|
for (int i = 0; i < capacity; i++) {
|
||||||
|
buckets.add(new ArrayList<>());
|
||||||
|
}
|
||||||
|
size = 0;
|
||||||
|
// 将键值对从原哈希表搬运至新哈希表
|
||||||
|
for (List<Pair> bucket : bucketsTmp) {
|
||||||
|
for (Pair pair : bucket) {
|
||||||
|
put(pair.key, pair.val);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/* 打印哈希表 */
|
||||||
|
void print() {
|
||||||
|
for (List<Pair> bucket : buckets) {
|
||||||
|
List<String> res = new ArrayList<>();
|
||||||
|
for (Pair pair : bucket) {
|
||||||
|
res.add(pair.key + " -> " + pair.val);
|
||||||
|
}
|
||||||
|
System.out.println(res);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public class hash_map_chaining {
|
||||||
|
public static void main(String[] args) {
|
||||||
|
/* 初始化哈希表 */
|
||||||
|
HashMapChaining map = new HashMapChaining();
|
||||||
|
|
||||||
|
/* 添加操作 */
|
||||||
|
// 在哈希表中添加键值对 (key, value)
|
||||||
|
map.put(12836, "小哈");
|
||||||
|
map.put(15937, "小啰");
|
||||||
|
map.put(16750, "小算");
|
||||||
|
map.put(13276, "小法");
|
||||||
|
map.put(10583, "小鸭");
|
||||||
|
System.out.println("\n添加完成后,哈希表为\nKey -> Value");
|
||||||
|
map.print();
|
||||||
|
|
||||||
|
/* 查询操作 */
|
||||||
|
// 向哈希表输入键 key ,得到值 value
|
||||||
|
String name = map.get(13276);
|
||||||
|
System.out.println("\n输入学号 13276 ,查询到姓名 " + name);
|
||||||
|
|
||||||
|
/* 删除操作 */
|
||||||
|
// 在哈希表中删除键值对 (key, value)
|
||||||
|
map.remove(12836);
|
||||||
|
System.out.println("\n删除 12836 后,哈希表为\nKey -> Value");
|
||||||
|
map.print();
|
||||||
|
}
|
||||||
|
}
|
165
codes/java/chapter_hashing/hash_map_open_addressing.java
Normal file
165
codes/java/chapter_hashing/hash_map_open_addressing.java
Normal file
@ -0,0 +1,165 @@
|
|||||||
|
/**
|
||||||
|
* File: hash_map_chaining.java
|
||||||
|
* Created Time: 2023-06-13
|
||||||
|
* Author: Krahets (krahets@163.com)
|
||||||
|
*/
|
||||||
|
|
||||||
|
package chapter_hashing;
|
||||||
|
|
||||||
|
/* 键值对 */
|
||||||
|
class Pair {
|
||||||
|
public int key;
|
||||||
|
public String val;
|
||||||
|
|
||||||
|
public Pair(int key, String val) {
|
||||||
|
this.key = key;
|
||||||
|
this.val = val;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/* 开放寻址哈希表 */
|
||||||
|
class HashMapOpenAddressing {
|
||||||
|
private int size; // 键值对数量
|
||||||
|
private int capacity; // 哈希表容量
|
||||||
|
private double loadThres; // 触发扩容的负载因子阈值
|
||||||
|
private int extendRatio; // 扩容倍数
|
||||||
|
private Pair[] buckets; // 桶数组
|
||||||
|
private Pair removed; // 删除标记
|
||||||
|
|
||||||
|
/* 构造方法 */
|
||||||
|
public HashMapOpenAddressing() {
|
||||||
|
size = 0;
|
||||||
|
capacity = 4;
|
||||||
|
loadThres = 2.0 / 3.0;
|
||||||
|
extendRatio = 2;
|
||||||
|
buckets = new Pair[capacity];
|
||||||
|
removed = new Pair(-1, "-1");
|
||||||
|
}
|
||||||
|
|
||||||
|
/* 哈希函数 */
|
||||||
|
public int hashFunc(int key) {
|
||||||
|
return key % capacity;
|
||||||
|
}
|
||||||
|
|
||||||
|
/* 负载因子 */
|
||||||
|
public double loadFactor() {
|
||||||
|
return (double) size / capacity;
|
||||||
|
}
|
||||||
|
|
||||||
|
/* 查询操作 */
|
||||||
|
public String get(int key) {
|
||||||
|
int index = hashFunc(key);
|
||||||
|
// 线性探测,从 index 开始向后遍历
|
||||||
|
for (int i = 0; i < capacity; i++) {
|
||||||
|
// 计算桶索引,越过尾部返回头部
|
||||||
|
int j = (index + i) % capacity;
|
||||||
|
// 若遇到空桶,说明无此 key ,则返回 null
|
||||||
|
if (buckets[j] == null)
|
||||||
|
return null;
|
||||||
|
// 若遇到指定 key ,则返回对应 val
|
||||||
|
if (buckets[j].key == key && buckets[j] != removed)
|
||||||
|
return buckets[j].val;
|
||||||
|
}
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
|
||||||
|
/* 添加操作 */
|
||||||
|
public void put(int key, String val) {
|
||||||
|
// 当负载因子超过阈值时,执行扩容
|
||||||
|
if (loadFactor() > loadThres) {
|
||||||
|
extend();
|
||||||
|
}
|
||||||
|
int index = hashFunc(key);
|
||||||
|
// 线性探测,从 index 开始向后遍历
|
||||||
|
for (int i = 0; i < capacity; i++) {
|
||||||
|
// 计算桶索引,越过尾部返回头部
|
||||||
|
int j = (index + i) % capacity;
|
||||||
|
// 若遇到空桶、或带有删除标记的桶,则将键值对放入该桶
|
||||||
|
if (buckets[j] == null || buckets[j] == removed) {
|
||||||
|
buckets[j] = new Pair(key, val);
|
||||||
|
size += 1;
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
// 若遇到指定 key ,则更新对应 val
|
||||||
|
if (buckets[j].key == key) {
|
||||||
|
buckets[j].val = val;
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/* 删除操作 */
|
||||||
|
public void remove(int key) {
|
||||||
|
int index = hashFunc(key);
|
||||||
|
// 线性探测,从 index 开始向后遍历
|
||||||
|
for (int i = 0; i < capacity; i++) {
|
||||||
|
// 计算桶索引,越过尾部返回头部
|
||||||
|
int j = (index + i) % capacity;
|
||||||
|
// 若遇到空桶,说明无此 key ,则直接返回
|
||||||
|
if (buckets[j] == null) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
// 若遇到指定 key ,则标记删除并返回
|
||||||
|
if (buckets[j].key == key) {
|
||||||
|
buckets[j] = removed;
|
||||||
|
size -= 1;
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/* 扩容哈希表 */
|
||||||
|
public void extend() {
|
||||||
|
// 暂存原哈希表
|
||||||
|
Pair[] bucketsTmp = buckets;
|
||||||
|
// 初始化扩容后的新哈希表
|
||||||
|
capacity *= extendRatio;
|
||||||
|
buckets = new Pair[capacity];
|
||||||
|
size = 0;
|
||||||
|
// 将键值对从原哈希表搬运至新哈希表
|
||||||
|
for (Pair pair : bucketsTmp) {
|
||||||
|
if (pair != null && pair != removed) {
|
||||||
|
put(pair.key, pair.val);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/* 打印哈希表 */
|
||||||
|
public void print() {
|
||||||
|
for (Pair pair : buckets) {
|
||||||
|
if (pair != null) {
|
||||||
|
System.out.println(pair.key + " -> " + pair.val);
|
||||||
|
} else {
|
||||||
|
System.out.println("null");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public class hash_map_open_addressing {
|
||||||
|
public static void main(String[] args) {
|
||||||
|
/* 初始化哈希表 */
|
||||||
|
HashMapOpenAddressing map = new HashMapOpenAddressing();
|
||||||
|
|
||||||
|
/* 添加操作 */
|
||||||
|
// 在哈希表中添加键值对 (key, value)
|
||||||
|
map.put(12836, "小哈");
|
||||||
|
map.put(15937, "小啰");
|
||||||
|
map.put(16750, "小算");
|
||||||
|
map.put(13276, "小法");
|
||||||
|
map.put(10583, "小鸭");
|
||||||
|
System.out.println("\n添加完成后,哈希表为\nKey -> Value");
|
||||||
|
map.print();
|
||||||
|
|
||||||
|
/* 查询操作 */
|
||||||
|
// 向哈希表输入键 key ,得到值 value
|
||||||
|
String name = map.get(13276);
|
||||||
|
System.out.println("\n输入学号 13276 ,查询到姓名 " + name);
|
||||||
|
|
||||||
|
/* 删除操作 */
|
||||||
|
// 在哈希表中删除键值对 (key, value)
|
||||||
|
map.remove(16750);
|
||||||
|
System.out.println("\n删除 16750 后,哈希表为\nKey -> Value");
|
||||||
|
map.print();
|
||||||
|
}
|
||||||
|
}
|
@ -5,7 +5,7 @@
|
|||||||
*/
|
*/
|
||||||
|
|
||||||
/* 键值对 Number -> String */
|
/* 键值对 Number -> String */
|
||||||
class Entry {
|
class Pair {
|
||||||
constructor(key, val) {
|
constructor(key, val) {
|
||||||
this.key = key;
|
this.key = key;
|
||||||
this.val = val;
|
this.val = val;
|
||||||
@ -28,15 +28,15 @@ class ArrayHashMap {
|
|||||||
/* 查询操作 */
|
/* 查询操作 */
|
||||||
get(key) {
|
get(key) {
|
||||||
let index = this.#hashFunc(key);
|
let index = this.#hashFunc(key);
|
||||||
let entry = this.#buckets[index];
|
let pair = this.#buckets[index];
|
||||||
if (entry === null) return null;
|
if (pair === null) return null;
|
||||||
return entry.val;
|
return pair.val;
|
||||||
}
|
}
|
||||||
|
|
||||||
/* 添加操作 */
|
/* 添加操作 */
|
||||||
set(key, val) {
|
set(key, val) {
|
||||||
let index = this.#hashFunc(key);
|
let index = this.#hashFunc(key);
|
||||||
this.#buckets[index] = new Entry(key, val);
|
this.#buckets[index] = new Pair(key, val);
|
||||||
}
|
}
|
||||||
|
|
||||||
/* 删除操作 */
|
/* 删除操作 */
|
||||||
@ -81,10 +81,10 @@ class ArrayHashMap {
|
|||||||
|
|
||||||
/* 打印哈希表 */
|
/* 打印哈希表 */
|
||||||
print() {
|
print() {
|
||||||
let entrySet = this.entries();
|
let pairSet = this.entries();
|
||||||
for (const entry of entrySet) {
|
for (const pair of pairSet) {
|
||||||
if (!entry) continue;
|
if (!pair) continue;
|
||||||
console.info(`${entry.key} -> ${entry.val}`);
|
console.info(`${pair.key} -> ${pair.val}`);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -115,9 +115,9 @@ map.print();
|
|||||||
|
|
||||||
/* 遍历哈希表 */
|
/* 遍历哈希表 */
|
||||||
console.info('\n遍历键值对 Key->Value');
|
console.info('\n遍历键值对 Key->Value');
|
||||||
for (const entry of map.entries()) {
|
for (const pair of map.entries()) {
|
||||||
if (!entry) continue;
|
if (!pair) continue;
|
||||||
console.info(entry.key + ' -> ' + entry.val);
|
console.info(pair.key + ' -> ' + pair.val);
|
||||||
}
|
}
|
||||||
console.info('\n单独遍历键 Key');
|
console.info('\n单独遍历键 Key');
|
||||||
for (const key of map.keys()) {
|
for (const key of map.keys()) {
|
||||||
|
@ -5,8 +5,8 @@ Author: msk397 (machangxinq@gmail.com)
|
|||||||
"""
|
"""
|
||||||
|
|
||||||
|
|
||||||
class Entry:
|
class Pair:
|
||||||
"""键值对 int->String"""
|
"""键值对"""
|
||||||
|
|
||||||
def __init__(self, key: int, val: str):
|
def __init__(self, key: int, val: str):
|
||||||
self.key = key
|
self.key = key
|
||||||
@ -19,7 +19,7 @@ class ArrayHashMap:
|
|||||||
def __init__(self):
|
def __init__(self):
|
||||||
"""构造方法"""
|
"""构造方法"""
|
||||||
# 初始化数组,包含 100 个桶
|
# 初始化数组,包含 100 个桶
|
||||||
self.buckets: list[Entry | None] = [None] * 100
|
self.buckets: list[Pair | None] = [None] * 100
|
||||||
|
|
||||||
def hash_func(self, key: int) -> int:
|
def hash_func(self, key: int) -> int:
|
||||||
"""哈希函数"""
|
"""哈希函数"""
|
||||||
@ -29,26 +29,26 @@ class ArrayHashMap:
|
|||||||
def get(self, key: int) -> str:
|
def get(self, key: int) -> str:
|
||||||
"""查询操作"""
|
"""查询操作"""
|
||||||
index: int = self.hash_func(key)
|
index: int = self.hash_func(key)
|
||||||
pair: Entry = self.buckets[index]
|
pair: Pair = self.buckets[index]
|
||||||
if pair is None:
|
if pair is None:
|
||||||
return None
|
return None
|
||||||
return pair.val
|
return pair.val
|
||||||
|
|
||||||
def put(self, key: int, val: str) -> None:
|
def put(self, key: int, val: str):
|
||||||
"""添加操作"""
|
"""添加操作"""
|
||||||
pair = Entry(key, val)
|
pair = Pair(key, val)
|
||||||
index: int = self.hash_func(key)
|
index: int = self.hash_func(key)
|
||||||
self.buckets[index] = pair
|
self.buckets[index] = pair
|
||||||
|
|
||||||
def remove(self, key: int) -> None:
|
def remove(self, key: int):
|
||||||
"""删除操作"""
|
"""删除操作"""
|
||||||
index: int = self.hash_func(key)
|
index: int = self.hash_func(key)
|
||||||
# 置为 None ,代表删除
|
# 置为 None ,代表删除
|
||||||
self.buckets[index] = None
|
self.buckets[index] = None
|
||||||
|
|
||||||
def entry_set(self) -> list[Entry]:
|
def entry_set(self) -> list[Pair]:
|
||||||
"""获取所有键值对"""
|
"""获取所有键值对"""
|
||||||
result: list[Entry] = []
|
result: list[Pair] = []
|
||||||
for pair in self.buckets:
|
for pair in self.buckets:
|
||||||
if pair is not None:
|
if pair is not None:
|
||||||
result.append(pair)
|
result.append(pair)
|
||||||
@ -70,7 +70,7 @@ class ArrayHashMap:
|
|||||||
result.append(pair.val)
|
result.append(pair.val)
|
||||||
return result
|
return result
|
||||||
|
|
||||||
def print(self) -> None:
|
def print(self):
|
||||||
"""打印哈希表"""
|
"""打印哈希表"""
|
||||||
for pair in self.buckets:
|
for pair in self.buckets:
|
||||||
if pair is not None:
|
if pair is not None:
|
||||||
|
119
codes/python/chapter_hashing/hash_map_chaining.py
Normal file
119
codes/python/chapter_hashing/hash_map_chaining.py
Normal file
@ -0,0 +1,119 @@
|
|||||||
|
"""
|
||||||
|
File: hash_map_chaining.py
|
||||||
|
Created Time: 2023-06-13
|
||||||
|
Author: Krahets (krahets@163.com)
|
||||||
|
"""
|
||||||
|
|
||||||
|
class Pair:
|
||||||
|
"""键值对"""
|
||||||
|
|
||||||
|
def __init__(self, key: int, val: str):
|
||||||
|
self.key = key
|
||||||
|
self.val = val
|
||||||
|
|
||||||
|
|
||||||
|
class HashMapChaining:
|
||||||
|
"""链式地址哈希表"""
|
||||||
|
|
||||||
|
def __init__(self):
|
||||||
|
"""构造方法"""
|
||||||
|
self.size = 0 # 键值对数量
|
||||||
|
self.capacity = 4 # 哈希表容量
|
||||||
|
self.load_thres = 2 / 3 # 触发扩容的负载因子阈值
|
||||||
|
self.extend_ratio = 2 # 扩容倍数
|
||||||
|
self.buckets = [[] for _ in range(self.capacity)] # 桶数组
|
||||||
|
|
||||||
|
def hash_func(self, key: int) -> int:
|
||||||
|
"""哈希函数"""
|
||||||
|
return key % self.capacity
|
||||||
|
|
||||||
|
def load_factor(self) -> float:
|
||||||
|
"""负载因子"""
|
||||||
|
return self.size / self.capacity
|
||||||
|
|
||||||
|
def get(self, key: int) -> str:
|
||||||
|
"""查询操作"""
|
||||||
|
index = self.hash_func(key)
|
||||||
|
bucket = self.buckets[index]
|
||||||
|
# 遍历桶,若找到 key 则返回对应 val
|
||||||
|
for pair in bucket:
|
||||||
|
if pair.key == key:
|
||||||
|
return pair.val
|
||||||
|
# 若未找到 key 则返回 None
|
||||||
|
return None
|
||||||
|
|
||||||
|
def put(self, key: int, val: str):
|
||||||
|
"""添加操作"""
|
||||||
|
# 当负载因子超过阈值时,执行扩容
|
||||||
|
if self.load_factor() > self.load_thres:
|
||||||
|
self.extend()
|
||||||
|
index = self.hash_func(key)
|
||||||
|
bucket = self.buckets[index]
|
||||||
|
# 遍历桶,若遇到指定 key ,则更新对应 val 并返回
|
||||||
|
for pair in bucket:
|
||||||
|
if pair.key == key:
|
||||||
|
pair.val = val
|
||||||
|
return
|
||||||
|
# 若无该 key ,则将键值对添加至尾部
|
||||||
|
pair = Pair(key, val)
|
||||||
|
bucket.append(pair)
|
||||||
|
self.size += 1
|
||||||
|
|
||||||
|
def remove(self, key: int):
|
||||||
|
"""删除操作"""
|
||||||
|
index = self.hash_func(key)
|
||||||
|
bucket = self.buckets[index]
|
||||||
|
# 遍历桶,从中删除键值对
|
||||||
|
for pair in bucket:
|
||||||
|
if pair.key == key:
|
||||||
|
bucket.remove(pair)
|
||||||
|
self.size -= 1
|
||||||
|
return
|
||||||
|
|
||||||
|
def extend(self):
|
||||||
|
"""扩容哈希表"""
|
||||||
|
# 暂存原哈希表
|
||||||
|
buckets = self.buckets
|
||||||
|
# 初始化扩容后的新哈希表
|
||||||
|
self.capacity *= self.extend_ratio
|
||||||
|
self.buckets = [[] for _ in range(self.capacity)]
|
||||||
|
self.size = 0
|
||||||
|
# 将键值对从原哈希表搬运至新哈希表
|
||||||
|
for bucket in buckets:
|
||||||
|
for pair in bucket:
|
||||||
|
self.put(pair.key, pair.val)
|
||||||
|
|
||||||
|
def print(self):
|
||||||
|
"""打印哈希表"""
|
||||||
|
for bucket in self.buckets:
|
||||||
|
res = []
|
||||||
|
for pair in bucket:
|
||||||
|
res.append(str(pair.key) + " -> " + pair.val)
|
||||||
|
print(res)
|
||||||
|
|
||||||
|
|
||||||
|
"""Driver Code"""
|
||||||
|
if __name__ == "__main__":
|
||||||
|
# 测试代码
|
||||||
|
hashmap = HashMapChaining()
|
||||||
|
|
||||||
|
# 添加操作
|
||||||
|
# 在哈希表中添加键值对 (key, value)
|
||||||
|
hashmap.put(12836, "小哈")
|
||||||
|
hashmap.put(15937, "小啰")
|
||||||
|
hashmap.put(16750, "小算")
|
||||||
|
hashmap.put(13276, "小法")
|
||||||
|
hashmap.put(10583, "小鸭")
|
||||||
|
print("\n添加完成后,哈希表为\n[Key1 -> Value1, Key2 -> Value2, ...]")
|
||||||
|
hashmap.print()
|
||||||
|
|
||||||
|
# 查询操作
|
||||||
|
# 向哈希表输入键 key ,得到值 value
|
||||||
|
name = hashmap.get(13276)
|
||||||
|
print("\n输入学号 13276 ,查询到姓名 " + name)
|
||||||
|
|
||||||
|
# 删除操作
|
||||||
|
# 在哈希表中删除键值对 (key, value)
|
||||||
|
hashmap.remove(12836)
|
||||||
|
print("\n删除 12836 后,哈希表为\n[Key1 -> Value1, Key2 -> Value2, ...]")
|
||||||
|
hashmap.print()
|
132
codes/python/chapter_hashing/hash_map_open_addressing.py
Normal file
132
codes/python/chapter_hashing/hash_map_open_addressing.py
Normal file
@ -0,0 +1,132 @@
|
|||||||
|
"""
|
||||||
|
File: hash_map_open_addressing.py
|
||||||
|
Created Time: 2023-06-13
|
||||||
|
Author: Krahets (krahets@163.com)
|
||||||
|
"""
|
||||||
|
|
||||||
|
|
||||||
|
class Pair:
|
||||||
|
"""键值对"""
|
||||||
|
|
||||||
|
def __init__(self, key: int, val: str):
|
||||||
|
self.key = key
|
||||||
|
self.val = val
|
||||||
|
|
||||||
|
|
||||||
|
class HashMapOpenAddressing:
|
||||||
|
"""开放寻址哈希表"""
|
||||||
|
|
||||||
|
def __init__(self):
|
||||||
|
"""构造方法"""
|
||||||
|
self.size = 0 # 键值对数量
|
||||||
|
self.capacity = 4 # 哈希表容量
|
||||||
|
self.load_thres = 2 / 3 # 触发扩容的负载因子阈值
|
||||||
|
self.extend_ratio = 2 # 扩容倍数
|
||||||
|
self.buckets: list[Pair | None] = [None] * self.capacity # 桶数组
|
||||||
|
self.removed = Pair(-1, "-1") # 删除标记
|
||||||
|
|
||||||
|
def hash_func(self, key: int) -> int:
|
||||||
|
"""哈希函数"""
|
||||||
|
return key % self.capacity
|
||||||
|
|
||||||
|
def load_factor(self) -> float:
|
||||||
|
"""负载因子"""
|
||||||
|
return self.size / self.capacity
|
||||||
|
|
||||||
|
def get(self, key: int) -> str:
|
||||||
|
"""查询操作"""
|
||||||
|
index = self.hash_func(key)
|
||||||
|
# 线性探测,从 index 开始向后遍历
|
||||||
|
for i in range(self.capacity):
|
||||||
|
# 计算桶索引,越过尾部返回头部
|
||||||
|
j = (index + i) % self.capacity
|
||||||
|
# 若遇到空桶,说明无此 key ,则返回 None
|
||||||
|
if self.buckets[j] is None:
|
||||||
|
return None
|
||||||
|
# 若遇到指定 key ,则返回对应 val
|
||||||
|
if self.buckets[j].key == key and self.buckets[j] != self.removed:
|
||||||
|
return self.buckets[j].val
|
||||||
|
|
||||||
|
def put(self, key: int, val: str):
|
||||||
|
"""添加操作"""
|
||||||
|
# 当负载因子超过阈值时,执行扩容
|
||||||
|
if self.load_factor() > self.load_thres:
|
||||||
|
self.extend()
|
||||||
|
index = self.hash_func(key)
|
||||||
|
# 线性探测,从 index 开始向后遍历
|
||||||
|
for i in range(self.capacity):
|
||||||
|
# 计算桶索引,越过尾部返回头部
|
||||||
|
j = (index + i) % self.capacity
|
||||||
|
# 若遇到空桶、或带有删除标记的桶,则将键值对放入该桶
|
||||||
|
if self.buckets[j] in [None, self.removed]:
|
||||||
|
self.buckets[j] = Pair(key, val)
|
||||||
|
self.size += 1
|
||||||
|
return
|
||||||
|
# 若遇到指定 key ,则更新对应 val
|
||||||
|
if self.buckets[j].key == key:
|
||||||
|
self.buckets[j].val = val
|
||||||
|
return
|
||||||
|
|
||||||
|
def remove(self, key: int):
|
||||||
|
"""删除操作"""
|
||||||
|
index = self.hash_func(key)
|
||||||
|
# 线性探测,从 index 开始向后遍历
|
||||||
|
for i in range(self.capacity):
|
||||||
|
# 计算桶索引,越过尾部返回头部
|
||||||
|
j = (index + i) % self.capacity
|
||||||
|
# 若遇到空桶,说明无此 key ,则直接返回
|
||||||
|
if self.buckets[j] is None:
|
||||||
|
return
|
||||||
|
# 若遇到指定 key ,则标记删除并返回
|
||||||
|
if self.buckets[j].key == key:
|
||||||
|
self.buckets[j] = self.removed
|
||||||
|
self.size -= 1
|
||||||
|
return
|
||||||
|
|
||||||
|
def extend(self):
|
||||||
|
"""扩容哈希表"""
|
||||||
|
# 暂存原哈希表
|
||||||
|
buckets_tmp = self.buckets
|
||||||
|
# 初始化扩容后的新哈希表
|
||||||
|
self.capacity *= self.extend_ratio
|
||||||
|
self.buckets = [None] * self.capacity
|
||||||
|
self.size = 0
|
||||||
|
# 将键值对从原哈希表搬运至新哈希表
|
||||||
|
for pair in buckets_tmp:
|
||||||
|
if pair not in [None, self.removed]:
|
||||||
|
self.put(pair.key, pair.val)
|
||||||
|
|
||||||
|
def print(self) -> None:
|
||||||
|
"""打印哈希表"""
|
||||||
|
for pair in self.buckets:
|
||||||
|
if pair is not None:
|
||||||
|
print(pair.key, "->", pair.val)
|
||||||
|
else:
|
||||||
|
print("None")
|
||||||
|
|
||||||
|
|
||||||
|
"""Driver Code"""
|
||||||
|
if __name__ == "__main__":
|
||||||
|
# 测试代码
|
||||||
|
hashmap = HashMapOpenAddressing()
|
||||||
|
|
||||||
|
# 添加操作
|
||||||
|
# 在哈希表中添加键值对 (key, val)
|
||||||
|
hashmap.put(12836, "小哈")
|
||||||
|
hashmap.put(15937, "小啰")
|
||||||
|
hashmap.put(16750, "小算")
|
||||||
|
hashmap.put(13276, "小法")
|
||||||
|
hashmap.put(10583, "小鸭")
|
||||||
|
print("\n添加完成后,哈希表为\nKey -> Value")
|
||||||
|
hashmap.print()
|
||||||
|
|
||||||
|
# 查询操作
|
||||||
|
# 向哈希表输入键 key ,得到值 val
|
||||||
|
name = hashmap.get(13276)
|
||||||
|
print("\n输入学号 13276 ,查询到姓名 " + name)
|
||||||
|
|
||||||
|
# 删除操作
|
||||||
|
# 在哈希表中删除键值对 (key, val)
|
||||||
|
hashmap.remove(16750)
|
||||||
|
print("\n删除 16750 后,哈希表为\nKey -> Value")
|
||||||
|
hashmap.print()
|
@ -5,14 +5,14 @@
|
|||||||
*/
|
*/
|
||||||
|
|
||||||
#[derive(Debug, Clone)]
|
#[derive(Debug, Clone)]
|
||||||
/* 键值对 int->String */
|
/* 键值对 */
|
||||||
pub struct Entry {
|
pub struct Pair {
|
||||||
pub key: i32,
|
pub key: i32,
|
||||||
pub val: String,
|
pub val: String,
|
||||||
}
|
}
|
||||||
|
|
||||||
/* 基于数组简易实现的哈希表 */
|
/* 基于数组简易实现的哈希表 */
|
||||||
pub struct ArrayHashMap { buckets: Vec<Option<Entry>> }
|
pub struct ArrayHashMap { buckets: Vec<Option<Pair>> }
|
||||||
|
|
||||||
impl ArrayHashMap {
|
impl ArrayHashMap {
|
||||||
pub fn new() -> ArrayHashMap {
|
pub fn new() -> ArrayHashMap {
|
||||||
@ -28,13 +28,13 @@ impl ArrayHashMap {
|
|||||||
/* 查询操作 */
|
/* 查询操作 */
|
||||||
pub fn get(&self, key: i32) -> Option<&String> {
|
pub fn get(&self, key: i32) -> Option<&String> {
|
||||||
let index = self.hash_func(key);
|
let index = self.hash_func(key);
|
||||||
self.buckets[index].as_ref().map(|entry| &entry.val)
|
self.buckets[index].as_ref().map(|pair| &pair.val)
|
||||||
}
|
}
|
||||||
|
|
||||||
/* 添加操作 */
|
/* 添加操作 */
|
||||||
pub fn put(&mut self, key: i32, val: &str) {
|
pub fn put(&mut self, key: i32, val: &str) {
|
||||||
let index = self.hash_func(key);
|
let index = self.hash_func(key);
|
||||||
self.buckets[index] = Some(Entry {
|
self.buckets[index] = Some(Pair {
|
||||||
key,
|
key,
|
||||||
val: val.to_string(),
|
val: val.to_string(),
|
||||||
});
|
});
|
||||||
@ -47,24 +47,24 @@ impl ArrayHashMap {
|
|||||||
}
|
}
|
||||||
|
|
||||||
/* 获取所有键值对 */
|
/* 获取所有键值对 */
|
||||||
pub fn entry_set(&self) -> Vec<&Entry> {
|
pub fn entry_set(&self) -> Vec<&Pair> {
|
||||||
self.buckets.iter().filter_map(|entry| entry.as_ref()).collect()
|
self.buckets.iter().filter_map(|pair| pair.as_ref()).collect()
|
||||||
}
|
}
|
||||||
|
|
||||||
/* 获取所有键 */
|
/* 获取所有键 */
|
||||||
pub fn key_set(&self) -> Vec<&i32> {
|
pub fn key_set(&self) -> Vec<&i32> {
|
||||||
self.buckets.iter().filter_map(|entry| entry.as_ref().map(|entry| &entry.key)).collect()
|
self.buckets.iter().filter_map(|pair| pair.as_ref().map(|pair| &pair.key)).collect()
|
||||||
}
|
}
|
||||||
|
|
||||||
/* 获取所有值 */
|
/* 获取所有值 */
|
||||||
pub fn value_set(&self) -> Vec<&String> {
|
pub fn value_set(&self) -> Vec<&String> {
|
||||||
self.buckets.iter().filter_map(|entry| entry.as_ref().map(|entry| &entry.val)).collect()
|
self.buckets.iter().filter_map(|pair| pair.as_ref().map(|pair| &pair.val)).collect()
|
||||||
}
|
}
|
||||||
|
|
||||||
/* 打印哈希表 */
|
/* 打印哈希表 */
|
||||||
pub fn print(&self) {
|
pub fn print(&self) {
|
||||||
for entry in self.entry_set() {
|
for pair in self.entry_set() {
|
||||||
println!("{} -> {}", entry.key, entry.val);
|
println!("{} -> {}", pair.key, pair.val);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -95,8 +95,8 @@ fn main() {
|
|||||||
|
|
||||||
/* 遍历哈希表 */
|
/* 遍历哈希表 */
|
||||||
println!("\n遍历键值对 Key->Value");
|
println!("\n遍历键值对 Key->Value");
|
||||||
for entry in map.entry_set() {
|
for pair in map.entry_set() {
|
||||||
println!("{} -> {}", entry.key, entry.val);
|
println!("{} -> {}", pair.key, pair.val);
|
||||||
}
|
}
|
||||||
|
|
||||||
println!("\n单独遍历键 Key");
|
println!("\n单独遍历键 Key");
|
||||||
|
@ -72,12 +72,12 @@ public class GraphAdjList {
|
|||||||
/* 打印邻接表 */
|
/* 打印邻接表 */
|
||||||
public func print() {
|
public func print() {
|
||||||
Swift.print("邻接表 =")
|
Swift.print("邻接表 =")
|
||||||
for entry in adjList {
|
for pair in adjList {
|
||||||
var tmp: [Int] = []
|
var tmp: [Int] = []
|
||||||
for vertex in entry.value {
|
for vertex in pair.value {
|
||||||
tmp.append(vertex.val)
|
tmp.append(vertex.val)
|
||||||
}
|
}
|
||||||
Swift.print("\(entry.key.val): \(tmp),")
|
Swift.print("\(pair.key.val): \(tmp),")
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -4,8 +4,8 @@
|
|||||||
* Author: nuomi1 (nuomi1@qq.com)
|
* Author: nuomi1 (nuomi1@qq.com)
|
||||||
*/
|
*/
|
||||||
|
|
||||||
/* 键值对 int->String */
|
/* 键值对 */
|
||||||
class Entry {
|
class Pair {
|
||||||
var key: Int
|
var key: Int
|
||||||
var val: String
|
var val: String
|
||||||
|
|
||||||
@ -17,7 +17,7 @@ class Entry {
|
|||||||
|
|
||||||
/* 基于数组简易实现的哈希表 */
|
/* 基于数组简易实现的哈希表 */
|
||||||
class ArrayHashMap {
|
class ArrayHashMap {
|
||||||
private var buckets: [Entry?] = []
|
private var buckets: [Pair?] = []
|
||||||
|
|
||||||
init() {
|
init() {
|
||||||
// 初始化数组,包含 100 个桶
|
// 初始化数组,包含 100 个桶
|
||||||
@ -41,7 +41,7 @@ class ArrayHashMap {
|
|||||||
|
|
||||||
/* 添加操作 */
|
/* 添加操作 */
|
||||||
func put(key: Int, val: String) {
|
func put(key: Int, val: String) {
|
||||||
let pair = Entry(key: key, val: val)
|
let pair = Pair(key: key, val: val)
|
||||||
let index = hashFunc(key: key)
|
let index = hashFunc(key: key)
|
||||||
buckets[index] = pair
|
buckets[index] = pair
|
||||||
}
|
}
|
||||||
@ -54,14 +54,14 @@ class ArrayHashMap {
|
|||||||
}
|
}
|
||||||
|
|
||||||
/* 获取所有键值对 */
|
/* 获取所有键值对 */
|
||||||
func entrySet() -> [Entry] {
|
func pairSet() -> [Pair] {
|
||||||
var entrySet: [Entry] = []
|
var pairSet: [Pair] = []
|
||||||
for pair in buckets {
|
for pair in buckets {
|
||||||
if let pair = pair {
|
if let pair = pair {
|
||||||
entrySet.append(pair)
|
pairSet.append(pair)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
return entrySet
|
return pairSet
|
||||||
}
|
}
|
||||||
|
|
||||||
/* 获取所有键 */
|
/* 获取所有键 */
|
||||||
@ -88,8 +88,8 @@ class ArrayHashMap {
|
|||||||
|
|
||||||
/* 打印哈希表 */
|
/* 打印哈希表 */
|
||||||
func print() {
|
func print() {
|
||||||
for entry in entrySet() {
|
for pair in pairSet() {
|
||||||
Swift.print("\(entry.key) -> \(entry.val)")
|
Swift.print("\(pair.key) -> \(pair.val)")
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -124,8 +124,8 @@ enum _ArrayHashMap {
|
|||||||
|
|
||||||
/* 遍历哈希表 */
|
/* 遍历哈希表 */
|
||||||
print("\n遍历键值对 Key->Value")
|
print("\n遍历键值对 Key->Value")
|
||||||
for entry in map.entrySet() {
|
for pair in map.pairSet() {
|
||||||
print("\(entry.key) -> \(entry.val)")
|
print("\(pair.key) -> \(pair.val)")
|
||||||
}
|
}
|
||||||
print("\n单独遍历键 Key")
|
print("\n单独遍历键 Key")
|
||||||
for key in map.keySet() {
|
for key in map.keySet() {
|
||||||
|
@ -5,7 +5,7 @@
|
|||||||
*/
|
*/
|
||||||
|
|
||||||
/* 键值对 Number -> String */
|
/* 键值对 Number -> String */
|
||||||
class Entry {
|
class Pair {
|
||||||
public key: number;
|
public key: number;
|
||||||
public val: string;
|
public val: string;
|
||||||
|
|
||||||
@ -17,7 +17,7 @@ class Entry {
|
|||||||
|
|
||||||
/* 基于数组简易实现的哈希表 */
|
/* 基于数组简易实现的哈希表 */
|
||||||
class ArrayHashMap {
|
class ArrayHashMap {
|
||||||
private readonly buckets: (Entry | null)[];
|
private readonly buckets: (Pair | null)[];
|
||||||
|
|
||||||
constructor() {
|
constructor() {
|
||||||
// 初始化数组,包含 100 个桶
|
// 初始化数组,包含 100 个桶
|
||||||
@ -32,15 +32,15 @@ class ArrayHashMap {
|
|||||||
/* 查询操作 */
|
/* 查询操作 */
|
||||||
public get(key: number): string | null {
|
public get(key: number): string | null {
|
||||||
let index = this.hashFunc(key);
|
let index = this.hashFunc(key);
|
||||||
let entry = this.buckets[index];
|
let pair = this.buckets[index];
|
||||||
if (entry === null) return null;
|
if (pair === null) return null;
|
||||||
return entry.val;
|
return pair.val;
|
||||||
}
|
}
|
||||||
|
|
||||||
/* 添加操作 */
|
/* 添加操作 */
|
||||||
public set(key: number, val: string) {
|
public set(key: number, val: string) {
|
||||||
let index = this.hashFunc(key);
|
let index = this.hashFunc(key);
|
||||||
this.buckets[index] = new Entry(key, val);
|
this.buckets[index] = new Pair(key, val);
|
||||||
}
|
}
|
||||||
|
|
||||||
/* 删除操作 */
|
/* 删除操作 */
|
||||||
@ -51,8 +51,8 @@ class ArrayHashMap {
|
|||||||
}
|
}
|
||||||
|
|
||||||
/* 获取所有键值对 */
|
/* 获取所有键值对 */
|
||||||
public entries(): (Entry | null)[] {
|
public entries(): (Pair | null)[] {
|
||||||
let arr: (Entry | null)[] = [];
|
let arr: (Pair | null)[] = [];
|
||||||
for (let i = 0; i < this.buckets.length; i++) {
|
for (let i = 0; i < this.buckets.length; i++) {
|
||||||
if (this.buckets[i]) {
|
if (this.buckets[i]) {
|
||||||
arr.push(this.buckets[i]);
|
arr.push(this.buckets[i]);
|
||||||
@ -85,10 +85,10 @@ class ArrayHashMap {
|
|||||||
|
|
||||||
/* 打印哈希表 */
|
/* 打印哈希表 */
|
||||||
public print() {
|
public print() {
|
||||||
let entrySet = this.entries();
|
let pairSet = this.entries();
|
||||||
for (const entry of entrySet) {
|
for (const pair of pairSet) {
|
||||||
if (!entry) continue;
|
if (!pair) continue;
|
||||||
console.info(`${entry.key} -> ${entry.val}`);
|
console.info(`${pair.key} -> ${pair.val}`);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -119,9 +119,9 @@ map.print();
|
|||||||
|
|
||||||
/* 遍历哈希表 */
|
/* 遍历哈希表 */
|
||||||
console.info('\n遍历键值对 Key->Value');
|
console.info('\n遍历键值对 Key->Value');
|
||||||
for (const entry of map.entries()) {
|
for (const pair of map.entries()) {
|
||||||
if (!entry) continue;
|
if (!pair) continue;
|
||||||
console.info(entry.key + ' -> ' + entry.val);
|
console.info(pair.key + ' -> ' + pair.val);
|
||||||
}
|
}
|
||||||
console.info('\n单独遍历键 Key');
|
console.info('\n单独遍历键 Key');
|
||||||
for (const key of map.keys()) {
|
for (const key of map.keys()) {
|
||||||
|
@ -5,13 +5,13 @@
|
|||||||
const std = @import("std");
|
const std = @import("std");
|
||||||
const inc = @import("include");
|
const inc = @import("include");
|
||||||
|
|
||||||
// 键值对 int->String
|
// 键值对
|
||||||
const Entry = struct {
|
const Pair = struct {
|
||||||
key: usize = undefined,
|
key: usize = undefined,
|
||||||
val: []const u8 = undefined,
|
val: []const u8 = undefined,
|
||||||
|
|
||||||
pub fn init(key: usize, val: []const u8) Entry {
|
pub fn init(key: usize, val: []const u8) Pair {
|
||||||
return Entry {
|
return Pair {
|
||||||
.key = key,
|
.key = key,
|
||||||
.val = val,
|
.val = val,
|
||||||
};
|
};
|
||||||
@ -57,7 +57,7 @@ pub fn ArrayHashMap(comptime T: type) type {
|
|||||||
|
|
||||||
// 添加操作
|
// 添加操作
|
||||||
pub fn put(self: *Self, key: usize, val: []const u8) !void {
|
pub fn put(self: *Self, key: usize, val: []const u8) !void {
|
||||||
var pair = Entry.init(key, val);
|
var pair = Pair.init(key, val);
|
||||||
var index = hashFunc(key);
|
var index = hashFunc(key);
|
||||||
self.buckets.?.items[index] = pair;
|
self.buckets.?.items[index] = pair;
|
||||||
}
|
}
|
||||||
@ -70,7 +70,7 @@ pub fn ArrayHashMap(comptime T: type) type {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// 获取所有键值对
|
// 获取所有键值对
|
||||||
pub fn entrySet(self: *Self) !*std.ArrayList(T) {
|
pub fn pairSet(self: *Self) !*std.ArrayList(T) {
|
||||||
var entry_set = std.ArrayList(T).init(self.mem_allocator);
|
var entry_set = std.ArrayList(T).init(self.mem_allocator);
|
||||||
for (self.buckets.?.items) |item| {
|
for (self.buckets.?.items) |item| {
|
||||||
if (item == null) continue;
|
if (item == null) continue;
|
||||||
@ -101,7 +101,7 @@ pub fn ArrayHashMap(comptime T: type) type {
|
|||||||
|
|
||||||
// 打印哈希表
|
// 打印哈希表
|
||||||
pub fn print(self: *Self) !void {
|
pub fn print(self: *Self) !void {
|
||||||
var entry_set = try self.entrySet();
|
var entry_set = try self.pairSet();
|
||||||
defer entry_set.deinit();
|
defer entry_set.deinit();
|
||||||
for (entry_set.items) |item| {
|
for (entry_set.items) |item| {
|
||||||
std.debug.print("{} -> {s}\n", .{item.key, item.val});
|
std.debug.print("{} -> {s}\n", .{item.key, item.val});
|
||||||
@ -113,7 +113,7 @@ pub fn ArrayHashMap(comptime T: type) type {
|
|||||||
// Driver Code
|
// Driver Code
|
||||||
pub fn main() !void {
|
pub fn main() !void {
|
||||||
// 初始化哈希表
|
// 初始化哈希表
|
||||||
var map = ArrayHashMap(Entry){};
|
var map = ArrayHashMap(Pair){};
|
||||||
try map.init(std.heap.page_allocator);
|
try map.init(std.heap.page_allocator);
|
||||||
defer map.deinit();
|
defer map.deinit();
|
||||||
|
|
||||||
@ -140,7 +140,7 @@ pub fn main() !void {
|
|||||||
|
|
||||||
// 遍历哈希表
|
// 遍历哈希表
|
||||||
std.debug.print("\n遍历键值对 Key->Value\n", .{});
|
std.debug.print("\n遍历键值对 Key->Value\n", .{});
|
||||||
var entry_set = try map.entrySet();
|
var entry_set = try map.pairSet();
|
||||||
for (entry_set.items) |kv| {
|
for (entry_set.items) |kv| {
|
||||||
std.debug.print("{} -> {s}\n", .{kv.key, kv.val});
|
std.debug.print("{} -> {s}\n", .{kv.key, kv.val});
|
||||||
}
|
}
|
||||||
|
@ -14,13 +14,13 @@
|
|||||||
|
|
||||||
因此,**当哈希表内的冲突总体较为严重时,编程语言通常通过扩容哈希表来缓解冲突**。类似于数组扩容,哈希表扩容需将所有键值对从原哈希表迁移至新哈希表,开销较大。
|
因此,**当哈希表内的冲突总体较为严重时,编程语言通常通过扩容哈希表来缓解冲突**。类似于数组扩容,哈希表扩容需将所有键值对从原哈希表迁移至新哈希表,开销较大。
|
||||||
|
|
||||||
编程语言通常使用「负载因子 Load Factor」来衡量哈希冲突的严重程度,**定义为哈希表中元素数量除以桶数量**,常作为哈希表扩容的触发条件。在 Java 中,当负载因子 $> 0.75$ 时,系统会将 HashMap 容量扩展为原先的 $2$ 倍。
|
编程语言通常使用「负载因子 Load Factor」来衡量哈希冲突的严重程度,**定义为哈希表中元素数量除以桶数量**,常作为哈希表扩容的触发条件。在 Java 中,当负载因子超过 $ 0.75$ 时,系统会将 HashMap 容量扩展为原先的 $2$ 倍。
|
||||||
|
|
||||||
## 链式地址
|
## 链式地址
|
||||||
|
|
||||||
在原始哈希表中,每个桶仅能存储一个键值对。**链式地址将单个元素转换为链表,将键值对作为链表节点,将所有发生冲突的键值对都存储在同一链表中**。
|
在原始哈希表中,每个桶仅能存储一个键值对。**链式地址将单个元素转换为链表,将键值对作为链表节点,将所有发生冲突的键值对都存储在同一链表中**。
|
||||||
|
|
||||||

|

|
||||||
|
|
||||||
链式地址下,哈希表的操作方法包括:
|
链式地址下,哈希表的操作方法包括:
|
||||||
|
|
||||||
@ -33,44 +33,225 @@
|
|||||||
- **占用空间增大**,由于链表或二叉树包含节点指针,相比数组更加耗费内存空间;
|
- **占用空间增大**,由于链表或二叉树包含节点指针,相比数组更加耗费内存空间;
|
||||||
- **查询效率降低**,因为需要线性遍历链表来查找对应元素;
|
- **查询效率降低**,因为需要线性遍历链表来查找对应元素;
|
||||||
|
|
||||||
为了提高操作效率,**可以将链表转换为「AVL 树」或「红黑树」**,将查询操作的时间复杂度优化至 $O(\log n)$ 。
|
以下给出了链式地址哈希表的简单实现,需要注意:
|
||||||
|
|
||||||
|
- 为了使得代码尽量简短,我们使用列表(动态数组)代替链表。换句话说,哈希表(数组)包含多个桶,每个桶都是一个列表。
|
||||||
|
- 以下代码实现了哈希表扩容方法。具体来看,当负载因子超过 $0.75$ 时,我们将哈希表扩容至 $2$ 倍。
|
||||||
|
|
||||||
|
=== "Java"
|
||||||
|
|
||||||
|
```java title="hash_map_chaining.java"
|
||||||
|
[class]{Pair}-[func]{}
|
||||||
|
|
||||||
|
[class]{HashMapChaining}-[func]{}
|
||||||
|
```
|
||||||
|
|
||||||
|
=== "C++"
|
||||||
|
|
||||||
|
```cpp title="hash_map_chaining.cpp"
|
||||||
|
[class]{Pair}-[func]{}
|
||||||
|
|
||||||
|
[class]{HashMapChaining}-[func]{}
|
||||||
|
```
|
||||||
|
|
||||||
|
=== "Python"
|
||||||
|
|
||||||
|
```python title="hash_map_chaining.py"
|
||||||
|
[class]{Pair}-[func]{}
|
||||||
|
|
||||||
|
[class]{HashMapChaining}-[func]{}
|
||||||
|
```
|
||||||
|
|
||||||
|
=== "Go"
|
||||||
|
|
||||||
|
```go title="hash_map_chaining.go"
|
||||||
|
[class]{pair}-[func]{}
|
||||||
|
|
||||||
|
[class]{hashMapChaining}-[func]{}
|
||||||
|
```
|
||||||
|
|
||||||
|
=== "JavaScript"
|
||||||
|
|
||||||
|
```javascript title="hash_map_chaining.js"
|
||||||
|
[class]{Pair}-[func]{}
|
||||||
|
|
||||||
|
[class]{HashMapChaining}-[func]{}
|
||||||
|
```
|
||||||
|
|
||||||
|
=== "TypeScript"
|
||||||
|
|
||||||
|
```typescript title="hash_map_chaining.ts"
|
||||||
|
[class]{Pair}-[func]{}
|
||||||
|
|
||||||
|
[class]{HashMapChaining}-[func]{}
|
||||||
|
```
|
||||||
|
|
||||||
|
=== "C"
|
||||||
|
|
||||||
|
```c title="hash_map_chaining.c"
|
||||||
|
[class]{pair}-[func]{}
|
||||||
|
|
||||||
|
[class]{hashMapChaining}-[func]{}
|
||||||
|
```
|
||||||
|
|
||||||
|
=== "C#"
|
||||||
|
|
||||||
|
```csharp title="hash_map_chaining.cs"
|
||||||
|
[class]{Pair}-[func]{}
|
||||||
|
|
||||||
|
[class]{HashMapChaining}-[func]{}
|
||||||
|
```
|
||||||
|
|
||||||
|
=== "Swift"
|
||||||
|
|
||||||
|
```swift title="hash_map_chaining.swift"
|
||||||
|
[class]{Pair}-[func]{}
|
||||||
|
|
||||||
|
[class]{HashMapChaining}-[func]{}
|
||||||
|
```
|
||||||
|
|
||||||
|
=== "Zig"
|
||||||
|
|
||||||
|
```zig title="hash_map_chaining.zig"
|
||||||
|
[class]{Pair}-[func]{}
|
||||||
|
|
||||||
|
[class]{HashMapChaining}-[func]{}
|
||||||
|
```
|
||||||
|
|
||||||
|
=== "Dart"
|
||||||
|
|
||||||
|
```dart title="hash_map_chaining.dart"
|
||||||
|
[class]{Pair}-[func]{}
|
||||||
|
|
||||||
|
[class]{HashMapChaining}-[func]{}
|
||||||
|
```
|
||||||
|
|
||||||
|
!!! tip
|
||||||
|
|
||||||
|
为了提高效率,**我们可以将链表转换为「AVL 树」或「红黑树」**,从而将查询操作的时间复杂度优化至 $O(\log n)$ 。
|
||||||
|
|
||||||
## 开放寻址
|
## 开放寻址
|
||||||
|
|
||||||
「开放寻址」方法不引入额外的数据结构,而是通过“多次探测”来解决哈希冲突,**探测方式主要包括线性探测、平方探测、多次哈希**。
|
开放寻址法不引入额外的数据结构,而是通过“多次探测”来解决哈希冲突,**探测方式主要包括线性探测、平方探测、多次哈希**。
|
||||||
|
|
||||||
### 线性探测
|
### 线性探测
|
||||||
|
|
||||||
「线性探测」采用固定步长的线性查找来解决哈希冲突。
|
线性探测采用固定步长的线性查找来解决哈希冲突。
|
||||||
|
|
||||||
**插入元素**:若出现哈希冲突,则从冲突位置向后线性遍历(步长通常为 $1$ ),直至找到空位,将元素插入其中。
|
- **插入元素**:通过哈希函数计算数组索引,若发现桶内已有元素,则从冲突位置向后线性遍历(步长通常为 $1$ ),直至找到空位,将元素插入其中。
|
||||||
|
- **查找元素**:若发现哈希冲突,则使用相同步长向后线性遍历,直到找到对应元素,返回 value 即可;或者若遇到空位,说明目标键值对不在哈希表中,返回 $\text{None}$ 。
|
||||||
**查找元素**:在出现哈希冲突时,使用相同步长进行线性查找,可能遇到以下两种情况。
|
|
||||||
|
|
||||||
1. 找到对应元素,返回 value 即可;
|
|
||||||
2. 若遇到空位,说明目标键值对不在哈希表中;
|
|
||||||
|
|
||||||

|

|
||||||
|
|
||||||
线性探测存在以下缺陷:
|
然而,线性探测存在以下缺陷:
|
||||||
|
|
||||||
- **不能直接删除元素**。删除元素会在数组内产生一个空位,查找其他元素时,该空位可能导致程序误判元素不存在(即上述第 `2.` 种情况)。因此,需要借助一个标志位来标记已删除元素。
|
- **不能直接删除元素**。删除元素会在数组内产生一个空位,查找其他元素时,该空位可能导致程序误判元素不存在。因此,需要借助一个标志位来标记已删除元素。
|
||||||
- **容易产生聚集**。数组内连续被占用位置越长,这些连续位置发生哈希冲突的可能性越大,进一步促使这一位置的“聚堆生长”,最终导致增删查改操作效率降低。
|
- **容易产生聚集**。数组内连续被占用位置越长,这些连续位置发生哈希冲突的可能性越大,进一步促使这一位置的“聚堆生长”,最终导致增删查改操作效率降低。
|
||||||
|
|
||||||
|
如以下代码所示,为开放寻址(线性探测)哈希表的简单实现,重点包括:
|
||||||
|
|
||||||
|
- 我们使用一个固定的键值对实例 `removed` 来标记已删除元素。也就是说,当一个桶为 $\text{None}$ 或 `removed` 时,这个桶都是空的,可用于放置键值对。
|
||||||
|
- 被标记为已删除的空间是可以再次被使用的。当插入元素时,若通过哈希函数找到了被标记为已删除的索引,则可将该元素放置到该索引。
|
||||||
|
- 在线性探测时,我们从当前索引 `index` 向后遍历;而当越过数组尾部时,需要回到头部继续遍历。
|
||||||
|
|
||||||
|
=== "Java"
|
||||||
|
|
||||||
|
```java title="hash_map_open_addressing.java"
|
||||||
|
[class]{Pair}-[func]{}
|
||||||
|
|
||||||
|
[class]{HashMapOpenAddressing}-[func]{}
|
||||||
|
```
|
||||||
|
|
||||||
|
=== "C++"
|
||||||
|
|
||||||
|
```cpp title="hash_map_open_addressing.cpp"
|
||||||
|
[class]{Pair}-[func]{}
|
||||||
|
|
||||||
|
[class]{HashMapOpenAddressing}-[func]{}
|
||||||
|
```
|
||||||
|
|
||||||
|
=== "Python"
|
||||||
|
|
||||||
|
```python title="hash_map_open_addressing.py"
|
||||||
|
[class]{Pair}-[func]{}
|
||||||
|
|
||||||
|
[class]{HashMapOpenAddressing}-[func]{}
|
||||||
|
```
|
||||||
|
|
||||||
|
=== "Go"
|
||||||
|
|
||||||
|
```go title="hash_map_open_addressing.go"
|
||||||
|
[class]{pair}-[func]{}
|
||||||
|
|
||||||
|
[class]{hashMapOpenAddressing}-[func]{}
|
||||||
|
```
|
||||||
|
|
||||||
|
=== "JavaScript"
|
||||||
|
|
||||||
|
```javascript title="hash_map_open_addressing.js"
|
||||||
|
[class]{Pair}-[func]{}
|
||||||
|
|
||||||
|
[class]{HashMapOpenAddressing}-[func]{}
|
||||||
|
```
|
||||||
|
|
||||||
|
=== "TypeScript"
|
||||||
|
|
||||||
|
```typescript title="hash_map_open_addressing.ts"
|
||||||
|
[class]{Pair}-[func]{}
|
||||||
|
|
||||||
|
[class]{HashMapOpenAddressing}-[func]{}
|
||||||
|
```
|
||||||
|
|
||||||
|
=== "C"
|
||||||
|
|
||||||
|
```c title="hash_map_open_addressing.c"
|
||||||
|
[class]{pair}-[func]{}
|
||||||
|
|
||||||
|
[class]{hashMapOpenAddressing}-[func]{}
|
||||||
|
```
|
||||||
|
|
||||||
|
=== "C#"
|
||||||
|
|
||||||
|
```csharp title="hash_map_open_addressing.cs"
|
||||||
|
[class]{Pair}-[func]{}
|
||||||
|
|
||||||
|
[class]{HashMapOpenAddressing}-[func]{}
|
||||||
|
```
|
||||||
|
|
||||||
|
=== "Swift"
|
||||||
|
|
||||||
|
```swift title="hash_map_open_addressing.swift"
|
||||||
|
[class]{Pair}-[func]{}
|
||||||
|
|
||||||
|
[class]{HashMapOpenAddressing}-[func]{}
|
||||||
|
```
|
||||||
|
|
||||||
|
=== "Zig"
|
||||||
|
|
||||||
|
```zig title="hash_map_open_addressing.zig"
|
||||||
|
[class]{Pair}-[func]{}
|
||||||
|
|
||||||
|
[class]{HashMapOpenAddressing}-[func]{}
|
||||||
|
```
|
||||||
|
|
||||||
|
=== "Dart"
|
||||||
|
|
||||||
|
```dart title="hash_map_open_addressing.dart"
|
||||||
|
[class]{Pair}-[func]{}
|
||||||
|
|
||||||
|
[class]{HashMapOpenAddressing}-[func]{}
|
||||||
|
```
|
||||||
|
|
||||||
### 多次哈希
|
### 多次哈希
|
||||||
|
|
||||||
顾名思义,「多次哈希」方法是使用多个哈希函数 $f_1(x)$ , $f_2(x)$ , $f_3(x)$ , $\cdots$ 进行探测。
|
顾名思义,多次哈希方法是使用多个哈希函数 $f_1(x)$ , $f_2(x)$ , $f_3(x)$ , $\cdots$ 进行探测。
|
||||||
|
|
||||||
**插入元素**:若哈希函数 $f_1(x)$ 出现冲突,则尝试 $f_2(x)$ ,以此类推,直到找到空位后插入元素。
|
- **插入元素**:若哈希函数 $f_1(x)$ 出现冲突,则尝试 $f_2(x)$ ,以此类推,直到找到空位后插入元素。
|
||||||
|
- **查找元素**:在相同的哈希函数顺序下进行查找,直到找到目标元素时返回;或遇到空位或已尝试所有哈希函数,说明哈希表中不存在该元素,则返回 $\text{None}$ 。
|
||||||
**查找元素**:在相同的哈希函数顺序下进行查找,存在以下两种情况:
|
|
||||||
|
|
||||||
1. 如果找到目标元素,则返回之;
|
|
||||||
2. 若遇到空位或已尝试所有哈希函数,则说明哈希表中不存在该元素;
|
|
||||||
|
|
||||||
与线性探测相比,多次哈希方法不易产生聚集,但多个哈希函数会增加额外的计算量。
|
与线性探测相比,多次哈希方法不易产生聚集,但多个哈希函数会增加额外的计算量。
|
||||||
|
|
||||||
!!! note "哈希表设计方案"
|
!!! note "编程语言的选择"
|
||||||
|
|
||||||
Java 采用「链式地址」。自 JDK 1.8 以来,当 HashMap 内数组长度达到 64 且链表长度达到 8 时,链表会被转换为红黑树以提升查找性能。
|
Java 采用「链式地址」。自 JDK 1.8 以来,当 HashMap 内数组长度达到 64 且链表长度达到 8 时,链表会被转换为红黑树以提升查找性能。
|
||||||
|
|
||||||
|
@ -6,16 +6,20 @@
|
|||||||
|
|
||||||

|

|
||||||
|
|
||||||
除哈希表外,我们还可以使用数组或链表实现查询功能,各项操作的时间复杂度如下表所示。
|
除哈希表外,我们还可以使用数组或链表实现元素查询,其中:
|
||||||
|
|
||||||
在哈希表中增删查改的时间复杂度都是 $O(1)$ ,全面胜出!因此,哈希表常用于对查找效率要求较高的场景。
|
- 查询元素需要遍历所有元素,使用 $O(n)$ 时间;
|
||||||
|
- 添加元素仅需添加至尾部即可,使用 $O(1)$ 时间;
|
||||||
|
- 删除元素需要先查询再删除,使用 $O(n)$ 时间;
|
||||||
|
|
||||||
|
然而,在哈希表中进行增删查的时间复杂度都是 $O(1)$ 。哈希表全面胜出!因此,哈希表常用于对查找效率要求较高的场景。
|
||||||
|
|
||||||
<div class="center-table" markdown>
|
<div class="center-table" markdown>
|
||||||
|
|
||||||
| | 数组 | 链表 | 哈希表 |
|
| | 数组 | 链表 | 哈希表 |
|
||||||
| -------- | ------ | ------ | ------ |
|
| -------- | ------ | ------ | ------ |
|
||||||
| 查找元素 | $O(n)$ | $O(n)$ | $O(1)$ |
|
| 查找元素 | $O(n)$ | $O(n)$ | $O(1)$ |
|
||||||
| 插入元素 | $O(1)$ | $O(1)$ | $O(1)$ |
|
| 添加元素 | $O(1)$ | $O(1)$ | $O(1)$ |
|
||||||
| 删除元素 | $O(n)$ | $O(n)$ | $O(1)$ |
|
| 删除元素 | $O(n)$ | $O(n)$ | $O(1)$ |
|
||||||
|
|
||||||
</div>
|
</div>
|
||||||
@ -430,12 +434,12 @@
|
|||||||
|
|
||||||
首先考虑最简单的情况,**仅使用一个数组来实现哈希表**。通常,我们将数组中的每个空位称为「桶 Bucket」,用于存储键值对。
|
首先考虑最简单的情况,**仅使用一个数组来实现哈希表**。通常,我们将数组中的每个空位称为「桶 Bucket」,用于存储键值对。
|
||||||
|
|
||||||
我们将键值对 key, value 封装成一个类 `Entry` ,并将所有 `Entry` 放入数组中。这样,数组中的每个 `Entry` 都具有唯一的索引。为了建立 key 和索引之间的映射关系,我们需要使用「哈希函数 Hash Function」。
|
我们将键值对 key, value 封装成一个类 `Pair` ,并将所有 `Pair` 放入数组中。这样,数组中的每个 `Pair` 都具有唯一的索引。为了建立 key 和索引之间的映射关系,我们需要使用「哈希函数 Hash Function」。
|
||||||
|
|
||||||
设哈希表的数组为 `buckets` ,哈希函数为 `f(x)` ,那么查询操作的步骤如下:
|
设哈希表的数组为 `buckets` ,哈希函数为 `f(x)` ,那么查询操作的步骤如下:
|
||||||
|
|
||||||
1. 输入 `key` ,通过哈希函数计算出索引 `index` ,即 `index = f(key)` ;
|
1. 输入 `key` ,通过哈希函数计算出索引 `index` ,即 `index = f(key)` ;
|
||||||
2. 通过索引在数组中访问到键值对 `entry` ,即 `entry = buckets[index]` ,然后从 `entry` 中获取对应的 `value` ;
|
2. 通过索引在数组中访问到键值对 `pair` ,即 `pair = buckets[index]` ,然后从 `pair` 中获取对应的 `value` ;
|
||||||
|
|
||||||
以学生数据 `key 学号 -> value 姓名` 为例,我们可以设计如下哈希函数:
|
以学生数据 `key 学号 -> value 姓名` 为例,我们可以设计如下哈希函数:
|
||||||
|
|
||||||
@ -450,7 +454,7 @@ $$
|
|||||||
=== "Java"
|
=== "Java"
|
||||||
|
|
||||||
```java title="array_hash_map.java"
|
```java title="array_hash_map.java"
|
||||||
[class]{Entry}-[func]{}
|
[class]{Pair}-[func]{}
|
||||||
|
|
||||||
[class]{ArrayHashMap}-[func]{}
|
[class]{ArrayHashMap}-[func]{}
|
||||||
```
|
```
|
||||||
@ -458,7 +462,7 @@ $$
|
|||||||
=== "C++"
|
=== "C++"
|
||||||
|
|
||||||
```cpp title="array_hash_map.cpp"
|
```cpp title="array_hash_map.cpp"
|
||||||
[class]{Entry}-[func]{}
|
[class]{Pair}-[func]{}
|
||||||
|
|
||||||
[class]{ArrayHashMap}-[func]{}
|
[class]{ArrayHashMap}-[func]{}
|
||||||
```
|
```
|
||||||
@ -466,7 +470,7 @@ $$
|
|||||||
=== "Python"
|
=== "Python"
|
||||||
|
|
||||||
```python title="array_hash_map.py"
|
```python title="array_hash_map.py"
|
||||||
[class]{Entry}-[func]{}
|
[class]{Pair}-[func]{}
|
||||||
|
|
||||||
[class]{ArrayHashMap}-[func]{}
|
[class]{ArrayHashMap}-[func]{}
|
||||||
```
|
```
|
||||||
@ -474,7 +478,7 @@ $$
|
|||||||
=== "Go"
|
=== "Go"
|
||||||
|
|
||||||
```go title="array_hash_map.go"
|
```go title="array_hash_map.go"
|
||||||
[class]{entry}-[func]{}
|
[class]{pair}-[func]{}
|
||||||
|
|
||||||
[class]{arrayHashMap}-[func]{}
|
[class]{arrayHashMap}-[func]{}
|
||||||
```
|
```
|
||||||
@ -482,7 +486,7 @@ $$
|
|||||||
=== "JavaScript"
|
=== "JavaScript"
|
||||||
|
|
||||||
```javascript title="array_hash_map.js"
|
```javascript title="array_hash_map.js"
|
||||||
[class]{Entry}-[func]{}
|
[class]{Pair}-[func]{}
|
||||||
|
|
||||||
[class]{ArrayHashMap}-[func]{}
|
[class]{ArrayHashMap}-[func]{}
|
||||||
```
|
```
|
||||||
@ -490,7 +494,7 @@ $$
|
|||||||
=== "TypeScript"
|
=== "TypeScript"
|
||||||
|
|
||||||
```typescript title="array_hash_map.ts"
|
```typescript title="array_hash_map.ts"
|
||||||
[class]{Entry}-[func]{}
|
[class]{Pair}-[func]{}
|
||||||
|
|
||||||
[class]{ArrayHashMap}-[func]{}
|
[class]{ArrayHashMap}-[func]{}
|
||||||
```
|
```
|
||||||
@ -498,7 +502,7 @@ $$
|
|||||||
=== "C"
|
=== "C"
|
||||||
|
|
||||||
```c title="array_hash_map.c"
|
```c title="array_hash_map.c"
|
||||||
[class]{entry}-[func]{}
|
[class]{pair}-[func]{}
|
||||||
|
|
||||||
[class]{arrayHashMap}-[func]{}
|
[class]{arrayHashMap}-[func]{}
|
||||||
```
|
```
|
||||||
@ -506,7 +510,7 @@ $$
|
|||||||
=== "C#"
|
=== "C#"
|
||||||
|
|
||||||
```csharp title="array_hash_map.cs"
|
```csharp title="array_hash_map.cs"
|
||||||
[class]{Entry}-[func]{}
|
[class]{Pair}-[func]{}
|
||||||
|
|
||||||
[class]{ArrayHashMap}-[func]{}
|
[class]{ArrayHashMap}-[func]{}
|
||||||
```
|
```
|
||||||
@ -514,7 +518,7 @@ $$
|
|||||||
=== "Swift"
|
=== "Swift"
|
||||||
|
|
||||||
```swift title="array_hash_map.swift"
|
```swift title="array_hash_map.swift"
|
||||||
[class]{Entry}-[func]{}
|
[class]{Pair}-[func]{}
|
||||||
|
|
||||||
[class]{ArrayHashMap}-[func]{}
|
[class]{ArrayHashMap}-[func]{}
|
||||||
```
|
```
|
||||||
@ -522,7 +526,7 @@ $$
|
|||||||
=== "Zig"
|
=== "Zig"
|
||||||
|
|
||||||
```zig title="array_hash_map.zig"
|
```zig title="array_hash_map.zig"
|
||||||
[class]{Entry}-[func]{}
|
[class]{Pair}-[func]{}
|
||||||
|
|
||||||
[class]{ArrayHashMap}-[func]{}
|
[class]{ArrayHashMap}-[func]{}
|
||||||
```
|
```
|
||||||
@ -530,7 +534,7 @@ $$
|
|||||||
=== "Dart"
|
=== "Dart"
|
||||||
|
|
||||||
```dart title="array_hash_map.dart"
|
```dart title="array_hash_map.dart"
|
||||||
[class]{Entry}-[func]{}
|
[class]{Pair}-[func]{}
|
||||||
|
|
||||||
[class]{ArrayHashMap}-[func]{}
|
[class]{ArrayHashMap}-[func]{}
|
||||||
```
|
```
|
||||||
|
@ -2,7 +2,7 @@
|
|||||||
|
|
||||||
- 哈希表能够在 $O(1)$ 时间内将键 key 映射到值 value,效率非常高。
|
- 哈希表能够在 $O(1)$ 时间内将键 key 映射到值 value,效率非常高。
|
||||||
- 常见的哈希表操作包括查询、添加与删除键值对、遍历键值对等。
|
- 常见的哈希表操作包括查询、添加与删除键值对、遍历键值对等。
|
||||||
- 哈希函数将 key 映射为数组索引(桶),以便访问对应的值 value 。
|
- 哈希函数将 key 映射为数组索引(桶索引),从而访问对应的值 value 。
|
||||||
- 两个不同的 key 可能在经过哈希函数后得到相同的索引,导致查询结果出错,这种现象被称为哈希冲突。
|
- 两个不同的 key 可能在经过哈希函数后得到相同的索引,导致查询结果出错,这种现象被称为哈希冲突。
|
||||||
- 缓解哈希冲突的方法主要有扩容哈希表和优化哈希表的表示方法。
|
- 缓解哈希冲突的方法主要有扩容哈希表和优化哈希表的表示方法。
|
||||||
- 负载因子定义为哈希表中元素数量除以桶数量,反映了哈希冲突的严重程度,常用作触发哈希表扩容的条件。与数组扩容类似,哈希表扩容操作也会产生较大的开销。
|
- 负载因子定义为哈希表中元素数量除以桶数量,反映了哈希冲突的严重程度,常用作触发哈希表扩容的条件。与数组扩容类似,哈希表扩容操作也会产生较大的开销。
|
||||||
|
@ -162,7 +162,7 @@ nav:
|
|||||||
- 6. 散列表:
|
- 6. 散列表:
|
||||||
- chapter_hashing/index.md
|
- chapter_hashing/index.md
|
||||||
- 6.1. 哈希表: chapter_hashing/hash_map.md
|
- 6.1. 哈希表: chapter_hashing/hash_map.md
|
||||||
- 6.2. 哈希冲突处理: chapter_hashing/hash_collision.md
|
- 6.2. 哈希冲突: chapter_hashing/hash_collision.md
|
||||||
- 6.3. 小结: chapter_hashing/summary.md
|
- 6.3. 小结: chapter_hashing/summary.md
|
||||||
- 7. 树:
|
- 7. 树:
|
||||||
- chapter_tree/index.md
|
- chapter_tree/index.md
|
||||||
|
Loading…
x
Reference in New Issue
Block a user