hello-algo/chapter_hashing/hash_map.md

1387 lines
36 KiB
Markdown
Raw Normal View History

2023-02-08 15:20:18 +08:00
---
comments: true
---
2023-05-21 19:29:43 +08:00
# 6.1.   哈希表
2023-02-08 15:20:18 +08:00
2023-04-09 02:53:25 +08:00
哈希表通过建立「键 key」与「值 value」之间的映射实现高效的元素查询。具体而言我们向哈希表输入一个 key则可以在 $O(1)$ 时间内获取对应的 value 。
2023-02-08 15:20:18 +08:00
2023-04-09 02:53:25 +08:00
以一个包含 $n$ 个学生的数据库为例,每个学生都有“姓名 `name`”和“学号 `id`”两项数据。假如我们希望实现查询功能,例如“输入一个学号,返回对应的姓名”,则可以采用哈希表来实现。
2023-02-08 15:20:18 +08:00
2023-02-26 18:17:57 +08:00
![哈希表的抽象表示](hash_map.assets/hash_map.png)
2023-02-08 15:20:18 +08:00
2023-02-26 19:53:26 +08:00
<p align="center"> Fig. 哈希表的抽象表示 </p>
2023-04-17 18:24:20 +08:00
除哈希表外,我们还可以使用数组或链表实现查询功能,各项操作的时间复杂度如下表所示。
2023-02-08 15:20:18 +08:00
2023-04-17 18:24:20 +08:00
在哈希表中增删查改的时间复杂度都是 $O(1)$ ,全面胜出!因此,哈希表常用于对查找效率要求较高的场景。
2023-02-08 15:20:18 +08:00
<div class="center-table" markdown>
2023-04-17 18:24:20 +08:00
| | 数组 | 链表 | 哈希表 |
| -------- | ------ | ------ | ------ |
| 查找元素 | $O(n)$ | $O(n)$ | $O(1)$ |
| 插入元素 | $O(1)$ | $O(1)$ | $O(1)$ |
| 删除元素 | $O(n)$ | $O(n)$ | $O(1)$ |
2023-02-08 15:20:18 +08:00
</div>
2023-05-21 19:29:43 +08:00
## 6.1.1. &nbsp; 哈希表常用操作
2023-02-08 15:20:18 +08:00
哈希表的基本操作包括 **初始化、查询操作、添加与删除键值对**
=== "Java"
```java title="hash_map.java"
/* 初始化哈希表 */
Map<Integer, String> map = new HashMap<>();
/* 添加操作 */
// 在哈希表中添加键值对 (key, value)
map.put(12836, "小哈");
map.put(15937, "小啰");
map.put(16750, "小算");
map.put(13276, "小法");
map.put(10583, "小鸭");
/* 查询操作 */
// 向哈希表输入键 key ,得到值 value
String name = map.get(15937);
/* 删除操作 */
// 在哈希表中删除键值对 (key, value)
map.remove(10583);
```
=== "C++"
```cpp title="hash_map.cpp"
/* 初始化哈希表 */
unordered_map<int, string> map;
/* 添加操作 */
// 在哈希表中添加键值对 (key, value)
map[12836] = "小哈";
map[15937] = "小啰";
map[16750] = "小算";
map[13276] = "小法";
map[10583] = "小鸭";
/* 查询操作 */
// 向哈希表输入键 key ,得到值 value
string name = map[15937];
/* 删除操作 */
// 在哈希表中删除键值对 (key, value)
map.erase(10583);
```
=== "Python"
```python title="hash_map.py"
2023-04-09 05:30:47 +08:00
# 初始化哈希表
2023-03-12 18:46:03 +08:00
mapp: Dict = {}
2023-02-08 15:20:18 +08:00
2023-04-09 05:30:47 +08:00
# 添加操作
2023-02-08 15:20:18 +08:00
# 在哈希表中添加键值对 (key, value)
mapp[12836] = "小哈"
mapp[15937] = "小啰"
mapp[16750] = "小算"
mapp[13276] = "小法"
mapp[10583] = "小鸭"
2023-04-09 05:30:47 +08:00
# 查询操作
2023-02-08 15:20:18 +08:00
# 向哈希表输入键 key ,得到值 value
2023-03-12 18:46:03 +08:00
name: str = mapp[15937]
2023-02-08 15:20:18 +08:00
2023-04-09 05:30:47 +08:00
# 删除操作
2023-02-08 15:20:18 +08:00
# 在哈希表中删除键值对 (key, value)
mapp.pop(10583)
```
=== "Go"
```go title="hash_map.go"
/* 初始化哈希表 */
mapp := make(map[int]string)
/* 添加操作 */
// 在哈希表中添加键值对 (key, value)
mapp[12836] = "小哈"
mapp[15937] = "小啰"
mapp[16750] = "小算"
mapp[13276] = "小法"
mapp[10583] = "小鸭"
/* 查询操作 */
// 向哈希表输入键 key ,得到值 value
name := mapp[15937]
/* 删除操作 */
// 在哈希表中删除键值对 (key, value)
delete(mapp, 10583)
```
=== "JavaScript"
```javascript title="hash_map.js"
/* 初始化哈希表 */
const map = new ArrayHashMap();
/* 添加操作 */
// 在哈希表中添加键值对 (key, value)
map.set(12836, '小哈');
map.set(15937, '小啰');
map.set(16750, '小算');
map.set(13276, '小法');
map.set(10583, '小鸭');
/* 查询操作 */
// 向哈希表输入键 key ,得到值 value
let name = map.get(15937);
/* 删除操作 */
// 在哈希表中删除键值对 (key, value)
map.delete(10583);
```
=== "TypeScript"
```typescript title="hash_map.ts"
/* 初始化哈希表 */
const map = new Map<number, string>();
/* 添加操作 */
// 在哈希表中添加键值对 (key, value)
map.set(12836, '小哈');
map.set(15937, '小啰');
map.set(16750, '小算');
map.set(13276, '小法');
map.set(10583, '小鸭');
console.info('\n添加完成后哈希表为\nKey -> Value');
console.info(map);
/* 查询操作 */
// 向哈希表输入键 key ,得到值 value
let name = map.get(15937);
console.info('\n输入学号 15937 ,查询到姓名 ' + name);
/* 删除操作 */
// 在哈希表中删除键值对 (key, value)
map.delete(10583);
console.info('\n删除 10583 后,哈希表为\nKey -> Value');
console.info(map);
```
=== "C"
```c title="hash_map.c"
2023-04-18 20:19:07 +08:00
// C 未提供内置哈希表
2023-02-08 15:20:18 +08:00
```
=== "C#"
```csharp title="hash_map.cs"
/* 初始化哈希表 */
Dictionary<int, String> map = new ();
/* 添加操作 */
// 在哈希表中添加键值对 (key, value)
map.Add(12836, "小哈");
map.Add(15937, "小啰");
map.Add(16750, "小算");
map.Add(13276, "小法");
map.Add(10583, "小鸭");
/* 查询操作 */
// 向哈希表输入键 key ,得到值 value
String name = map[15937];
/* 删除操作 */
// 在哈希表中删除键值对 (key, value)
map.Remove(10583);
```
=== "Swift"
```swift title="hash_map.swift"
/* 初始化哈希表 */
var map: [Int: String] = [:]
/* 添加操作 */
// 在哈希表中添加键值对 (key, value)
map[12836] = "小哈"
map[15937] = "小啰"
map[16750] = "小算"
map[13276] = "小法"
map[10583] = "小鸭"
/* 查询操作 */
// 向哈希表输入键 key ,得到值 value
let name = map[15937]!
/* 删除操作 */
// 在哈希表中删除键值对 (key, value)
map.removeValue(forKey: 10583)
```
=== "Zig"
```zig title="hash_map.zig"
```
2023-06-02 02:38:24 +08:00
=== "Dart"
```dart title="hash_map.dart"
```
2023-02-08 15:20:18 +08:00
遍历哈希表有三种方式,即 **遍历键值对、遍历键、遍历值**
=== "Java"
```java title="hash_map.java"
/* 遍历哈希表 */
// 遍历键值对 key->value
for (Map.Entry <Integer, String> kv: map.entrySet()) {
System.out.println(kv.getKey() + " -> " + kv.getValue());
}
// 单独遍历键 key
for (int key: map.keySet()) {
System.out.println(key);
}
// 单独遍历值 value
for (String val: map.values()) {
System.out.println(val);
}
```
=== "C++"
```cpp title="hash_map.cpp"
/* 遍历哈希表 */
// 遍历键值对 key->value
for (auto kv: map) {
cout << kv.first << " -> " << kv.second << endl;
}
// 单独遍历键 key
for (auto key: map) {
cout << key.first << endl;
}
// 单独遍历值 value
for (auto val: map) {
cout << val.second << endl;
}
```
=== "Python"
```python title="hash_map.py"
2023-04-09 05:30:47 +08:00
# 遍历哈希表
2023-02-08 15:20:18 +08:00
# 遍历键值对 key->value
for key, value in mapp.items():
print(key, "->", value)
# 单独遍历键 key
for key in mapp.keys():
print(key)
# 单独遍历值 value
for value in mapp.values():
print(value)
```
=== "Go"
```go title="hash_map_test.go"
/* 遍历哈希表 */
// 遍历键值对 key->value
for key, value := range mapp {
fmt.Println(key, "->", value)
}
// 单独遍历键 key
for key := range mapp {
fmt.Println(key)
}
// 单独遍历值 value
for _, value := range mapp {
fmt.Println(value)
}
```
=== "JavaScript"
```javascript title="hash_map.js"
/* 遍历哈希表 */
2023-02-13 16:26:27 +08:00
console.info('\n遍历键值对 Key->Value');
for (const [k, v] of map.entries()) {
console.info(k + ' -> ' + v);
2023-02-08 15:20:18 +08:00
}
2023-02-13 16:26:27 +08:00
console.info('\n单独遍历键 Key');
for (const k of map.keys()) {
console.info(k);
2023-02-08 15:20:18 +08:00
}
2023-02-13 16:26:27 +08:00
console.info('\n单独遍历值 Value');
for (const v of map.values()) {
console.info(v);
2023-02-08 15:20:18 +08:00
}
```
=== "TypeScript"
```typescript title="hash_map.ts"
/* 遍历哈希表 */
console.info('\n遍历键值对 Key->Value');
for (const [k, v] of map.entries()) {
console.info(k + ' -> ' + v);
}
console.info('\n单独遍历键 Key');
for (const k of map.keys()) {
console.info(k);
}
console.info('\n单独遍历值 Value');
for (const v of map.values()) {
console.info(v);
}
```
=== "C"
```c title="hash_map.c"
2023-04-18 20:19:07 +08:00
// C 未提供内置哈希表
2023-02-08 15:20:18 +08:00
```
=== "C#"
```csharp title="hash_map.cs"
/* 遍历哈希表 */
// 遍历键值对 Key->Value
2023-03-01 03:16:47 +08:00
foreach (var kv in map)
{
2023-02-08 15:20:18 +08:00
Console.WriteLine(kv.Key + " -> " + kv.Value);
}
// 单独遍历键 key
2023-03-01 03:16:47 +08:00
foreach (int key in map.Keys)
{
2023-02-08 15:20:18 +08:00
Console.WriteLine(key);
}
// 单独遍历值 value
2023-03-01 03:16:47 +08:00
foreach (String val in map.Values)
{
2023-02-08 15:20:18 +08:00
Console.WriteLine(val);
}
```
=== "Swift"
```swift title="hash_map.swift"
/* 遍历哈希表 */
// 遍历键值对 Key->Value
for (key, value) in map {
print("\(key) -> \(value)")
}
// 单独遍历键 Key
for key in map.keys {
print(key)
}
// 单独遍历值 Value
for value in map.values {
print(value)
}
```
=== "Zig"
```zig title="hash_map.zig"
```
2023-06-02 02:38:24 +08:00
=== "Dart"
```dart title="hash_map.dart"
```
2023-05-21 19:29:43 +08:00
## 6.1.2. &nbsp; 哈希函数
2023-02-08 15:20:18 +08:00
2023-04-09 02:53:25 +08:00
哈希表的底层实现为数组,同时可能包含链表、二叉树(红黑树)等数据结构,以提高查询性能(将在下节讨论)。
2023-02-08 15:20:18 +08:00
2023-04-09 02:53:25 +08:00
首先考虑最简单的情况,**仅使用一个数组来实现哈希表**。通常,我们将数组中的每个空位称为「桶 Bucket」用于存储键值对。
2023-02-08 15:20:18 +08:00
2023-04-09 02:53:25 +08:00
我们将键值对 key, value 封装成一个类 `Entry` ,并将所有 `Entry` 放入数组中。这样,数组中的每个 `Entry` 都具有唯一的索引。为了建立 key 和索引之间的映射关系,我们需要使用「哈希函数 Hash Function」。
2023-03-15 03:12:57 +08:00
2023-04-09 02:53:25 +08:00
设哈希表的数组为 `buckets` ,哈希函数为 `f(x)` ,那么查询操作的步骤如下:
2023-02-08 15:20:18 +08:00
2023-03-15 23:44:13 +08:00
1. 输入 `key` ,通过哈希函数计算出索引 `index` ,即 `index = f(key)`
2023-04-09 02:53:25 +08:00
2. 通过索引在数组中访问到键值对 `entry` ,即 `entry = buckets[index]` ,然后从 `entry` 中获取对应的 `value`
2023-02-08 15:20:18 +08:00
2023-04-09 02:53:25 +08:00
以学生数据 `key 学号 -> value 姓名` 为例,我们可以设计如下哈希函数:
2023-02-08 15:20:18 +08:00
$$
2023-05-18 17:17:33 +08:00
f(x) = x \bmod {100}
2023-02-08 15:20:18 +08:00
$$
2023-05-18 17:17:33 +08:00
其中 $\bmod$ 表示取余运算。
2023-03-15 23:44:13 +08:00
![哈希函数工作原理](hash_map.assets/hash_function.png)
2023-03-15 03:12:57 +08:00
2023-03-15 23:44:13 +08:00
<p align="center"> Fig. 哈希函数工作原理 </p>
2023-02-26 19:53:26 +08:00
2023-02-08 15:20:18 +08:00
=== "Java"
```java title="array_hash_map.java"
/* 键值对 int->String */
class Entry {
public int key;
public String val;
2023-04-14 04:01:38 +08:00
2023-02-08 15:20:18 +08:00
public Entry(int key, String val) {
this.key = key;
this.val = val;
}
}
/* 基于数组简易实现的哈希表 */
class ArrayHashMap {
2023-03-15 03:12:57 +08:00
private List<Entry> buckets;
2023-04-14 04:01:38 +08:00
2023-02-08 15:20:18 +08:00
public ArrayHashMap() {
2023-03-15 03:48:59 +08:00
// 初始化数组,包含 100 个桶
2023-03-15 03:12:57 +08:00
buckets = new ArrayList<>();
2023-02-08 15:20:18 +08:00
for (int i = 0; i < 100; i++) {
2023-03-15 03:12:57 +08:00
buckets.add(null);
2023-02-08 15:20:18 +08:00
}
}
/* 哈希函数 */
private int hashFunc(int key) {
int index = key % 100;
return index;
}
/* 查询操作 */
public String get(int key) {
int index = hashFunc(key);
2023-03-15 03:12:57 +08:00
Entry pair = buckets.get(index);
2023-04-14 04:01:38 +08:00
if (pair == null)
return null;
2023-02-08 15:20:18 +08:00
return pair.val;
}
/* 添加操作 */
public void put(int key, String val) {
Entry pair = new Entry(key, val);
int index = hashFunc(key);
2023-03-15 03:12:57 +08:00
buckets.set(index, pair);
2023-02-08 15:20:18 +08:00
}
/* 删除操作 */
public void remove(int key) {
int index = hashFunc(key);
// 置为 null ,代表删除
2023-03-15 03:12:57 +08:00
buckets.set(index, null);
2023-02-08 15:20:18 +08:00
}
/* 获取所有键值对 */
public List<Entry> entrySet() {
List<Entry> entrySet = new ArrayList<>();
2023-03-15 03:12:57 +08:00
for (Entry pair : buckets) {
2023-02-08 15:20:18 +08:00
if (pair != null)
entrySet.add(pair);
}
return entrySet;
}
/* 获取所有键 */
public List<Integer> keySet() {
List<Integer> keySet = new ArrayList<>();
2023-03-15 03:12:57 +08:00
for (Entry pair : buckets) {
2023-02-08 15:20:18 +08:00
if (pair != null)
keySet.add(pair.key);
}
return keySet;
}
/* 获取所有值 */
public List<String> valueSet() {
List<String> valueSet = new ArrayList<>();
2023-03-15 03:12:57 +08:00
for (Entry pair : buckets) {
2023-02-08 15:20:18 +08:00
if (pair != null)
valueSet.add(pair.val);
}
return valueSet;
}
/* 打印哈希表 */
public void print() {
2023-04-14 04:01:38 +08:00
for (Entry kv : entrySet()) {
2023-02-08 15:20:18 +08:00
System.out.println(kv.key + " -> " + kv.val);
}
}
}
```
=== "C++"
```cpp title="array_hash_map.cpp"
/* 键值对 int->String */
struct Entry {
2023-04-14 04:01:38 +08:00
public:
2023-02-08 15:20:18 +08:00
int key;
string val;
Entry(int key, string val) {
this->key = key;
this->val = val;
}
};
/* 基于数组简易实现的哈希表 */
class ArrayHashMap {
2023-04-14 04:01:38 +08:00
private:
vector<Entry *> buckets;
public:
2023-02-08 15:20:18 +08:00
ArrayHashMap() {
2023-03-15 03:48:59 +08:00
// 初始化数组,包含 100 个桶
2023-04-14 04:01:38 +08:00
buckets = vector<Entry *>(100);
2023-02-08 15:20:18 +08:00
}
2023-04-16 04:55:51 +08:00
~ArrayHashMap() {
// 释放内存
for (const auto &bucket : buckets) {
delete bucket;
}
buckets.clear();
}
2023-02-08 15:20:18 +08:00
/* 哈希函数 */
int hashFunc(int key) {
int index = key % 100;
return index;
}
/* 查询操作 */
string get(int key) {
int index = hashFunc(key);
2023-04-14 04:01:38 +08:00
Entry *pair = buckets[index];
2023-02-08 15:20:18 +08:00
if (pair == nullptr)
return nullptr;
return pair->val;
}
/* 添加操作 */
void put(int key, string val) {
2023-04-14 04:01:38 +08:00
Entry *pair = new Entry(key, val);
2023-02-08 15:20:18 +08:00
int index = hashFunc(key);
2023-03-15 03:12:57 +08:00
buckets[index] = pair;
2023-02-08 15:20:18 +08:00
}
/* 删除操作 */
void remove(int key) {
int index = hashFunc(key);
2023-04-16 04:55:51 +08:00
// 释放内存并置为 nullptr
delete buckets[index];
2023-03-15 03:12:57 +08:00
buckets[index] = nullptr;
2023-02-08 15:20:18 +08:00
}
/* 获取所有键值对 */
2023-04-14 04:01:38 +08:00
vector<Entry *> entrySet() {
vector<Entry *> entrySet;
for (Entry *pair : buckets) {
2023-02-08 15:20:18 +08:00
if (pair != nullptr) {
entrySet.push_back(pair);
}
}
return entrySet;
}
/* 获取所有键 */
vector<int> keySet() {
vector<int> keySet;
2023-04-14 04:01:38 +08:00
for (Entry *pair : buckets) {
2023-02-08 15:20:18 +08:00
if (pair != nullptr) {
keySet.push_back(pair->key);
}
}
return keySet;
}
/* 获取所有值 */
vector<string> valueSet() {
vector<string> valueSet;
2023-04-14 04:01:38 +08:00
for (Entry *pair : buckets) {
if (pair != nullptr) {
2023-02-08 15:20:18 +08:00
valueSet.push_back(pair->val);
}
}
return valueSet;
}
/* 打印哈希表 */
void print() {
2023-04-14 04:01:38 +08:00
for (Entry *kv : entrySet()) {
2023-02-08 15:20:18 +08:00
cout << kv->key << " -> " << kv->val << endl;
}
}
};
```
=== "Python"
```python title="array_hash_map.py"
class Entry:
2023-04-09 05:12:22 +08:00
"""键值对 int->String"""
2023-03-12 18:46:03 +08:00
def __init__(self, key: int, val: str):
2023-02-08 15:20:18 +08:00
self.key = key
self.val = val
class ArrayHashMap:
2023-04-09 05:12:22 +08:00
"""基于数组简易实现的哈希表"""
2023-02-08 15:20:18 +08:00
def __init__(self):
2023-04-09 05:12:22 +08:00
"""构造方法"""
2023-03-15 03:48:59 +08:00
# 初始化数组,包含 100 个桶
2023-03-23 18:56:56 +08:00
self.buckets: list[Entry | None] = [None] * 100
2023-02-08 15:20:18 +08:00
2023-03-12 18:46:03 +08:00
def hash_func(self, key: int) -> int:
2023-04-09 05:12:22 +08:00
"""哈希函数"""
2023-05-22 22:01:18 +08:00
index = key % 100
2023-02-08 15:20:18 +08:00
return index
2023-03-12 18:46:03 +08:00
def get(self, key: int) -> str:
2023-04-09 05:12:22 +08:00
"""查询操作"""
2023-03-12 18:46:03 +08:00
index: int = self.hash_func(key)
2023-03-15 03:12:57 +08:00
pair: Entry = self.buckets[index]
2023-02-08 15:20:18 +08:00
if pair is None:
return None
return pair.val
2023-03-12 18:46:03 +08:00
def put(self, key: int, val: str) -> None:
2023-04-09 05:12:22 +08:00
"""添加操作"""
2023-02-08 15:20:18 +08:00
pair = Entry(key, val)
2023-03-12 18:46:03 +08:00
index: int = self.hash_func(key)
2023-03-15 03:12:57 +08:00
self.buckets[index] = pair
2023-02-08 15:20:18 +08:00
2023-03-12 18:46:03 +08:00
def remove(self, key: int) -> None:
2023-04-09 05:12:22 +08:00
"""删除操作"""
2023-03-12 18:46:03 +08:00
index: int = self.hash_func(key)
2023-02-08 15:20:18 +08:00
# 置为 None ,代表删除
2023-03-15 03:12:57 +08:00
self.buckets[index] = None
2023-02-08 15:20:18 +08:00
2023-03-23 18:56:56 +08:00
def entry_set(self) -> list[Entry]:
2023-04-09 05:12:22 +08:00
"""获取所有键值对"""
2023-03-23 18:56:56 +08:00
result: list[Entry] = []
2023-03-15 03:12:57 +08:00
for pair in self.buckets:
2023-02-08 15:20:18 +08:00
if pair is not None:
result.append(pair)
return result
2023-03-23 18:56:56 +08:00
def key_set(self) -> list[int]:
2023-04-09 05:12:22 +08:00
"""获取所有键"""
2023-05-22 22:01:18 +08:00
result = []
2023-03-15 03:12:57 +08:00
for pair in self.buckets:
2023-02-08 15:20:18 +08:00
if pair is not None:
result.append(pair.key)
return result
2023-03-23 18:56:56 +08:00
def value_set(self) -> list[str]:
2023-04-09 05:12:22 +08:00
"""获取所有值"""
2023-05-22 22:01:18 +08:00
result = []
2023-03-15 03:12:57 +08:00
for pair in self.buckets:
2023-02-08 15:20:18 +08:00
if pair is not None:
result.append(pair.val)
return result
2023-03-12 18:46:03 +08:00
def print(self) -> None:
2023-04-09 05:12:22 +08:00
"""打印哈希表"""
2023-03-15 03:12:57 +08:00
for pair in self.buckets:
2023-02-08 15:20:18 +08:00
if pair is not None:
print(pair.key, "->", pair.val)
```
=== "Go"
```go title="array_hash_map.go"
/* 键值对 int->String */
type entry struct {
key int
val string
}
2023-02-09 04:43:12 +08:00
2023-02-08 15:20:18 +08:00
/* 基于数组简易实现的哈希表 */
type arrayHashMap struct {
2023-03-15 03:12:57 +08:00
buckets []*entry
2023-02-08 15:20:18 +08:00
}
2023-02-09 04:43:12 +08:00
/* 初始化哈希表 */
2023-02-08 15:20:18 +08:00
func newArrayHashMap() *arrayHashMap {
2023-03-15 03:48:59 +08:00
// 初始化数组,包含 100 个桶
2023-03-15 03:12:57 +08:00
buckets := make([]*entry, 100)
return &arrayHashMap{buckets: buckets}
2023-02-08 15:20:18 +08:00
}
2023-02-09 04:43:12 +08:00
2023-02-08 15:20:18 +08:00
/* 哈希函数 */
func (a *arrayHashMap) hashFunc(key int) int {
index := key % 100
return index
}
2023-02-09 04:43:12 +08:00
2023-02-08 15:20:18 +08:00
/* 查询操作 */
func (a *arrayHashMap) get(key int) string {
index := a.hashFunc(key)
2023-03-15 03:12:57 +08:00
pair := a.buckets[index]
2023-02-08 15:20:18 +08:00
if pair == nil {
return "Not Found"
}
return pair.val
}
2023-02-09 04:43:12 +08:00
2023-02-08 15:20:18 +08:00
/* 添加操作 */
func (a *arrayHashMap) put(key int, val string) {
pair := &entry{key: key, val: val}
index := a.hashFunc(key)
2023-03-15 03:12:57 +08:00
a.buckets[index] = pair
2023-02-08 15:20:18 +08:00
}
2023-02-09 04:43:12 +08:00
2023-02-08 15:20:18 +08:00
/* 删除操作 */
func (a *arrayHashMap) remove(key int) {
index := a.hashFunc(key)
// 置为 nil ,代表删除
2023-03-15 03:12:57 +08:00
a.buckets[index] = nil
2023-02-08 15:20:18 +08:00
}
2023-02-09 04:43:12 +08:00
/* 获取所有键对 */
func (a *arrayHashMap) entrySet() []*entry {
var pairs []*entry
2023-03-15 03:12:57 +08:00
for _, pair := range a.buckets {
2023-02-09 04:43:12 +08:00
if pair != nil {
pairs = append(pairs, pair)
}
}
return pairs
}
/* 获取所有键 */
func (a *arrayHashMap) keySet() []int {
var keys []int
2023-03-15 03:12:57 +08:00
for _, pair := range a.buckets {
2023-02-09 04:43:12 +08:00
if pair != nil {
keys = append(keys, pair.key)
}
}
return keys
}
/* 获取所有值 */
func (a *arrayHashMap) valueSet() []string {
var values []string
2023-03-15 03:12:57 +08:00
for _, pair := range a.buckets {
2023-02-09 04:43:12 +08:00
if pair != nil {
values = append(values, pair.val)
}
}
return values
}
/* 打印哈希表 */
func (a *arrayHashMap) print() {
2023-03-15 03:12:57 +08:00
for _, pair := range a.buckets {
2023-02-09 04:43:12 +08:00
if pair != nil {
fmt.Println(pair.key, "->", pair.val)
}
}
}
2023-02-08 15:20:18 +08:00
```
=== "JavaScript"
```javascript title="array_hash_map.js"
/* 键值对 Number -> String */
class Entry {
constructor(key, val) {
this.key = key;
this.val = val;
}
}
2023-02-08 16:47:52 +08:00
2023-02-08 15:20:18 +08:00
/* 基于数组简易实现的哈希表 */
class ArrayHashMap {
2023-03-15 03:12:57 +08:00
#buckets;
2023-02-08 15:20:18 +08:00
constructor() {
2023-03-15 03:48:59 +08:00
// 初始化数组,包含 100 个桶
2023-03-15 03:12:57 +08:00
this.#buckets = new Array(100).fill(null);
2023-02-08 15:20:18 +08:00
}
2023-02-08 16:47:52 +08:00
2023-02-08 15:20:18 +08:00
/* 哈希函数 */
#hashFunc(key) {
return key % 100;
}
2023-02-08 16:47:52 +08:00
2023-02-08 15:20:18 +08:00
/* 查询操作 */
get(key) {
let index = this.#hashFunc(key);
2023-03-15 03:12:57 +08:00
let entry = this.#buckets[index];
2023-02-08 15:20:18 +08:00
if (entry === null) return null;
return entry.val;
}
2023-02-08 16:47:52 +08:00
2023-02-08 15:20:18 +08:00
/* 添加操作 */
set(key, val) {
let index = this.#hashFunc(key);
2023-03-15 03:12:57 +08:00
this.#buckets[index] = new Entry(key, val);
2023-02-08 15:20:18 +08:00
}
2023-02-08 16:47:52 +08:00
2023-02-08 15:20:18 +08:00
/* 删除操作 */
delete(key) {
let index = this.#hashFunc(key);
// 置为 null ,代表删除
2023-03-15 03:12:57 +08:00
this.#buckets[index] = null;
2023-02-08 15:20:18 +08:00
}
2023-02-08 16:47:52 +08:00
/* 获取所有键值对 */
entries() {
let arr = [];
2023-03-15 03:12:57 +08:00
for (let i = 0; i < this.#buckets.length; i++) {
if (this.#buckets[i]) {
arr.push(this.#buckets[i]);
2023-02-08 16:47:52 +08:00
}
}
return arr;
}
/* 获取所有键 */
keys() {
let arr = [];
2023-03-15 03:12:57 +08:00
for (let i = 0; i < this.#buckets.length; i++) {
if (this.#buckets[i]) {
arr.push(this.#buckets[i]?.key);
2023-02-08 16:47:52 +08:00
}
}
return arr;
}
/* 获取所有值 */
values() {
let arr = [];
2023-03-15 03:12:57 +08:00
for (let i = 0; i < this.#buckets.length; i++) {
if (this.#buckets[i]) {
arr.push(this.#buckets[i]?.val);
2023-02-08 16:47:52 +08:00
}
}
return arr;
}
/* 打印哈希表 */
print() {
let entrySet = this.entries();
for (const entry of entrySet) {
if (!entry) continue;
console.info(`${entry.key} -> ${entry.val}`);
}
}
2023-02-08 15:20:18 +08:00
}
```
=== "TypeScript"
```typescript title="array_hash_map.ts"
/* 键值对 Number -> String */
class Entry {
2023-02-08 16:47:52 +08:00
public key: number;
public val: string;
2023-02-08 15:20:18 +08:00
constructor(key: number, val: string) {
this.key = key;
this.val = val;
}
}
2023-02-08 16:47:52 +08:00
2023-02-08 15:20:18 +08:00
/* 基于数组简易实现的哈希表 */
class ArrayHashMap {
2023-03-15 03:12:57 +08:00
private readonly buckets: (Entry | null)[];
2023-02-08 16:47:52 +08:00
2023-02-08 15:20:18 +08:00
constructor() {
2023-03-15 03:48:59 +08:00
// 初始化数组,包含 100 个桶
2023-04-17 21:57:42 +08:00
this.buckets = new Array(100).fill(null);
2023-02-08 15:20:18 +08:00
}
2023-02-08 16:47:52 +08:00
2023-02-08 15:20:18 +08:00
/* 哈希函数 */
private hashFunc(key: number): number {
return key % 100;
}
2023-02-08 16:47:52 +08:00
2023-02-08 15:20:18 +08:00
/* 查询操作 */
public get(key: number): string | null {
let index = this.hashFunc(key);
2023-03-15 03:12:57 +08:00
let entry = this.buckets[index];
2023-02-08 15:20:18 +08:00
if (entry === null) return null;
return entry.val;
}
2023-02-08 16:47:52 +08:00
2023-02-08 15:20:18 +08:00
/* 添加操作 */
public set(key: number, val: string) {
let index = this.hashFunc(key);
2023-03-15 03:12:57 +08:00
this.buckets[index] = new Entry(key, val);
2023-02-08 15:20:18 +08:00
}
2023-02-08 16:47:52 +08:00
2023-02-08 15:20:18 +08:00
/* 删除操作 */
public delete(key: number) {
let index = this.hashFunc(key);
// 置为 null ,代表删除
2023-03-15 03:12:57 +08:00
this.buckets[index] = null;
2023-02-08 15:20:18 +08:00
}
2023-02-08 16:47:52 +08:00
/* 获取所有键值对 */
public entries(): (Entry | null)[] {
let arr: (Entry | null)[] = [];
2023-03-15 03:12:57 +08:00
for (let i = 0; i < this.buckets.length; i++) {
if (this.buckets[i]) {
arr.push(this.buckets[i]);
2023-02-08 16:47:52 +08:00
}
}
return arr;
}
/* 获取所有键 */
public keys(): (number | undefined)[] {
let arr: (number | undefined)[] = [];
2023-03-15 03:12:57 +08:00
for (let i = 0; i < this.buckets.length; i++) {
if (this.buckets[i]) {
arr.push(this.buckets[i]?.key);
2023-02-08 16:47:52 +08:00
}
}
return arr;
}
/* 获取所有值 */
public values(): (string | undefined)[] {
let arr: (string | undefined)[] = [];
2023-03-15 03:12:57 +08:00
for (let i = 0; i < this.buckets.length; i++) {
if (this.buckets[i]) {
arr.push(this.buckets[i]?.val);
2023-02-08 16:47:52 +08:00
}
}
return arr;
}
/* 打印哈希表 */
public print() {
let entrySet = this.entries();
for (const entry of entrySet) {
if (!entry) continue;
console.info(`${entry.key} -> ${entry.val}`);
}
}
2023-02-08 15:20:18 +08:00
}
```
=== "C"
```c title="array_hash_map.c"
2023-02-11 18:21:44 +08:00
[class]{entry}-[func]{}
[class]{arrayHashMap}-[func]{}
2023-02-08 15:20:18 +08:00
```
=== "C#"
```csharp title="array_hash_map.cs"
2023-04-22 01:35:51 +08:00
/* 键值对 int->string */
2023-04-23 14:58:03 +08:00
class Entry {
2023-02-08 15:20:18 +08:00
public int key;
2023-04-22 01:35:51 +08:00
public string val;
2023-04-23 14:58:03 +08:00
public Entry(int key, string val) {
2023-02-08 15:20:18 +08:00
this.key = key;
this.val = val;
}
}
2023-02-08 22:16:25 +08:00
2023-02-08 15:20:18 +08:00
/* 基于数组简易实现的哈希表 */
2023-04-23 14:58:03 +08:00
class ArrayHashMap {
2023-03-15 03:12:57 +08:00
private List<Entry?> buckets;
2023-04-23 14:58:03 +08:00
public ArrayHashMap() {
2023-03-15 03:48:59 +08:00
// 初始化数组,包含 100 个桶
2023-03-15 03:12:57 +08:00
buckets = new();
2023-04-23 14:58:03 +08:00
for (int i = 0; i < 100; i++) {
2023-03-15 03:12:57 +08:00
buckets.Add(null);
2023-02-08 15:20:18 +08:00
}
}
2023-02-08 22:16:25 +08:00
2023-02-08 15:20:18 +08:00
/* 哈希函数 */
2023-04-23 14:58:03 +08:00
private int hashFunc(int key) {
2023-02-08 15:20:18 +08:00
int index = key % 100;
return index;
}
2023-02-08 22:16:25 +08:00
2023-02-08 15:20:18 +08:00
/* 查询操作 */
2023-04-23 14:58:03 +08:00
public string? get(int key) {
2023-02-08 15:20:18 +08:00
int index = hashFunc(key);
2023-03-15 03:12:57 +08:00
Entry? pair = buckets[index];
2023-02-08 15:20:18 +08:00
if (pair == null) return null;
return pair.val;
}
2023-02-08 22:16:25 +08:00
2023-02-08 15:20:18 +08:00
/* 添加操作 */
2023-04-23 14:58:03 +08:00
public void put(int key, string val) {
2023-02-08 15:20:18 +08:00
Entry pair = new Entry(key, val);
int index = hashFunc(key);
2023-03-15 03:12:57 +08:00
buckets[index] = pair;
2023-02-08 15:20:18 +08:00
}
2023-02-08 22:16:25 +08:00
2023-02-08 15:20:18 +08:00
/* 删除操作 */
2023-04-23 14:58:03 +08:00
public void remove(int key) {
2023-02-08 15:20:18 +08:00
int index = hashFunc(key);
// 置为 null ,代表删除
2023-03-15 03:12:57 +08:00
buckets[index] = null;
2023-02-08 22:16:25 +08:00
}
/* 获取所有键值对 */
2023-04-23 14:58:03 +08:00
public List<Entry> entrySet() {
2023-02-08 22:16:25 +08:00
List<Entry> entrySet = new();
2023-04-23 14:58:03 +08:00
foreach (Entry? pair in buckets) {
2023-02-08 22:16:25 +08:00
if (pair != null)
entrySet.Add(pair);
}
return entrySet;
}
/* 获取所有键 */
2023-04-23 14:58:03 +08:00
public List<int> keySet() {
2023-02-08 22:16:25 +08:00
List<int> keySet = new();
2023-04-23 14:58:03 +08:00
foreach (Entry? pair in buckets) {
2023-02-08 22:16:25 +08:00
if (pair != null)
keySet.Add(pair.key);
}
return keySet;
}
/* 获取所有值 */
2023-04-23 14:58:03 +08:00
public List<string> valueSet() {
2023-04-22 01:35:51 +08:00
List<string> valueSet = new();
2023-04-23 14:58:03 +08:00
foreach (Entry? pair in buckets) {
2023-02-08 22:16:25 +08:00
if (pair != null)
valueSet.Add(pair.val);
}
return valueSet;
}
/* 打印哈希表 */
2023-04-23 14:58:03 +08:00
public void print() {
foreach (Entry kv in entrySet()) {
2023-02-08 22:16:25 +08:00
Console.WriteLine(kv.key + " -> " + kv.val);
}
2023-02-08 15:20:18 +08:00
}
}
```
=== "Swift"
```swift title="array_hash_map.swift"
/* 键值对 int->String */
class Entry {
var key: Int
var val: String
2023-02-08 20:30:14 +08:00
2023-02-08 15:20:18 +08:00
init(key: Int, val: String) {
self.key = key
self.val = val
}
}
2023-02-08 20:30:14 +08:00
2023-02-08 15:20:18 +08:00
/* 基于数组简易实现的哈希表 */
class ArrayHashMap {
2023-03-15 03:12:57 +08:00
private var buckets: [Entry?] = []
2023-02-08 20:30:14 +08:00
2023-02-08 15:20:18 +08:00
init() {
2023-03-15 03:48:59 +08:00
// 初始化数组,包含 100 个桶
2023-02-08 15:20:18 +08:00
for _ in 0 ..< 100 {
2023-03-15 03:12:57 +08:00
buckets.append(nil)
2023-02-08 15:20:18 +08:00
}
}
2023-02-08 20:30:14 +08:00
2023-02-08 15:20:18 +08:00
/* 哈希函数 */
private func hashFunc(key: Int) -> Int {
let index = key % 100
return index
}
2023-02-08 20:30:14 +08:00
2023-02-08 15:20:18 +08:00
/* 查询操作 */
func get(key: Int) -> String? {
let index = hashFunc(key: key)
2023-03-15 03:12:57 +08:00
let pair = buckets[index]
2023-02-08 15:20:18 +08:00
return pair?.val
}
2023-02-08 20:30:14 +08:00
2023-02-08 15:20:18 +08:00
/* 添加操作 */
func put(key: Int, val: String) {
let pair = Entry(key: key, val: val)
let index = hashFunc(key: key)
2023-03-15 03:12:57 +08:00
buckets[index] = pair
2023-02-08 15:20:18 +08:00
}
2023-02-08 20:30:14 +08:00
2023-02-08 15:20:18 +08:00
/* 删除操作 */
func remove(key: Int) {
let index = hashFunc(key: key)
// 置为 nil ,代表删除
2023-03-15 03:12:57 +08:00
buckets[index] = nil
2023-02-08 15:20:18 +08:00
}
2023-02-08 20:30:14 +08:00
/* 获取所有键值对 */
func entrySet() -> [Entry] {
var entrySet: [Entry] = []
2023-03-15 03:12:57 +08:00
for pair in buckets {
2023-02-08 20:30:14 +08:00
if let pair = pair {
entrySet.append(pair)
}
}
return entrySet
}
/* 获取所有键 */
func keySet() -> [Int] {
var keySet: [Int] = []
2023-03-15 03:12:57 +08:00
for pair in buckets {
2023-02-08 20:30:14 +08:00
if let pair = pair {
keySet.append(pair.key)
}
}
return keySet
}
/* 获取所有值 */
func valueSet() -> [String] {
var valueSet: [String] = []
2023-03-15 03:12:57 +08:00
for pair in buckets {
2023-02-08 20:30:14 +08:00
if let pair = pair {
valueSet.append(pair.val)
}
}
return valueSet
}
/* 打印哈希表 */
func print() {
for entry in entrySet() {
Swift.print("\(entry.key) -> \(entry.val)")
}
}
2023-02-08 15:20:18 +08:00
}
```
=== "Zig"
```zig title="array_hash_map.zig"
2023-02-09 22:55:29 +08:00
// 键值对 int->String
const Entry = struct {
key: usize = undefined,
val: []const u8 = undefined,
2023-02-08 15:20:18 +08:00
2023-02-09 22:55:29 +08:00
pub fn init(key: usize, val: []const u8) Entry {
return Entry {
.key = key,
.val = val,
};
}
};
// 基于数组简易实现的哈希表
fn ArrayHashMap(comptime T: type) type {
return struct {
2023-03-15 03:12:57 +08:00
buckets: ?std.ArrayList(?T) = null,
2023-02-09 22:55:29 +08:00
mem_allocator: std.mem.Allocator = undefined,
const Self = @This();
2023-02-10 01:03:37 +08:00
// 构造方法
2023-02-09 22:55:29 +08:00
pub fn init(self: *Self, allocator: std.mem.Allocator) !void {
self.mem_allocator = allocator;
2023-03-15 03:48:59 +08:00
// 初始化数组,包含 100 个桶
2023-03-15 03:12:57 +08:00
self.buckets = std.ArrayList(?T).init(self.mem_allocator);
2023-02-09 22:55:29 +08:00
var i: i32 = 0;
while (i < 100) : (i += 1) {
2023-03-15 03:12:57 +08:00
try self.buckets.?.append(null);
2023-02-09 22:55:29 +08:00
}
}
2023-02-10 01:03:37 +08:00
// 析构方法
2023-02-09 22:55:29 +08:00
pub fn deinit(self: *Self) void {
2023-03-15 03:12:57 +08:00
if (self.buckets != null) self.buckets.?.deinit();
2023-02-09 22:55:29 +08:00
}
// 哈希函数
fn hashFunc(key: usize) usize {
var index = key % 100;
return index;
}
// 查询操作
pub fn get(self: *Self, key: usize) []const u8 {
var index = hashFunc(key);
2023-03-15 03:12:57 +08:00
var pair = self.buckets.?.items[index];
2023-02-09 22:55:29 +08:00
return pair.?.val;
}
// 添加操作
pub fn put(self: *Self, key: usize, val: []const u8) !void {
var pair = Entry.init(key, val);
var index = hashFunc(key);
2023-03-15 03:12:57 +08:00
self.buckets.?.items[index] = pair;
2023-02-09 22:55:29 +08:00
}
// 删除操作
pub fn remove(self: *Self, key: usize) !void {
var index = hashFunc(key);
// 置为 null ,代表删除
2023-03-15 03:12:57 +08:00
self.buckets.?.items[index] = null;
2023-02-09 22:55:29 +08:00
}
// 获取所有键值对
pub fn entrySet(self: *Self) !*std.ArrayList(T) {
var entry_set = std.ArrayList(T).init(self.mem_allocator);
2023-03-15 03:12:57 +08:00
for (self.buckets.?.items) |item| {
2023-02-09 22:55:29 +08:00
if (item == null) continue;
try entry_set.append(item.?);
}
return &entry_set;
}
// 获取所有键
pub fn keySet(self: *Self) !*std.ArrayList(usize) {
var key_set = std.ArrayList(usize).init(self.mem_allocator);
2023-03-15 03:12:57 +08:00
for (self.buckets.?.items) |item| {
2023-02-09 22:55:29 +08:00
if (item == null) continue;
try key_set.append(item.?.key);
}
return &key_set;
}
// 获取所有值
pub fn valueSet(self: *Self) !*std.ArrayList([]const u8) {
var value_set = std.ArrayList([]const u8).init(self.mem_allocator);
2023-03-15 03:12:57 +08:00
for (self.buckets.?.items) |item| {
2023-02-09 22:55:29 +08:00
if (item == null) continue;
try value_set.append(item.?.val);
}
return &value_set;
}
// 打印哈希表
pub fn print(self: *Self) !void {
var entry_set = try self.entrySet();
defer entry_set.deinit();
for (entry_set.items) |item| {
std.debug.print("{} -> {s}\n", .{item.key, item.val});
}
}
};
}
2023-02-08 15:20:18 +08:00
```
2023-06-02 02:38:24 +08:00
=== "Dart"
```dart title="array_hash_map.dart"
/* 键值对 int -> String */
class Entry {
int key;
String val;
Entry(this.key, this.val);
}
/* 基于数组简易实现的哈希表 */
class ArrayHashMap {
late List<Entry?> _buckets;
ArrayHashMap() {
// 初始化数组,包含 100 个桶
_buckets = List.filled(100, null);
}
/* 哈希函数 */
int _hashFunc(int key) {
final int index = key % 100;
return index;
}
/* 查询操作 */
String? get(int key) {
final int index = _hashFunc(key);
final Entry? pair = _buckets[index];
if (pair == null) {
return null;
}
return pair.val;
}
/* 添加操作 */
void put(int key, String val) {
final Entry pair = Entry(key, val);
final int index = _hashFunc(key);
_buckets[index] = pair;
}
/* 删除操作 */
void remove(int key) {
final int index = _hashFunc(key);
_buckets[index] = null;
}
/* 获取所有键值对 */
List<Entry> entrySet() {
List<Entry> entrySet = [];
for (final Entry? pair in _buckets) {
if (pair != null) {
entrySet.add(pair);
}
}
return entrySet;
}
/* 获取所有键 */
List<int> keySet() {
List<int> keySet = [];
for (final Entry? pair in _buckets) {
if (pair != null) {
keySet.add(pair.key);
}
}
return keySet;
}
/* 获取所有值 */
List<String> values() {
List<String> valueSet = [];
for (final Entry? pair in _buckets) {
if (pair != null) {
valueSet.add(pair.val);
}
}
return valueSet;
}
/* 打印哈希表 */
void printHashMap() {
for (final Entry kv in entrySet()) {
print("${kv.key} -> ${kv.val}");
}
}
}
```
2023-02-08 15:20:18 +08:00
2023-05-21 19:29:43 +08:00
## 6.1.3. &nbsp; 哈希冲突
2023-02-08 15:20:18 +08:00
2023-05-18 17:17:33 +08:00
细心的你可能已经注意到,**在某些情况下,哈希函数 $f(x) = x \bmod 100$ 可能无法正常工作**。具体来说,当输入的 key 后两位相同时,哈希函数的计算结果也会相同,从而指向同一个 value 。例如,查询学号为 $12836$ 和 $20336$ 的两个学生时,我们得到:
2023-02-08 15:20:18 +08:00
$$
f(12836) = f(20336) = 36
$$
2023-04-09 02:53:25 +08:00
这两个学号指向了同一个姓名,这显然是错误的。我们把这种情况称为「哈希冲突 Hash Collision」。在后续章节中我们将讨论如何解决哈希冲突的问题。
2023-02-08 15:20:18 +08:00
2023-02-26 18:17:57 +08:00
![哈希冲突示例](hash_map.assets/hash_collision.png)
2023-02-08 15:20:18 +08:00
2023-02-26 19:53:26 +08:00
<p align="center"> Fig. 哈希冲突示例 </p>
2023-04-09 02:53:25 +08:00
综上所述,一个优秀的哈希函数应具备以下特性:
2023-02-08 15:20:18 +08:00
2023-04-09 02:53:25 +08:00
- 尽可能减少哈希冲突的发生;
- 查询效率高且稳定,能够在绝大多数情况下达到 $O(1)$ 时间复杂度;
- 较高的空间利用率,即使“键值对占用空间 / 哈希表总占用空间”比例最大化;