Skip to content

Commit 7f43f92

Browse files
authored
Bug fixes and improvements (krahets#1152)
* Update avl_tree.md * Remove the empty space * Simplify the heading of the paperbook chapter * Update hash_map_open_addressing.go to the latest version * Improvements
1 parent 6f1ec66 commit 7f43f92

File tree

15 files changed

+83
-106
lines changed

15 files changed

+83
-106
lines changed

codes/csharp/chapter_computational_complexity/time_complexity.cs

+1-1
Original file line numberDiff line numberDiff line change
@@ -85,7 +85,7 @@ int BubbleSort(int[] nums) {
8585
int count = 0; // 计数器
8686
// 外循环:未排序区间为 [0, i]
8787
for (int i = nums.Length - 1; i > 0; i--) {
88-
// 内循环:将未排序区间 [0, i] 中的最大元素交换至该区间的最右端
88+
// 内循环:将未排序区间 [0, i] 中的最大元素交换至该区间的最右端
8989
for (int j = 0; j < i; j++) {
9090
if (nums[j] > nums[j + 1]) {
9191
// 交换 nums[j] 与 nums[j + 1]

codes/csharp/chapter_sorting/bubble_sort.cs

+2-2
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ public class bubble_sort {
1111
void BubbleSort(int[] nums) {
1212
// 外循环:未排序区间为 [0, i]
1313
for (int i = nums.Length - 1; i > 0; i--) {
14-
// 内循环:将未排序区间 [0, i] 中的最大元素交换至该区间的最右端
14+
// 内循环:将未排序区间 [0, i] 中的最大元素交换至该区间的最右端
1515
for (int j = 0; j < i; j++) {
1616
if (nums[j] > nums[j + 1]) {
1717
// 交换 nums[j] 与 nums[j + 1]
@@ -26,7 +26,7 @@ void BubbleSortWithFlag(int[] nums) {
2626
// 外循环:未排序区间为 [0, i]
2727
for (int i = nums.Length - 1; i > 0; i--) {
2828
bool flag = false; // 初始化标志位
29-
// 内循环:将未排序区间 [0, i] 中的最大元素交换至该区间的最右端
29+
// 内循环:将未排序区间 [0, i] 中的最大元素交换至该区间的最右端
3030
for (int j = 0; j < i; j++) {
3131
if (nums[j] > nums[j + 1]) {
3232
// 交换 nums[j] 与 nums[j + 1]

codes/go/chapter_hashing/hash_collision_test.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ func TestHashMapChaining(t *testing.T) {
2626
/* 查询操作 */
2727
// 向哈希表中输入键 key ,得到值 value
2828
name := hmap.get(15937)
29-
fmt.Println("\n输入学号 15937 ,查询到姓名 ", name)
29+
fmt.Println("\n输入学号 15937 ,查询到姓名", name)
3030

3131
/* 删除操作 */
3232
// 在哈希表中删除键值对 (key, value)

codes/go/chapter_hashing/hash_map_open_addressing.go

+67-84
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@ package chapter_hashing
66

77
import (
88
"fmt"
9-
"strconv"
109
)
1110

1211
/* 开放寻址哈希表 */
@@ -15,129 +14,113 @@ type hashMapOpenAddressing struct {
1514
capacity int // 哈希表容量
1615
loadThres float64 // 触发扩容的负载因子阈值
1716
extendRatio int // 扩容倍数
18-
buckets []pair // 桶数组
19-
removed pair // 删除标记
17+
buckets []*pair // 桶数组
18+
TOMBSTONE *pair // 删除标记
2019
}
2120

2221
/* 构造方法 */
2322
func newHashMapOpenAddressing() *hashMapOpenAddressing {
24-
buckets := make([]pair, 4)
2523
return &hashMapOpenAddressing{
2624
size: 0,
2725
capacity: 4,
2826
loadThres: 2.0 / 3.0,
2927
extendRatio: 2,
30-
buckets: buckets,
31-
removed: pair{
32-
key: -1,
33-
val: "-1",
34-
},
28+
buckets: make([]*pair, 4),
29+
TOMBSTONE: &pair{-1, "-1"},
3530
}
3631
}
3732

3833
/* 哈希函数 */
39-
func (m *hashMapOpenAddressing) hashFunc(key int) int {
40-
return key % m.capacity
34+
func (h *hashMapOpenAddressing) hashFunc(key int) int {
35+
return key % h.capacity // 根据键计算哈希值
4136
}
4237

4338
/* 负载因子 */
44-
func (m *hashMapOpenAddressing) loadFactor() float64 {
45-
return float64(m.size) / float64(m.capacity)
39+
func (h *hashMapOpenAddressing) loadFactor() float64 {
40+
return float64(h.size) / float64(h.capacity) // 计算当前负载因子
4641
}
4742

48-
/* 查询操作 */
49-
func (m *hashMapOpenAddressing) get(key int) string {
50-
idx := m.hashFunc(key)
51-
// 线性探测,从 index 开始向后遍历
52-
for i := 0; i < m.capacity; i++ {
53-
// 计算桶索引,越过尾部则返回头部
54-
j := (idx + i) % m.capacity
55-
// 若遇到空桶,说明无此 key ,则返回 null
56-
if m.buckets[j] == (pair{}) {
57-
return ""
43+
/* 搜索 key 对应的桶索引 */
44+
func (h *hashMapOpenAddressing) findBucket(key int) int {
45+
index := h.hashFunc(key) // 获取初始索引
46+
firstTombstone := -1 // 记录遇到的第一个TOMBSTONE的位置
47+
for h.buckets[index] != nil {
48+
if h.buckets[index].key == key {
49+
if firstTombstone != -1 {
50+
// 若之前遇到了删除标记,则将键值对移动至该索引处
51+
h.buckets[firstTombstone] = h.buckets[index]
52+
h.buckets[index] = h.TOMBSTONE
53+
return firstTombstone // 返回移动后的桶索引
54+
}
55+
return index // 返回找到的索引
5856
}
59-
// 若遇到指定 key ,则返回对应 val
60-
if m.buckets[j].key == key && m.buckets[j] != m.removed {
61-
return m.buckets[j].val
57+
if firstTombstone == -1 && h.buckets[index] == h.TOMBSTONE {
58+
firstTombstone = index // 记录遇到的首个删除标记的位置
6259
}
60+
index = (index + 1) % h.capacity // 线性探测,越过尾部则返回头部
61+
}
62+
// 若 key 不存在,则返回添加点的索引
63+
if firstTombstone != -1 {
64+
return firstTombstone
65+
}
66+
return index
67+
}
68+
69+
/* 查询操作 */
70+
func (h *hashMapOpenAddressing) get(key int) string {
71+
index := h.findBucket(key) // 搜索 key 对应的桶索引
72+
if h.buckets[index] != nil && h.buckets[index] != h.TOMBSTONE {
73+
return h.buckets[index].val // 若找到键值对,则返回对应 val
6374
}
64-
// 若未找到 key ,则返回空字符串
65-
return ""
75+
return "" // 若键值对不存在,则返回 ""
6676
}
6777

6878
/* 添加操作 */
69-
func (m *hashMapOpenAddressing) put(key int, val string) {
70-
// 当负载因子超过阈值时,执行扩容
71-
if m.loadFactor() > m.loadThres {
72-
m.extend()
79+
func (h *hashMapOpenAddressing) put(key int, val string) {
80+
if h.loadFactor() > h.loadThres {
81+
h.extend() // 当负载因子超过阈值时,执行扩容
7382
}
74-
idx := m.hashFunc(key)
75-
// 线性探测,从 index 开始向后遍历
76-
for i := 0; i < m.capacity; i++ {
77-
// 计算桶索引,越过尾部则返回头部
78-
j := (idx + i) % m.capacity
79-
// 若遇到空桶、或带有删除标记的桶,则将键值对放入该桶
80-
if m.buckets[j] == (pair{}) || m.buckets[j] == m.removed {
81-
m.buckets[j] = pair{
82-
key: key,
83-
val: val,
84-
}
85-
m.size += 1
86-
return
87-
}
88-
// 若遇到指定 key ,则更新对应 val
89-
if m.buckets[j].key == key {
90-
m.buckets[j].val = val
91-
return
92-
}
83+
index := h.findBucket(key) // 搜索 key 对应的桶索引
84+
if h.buckets[index] == nil || h.buckets[index] == h.TOMBSTONE {
85+
h.buckets[index] = &pair{key, val} // 若键值对不存在,则添加该键值对
86+
h.size++
87+
} else {
88+
h.buckets[index].val = val // 若找到键值对,则覆盖 val
9389
}
9490
}
9591

9692
/* 删除操作 */
97-
func (m *hashMapOpenAddressing) remove(key int) {
98-
idx := m.hashFunc(key)
99-
// 遍历桶,从中删除键值对
100-
// 线性探测,从 index 开始向后遍历
101-
for i := 0; i < m.capacity; i++ {
102-
// 计算桶索引,越过尾部则返回头部
103-
j := (idx + i) % m.capacity
104-
// 若遇到空桶,说明无此 key ,则直接返回
105-
if m.buckets[j] == (pair{}) {
106-
return
107-
}
108-
// 若遇到指定 key ,则标记删除并返回
109-
if m.buckets[j].key == key {
110-
m.buckets[j] = m.removed
111-
m.size -= 1
112-
}
93+
func (h *hashMapOpenAddressing) remove(key int) {
94+
index := h.findBucket(key) // 搜索 key 对应的桶索引
95+
if h.buckets[index] != nil && h.buckets[index] != h.TOMBSTONE {
96+
h.buckets[index] = h.TOMBSTONE // 若找到键值对,则用删除标记覆盖它
97+
h.size--
11398
}
11499
}
115100

116101
/* 扩容哈希表 */
117-
func (m *hashMapOpenAddressing) extend() {
118-
// 暂存原哈希表
119-
tmpBuckets := make([]pair, len(m.buckets))
120-
copy(tmpBuckets, m.buckets)
121-
122-
// 初始化扩容后的新哈希表
123-
m.capacity *= m.extendRatio
124-
m.buckets = make([]pair, m.capacity)
125-
m.size = 0
102+
func (h *hashMapOpenAddressing) extend() {
103+
oldBuckets := h.buckets // 暂存原哈希表
104+
h.capacity *= h.extendRatio // 更新容量
105+
h.buckets = make([]*pair, h.capacity) // 初始化扩容后的新哈希表
106+
h.size = 0 // 重置大小
126107
// 将键值对从原哈希表搬运至新哈希表
127-
for _, p := range tmpBuckets {
128-
if p != (pair{}) && p != m.removed {
129-
m.put(p.key, p.val)
108+
for _, pair := range oldBuckets {
109+
if pair != nil && pair != h.TOMBSTONE {
110+
h.put(pair.key, pair.val)
130111
}
131112
}
132113
}
133114

134115
/* 打印哈希表 */
135-
func (m *hashMapOpenAddressing) print() {
136-
for _, p := range m.buckets {
137-
if p != (pair{}) {
138-
fmt.Println(strconv.Itoa(p.key) + " -> " + p.val)
139-
} else {
116+
func (h *hashMapOpenAddressing) print() {
117+
for _, pair := range h.buckets {
118+
if pair == nil {
140119
fmt.Println("nil")
120+
} else if pair == h.TOMBSTONE {
121+
fmt.Println("TOMBSTONE")
122+
} else {
123+
fmt.Printf("%d -> %s\n", pair.key, pair.val)
141124
}
142125
}
143126
}

codes/rust/chapter_sorting/bubble_sort.rs

-1
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,6 @@ fn bubble_sort_with_flag(nums: &mut [i32]) {
2727
// 外循环:未排序区间为 [0, i]
2828
for i in (1..nums.len()).rev() {
2929
let mut flag = false; // 初始化标志位
30-
3130
// 内循环:将未排序区间 [0, i] 中的最大元素交换至该区间的最右端
3231
for j in 0..i {
3332
if nums[j] > nums[j + 1] {

codes/swift/chapter_computational_complexity/time_complexity.swift

+1-1
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@ func bubbleSort(nums: inout [Int]) -> Int {
5050
var count = 0 // 计数器
5151
// 外循环:未排序区间为 [0, i]
5252
for i in stride(from: nums.count - 1, to: 0, by: -1) {
53-
// 内循环:将未排序区间 [0, i] 中的最大元素交换至该区间的最右端
53+
// 内循环:将未排序区间 [0, i] 中的最大元素交换至该区间的最右端
5454
for j in 0 ..< i {
5555
if nums[j] > nums[j + 1] {
5656
// 交换 nums[j] 与 nums[j + 1]

codes/swift/chapter_sorting/bubble_sort.swift

+1-1
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
func bubbleSort(nums: inout [Int]) {
99
// 外循环:未排序区间为 [0, i]
1010
for i in stride(from: nums.count - 1, to: 0, by: -1) {
11-
// 内循环:将未排序区间 [0, i] 中的最大元素交换至该区间的最右端
11+
// 内循环:将未排序区间 [0, i] 中的最大元素交换至该区间的最右端
1212
for j in stride(from: 0, to: i, by: 1) {
1313
if nums[j] > nums[j + 1] {
1414
// 交换 nums[j] 与 nums[j + 1]

codes/zig/chapter_computational_complexity/time_complexity.zig

+1-1
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,7 @@ fn bubbleSort(nums: []i32) i32 {
5757
var i: i32 = @as(i32, @intCast(nums.len)) - 1;
5858
while (i > 0) : (i -= 1) {
5959
var j: usize = 0;
60-
// 内循环:将未排序区间 [0, i] 中的最大元素交换至该区间的最右端
60+
// 内循环:将未排序区间 [0, i] 中的最大元素交换至该区间的最右端
6161
while (j < i) : (j += 1) {
6262
if (nums[j] > nums[j + 1]) {
6363
// 交换 nums[j] 与 nums[j + 1]

codes/zig/chapter_sorting/bubble_sort.zig

+2-2
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ fn bubbleSort(nums: []i32) void {
1111
var i: usize = nums.len - 1;
1212
while (i > 0) : (i -= 1) {
1313
var j: usize = 0;
14-
// 内循环:将未排序区间 [0, i] 中的最大元素交换至该区间的最右端
14+
// 内循环:将未排序区间 [0, i] 中的最大元素交换至该区间的最右端
1515
while (j < i) : (j += 1) {
1616
if (nums[j] > nums[j + 1]) {
1717
// 交换 nums[j] 与 nums[j + 1]
@@ -30,7 +30,7 @@ fn bubbleSortWithFlag(nums: []i32) void {
3030
while (i > 0) : (i -= 1) {
3131
var flag = false; // 初始化标志位
3232
var j: usize = 0;
33-
// 内循环:将未排序区间 [0, i] 中的最大元素交换至该区间的最右端
33+
// 内循环:将未排序区间 [0, i] 中的最大元素交换至该区间的最右端
3434
while (j < i) : (j += 1) {
3535
if (nums[j] > nums[j + 1]) {
3636
// 交换 nums[j] 与 nums[j + 1]

docs/chapter_array_and_linkedlist/summary.md

+2-6
Original file line numberDiff line numberDiff line change
@@ -31,11 +31,11 @@
3131
# 元素内存地址 = 数组内存地址(首元素内存地址) + 元素长度 * 元素索引
3232
```
3333

34-
**Q**删除节点后,是否需要把 `P.next` 设为 `None` 呢?
34+
**Q**删除节点 `P`,是否需要把 `P.next` 设为 `None` 呢?
3535

3636
不修改 `P.next` 也可以。从该链表的角度看,从头节点遍历到尾节点已经不会遇到 `P` 了。这意味着节点 `P` 已经从链表中删除了,此时节点 `P` 指向哪里都不会对该链表产生影响。
3737

38-
从垃圾回收的角度看,对于 Java、Python、Go 等拥有自动垃圾回收机制的语言来说,节点 `P` 是否被回收取决于是否仍存在指向它的引用,而不是 `P.next` 的值。在 C 和 C++ 等语言中,我们需要手动释放节点内存
38+
从数据结构与算法(做题)的角度看,不断开没有关系,只要保证程序的逻辑是正确的就行。从标准库的角度看,断开更加安全、逻辑更加清晰。如果不断开,假设被删除节点未被正常回收,那么它会影响后继节点的内存回收
3939

4040
**Q**:在链表中插入和删除操作的时间复杂度是 $O(1)$ 。但是增删之前都需要 $O(n)$ 的时间查找元素,那为什么时间复杂度不是 $O(n)$ 呢?
4141

@@ -74,7 +74,3 @@
7474
**Q**:初始化列表 `res = [0] * self.size()` 操作,会导致 `res` 的每个元素引用相同的地址吗?
7575

7676
不会。但二维数组会有这个问题,例如初始化二维列表 `res = [[0] * self.size()]` ,则多次引用了同一个列表 `[0]`
77-
78-
**Q**:在删除节点中,需要断开该节点与其后继节点之间的引用指向吗?
79-
80-
从数据结构与算法(做题)的角度看,不断开没有关系,只要保证程序的逻辑是正确的就行。从标准库的角度看,断开更加安全、逻辑更加清晰。如果不断开,假设被删除节点未被正常回收,那么它会影响后继节点的内存回收。

docs/chapter_paperbook/index.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ icon: fontawesome/solid/book
44
status: new
55
---
66

7-
# 纸质书介绍
7+
# 纸质书
88

99
经过长时间的打磨,《Hello 算法》纸质书终于发布了!此时的心情可以用一句诗来形容:
1010

docs/chapter_tree/array_representation_of_tree.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212

1313
![完美二叉树的数组表示](array_representation_of_tree.assets/array_representation_binary_tree.png)
1414

15-
**映射公式的角色相当于链表中的指针**。给定数组中的任意一个节点,我们都可以通过映射公式来访问它的左(右)子节点。
15+
**映射公式的角色相当于链表中的引用**。给定数组中的任意一个节点,我们都可以通过映射公式来访问它的左(右)子节点。
1616

1717
## 表示任意二叉树
1818

docs/chapter_tree/avl_tree.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -333,4 +333,4 @@ AVL 树的节点查找操作与二叉搜索树一致,在此不再赘述。
333333

334334
- 组织和存储大型数据,适用于高频查找、低频增删的场景。
335335
- 用于构建数据库中的索引系统。
336-
- 红黑树也是一种常见的平衡二叉搜索树。相较于 AVL 树,红黑树的平衡条件相对宽松,插入与删除节点所需的旋转操作相对较少,节点增删操作的平均效率更高。
336+
- 红黑树也是一种常见的平衡二叉搜索树。相较于 AVL 树,红黑树的平衡条件更宽松,插入与删除节点所需的旋转操作更少,节点增删操作的平均效率更高。

mkdocs.yml

+1-2
Original file line numberDiff line numberDiff line change
@@ -290,6 +290,5 @@ nav:
290290
- 16.3 &nbsp; 术语表: chapter_appendix/terminology.md
291291
- 参考文献:
292292
- chapter_reference/index.md
293-
- 纸质书介绍:
294-
# [status: new]
293+
- 纸质书:
295294
- chapter_paperbook/index.md

overrides/main.html

+1-1
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
{% block announce %}
44
{% if config.theme.language == 'zh' %}
5-
{% set announcements = '纸质书已发布,详情请见<a href="/chapter_paperbook/">纸质书介绍</a>' %}
5+
{% set announcements = '纸质书已发布,详情请见<a href="/chapter_paperbook/">这里</a>' %}
66
{% elif config.theme.language == 'en' %}
77
{% set announcements = 'The paper book (Chinese edition) published. Please visit <a href="/chapter_paperbook/">this link</a> for more details.' %}
88
{% endif %}

0 commit comments

Comments
 (0)