2022-12-05 02:37:16 +08:00
|
|
|
|
---
|
|
|
|
|
comments: true
|
|
|
|
|
---
|
|
|
|
|
|
2023-01-31 03:37:50 +08:00
|
|
|
|
# 6.1. 哈希表
|
2022-12-05 02:37:16 +08:00
|
|
|
|
|
2022-12-12 02:11:42 +08:00
|
|
|
|
哈希表通过建立「键 key」和「值 value」之间的映射,实现高效的元素查找。具体地,输入一个 key ,在哈希表中查询并获取 value ,时间复杂度为 $O(1)$ 。
|
2022-12-05 02:37:16 +08:00
|
|
|
|
|
2022-12-21 01:37:58 +08:00
|
|
|
|
例如,给定一个包含 $n$ 个学生的数据库,每个学生有“姓名 `name` ”和“学号 `id` ”两项数据,希望实现一个查询功能:**输入一个学号,返回对应的姓名**,则可以使用哈希表实现。
|
2022-12-06 01:00:21 +08:00
|
|
|
|
|
|
|
|
|
![hash_map](hash_map.assets/hash_map.png)
|
|
|
|
|
|
|
|
|
|
<p align="center"> Fig. 哈希表抽象表示 </p>
|
|
|
|
|
|
2023-01-31 03:37:50 +08:00
|
|
|
|
## 6.1.1. 哈希表效率
|
2022-12-06 01:00:21 +08:00
|
|
|
|
|
|
|
|
|
除了哈希表之外,还可以使用以下数据结构来实现上述查询功能:
|
|
|
|
|
|
2023-01-09 22:39:30 +08:00
|
|
|
|
1. **无序数组**:每个元素为 `[学号, 姓名]` ;
|
|
|
|
|
2. **有序数组**:将 `1.` 中的数组按照学号从小到大排序;
|
|
|
|
|
3. **链表**:每个结点的值为 `[学号, 姓名]` ;
|
|
|
|
|
4. **二叉搜索树**:每个结点的值为 `[学号, 姓名]` ,根据学号大小来构建树;
|
2022-12-06 01:00:21 +08:00
|
|
|
|
|
|
|
|
|
使用上述方法,各项操作的时间复杂度如下表所示(在此不做赘述,详解可见 [二叉搜索树章节](https://www.hello-algo.com/chapter_tree/binary_search_tree/#_6))。无论是查找元素、还是增删元素,哈希表的时间复杂度都是 $O(1)$ ,全面胜出!
|
|
|
|
|
|
|
|
|
|
<div class="center-table" markdown>
|
|
|
|
|
|
|
|
|
|
| | 无序数组 | 有序数组 | 链表 | 二叉搜索树 | 哈希表 |
|
|
|
|
|
| -------- | -------- | ----------- | ------ | ----------- | ------ |
|
|
|
|
|
| 查找元素 | $O(n)$ | $O(\log n)$ | $O(n)$ | $O(\log n)$ | $O(1)$ |
|
|
|
|
|
| 插入元素 | $O(1)$ | $O(n)$ | $O(1)$ | $O(\log n)$ | $O(1)$ |
|
|
|
|
|
| 删除元素 | $O(n)$ | $O(n)$ | $O(n)$ | $O(\log n)$ | $O(1)$ |
|
|
|
|
|
|
|
|
|
|
</div>
|
2022-12-05 02:37:16 +08:00
|
|
|
|
|
2023-01-31 03:37:50 +08:00
|
|
|
|
## 6.1.2. 哈希表常用操作
|
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"
|
2022-12-15 09:43:20 +08:00
|
|
|
|
""" 初始化哈希表 """
|
|
|
|
|
mapp = {}
|
2023-01-30 03:55:13 +08:00
|
|
|
|
|
2022-12-15 09:43:20 +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
|
|
|
|
|
2022-12-15 09:43:20 +08:00
|
|
|
|
""" 查询操作 """
|
2022-12-14 17:18:32 +08:00
|
|
|
|
# 向哈希表输入键 key ,得到值 value
|
2022-12-15 09:43:20 +08:00
|
|
|
|
name = mapp[15937]
|
2023-01-30 03:55:13 +08:00
|
|
|
|
|
2022-12-15 09:43:20 +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-01-30 03:55:13 +08:00
|
|
|
|
|
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"
|
|
|
|
|
|
|
|
|
|
```
|
|
|
|
|
|
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"
|
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
|
|
|
|
/* 遍历哈希表 */
|
|
|
|
|
// 遍历键值对 key->value
|
|
|
|
|
for (const entry of map.entries()) {
|
|
|
|
|
if (!entry) continue;
|
|
|
|
|
console.info(entry.key + ' -> ' + entry.val);
|
|
|
|
|
}
|
|
|
|
|
// 单独遍历键 key
|
|
|
|
|
for (const key of map.keys()) {
|
|
|
|
|
console.info(key);
|
|
|
|
|
}
|
|
|
|
|
// 单独遍历值 value
|
|
|
|
|
for (const val of map.values()) {
|
|
|
|
|
console.info(val);
|
|
|
|
|
}
|
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-01-30 03:55:13 +08:00
|
|
|
|
|
2022-12-13 01:36:40 +08:00
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
=== "C#"
|
|
|
|
|
|
|
|
|
|
```csharp title="hash_map.cs"
|
2022-12-23 15:42:02 +08:00
|
|
|
|
/* 遍历哈希表 */
|
|
|
|
|
// 遍历键值对 Key->Value
|
|
|
|
|
foreach (var kv in map) {
|
|
|
|
|
Console.WriteLine(kv.Key + " -> " + kv.Value);
|
|
|
|
|
}
|
|
|
|
|
// 单独遍历键 key
|
|
|
|
|
foreach (int key in map.Keys) {
|
|
|
|
|
Console.WriteLine(key);
|
|
|
|
|
}
|
|
|
|
|
// 单独遍历值 value
|
|
|
|
|
foreach (String val in map.Values) {
|
|
|
|
|
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-01-31 03:37:50 +08:00
|
|
|
|
## 6.1.3. 哈希函数
|
2022-12-05 02:37:16 +08:00
|
|
|
|
|
|
|
|
|
哈希表中存储元素的数据结构被称为「桶 Bucket」,底层实现可能是数组、链表、二叉树(红黑树),或是它们的组合。
|
|
|
|
|
|
2022-12-12 02:11:42 +08:00
|
|
|
|
最简单地,**我们可以仅用一个「数组」来实现哈希表**。首先,将所有 value 放入数组中,那么每个 value 在数组中都有唯一的「索引」。显然,访问 value 需要给定索引,而为了 **建立 key 和索引之间的映射关系**,我们需要使用「哈希函数 Hash Function」。
|
2022-12-05 02:37:16 +08:00
|
|
|
|
|
2022-12-12 02:11:42 +08:00
|
|
|
|
设数组为 `bucket` ,哈希函数为 `f(x)` ,输入键为 `key` 。那么获取 value 的步骤为:
|
2022-12-05 02:37:16 +08:00
|
|
|
|
|
|
|
|
|
1. 通过哈希函数计算出索引,即 `index = f(key)` ;
|
|
|
|
|
2. 通过索引在数组中获取值,即 `value = bucket[index]` ;
|
|
|
|
|
|
2022-12-12 02:11:42 +08:00
|
|
|
|
以上述学生数据 `key 学号 -> value 姓名` 为例,我们可以将「哈希函数」设计为
|
2022-12-05 02:37:16 +08:00
|
|
|
|
|
|
|
|
|
$$
|
2022-12-06 01:00:21 +08:00
|
|
|
|
f(x) = x \% 100
|
2022-12-05 02:37:16 +08:00
|
|
|
|
$$
|
|
|
|
|
|
2022-12-06 01:00:21 +08:00
|
|
|
|
![hash_function](hash_map.assets/hash_function.png)
|
|
|
|
|
|
|
|
|
|
<p align="center"> Fig. 哈希函数 </p>
|
2022-12-05 02:37:16 +08:00
|
|
|
|
|
2022-12-13 01:36:40 +08:00
|
|
|
|
=== "Java"
|
|
|
|
|
|
|
|
|
|
```java title="array_hash_map.java"
|
2023-02-07 04:43:52 +08:00
|
|
|
|
[class]{Entry}-[func]{}
|
|
|
|
|
|
|
|
|
|
[class]{ArrayHashMap}-[func]{}
|
2022-12-13 01:36:40 +08:00
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
=== "C++"
|
|
|
|
|
|
|
|
|
|
```cpp title="array_hash_map.cpp"
|
2023-02-08 04:17:26 +08:00
|
|
|
|
[class]{Entry}-[func]{}
|
|
|
|
|
|
|
|
|
|
[class]{ArrayHashMap}-[func]{}
|
2022-12-13 01:36:40 +08:00
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
=== "Python"
|
|
|
|
|
|
|
|
|
|
```python title="array_hash_map.py"
|
2023-02-06 23:23:21 +08:00
|
|
|
|
[class]{Entry}-[func]{}
|
|
|
|
|
|
|
|
|
|
[class]{ArrayHashMap}-[func]{}
|
2022-12-13 01:36:40 +08:00
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
=== "Go"
|
|
|
|
|
|
|
|
|
|
```go title="array_hash_map.go"
|
2022-12-15 09:43:20 +08:00
|
|
|
|
/* 键值对 int->String */
|
2023-01-08 20:29:13 +08:00
|
|
|
|
type entry struct {
|
2022-12-14 17:18:32 +08:00
|
|
|
|
key int
|
|
|
|
|
val string
|
|
|
|
|
}
|
2023-01-30 03:55:13 +08:00
|
|
|
|
|
2022-12-15 09:43:20 +08:00
|
|
|
|
/* 基于数组简易实现的哈希表 */
|
2023-01-08 20:29:13 +08:00
|
|
|
|
type arrayHashMap struct {
|
|
|
|
|
bucket []*entry
|
2022-12-14 17:18:32 +08:00
|
|
|
|
}
|
2023-01-30 03:55:13 +08:00
|
|
|
|
|
2023-01-08 20:29:13 +08:00
|
|
|
|
func newArrayHashMap() *arrayHashMap {
|
2022-12-14 17:18:32 +08:00
|
|
|
|
// 初始化一个长度为 100 的桶(数组)
|
2023-01-08 20:29:13 +08:00
|
|
|
|
bucket := make([]*entry, 100)
|
|
|
|
|
return &arrayHashMap{bucket: bucket}
|
2022-12-14 17:18:32 +08:00
|
|
|
|
}
|
2023-01-30 03:55:13 +08:00
|
|
|
|
|
2022-12-15 09:43:20 +08:00
|
|
|
|
/* 哈希函数 */
|
2023-01-08 20:29:13 +08:00
|
|
|
|
func (a *arrayHashMap) hashFunc(key int) int {
|
2022-12-14 17:18:32 +08:00
|
|
|
|
index := key % 100
|
|
|
|
|
return index
|
|
|
|
|
}
|
2023-01-30 03:55:13 +08:00
|
|
|
|
|
2022-12-15 09:43:20 +08:00
|
|
|
|
/* 查询操作 */
|
2023-01-08 20:29:13 +08:00
|
|
|
|
func (a *arrayHashMap) get(key int) string {
|
2022-12-14 17:18:32 +08:00
|
|
|
|
index := a.hashFunc(key)
|
|
|
|
|
pair := a.bucket[index]
|
2022-12-15 09:43:20 +08:00
|
|
|
|
if pair == nil {
|
|
|
|
|
return "Not Found"
|
2022-12-14 17:18:32 +08:00
|
|
|
|
}
|
|
|
|
|
return pair.val
|
|
|
|
|
}
|
2023-01-30 03:55:13 +08:00
|
|
|
|
|
2022-12-15 09:43:20 +08:00
|
|
|
|
/* 添加操作 */
|
2023-01-08 20:29:13 +08:00
|
|
|
|
func (a *arrayHashMap) put(key int, val string) {
|
|
|
|
|
pair := &entry{key: key, val: val}
|
2022-12-14 17:18:32 +08:00
|
|
|
|
index := a.hashFunc(key)
|
|
|
|
|
a.bucket[index] = pair
|
|
|
|
|
}
|
2023-01-30 03:55:13 +08:00
|
|
|
|
|
2022-12-15 09:43:20 +08:00
|
|
|
|
/* 删除操作 */
|
2023-01-08 20:29:13 +08:00
|
|
|
|
func (a *arrayHashMap) remove(key int) {
|
2022-12-14 17:18:32 +08:00
|
|
|
|
index := a.hashFunc(key)
|
2023-01-08 20:29:13 +08:00
|
|
|
|
// 置为 nil ,代表删除
|
2022-12-15 09:43:20 +08:00
|
|
|
|
a.bucket[index] = nil
|
2022-12-14 17:18:32 +08:00
|
|
|
|
}
|
2022-12-13 01:36:40 +08:00
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
=== "JavaScript"
|
|
|
|
|
|
2023-02-08 04:27:55 +08:00
|
|
|
|
```javascript title="array_hash_map.js"
|
2023-02-08 19:45:06 +08:00
|
|
|
|
[class]{Entry}-[func]{}
|
|
|
|
|
|
|
|
|
|
[class]{ArrayHashMap}-[func]{}
|
2022-12-13 01:36:40 +08:00
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
=== "TypeScript"
|
|
|
|
|
|
|
|
|
|
```typescript title="array_hash_map.ts"
|
2023-02-08 19:45:06 +08:00
|
|
|
|
[class]{Entry}-[func]{}
|
|
|
|
|
|
|
|
|
|
[class]{ArrayHashMap}-[func]{}
|
2022-12-13 01:36:40 +08:00
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
=== "C"
|
|
|
|
|
|
|
|
|
|
```c title="array_hash_map.c"
|
2023-01-30 03:55:13 +08:00
|
|
|
|
|
2022-12-13 01:36:40 +08:00
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
=== "C#"
|
|
|
|
|
|
|
|
|
|
```csharp title="array_hash_map.cs"
|
2022-12-23 15:42:02 +08:00
|
|
|
|
/* 键值对 int->String */
|
|
|
|
|
class Entry
|
|
|
|
|
{
|
|
|
|
|
public int key;
|
|
|
|
|
public String val;
|
|
|
|
|
public Entry(int key, String val)
|
|
|
|
|
{
|
|
|
|
|
this.key = key;
|
|
|
|
|
this.val = val;
|
|
|
|
|
}
|
|
|
|
|
}
|
2023-01-30 03:55:13 +08:00
|
|
|
|
|
2022-12-23 15:42:02 +08:00
|
|
|
|
/* 基于数组简易实现的哈希表 */
|
|
|
|
|
class ArrayHashMap
|
|
|
|
|
{
|
|
|
|
|
private List<Entry?> bucket;
|
|
|
|
|
public ArrayHashMap()
|
|
|
|
|
{
|
|
|
|
|
// 初始化一个长度为 100 的桶(数组)
|
|
|
|
|
bucket = new ();
|
|
|
|
|
for (int i = 0; i < 100; i++)
|
|
|
|
|
{
|
|
|
|
|
bucket.Add(null);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
/* 哈希函数 */
|
|
|
|
|
private int hashFunc(int key)
|
|
|
|
|
{
|
|
|
|
|
int index = key % 100;
|
|
|
|
|
return index;
|
|
|
|
|
}
|
|
|
|
|
/* 查询操作 */
|
|
|
|
|
public String? get(int key)
|
|
|
|
|
{
|
|
|
|
|
int index = hashFunc(key);
|
|
|
|
|
Entry? pair = bucket[index];
|
|
|
|
|
if (pair == null) return null;
|
|
|
|
|
return pair.val;
|
|
|
|
|
}
|
|
|
|
|
/* 添加操作 */
|
|
|
|
|
public void put(int key, String val)
|
|
|
|
|
{
|
|
|
|
|
Entry pair = new Entry(key, val);
|
|
|
|
|
int index = hashFunc(key);
|
|
|
|
|
bucket[index]=pair;
|
|
|
|
|
}
|
|
|
|
|
/* 删除操作 */
|
|
|
|
|
public void remove(int key)
|
|
|
|
|
{
|
|
|
|
|
int index = hashFunc(key);
|
|
|
|
|
// 置为 null ,代表删除
|
|
|
|
|
bucket[index]=null;
|
|
|
|
|
}
|
|
|
|
|
}
|
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-02-08 20:30:05 +08:00
|
|
|
|
[class]{Entry}-[func]{}
|
|
|
|
|
|
|
|
|
|
[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-01-31 03:37:50 +08:00
|
|
|
|
## 6.1.4. 哈希冲突
|
2022-12-05 02:37:16 +08:00
|
|
|
|
|
2023-01-30 03:55:13 +08:00
|
|
|
|
细心的同学可能会发现,**哈希函数 $f(x) = x \% 100$ 会在某些情况下失效**。具体地,当输入的 key 后两位相同时,哈希函数的计算结果也相同,指向同一个 value 。例如,分别查询两个学号 $12836$ 和 $20336$ ,则有
|
2022-12-12 02:11:42 +08:00
|
|
|
|
|
2022-12-06 01:00:21 +08:00
|
|
|
|
$$
|
|
|
|
|
f(12836) = f(20336) = 36
|
|
|
|
|
$$
|
2022-12-12 02:11:42 +08:00
|
|
|
|
|
2023-01-30 03:55:13 +08:00
|
|
|
|
两个学号指向了同一个姓名,这明显是不对的,我们将这种现象称为「哈希冲突 Hash Collision」。如何避免哈希冲突的问题将被留在下章讨论。
|
2022-12-06 01:00:21 +08:00
|
|
|
|
|
|
|
|
|
![hash_collision](hash_map.assets/hash_collision.png)
|
|
|
|
|
|
|
|
|
|
<p align="center"> Fig. 哈希冲突 </p>
|
2022-12-05 02:37:16 +08:00
|
|
|
|
|
2022-12-06 01:00:21 +08:00
|
|
|
|
综上所述,一个优秀的「哈希函数」应该具备以下特性:
|
2022-12-05 02:37:16 +08:00
|
|
|
|
|
2022-12-06 01:00:21 +08:00
|
|
|
|
- 尽量少地发生哈希冲突;
|
|
|
|
|
- 时间复杂度 $O(1)$ ,计算尽可能高效;
|
2022-12-21 01:37:58 +08:00
|
|
|
|
- 空间使用率高,即“键值对占用空间 / 哈希表总占用空间”尽可能大;
|