Skip to content

Commit

Permalink
Update 01.Hash-Table.md
Browse files Browse the repository at this point in the history
  • Loading branch information
itcharge committed May 9, 2024
1 parent 81544f4 commit 8487306
Showing 1 changed file with 4 additions and 4 deletions.
8 changes: 4 additions & 4 deletions Contents/05.Hash-Table/01.Hash-Table.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@

哈希表的原理示例图如下所示:

![](https://qcdn.itcharge.cn/images/20220114120000.png)
![哈希表](https://qcdn.itcharge.cn/images/202405092317578.png)

在上图例子中,我们使用 $value = Hash(key) = key // 1000$ 作为哈希函数。$//$ 符号代表整除。我们以这个例子来说明一下哈希表的插入和查找策略。

Expand All @@ -25,7 +25,7 @@

比如为了查找 **「赞」** 这个字的具体意思,我们在字典中根据这个字的拼音索引 `zan`,查找到对应的页码为 $599$。然后我们就可以翻到字典的第 $599$ 页查看 **「赞」** 字相关的解释了。

![](https://qcdn.itcharge.cn/images/20220111174223.png)
![查字典](https://qcdn.itcharge.cn/images/20220111174223.png)

在这个例子中:

Expand Down Expand Up @@ -120,7 +120,7 @@ $343246_{13} = 3 \times 13^5 + 4 \times 13^4 + 3 \times 13^3 + 2 \times 13^2 + 4

使用这三种方法处理冲突的结果如下图所示:

![](https://qcdn.itcharge.cn/images/20220115162728.png)
![开放地址法](https://qcdn.itcharge.cn/images/202405092318809.png)

### 3.2 链地址法

Expand All @@ -136,7 +136,7 @@ $343246_{13} = 3 \times 13^5 + 4 \times 13^4 + 3 \times 13^3 + 2 \times 13^2 + 4

举个例子来说明如何使用链地址法处理冲突。假设现在要存入的关键字集合 $keys = [88, 60, 65, 69, 90, 39, 07, 06, 14, 44, 52, 70, 21, 45, 19, 32]$。再假定哈希函数为 $Hash(key) = key \mod 13$,哈希表的表长 $m = 13$,哈希地址范围为 $[0, m - 1]$。将这些关键字使用链地址法处理冲突,并按顺序加入哈希表中(图示为插入链表表尾位置),最终得到的哈希表如下图所示。

![](https://qcdn.itcharge.cn/images/20220115182535.png)
![链地址法](https://qcdn.itcharge.cn/images/202405092319327.png)

相对于开放地址法,采用链地址法处理冲突要多占用一些存储空间(主要是链节点占用空间)。但它可以减少在进行插入和查找具有相同哈希地址的关键字的操作过程中的平均查找长度。这是因为在链地址法中,待比较的关键字都是具有相同哈希地址的元素,而在开放地址法中,待比较的关键字不仅包含具有相同哈希地址的元素,而且还包含哈希地址不相同的元素。

Expand Down

0 comments on commit 8487306

Please sign in to comment.