2023-02-28 20:03:53 +08:00
|
|
|
|
# 哈希表
|
2022-12-05 02:37:16 +08:00
|
|
|
|
|
2023-06-15 01:53:52 +08:00
|
|
|
|
「哈希表 Hash Table」通过建立键 `key` 与值 `value` 之间的映射,实现高效的元素查询。具体而言,我们向哈希表输入一个 `key` ,则可以在 $O(1)$ 时间内获取对应的 `value` 。
|
2022-12-05 02:37:16 +08:00
|
|
|
|
|
2023-06-16 21:20:57 +08:00
|
|
|
|
以一个包含 $n$ 个学生的数据库为例,每个学生都有“姓名”和“学号”两项数据。假如我们希望实现“输入一个学号,返回对应的姓名”的查询功能,则可以采用哈希表来实现。
|
2022-12-06 01:00:21 +08:00
|
|
|
|
|
2023-06-15 01:53:52 +08:00
|
|
|
|
![哈希表的抽象表示](hash_map.assets/hash_table_lookup.png)
|
2022-12-06 01:00:21 +08:00
|
|
|
|
|
2023-07-06 00:04:11 +08:00
|
|
|
|
除哈希表外,我们还可以使用数组或链表实现查询功能。若将学生数据看作数组(链表)元素,则有:
|
2022-12-06 01:00:21 +08:00
|
|
|
|
|
2023-07-06 00:04:11 +08:00
|
|
|
|
- **添加元素**:仅需将元素添加至数组(链表)的尾部即可,使用 $O(1)$ 时间;
|
|
|
|
|
- **查询元素**:由于数组(链表)是乱序的,因此需要遍历数组(链表)中的所有元素,使用 $O(n)$ 时间;
|
|
|
|
|
- **删除元素**:需要先查询到元素,再从数组中删除,使用 $O(n)$ 时间;
|
2023-06-14 02:01:06 +08:00
|
|
|
|
|
2022-12-06 01:00:21 +08:00
|
|
|
|
<div class="center-table" markdown>
|
|
|
|
|
|
2023-04-17 18:22:18 +08:00
|
|
|
|
| | 数组 | 链表 | 哈希表 |
|
|
|
|
|
| -------- | ------ | ------ | ------ |
|
|
|
|
|
| 查找元素 | $O(n)$ | $O(n)$ | $O(1)$ |
|
2023-06-14 02:01:06 +08:00
|
|
|
|
| 添加元素 | $O(1)$ | $O(1)$ | $O(1)$ |
|
2023-04-17 18:22:18 +08:00
|
|
|
|
| 删除元素 | $O(n)$ | $O(n)$ | $O(1)$ |
|
2022-12-06 01:00:21 +08:00
|
|
|
|
|
|
|
|
|
</div>
|
2022-12-05 02:37:16 +08:00
|
|
|
|
|
2023-06-16 21:20:57 +08:00
|
|
|
|
观察发现,**在哈希表中进行增删查改的时间复杂度都是 $O(1)$** ,非常高效。因此,哈希表常用于对查找效率要求较高的场景。
|
2023-06-15 01:53:52 +08:00
|
|
|
|
|
2023-02-16 03:39:01 +08:00
|
|
|
|
## 哈希表常用操作
|
2022-12-05 02:37:16 +08:00
|
|
|
|
|
2023-06-16 21:20:57 +08:00
|
|
|
|
哈希表的常见操作包括:初始化、查询操作、添加键值对和删除键值对等。
|
2022-12-05 02:37:16 +08:00
|
|
|
|
|
2022-12-13 01:36:40 +08:00
|
|
|
|
=== "Java"
|
|
|
|
|
|
|
|
|
|
```java title="hash_map.java"
|
|
|
|
|
/* 初始化哈希表 */
|
|
|
|
|
Map<Integer, String> map = new HashMap<>();
|
2023-01-30 03:55:13 +08:00
|
|
|
|
|
2022-12-13 01:36:40 +08:00
|
|
|
|
/* 添加操作 */
|
|
|
|
|
// 在哈希表中添加键值对 (key, value)
|
|
|
|
|
map.put(12836, "小哈");
|
|
|
|
|
map.put(15937, "小啰");
|
|
|
|
|
map.put(16750, "小算");
|
|
|
|
|
map.put(13276, "小法");
|
|
|
|
|
map.put(10583, "小鸭");
|
2023-01-30 03:55:13 +08:00
|
|
|
|
|
2022-12-13 01:36:40 +08:00
|
|
|
|
/* 查询操作 */
|
|
|
|
|
// 向哈希表输入键 key ,得到值 value
|
|
|
|
|
String name = map.get(15937);
|
2023-01-30 03:55:13 +08:00
|
|
|
|
|
2022-12-13 01:36:40 +08:00
|
|
|
|
/* 删除操作 */
|
|
|
|
|
// 在哈希表中删除键值对 (key, value)
|
|
|
|
|
map.remove(10583);
|
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
=== "C++"
|
|
|
|
|
|
|
|
|
|
```cpp title="hash_map.cpp"
|
2022-12-14 17:18:32 +08:00
|
|
|
|
/* 初始化哈希表 */
|
|
|
|
|
unordered_map<int, string> map;
|
2023-01-30 03:55:13 +08:00
|
|
|
|
|
2022-12-14 17:18:32 +08:00
|
|
|
|
/* 添加操作 */
|
|
|
|
|
// 在哈希表中添加键值对 (key, value)
|
|
|
|
|
map[12836] = "小哈";
|
|
|
|
|
map[15937] = "小啰";
|
|
|
|
|
map[16750] = "小算";
|
|
|
|
|
map[13276] = "小法";
|
|
|
|
|
map[10583] = "小鸭";
|
2023-01-30 03:55:13 +08:00
|
|
|
|
|
2022-12-14 17:18:32 +08:00
|
|
|
|
/* 查询操作 */
|
|
|
|
|
// 向哈希表输入键 key ,得到值 value
|
|
|
|
|
string name = map[15937];
|
2023-01-30 03:55:13 +08:00
|
|
|
|
|
2022-12-14 17:18:32 +08:00
|
|
|
|
/* 删除操作 */
|
|
|
|
|
// 在哈希表中删除键值对 (key, value)
|
|
|
|
|
map.erase(10583);
|
2022-12-13 01:36:40 +08:00
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
=== "Python"
|
|
|
|
|
|
|
|
|
|
```python title="hash_map.py"
|
2023-04-09 05:30:02 +08:00
|
|
|
|
# 初始化哈希表
|
2023-03-12 18:49:52 +08:00
|
|
|
|
mapp: Dict = {}
|
2023-01-30 03:55:13 +08:00
|
|
|
|
|
2023-04-09 05:30:02 +08:00
|
|
|
|
# 添加操作
|
2022-12-14 17:18:32 +08:00
|
|
|
|
# 在哈希表中添加键值对 (key, value)
|
2022-12-15 09:43:20 +08:00
|
|
|
|
mapp[12836] = "小哈"
|
|
|
|
|
mapp[15937] = "小啰"
|
|
|
|
|
mapp[16750] = "小算"
|
|
|
|
|
mapp[13276] = "小法"
|
|
|
|
|
mapp[10583] = "小鸭"
|
2023-01-30 03:55:13 +08:00
|
|
|
|
|
2023-04-09 05:30:02 +08:00
|
|
|
|
# 查询操作
|
2022-12-14 17:18:32 +08:00
|
|
|
|
# 向哈希表输入键 key ,得到值 value
|
2023-03-12 18:49:52 +08:00
|
|
|
|
name: str = mapp[15937]
|
2023-01-30 03:55:13 +08:00
|
|
|
|
|
2023-04-09 05:30:02 +08:00
|
|
|
|
# 删除操作
|
2022-12-14 17:18:32 +08:00
|
|
|
|
# 在哈希表中删除键值对 (key, value)
|
2022-12-15 09:43:20 +08:00
|
|
|
|
mapp.pop(10583)
|
2022-12-13 01:36:40 +08:00
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
=== "Go"
|
|
|
|
|
|
2023-01-08 19:41:05 +08:00
|
|
|
|
```go title="hash_map.go"
|
|
|
|
|
/* 初始化哈希表 */
|
|
|
|
|
mapp := make(map[int]string)
|
2023-01-30 03:55:13 +08:00
|
|
|
|
|
2023-01-08 19:41:05 +08:00
|
|
|
|
/* 添加操作 */
|
|
|
|
|
// 在哈希表中添加键值对 (key, value)
|
|
|
|
|
mapp[12836] = "小哈"
|
|
|
|
|
mapp[15937] = "小啰"
|
|
|
|
|
mapp[16750] = "小算"
|
|
|
|
|
mapp[13276] = "小法"
|
|
|
|
|
mapp[10583] = "小鸭"
|
2023-01-30 03:55:13 +08:00
|
|
|
|
|
2023-01-08 19:41:05 +08:00
|
|
|
|
/* 查询操作 */
|
|
|
|
|
// 向哈希表输入键 key ,得到值 value
|
|
|
|
|
name := mapp[15937]
|
2023-01-30 03:55:13 +08:00
|
|
|
|
|
2023-01-08 19:41:05 +08:00
|
|
|
|
/* 删除操作 */
|
|
|
|
|
// 在哈希表中删除键值对 (key, value)
|
|
|
|
|
delete(mapp, 10583)
|
2022-12-13 01:36:40 +08:00
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
=== "JavaScript"
|
|
|
|
|
|
2023-02-08 04:27:55 +08:00
|
|
|
|
```javascript title="hash_map.js"
|
2022-12-26 23:03:12 +08:00
|
|
|
|
/* 初始化哈希表 */
|
|
|
|
|
const map = new ArrayHashMap();
|
|
|
|
|
/* 添加操作 */
|
|
|
|
|
// 在哈希表中添加键值对 (key, value)
|
|
|
|
|
map.set(12836, '小哈');
|
|
|
|
|
map.set(15937, '小啰');
|
|
|
|
|
map.set(16750, '小算');
|
|
|
|
|
map.set(13276, '小法');
|
|
|
|
|
map.set(10583, '小鸭');
|
2023-01-30 03:55:13 +08:00
|
|
|
|
|
2022-12-26 23:03:12 +08:00
|
|
|
|
/* 查询操作 */
|
|
|
|
|
// 向哈希表输入键 key ,得到值 value
|
|
|
|
|
let name = map.get(15937);
|
2023-01-30 03:55:13 +08:00
|
|
|
|
|
2022-12-26 23:03:12 +08:00
|
|
|
|
/* 删除操作 */
|
|
|
|
|
// 在哈希表中删除键值对 (key, value)
|
|
|
|
|
map.delete(10583);
|
2022-12-13 01:36:40 +08:00
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
=== "TypeScript"
|
|
|
|
|
|
|
|
|
|
```typescript title="hash_map.ts"
|
2022-12-20 09:02:21 +08:00
|
|
|
|
/* 初始化哈希表 */
|
2022-12-22 07:44:44 +08:00
|
|
|
|
const map = new Map<number, string>();
|
2022-12-20 09:02:21 +08:00
|
|
|
|
/* 添加操作 */
|
|
|
|
|
// 在哈希表中添加键值对 (key, value)
|
2022-12-22 07:44:44 +08:00
|
|
|
|
map.set(12836, '小哈');
|
|
|
|
|
map.set(15937, '小啰');
|
|
|
|
|
map.set(16750, '小算');
|
|
|
|
|
map.set(13276, '小法');
|
|
|
|
|
map.set(10583, '小鸭');
|
2022-12-20 09:02:21 +08:00
|
|
|
|
console.info('\n添加完成后,哈希表为\nKey -> Value');
|
2022-12-22 07:44:44 +08:00
|
|
|
|
console.info(map);
|
2023-01-30 03:55:13 +08:00
|
|
|
|
|
2022-12-20 09:02:21 +08:00
|
|
|
|
/* 查询操作 */
|
|
|
|
|
// 向哈希表输入键 key ,得到值 value
|
|
|
|
|
let name = map.get(15937);
|
|
|
|
|
console.info('\n输入学号 15937 ,查询到姓名 ' + name);
|
2023-01-30 03:55:13 +08:00
|
|
|
|
|
2022-12-20 09:02:21 +08:00
|
|
|
|
/* 删除操作 */
|
|
|
|
|
// 在哈希表中删除键值对 (key, value)
|
2022-12-22 07:44:44 +08:00
|
|
|
|
map.delete(10583);
|
2022-12-20 09:02:21 +08:00
|
|
|
|
console.info('\n删除 10583 后,哈希表为\nKey -> Value');
|
2022-12-22 07:44:44 +08:00
|
|
|
|
console.info(map);
|
2022-12-13 01:36:40 +08:00
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
=== "C"
|
|
|
|
|
|
|
|
|
|
```c title="hash_map.c"
|
2023-04-18 20:21:31 +08:00
|
|
|
|
// C 未提供内置哈希表
|
2022-12-13 01:36:40 +08:00
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
=== "C#"
|
|
|
|
|
|
|
|
|
|
```csharp title="hash_map.cs"
|
2022-12-23 15:42:02 +08:00
|
|
|
|
/* 初始化哈希表 */
|
|
|
|
|
Dictionary<int, String> map = new ();
|
2023-01-30 03:55:13 +08:00
|
|
|
|
|
2022-12-23 15:42:02 +08:00
|
|
|
|
/* 添加操作 */
|
|
|
|
|
// 在哈希表中添加键值对 (key, value)
|
|
|
|
|
map.Add(12836, "小哈");
|
|
|
|
|
map.Add(15937, "小啰");
|
|
|
|
|
map.Add(16750, "小算");
|
|
|
|
|
map.Add(13276, "小法");
|
|
|
|
|
map.Add(10583, "小鸭");
|
2023-01-30 03:55:13 +08:00
|
|
|
|
|
2022-12-23 15:42:02 +08:00
|
|
|
|
/* 查询操作 */
|
|
|
|
|
// 向哈希表输入键 key ,得到值 value
|
|
|
|
|
String name = map[15937];
|
2023-01-30 03:55:13 +08:00
|
|
|
|
|
2022-12-23 15:42:02 +08:00
|
|
|
|
/* 删除操作 */
|
|
|
|
|
// 在哈希表中删除键值对 (key, value)
|
|
|
|
|
map.Remove(10583);
|
2022-12-13 01:36:40 +08:00
|
|
|
|
```
|
2022-12-05 02:37:16 +08:00
|
|
|
|
|
2023-01-08 19:41:05 +08:00
|
|
|
|
=== "Swift"
|
|
|
|
|
|
|
|
|
|
```swift title="hash_map.swift"
|
2023-01-17 00:02:23 +08:00
|
|
|
|
/* 初始化哈希表 */
|
|
|
|
|
var map: [Int: String] = [:]
|
2023-01-30 03:55:13 +08:00
|
|
|
|
|
2023-01-17 00:02:23 +08:00
|
|
|
|
/* 添加操作 */
|
|
|
|
|
// 在哈希表中添加键值对 (key, value)
|
|
|
|
|
map[12836] = "小哈"
|
|
|
|
|
map[15937] = "小啰"
|
|
|
|
|
map[16750] = "小算"
|
|
|
|
|
map[13276] = "小法"
|
|
|
|
|
map[10583] = "小鸭"
|
2023-01-30 03:55:13 +08:00
|
|
|
|
|
2023-01-17 00:02:23 +08:00
|
|
|
|
/* 查询操作 */
|
|
|
|
|
// 向哈希表输入键 key ,得到值 value
|
|
|
|
|
let name = map[15937]!
|
2023-01-30 03:55:13 +08:00
|
|
|
|
|
2023-01-17 00:02:23 +08:00
|
|
|
|
/* 删除操作 */
|
|
|
|
|
// 在哈希表中删除键值对 (key, value)
|
|
|
|
|
map.removeValue(forKey: 10583)
|
2023-01-08 19:41:05 +08:00
|
|
|
|
```
|
|
|
|
|
|
2023-02-01 22:03:04 +08:00
|
|
|
|
=== "Zig"
|
|
|
|
|
|
|
|
|
|
```zig title="hash_map.zig"
|
|
|
|
|
|
|
|
|
|
```
|
|
|
|
|
|
2023-06-02 02:40:26 +08:00
|
|
|
|
=== "Dart"
|
|
|
|
|
|
|
|
|
|
```dart title="hash_map.dart"
|
2023-06-02 14:56:29 +08:00
|
|
|
|
/* 初始化哈希表 */
|
|
|
|
|
Map<int, String> map = {};
|
2023-06-02 02:40:26 +08:00
|
|
|
|
|
2023-06-02 14:56:29 +08:00
|
|
|
|
/* 添加操作 */
|
|
|
|
|
// 在哈希表中添加键值对 (key, value)
|
|
|
|
|
map[12836] = "小哈";
|
|
|
|
|
map[15937] = "小啰";
|
|
|
|
|
map[16750] = "小算";
|
|
|
|
|
map[13276] = "小法";
|
|
|
|
|
map[10583] = "小鸭";
|
|
|
|
|
|
|
|
|
|
/* 查询操作 */
|
|
|
|
|
// 向哈希表输入键 key ,得到值 value
|
|
|
|
|
String name = map[15937];
|
|
|
|
|
|
|
|
|
|
/* 删除操作 */
|
|
|
|
|
// 在哈希表中删除键值对 (key, value)
|
|
|
|
|
map.remove(10583);
|
2023-06-02 02:40:26 +08:00
|
|
|
|
```
|
|
|
|
|
|
2023-06-16 21:20:57 +08:00
|
|
|
|
哈希表有三种常用遍历方式:遍历键值对、遍历键和遍历值。
|
2022-12-05 02:37:16 +08:00
|
|
|
|
|
2022-12-13 01:36:40 +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"
|
2022-12-14 17:18:32 +08:00
|
|
|
|
/* 遍历哈希表 */
|
|
|
|
|
// 遍历键值对 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;
|
|
|
|
|
}
|
2022-12-13 01:36:40 +08:00
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
=== "Python"
|
|
|
|
|
|
|
|
|
|
```python title="hash_map.py"
|
2023-04-09 05:30:02 +08:00
|
|
|
|
# 遍历哈希表
|
2022-12-14 17:18:32 +08:00
|
|
|
|
# 遍历键值对 key->value
|
2022-12-15 09:43:20 +08:00
|
|
|
|
for key, value in mapp.items():
|
2022-12-14 17:18:32 +08:00
|
|
|
|
print(key, "->", value)
|
|
|
|
|
# 单独遍历键 key
|
2022-12-15 09:43:20 +08:00
|
|
|
|
for key in mapp.keys():
|
2022-12-14 17:18:32 +08:00
|
|
|
|
print(key)
|
|
|
|
|
# 单独遍历值 value
|
2022-12-15 09:43:20 +08:00
|
|
|
|
for value in mapp.values():
|
2022-12-14 17:18:32 +08:00
|
|
|
|
print(value)
|
2022-12-13 01:36:40 +08:00
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
=== "Go"
|
|
|
|
|
|
2022-12-14 17:18:32 +08:00
|
|
|
|
```go title="hash_map_test.go"
|
|
|
|
|
/* 遍历哈希表 */
|
|
|
|
|
// 遍历键值对 key->value
|
2022-12-15 09:43:20 +08:00
|
|
|
|
for key, value := range mapp {
|
2022-12-14 17:18:32 +08:00
|
|
|
|
fmt.Println(key, "->", value)
|
|
|
|
|
}
|
|
|
|
|
// 单独遍历键 key
|
2022-12-15 09:43:20 +08:00
|
|
|
|
for key := range mapp {
|
2022-12-14 17:18:32 +08:00
|
|
|
|
fmt.Println(key)
|
|
|
|
|
}
|
|
|
|
|
// 单独遍历值 value
|
2022-12-15 09:43:20 +08:00
|
|
|
|
for _, value := range mapp {
|
2022-12-14 17:18:32 +08:00
|
|
|
|
fmt.Println(value)
|
|
|
|
|
}
|
2022-12-13 01:36:40 +08:00
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
=== "JavaScript"
|
|
|
|
|
|
2023-02-08 04:27:55 +08:00
|
|
|
|
```javascript title="hash_map.js"
|
2022-12-26 23:03:12 +08:00
|
|
|
|
/* 遍历哈希表 */
|
2023-02-13 16:29:31 +08:00
|
|
|
|
console.info('\n遍历键值对 Key->Value');
|
|
|
|
|
for (const [k, v] of map.entries()) {
|
|
|
|
|
console.info(k + ' -> ' + v);
|
2022-12-26 23:03:12 +08:00
|
|
|
|
}
|
2023-02-13 16:29:31 +08:00
|
|
|
|
console.info('\n单独遍历键 Key');
|
|
|
|
|
for (const k of map.keys()) {
|
|
|
|
|
console.info(k);
|
2022-12-26 23:03:12 +08:00
|
|
|
|
}
|
2023-02-13 16:29:31 +08:00
|
|
|
|
console.info('\n单独遍历值 Value');
|
|
|
|
|
for (const v of map.values()) {
|
|
|
|
|
console.info(v);
|
2022-12-26 23:03:12 +08:00
|
|
|
|
}
|
2022-12-13 01:36:40 +08:00
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
=== "TypeScript"
|
|
|
|
|
|
|
|
|
|
```typescript title="hash_map.ts"
|
2022-12-20 09:02:21 +08:00
|
|
|
|
/* 遍历哈希表 */
|
|
|
|
|
console.info('\n遍历键值对 Key->Value');
|
2022-12-22 07:44:44 +08:00
|
|
|
|
for (const [k, v] of map.entries()) {
|
|
|
|
|
console.info(k + ' -> ' + v);
|
2022-12-20 09:02:21 +08:00
|
|
|
|
}
|
|
|
|
|
console.info('\n单独遍历键 Key');
|
2022-12-22 07:44:44 +08:00
|
|
|
|
for (const k of map.keys()) {
|
|
|
|
|
console.info(k);
|
2022-12-20 09:02:21 +08:00
|
|
|
|
}
|
|
|
|
|
console.info('\n单独遍历值 Value');
|
2022-12-22 07:44:44 +08:00
|
|
|
|
for (const v of map.values()) {
|
|
|
|
|
console.info(v);
|
2022-12-20 09:02:21 +08:00
|
|
|
|
}
|
2022-12-13 01:36:40 +08:00
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
=== "C"
|
|
|
|
|
|
|
|
|
|
```c title="hash_map.c"
|
2023-04-18 20:21:31 +08:00
|
|
|
|
// C 未提供内置哈希表
|
2022-12-13 01:36:40 +08:00
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
=== "C#"
|
|
|
|
|
|
|
|
|
|
```csharp title="hash_map.cs"
|
2022-12-23 15:42:02 +08:00
|
|
|
|
/* 遍历哈希表 */
|
|
|
|
|
// 遍历键值对 Key->Value
|
2023-03-01 03:17:07 +08:00
|
|
|
|
foreach (var kv in map)
|
|
|
|
|
{
|
2022-12-23 15:42:02 +08:00
|
|
|
|
Console.WriteLine(kv.Key + " -> " + kv.Value);
|
|
|
|
|
}
|
|
|
|
|
// 单独遍历键 key
|
2023-03-01 03:17:07 +08:00
|
|
|
|
foreach (int key in map.Keys)
|
|
|
|
|
{
|
2022-12-23 15:42:02 +08:00
|
|
|
|
Console.WriteLine(key);
|
|
|
|
|
}
|
|
|
|
|
// 单独遍历值 value
|
2023-03-01 03:17:07 +08:00
|
|
|
|
foreach (String val in map.Values)
|
|
|
|
|
{
|
2022-12-23 15:42:02 +08:00
|
|
|
|
Console.WriteLine(val);
|
|
|
|
|
}
|
2022-12-13 01:36:40 +08:00
|
|
|
|
```
|
2022-12-05 02:37:16 +08:00
|
|
|
|
|
2023-01-08 19:41:05 +08:00
|
|
|
|
=== "Swift"
|
|
|
|
|
|
|
|
|
|
```swift title="hash_map.swift"
|
2023-01-17 00:02:23 +08:00
|
|
|
|
/* 遍历哈希表 */
|
|
|
|
|
// 遍历键值对 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)
|
|
|
|
|
}
|
2023-01-08 19:41:05 +08:00
|
|
|
|
```
|
|
|
|
|
|
2023-02-01 22:03:04 +08:00
|
|
|
|
=== "Zig"
|
|
|
|
|
|
|
|
|
|
```zig title="hash_map.zig"
|
|
|
|
|
|
|
|
|
|
```
|
|
|
|
|
|
2023-06-02 02:40:26 +08:00
|
|
|
|
=== "Dart"
|
|
|
|
|
|
|
|
|
|
```dart title="hash_map.dart"
|
2023-06-02 14:56:29 +08:00
|
|
|
|
/* 遍历哈希表 */
|
|
|
|
|
// 遍历键值对 Key->Value
|
|
|
|
|
map.forEach((key, value) {
|
|
|
|
|
print('$key -> $value');
|
|
|
|
|
});
|
2023-06-02 02:40:26 +08:00
|
|
|
|
|
2023-06-02 14:56:29 +08:00
|
|
|
|
// 单独遍历键 Key
|
|
|
|
|
map.keys.forEach((key) {
|
|
|
|
|
print(key);
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
// 单独遍历值 Value
|
|
|
|
|
map.values.forEach((value) {
|
|
|
|
|
print(value);
|
|
|
|
|
});
|
2023-06-02 02:40:26 +08:00
|
|
|
|
```
|
|
|
|
|
|
2023-06-15 01:53:52 +08:00
|
|
|
|
## 哈希表简单实现
|
2023-03-15 03:06:52 +08:00
|
|
|
|
|
2023-06-16 21:20:57 +08:00
|
|
|
|
我们先考虑最简单的情况,**仅用一个数组来实现哈希表**。在哈希表中,我们将数组中的每个空位称为「桶 Bucket」,每个桶可存储一个键值对。因此,查询操作就是找到 `key` 对应的桶,并在桶中获取 `value` 。
|
2022-12-05 02:37:16 +08:00
|
|
|
|
|
2023-06-16 21:20:57 +08:00
|
|
|
|
那么,如何基于 `key` 来定位对应的桶呢?这是通过「哈希函数 Hash Function」实现的。哈希函数的作用是将一个较大的输入空间映射到一个较小的输出空间。在哈希表中,输入空间是所有 `key` ,输出空间是所有桶(数组索引)。换句话说,输入一个 `key` ,**我们可以通过哈希函数得到该 `key` 对应的键值对在数组中的存储位置**。
|
2022-12-05 02:37:16 +08:00
|
|
|
|
|
2023-06-16 21:20:57 +08:00
|
|
|
|
输入一个 `key` ,哈希函数的计算过程分为两步:首先,通过哈希算法 `hash()` 计算得到哈希值;接下来,将哈希值对桶数量(数组长度)`capacity` 取模,从而获取该 `key` 对应的数组索引 `index` 。
|
2022-12-05 02:37:16 +08:00
|
|
|
|
|
2023-06-16 21:20:57 +08:00
|
|
|
|
```shell
|
|
|
|
|
index = hash(key) % capacity
|
|
|
|
|
```
|
2022-12-05 02:37:16 +08:00
|
|
|
|
|
2023-06-16 21:20:57 +08:00
|
|
|
|
随后,我们就可以利用 `index` 在哈希表中访问对应的桶,从而获取 `value` 。
|
2023-06-15 01:53:52 +08:00
|
|
|
|
|
2023-06-16 21:20:57 +08:00
|
|
|
|
设数组长度 `capacity = 100` 、哈希算法 `hash(key) = key` ,易得哈希函数为 `key % 100` 。下图以 `key` 学号和 `value` 姓名为例,展示了哈希函数的工作原理。
|
2023-05-18 20:27:58 +08:00
|
|
|
|
|
2023-03-16 00:05:46 +08:00
|
|
|
|
![哈希函数工作原理](hash_map.assets/hash_function.png)
|
2022-12-05 02:37:16 +08:00
|
|
|
|
|
2023-06-15 01:53:52 +08:00
|
|
|
|
以下代码给出了一个简单哈希表实现。其中,我们将 `key` 和 `value` 封装成一个类 `Pair` ,以表示键值对。
|
|
|
|
|
|
2022-12-13 01:36:40 +08:00
|
|
|
|
=== "Java"
|
|
|
|
|
|
|
|
|
|
```java title="array_hash_map.java"
|
2023-06-14 02:01:06 +08:00
|
|
|
|
[class]{Pair}-[func]{}
|
2023-02-07 04:43:52 +08:00
|
|
|
|
|
|
|
|
|
[class]{ArrayHashMap}-[func]{}
|
2022-12-13 01:36:40 +08:00
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
=== "C++"
|
|
|
|
|
|
|
|
|
|
```cpp title="array_hash_map.cpp"
|
2023-06-14 02:01:06 +08:00
|
|
|
|
[class]{Pair}-[func]{}
|
2023-02-08 04:17:26 +08:00
|
|
|
|
|
|
|
|
|
[class]{ArrayHashMap}-[func]{}
|
2022-12-13 01:36:40 +08:00
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
=== "Python"
|
|
|
|
|
|
|
|
|
|
```python title="array_hash_map.py"
|
2023-06-14 02:01:06 +08:00
|
|
|
|
[class]{Pair}-[func]{}
|
2023-02-06 23:23:21 +08:00
|
|
|
|
|
|
|
|
|
[class]{ArrayHashMap}-[func]{}
|
2022-12-13 01:36:40 +08:00
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
=== "Go"
|
|
|
|
|
|
|
|
|
|
```go title="array_hash_map.go"
|
2023-06-14 02:01:06 +08:00
|
|
|
|
[class]{pair}-[func]{}
|
2023-02-09 04:45:06 +08:00
|
|
|
|
|
|
|
|
|
[class]{arrayHashMap}-[func]{}
|
2022-12-13 01:36:40 +08:00
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
=== "JavaScript"
|
|
|
|
|
|
2023-02-08 04:27:55 +08:00
|
|
|
|
```javascript title="array_hash_map.js"
|
2023-06-14 02:01:06 +08:00
|
|
|
|
[class]{Pair}-[func]{}
|
2023-02-08 19:45:06 +08:00
|
|
|
|
|
|
|
|
|
[class]{ArrayHashMap}-[func]{}
|
2022-12-13 01:36:40 +08:00
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
=== "TypeScript"
|
|
|
|
|
|
|
|
|
|
```typescript title="array_hash_map.ts"
|
2023-06-14 02:01:06 +08:00
|
|
|
|
[class]{Pair}-[func]{}
|
2023-02-08 19:45:06 +08:00
|
|
|
|
|
|
|
|
|
[class]{ArrayHashMap}-[func]{}
|
2022-12-13 01:36:40 +08:00
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
=== "C"
|
|
|
|
|
|
|
|
|
|
```c title="array_hash_map.c"
|
2023-06-14 02:01:06 +08:00
|
|
|
|
[class]{pair}-[func]{}
|
2023-02-11 18:22:27 +08:00
|
|
|
|
|
|
|
|
|
[class]{arrayHashMap}-[func]{}
|
2022-12-13 01:36:40 +08:00
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
=== "C#"
|
|
|
|
|
|
|
|
|
|
```csharp title="array_hash_map.cs"
|
2023-06-14 02:01:06 +08:00
|
|
|
|
[class]{Pair}-[func]{}
|
2023-02-08 22:18:02 +08:00
|
|
|
|
|
|
|
|
|
[class]{ArrayHashMap}-[func]{}
|
2022-12-13 01:36:40 +08:00
|
|
|
|
```
|
2022-12-05 02:37:16 +08:00
|
|
|
|
|
2023-01-08 19:41:05 +08:00
|
|
|
|
=== "Swift"
|
|
|
|
|
|
|
|
|
|
```swift title="array_hash_map.swift"
|
2023-07-01 20:39:55 +08:00
|
|
|
|
/* 键值对 */
|
2023-07-01 22:37:26 +08:00
|
|
|
|
class Pair {
|
|
|
|
|
var key: Int
|
|
|
|
|
var val: String
|
|
|
|
|
|
|
|
|
|
init(key: Int, val: String) {
|
2023-07-01 20:39:55 +08:00
|
|
|
|
self.key = key
|
|
|
|
|
self.val = val
|
|
|
|
|
}
|
|
|
|
|
}
|
2023-02-08 20:30:05 +08:00
|
|
|
|
|
|
|
|
|
[class]{ArrayHashMap}-[func]{}
|
2023-01-08 19:41:05 +08:00
|
|
|
|
```
|
|
|
|
|
|
2023-02-01 22:03:04 +08:00
|
|
|
|
=== "Zig"
|
|
|
|
|
|
|
|
|
|
```zig title="array_hash_map.zig"
|
2023-06-14 02:01:06 +08:00
|
|
|
|
[class]{Pair}-[func]{}
|
2023-06-02 02:40:26 +08:00
|
|
|
|
|
|
|
|
|
[class]{ArrayHashMap}-[func]{}
|
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
=== "Dart"
|
|
|
|
|
|
|
|
|
|
```dart title="array_hash_map.dart"
|
2023-06-14 02:01:06 +08:00
|
|
|
|
[class]{Pair}-[func]{}
|
2023-02-01 22:03:04 +08:00
|
|
|
|
|
2023-02-09 22:57:25 +08:00
|
|
|
|
[class]{ArrayHashMap}-[func]{}
|
2023-02-01 22:03:04 +08:00
|
|
|
|
```
|
|
|
|
|
|
2023-06-16 21:20:57 +08:00
|
|
|
|
## 哈希冲突与扩容
|
2022-12-05 02:37:16 +08:00
|
|
|
|
|
2023-06-25 21:11:24 +08:00
|
|
|
|
本质上看,哈希函数的作用是将输入空间(`key` 范围)映射到输出空间(数组索引范围),而输入空间往往远大于输出空间。因此,**理论上一定存在“多个输入对应相同输出”的情况**。
|
2023-06-15 01:53:52 +08:00
|
|
|
|
|
2023-06-16 21:20:57 +08:00
|
|
|
|
对于上述示例中的哈希函数,当输入的 `key` 后两位相同时,哈希函数的输出结果也相同。例如,查询学号为 12836 和 20336 的两个学生时,我们得到:
|
2022-12-12 02:11:42 +08:00
|
|
|
|
|
2023-06-16 21:20:57 +08:00
|
|
|
|
```shell
|
|
|
|
|
12386 % 100 = 36
|
|
|
|
|
20386 % 100 = 36
|
|
|
|
|
```
|
2022-12-12 02:11:42 +08:00
|
|
|
|
|
2023-06-16 21:20:57 +08:00
|
|
|
|
如下图所示,两个学号指向了同一个姓名,这显然是不对的。我们将这种多个输入对应同一输出的情况称为「哈希冲突 Hash Collision」。
|
2022-12-06 01:00:21 +08:00
|
|
|
|
|
2023-02-26 18:18:34 +08:00
|
|
|
|
![哈希冲突示例](hash_map.assets/hash_collision.png)
|
2023-06-16 21:20:57 +08:00
|
|
|
|
|
|
|
|
|
容易想到,哈希表容量 $n$ 越大,多个 `key` 被分配到同一个桶中的概率就越低,冲突就越少。因此,**我们可以通过扩容哈希表来减少哈希冲突**。如下图所示,扩容前键值对 `(136, A)` 和 `(236, D)` 发生冲突,扩容后冲突消失。
|
|
|
|
|
|
|
|
|
|
![哈希表扩容](hash_map.assets/hash_table_reshash.png)
|
|
|
|
|
|
|
|
|
|
类似于数组扩容,哈希表扩容需将所有键值对从原哈希表迁移至新哈希表,非常耗时。并且由于哈希表容量 `capacity` 改变,我们需要重新计算所有键值对的存储位置,进一步提高了扩容过程的计算开销。因此,编程语言通常会预留足够大的哈希表容量,防止频繁扩容。
|
|
|
|
|
|
|
|
|
|
在哈希表中,「负载因子 Load Factor」是一个重要概念,其定义为哈希表的元素数量除以桶数量,为了衡量哈希冲突的严重程度,**也常被作为哈希表扩容的触发条件**。例如在 Java 中,当负载因子超过 $0.75$ 时,系统会将哈希表容量扩展为原先的 $2$ 倍。
|