Skip to content

Commit

Permalink
Bug fixes and improvements (krahets#1318)
Browse files Browse the repository at this point in the history
* Sync zh and zh-hant versions

* Update en/README.md

* Add a Q&A for chapter of introduction

* Update the callout headers

* Sync zh ang zh-hant versions

* Bug fixes
  • Loading branch information
krahets authored Apr 30, 2024
1 parent 84b1ce2 commit 870e3e5
Show file tree
Hide file tree
Showing 107 changed files with 779 additions and 87 deletions.
2 changes: 1 addition & 1 deletion codes/python/chapter_tree/array_binary_tree.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ def size(self):
"""列表容量"""
return len(self._tree)

def val(self, i: int) -> int:
def val(self, i: int) -> int | None:
"""获取索引为 i 节点的值"""
# 若索引越界,则返回 None ,代表空位
if i < 0 or i >= self.size():
Expand Down
2 changes: 1 addition & 1 deletion docs/chapter_array_and_linkedlist/ram_and_cache.md
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@

![计算机存储系统](ram_and_cache.assets/storage_pyramid.png)

!!! note
!!! tip

计算机的存储层次结构体现了速度、容量和成本三者之间的精妙平衡。实际上,这种权衡普遍存在于所有工业领域,它要求我们在不同的优势和限制之间找到最佳平衡点。

Expand Down
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
2 changes: 1 addition & 1 deletion docs/chapter_computational_complexity/time_complexity.md
Original file line number Diff line number Diff line change
Expand Up @@ -747,7 +747,7 @@ $T(n)$ 是一次函数,说明其运行时间的增长趋势是线性的,因

时间复杂度分析本质上是计算“操作数量 $T(n)$”的渐近上界,它具有明确的数学定义。

!!! abstract "函数渐近上界"
!!! note "函数渐近上界"

若存在正实数 $c$ 和实数 $n_0$ ,使得对于所有的 $n > n_0$ ,均有 $T(n) \leq c \cdot f(n)$ ,则可认为 $f(n)$ 给出了 $T(n)$ 的一个渐近上界,记为 $T(n) = O(f(n))$ 。

Expand Down
2 changes: 1 addition & 1 deletion docs/chapter_data_structure/number_encoding.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# 数字编码 *

!!! note
!!! tip

在本书中,标题带有 * 符号的是选读章节。如果你时间有限或感到理解困难,可以先跳过,等学完必读章节后再单独攻克。

Expand Down
Binary file modified docs/chapter_graph/graph_traversal.assets/graph_bfs_step1.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified docs/chapter_graph/graph_traversal.assets/graph_bfs_step10.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified docs/chapter_graph/graph_traversal.assets/graph_bfs_step11.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified docs/chapter_graph/graph_traversal.assets/graph_bfs_step2.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified docs/chapter_graph/graph_traversal.assets/graph_bfs_step3.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified docs/chapter_graph/graph_traversal.assets/graph_bfs_step4.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified docs/chapter_graph/graph_traversal.assets/graph_bfs_step5.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified docs/chapter_graph/graph_traversal.assets/graph_bfs_step6.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified docs/chapter_graph/graph_traversal.assets/graph_bfs_step7.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified docs/chapter_graph/graph_traversal.assets/graph_bfs_step8.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified docs/chapter_graph/graph_traversal.assets/graph_bfs_step9.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified docs/chapter_graph/graph_traversal.assets/graph_dfs_step1.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified docs/chapter_graph/graph_traversal.assets/graph_dfs_step10.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified docs/chapter_graph/graph_traversal.assets/graph_dfs_step11.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified docs/chapter_graph/graph_traversal.assets/graph_dfs_step2.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified docs/chapter_graph/graph_traversal.assets/graph_dfs_step3.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified docs/chapter_graph/graph_traversal.assets/graph_dfs_step4.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified docs/chapter_graph/graph_traversal.assets/graph_dfs_step5.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified docs/chapter_graph/graph_traversal.assets/graph_dfs_step6.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified docs/chapter_graph/graph_traversal.assets/graph_dfs_step7.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified docs/chapter_graph/graph_traversal.assets/graph_dfs_step8.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified docs/chapter_graph/graph_traversal.assets/graph_dfs_step9.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
13 changes: 13 additions & 0 deletions docs/chapter_introduction/summary.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,3 +7,16 @@
- 算法是在有限时间内解决特定问题的一组指令或操作步骤,而数据结构是计算机中组织和存储数据的方式。
- 数据结构与算法紧密相连。数据结构是算法的基石,而算法是数据结构发挥作用的舞台。
- 我们可以将数据结构与算法类比为拼装积木,积木代表数据,积木的形状和连接方式等代表数据结构,拼装积木的步骤则对应算法。

### Q & A

**Q**:作为一名程序员,我在日常工作中从未用算法解决过问题,常用算法都被编程语言封装好了,直接用就可以了;这是否意味着我们工作中的问题还没有到达需要算法的程度?

如果把具体的工作技能比作是武功的“招式”的话,那么基础科目应该更像是“内功”。

我认为学算法(以及其他基础科目)的意义不是在于在工作中从零实现它,而是基于学到的知识,在解决问题时能够作出专业的反应和判断,从而提升工作的整体质量。举一个简单例子,每种编程语言都内置了排序函数:

- 如果我们没有学过数据结构与算法,那么给定任何数据,我们可能都塞给这个排序函数去做了。运行顺畅、性能不错,看上去并没有什么问题。
- 但如果学过算法,我们就会知道内置排序函数的时间复杂度是 $O(n \log n)$ ;而如果给定的数据是固定位数的整数(例如学号),那么我们就可以用效率更高的“基数排序”来做,将时间复杂度降为 $O(nk)$ ,其中 $k$ 为位数。当数据体量很大时,节省出来的运行时间就能创造较大价值(成本降低、体验变好等)。

在工程领域中,大量问题是难以达到最优解的,许多问题只是被“差不多”地解决了。问题的难易程度一方面取决于问题本身的性质,另一方面也取决于观测问题的人的知识储备。人的知识越完备、经验越多,分析问题就会越深入,问题就能被解决得更优雅。
24 changes: 12 additions & 12 deletions docs/chapter_stack_and_queue/deque.md
Original file line number Diff line number Diff line change
Expand Up @@ -27,28 +27,28 @@
from collections import deque

# 初始化双向队列
deque: deque[int] = deque()
deq: deque[int] = deque()

# 元素入队
deque.append(2) # 添加至队尾
deque.append(5)
deque.append(4)
deque.appendleft(3) # 添加至队首
deque.appendleft(1)
deq.append(2) # 添加至队尾
deq.append(5)
deq.append(4)
deq.appendleft(3) # 添加至队首
deq.appendleft(1)

# 访问元素
front: int = deque[0] # 队首元素
rear: int = deque[-1] # 队尾元素
front: int = deq[0] # 队首元素
rear: int = deq[-1] # 队尾元素

# 元素出队
pop_front: int = deque.popleft() # 队首元素出队
pop_rear: int = deque.pop() # 队尾元素出队
pop_front: int = deq.popleft() # 队首元素出队
pop_rear: int = deq.pop() # 队尾元素出队

# 获取双向队列的长度
size: int = len(deque)
size: int = len(deq)

# 判断双向队列是否为空
is_empty: bool = len(deque) == 0
is_empty: bool = len(deq) == 0
```

=== "C++"
Expand Down
2 changes: 1 addition & 1 deletion docs/chapter_tree/avl_tree.md
Original file line number Diff line number Diff line change
Expand Up @@ -248,7 +248,7 @@ AVL 树既是二叉搜索树,也是平衡二叉树,同时满足这两类二
[file]{avl_tree}-[class]{avl_tree}-[func]{balance_factor}
```

!!! note
!!! tip

设平衡因子为 $f$ ,则一棵 AVL 树的任意节点的平衡因子皆满足 $-1 \le f \le 1$ 。

Expand Down
2 changes: 1 addition & 1 deletion docs/chapter_tree/binary_tree.md
Original file line number Diff line number Diff line change
Expand Up @@ -633,7 +633,7 @@

https://pythontutor.com/render.html#code=class%20TreeNode%3A%0A%20%20%20%20%22%22%22%E4%BA%8C%E5%8F%89%E6%A0%91%E8%8A%82%E7%82%B9%E7%B1%BB%22%22%22%0A%20%20%20%20def%20__init__%28self,%20val%3A%20int%29%3A%0A%20%20%20%20%20%20%20%20self.val%3A%20int%20%3D%20val%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%23%20%E8%8A%82%E7%82%B9%E5%80%BC%0A%20%20%20%20%20%20%20%20self.left%3A%20TreeNode%20%7C%20None%20%3D%20None%20%20%23%20%E5%B7%A6%E5%AD%90%E8%8A%82%E7%82%B9%E5%BC%95%E7%94%A8%0A%20%20%20%20%20%20%20%20self.right%3A%20TreeNode%20%7C%20None%20%3D%20None%20%23%20%E5%8F%B3%E5%AD%90%E8%8A%82%E7%82%B9%E5%BC%95%E7%94%A8%0A%0A%22%22%22Driver%20Code%22%22%22%0Aif%20__name__%20%3D%3D%20%22__main__%22%3A%0A%20%20%20%20%23%20%E5%88%9D%E5%A7%8B%E5%8C%96%E4%BA%8C%E5%8F%89%E6%A0%91%0A%20%20%20%20%23%20%E5%88%9D%E5%A7%8B%E5%8C%96%E8%8A%82%E7%82%B9%0A%20%20%20%20n1%20%3D%20TreeNode%28val%3D1%29%0A%20%20%20%20n2%20%3D%20TreeNode%28val%3D2%29%0A%20%20%20%20n3%20%3D%20TreeNode%28val%3D3%29%0A%20%20%20%20n4%20%3D%20TreeNode%28val%3D4%29%0A%20%20%20%20n5%20%3D%20TreeNode%28val%3D5%29%0A%20%20%20%20%23%20%E6%9E%84%E5%BB%BA%E8%8A%82%E7%82%B9%E4%B9%8B%E9%97%B4%E7%9A%84%E5%BC%95%E7%94%A8%EF%BC%88%E6%8C%87%E9%92%88%EF%BC%89%0A%20%20%20%20n1.left%20%3D%20n2%0A%20%20%20%20n1.right%20%3D%20n3%0A%20%20%20%20n2.left%20%3D%20n4%0A%20%20%20%20n2.right%20%3D%20n5%0A%0A%20%20%20%20%23%20%E6%8F%92%E5%85%A5%E4%B8%8E%E5%88%A0%E9%99%A4%E8%8A%82%E7%82%B9%0A%20%20%20%20p%20%3D%20TreeNode%280%29%0A%20%20%20%20%23%20%E5%9C%A8%20n1%20-%3E%20n2%20%E4%B8%AD%E9%97%B4%E6%8F%92%E5%85%A5%E8%8A%82%E7%82%B9%20P%0A%20%20%20%20n1.left%20%3D%20p%0A%20%20%20%20p.left%20%3D%20n2%0A%20%20%20%20%23%20%E5%88%A0%E9%99%A4%E8%8A%82%E7%82%B9%20P%0A%20%20%20%20n1.left%20%3D%20n2&cumulative=false&curInstr=37&heapPrimitives=nevernest&mode=display&origin=opt-frontend.js&py=311&rawInputLstJSON=%5B%5D&textReferences=false

!!! note
!!! tip

需要注意的是,插入节点可能会改变二叉树的原有逻辑结构,而删除节点通常意味着删除该节点及其所有子树。因此,在二叉树中,插入与删除通常是由一套操作配合完成的,以实现有实际意义的操作。

Expand Down
2 changes: 1 addition & 1 deletion en/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@
English
</p>

## About
## The book

This open-source project aims to create a free and beginner-friendly crash course for data structures and algorithms.

Expand Down
2 changes: 1 addition & 1 deletion en/docs/chapter_array_and_linkedlist/ram_and_cache.md
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ We can imagine the computer storage system as a pyramid structure shown in the f

![Computer storage system](ram_and_cache.assets/storage_pyramid.png)

!!! note
!!! tip

The storage hierarchy of computers reflects a delicate balance between speed, capacity, and cost. In fact, this kind of trade-off is common in all industrial fields, requiring us to find the best balance between different advantages and limitations.

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -665,7 +665,7 @@ Since $T(n)$ is a linear function, its growth trend is linear, and therefore, it

In essence, time complexity analysis is about finding the asymptotic upper bound of the "number of operations $T(n)$". It has a precise mathematical definition.

!!! abstract "Asymptotic Upper Bound"
!!! note "Asymptotic Upper Bound"

If there exist positive real numbers $c$ and $n_0$ such that for all $n > n_0$, $T(n) \leq c \cdot f(n)$, then $f(n)$ is considered an asymptotic upper bound of $T(n)$, denoted as $T(n) = O(f(n))$.

Expand Down
2 changes: 1 addition & 1 deletion en/docs/chapter_data_structure/number_encoding.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# Number encoding *

!!! note
!!! tip

In this book, chapters marked with an asterisk '*' are optional readings. If you are short on time or find them challenging, you may skip these initially and return to them after completing the essential chapters.

Expand Down
24 changes: 12 additions & 12 deletions en/docs/chapter_stack_and_queue/deque.md
Original file line number Diff line number Diff line change
Expand Up @@ -27,28 +27,28 @@ Similarly, we can directly use the double-ended queue classes implemented in pro
from collections import deque

# Initialize the deque
deque: deque[int] = deque()
deq: deque[int] = deque()

# Enqueue elements
deque.append(2) # Add to the tail
deque.append(5)
deque.append(4)
deque.appendleft(3) # Add to the head
deque.appendleft(1)
deq.append(2) # Add to the tail
deq.append(5)
deq.append(4)
deq.appendleft(3) # Add to the head
deq.appendleft(1)

# Access elements
front: int = deque[0] # The first element
rear: int = deque[-1] # The last element
front: int = deq[0] # The first element
rear: int = deq[-1] # The last element

# Dequeue elements
pop_front: int = deque.popleft() # The first element dequeued
pop_rear: int = deque.pop() # The last element dequeued
pop_front: int = deq.popleft() # The first element dequeued
pop_rear: int = deq.pop() # The last element dequeued

# Get the length of the deque
size: int = len(deque)
size: int = len(deq)

# Check if the deque is empty
is_empty: bool = len(deque) == 0
is_empty: bool = len(deq) == 0
```

=== "C++"
Expand Down
2 changes: 1 addition & 1 deletion en/docs/chapter_tree/avl_tree.md
Original file line number Diff line number Diff line change
Expand Up @@ -237,7 +237,7 @@ The "balance factor" of a node is defined as the height of the node's left subtr
[file]{avl_tree}-[class]{avl_tree}-[func]{balance_factor}
```

!!! note
!!! tip

Let the balance factor be $f$, then the balance factor of any node in an AVL tree satisfies $-1 \le f \le 1$.

Expand Down
2 changes: 1 addition & 1 deletion en/docs/chapter_tree/binary_tree.md
Original file line number Diff line number Diff line change
Expand Up @@ -607,7 +607,7 @@ Similar to a linked list, inserting and removing nodes in a binary tree can be a

https://pythontutor.com/render.html#code=class%20TreeNode%3A%0A%20%20%20%20%22%22%22%E4%BA%8C%E5%8F%89%E6%A0%91%E8%8A%82%E7%82%B9%E7%B1%BB%22%22%22%0A%20%20%20%20def%20__init__%28self,%20val%3A%20int%29%3A%0A%20%20%20%20%20%20%20%20self.val%3A%20int%20%3D%20val%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%23%20%E8%8A%82%E7%82%B9%E5%80%BC%0A%20%20%20%20%20%20%20%20self.left%3A%20TreeNode%20%7C%20None%20%3D%20None%20%20%23%20%E5%B7%A6%E5%AD%90%E8%8A%82%E7%82%B9%E5%BC%95%E7%94%A8%0A%20%20%20%20%20%20%20%20self.right%3A%20TreeNode%20%7C%20None%20%3D%20None%20%23%20%E5%8F%B3%E5%AD%90%E8%8A%82%E7%82%B9%E5%BC%95%E7%94%A8%0A%0A%22%22%22Driver%20Code%22%22%22%0Aif%20__name__%20%3D%3D%20%22__main__%22%3A%0A%20%20%20%20%23%20%E5%88%9D%E5%A7%8B%E5%8C%96%E4%BA%8C%E5%8F%89%E6%A0%91%0A%20%20%20%20%23%20%E5%88%9D%E5%A7%8B%E5%8C%96%E8%8A%82%E7%82%B9%0A%20%20%20%20n1%20%3D%20TreeNode%28val%3D1%29%0A%20%20%20%20n2%20%3D%20TreeNode%28val%3D2%29%0A%20%20%20%20n3%20%3D%20TreeNode%28val%3D3%29%0A%20%20%20%20n4%20%3D%20TreeNode%28val%3D4%29%0A%20%20%20%20n5%20%3D%20TreeNode%28val%3D5%29%0A%20%20%20%20%23%20%E6%9E%84%E5%BB%BA%E8%8A%82%E7%82%B9%E4%B9%8B%E9%97%B4%E7%9A%84%E5%BC%95%E7%94%A8%EF%BC%88%E6%8C%87%E9%92%88%EF%BC%89%0A%20%20%20%20n1.left%20%3D%20n2%0A%20%20%20%20n1.right%20%3D%20n3%0A%20%20%20%20n2.left%20%3D%20n4%0A%20%20%20%20n2.right%20%3D%20n5%0A%0A%20%20%20%20%23%20%E6%8F%92%E5%85%A5%E4%B8%8E%E5%88%A0%E9%99%A4%E8%8A%82%E7%82%B9%0A%20%20%20%20p%20%3D%20TreeNode%280%29%0A%20%20%20%20%23%20%E5%9C%A8%20n1%20-%3E%20n2%20%E4%B8%AD%E9%97%B4%E6%8F%92%E5%85%A5%E8%8A%82%E7%82%B9%20P%0A%20%20%20%20n1.left%20%3D%20p%0A%20%20%20%20p.left%20%3D%20n2%0A%20%20%20%20%23%20%E5%88%A0%E9%99%A4%E8%8A%82%E7%82%B9%20P%0A%20%20%20%20n1.left%20%3D%20n2&cumulative=false&curInstr=37&heapPrimitives=nevernest&mode=display&origin=opt-frontend.js&py=311&rawInputLstJSON=%5B%5D&textReferences=false

!!! note
!!! tip

It's important to note that inserting nodes may change the original logical structure of the binary tree, while removing nodes usually means removing the node and all its subtrees. Therefore, in a binary tree, insertion and removal are usually performed through a set of operations to achieve meaningful actions.

Expand Down
2 changes: 1 addition & 1 deletion zh-hant/codes/cpp/chapter_graph/graph_bfs.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
vector<Vertex *> graphBFS(GraphAdjList &graph, Vertex *startVet) {
// 頂點走訪序列
vector<Vertex *> res;
// 雜湊表,用於記錄已被訪問過的頂點
// 雜湊集合,用於記錄已被訪問過的頂點
unordered_set<Vertex *> visited = {startVet};
// 佇列用於實現 BFS
queue<Vertex *> que;
Expand Down
2 changes: 1 addition & 1 deletion zh-hant/codes/cpp/chapter_graph/graph_dfs.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ void dfs(GraphAdjList &graph, unordered_set<Vertex *> &visited, vector<Vertex *>
vector<Vertex *> graphDFS(GraphAdjList &graph, Vertex *startVet) {
// 頂點走訪序列
vector<Vertex *> res;
// 雜湊表,用於記錄已被訪問過的頂點
// 雜湊集合,用於記錄已被訪問過的頂點
unordered_set<Vertex *> visited;
dfs(graph, visited, res, startVet);
return res;
Expand Down
2 changes: 1 addition & 1 deletion zh-hant/codes/csharp/chapter_graph/graph_bfs.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ public class graph_bfs {
List<Vertex> GraphBFS(GraphAdjList graph, Vertex startVet) {
// 頂點走訪序列
List<Vertex> res = [];
// 雜湊表,用於記錄已被訪問過的頂點
// 雜湊集合,用於記錄已被訪問過的頂點
HashSet<Vertex> visited = [startVet];
// 佇列用於實現 BFS
Queue<Vertex> que = new();
Expand Down
2 changes: 1 addition & 1 deletion zh-hant/codes/csharp/chapter_graph/graph_dfs.cs
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ void DFS(GraphAdjList graph, HashSet<Vertex> visited, List<Vertex> res, Vertex v
List<Vertex> GraphDFS(GraphAdjList graph, Vertex startVet) {
// 頂點走訪序列
List<Vertex> res = [];
// 雜湊表,用於記錄已被訪問過的頂點
// 雜湊集合,用於記錄已被訪問過的頂點
HashSet<Vertex> visited = [];
DFS(graph, visited, res, startVet);
return res;
Expand Down
2 changes: 1 addition & 1 deletion zh-hant/codes/dart/chapter_graph/graph_bfs.dart
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ List<Vertex> graphBFS(GraphAdjList graph, Vertex startVet) {
// 使用鄰接表來表示圖,以便獲取指定頂點的所有鄰接頂點
// 頂點走訪序列
List<Vertex> res = [];
// 雜湊表,用於記錄已被訪問過的頂點
// 雜湊集合,用於記錄已被訪問過的頂點
Set<Vertex> visited = {};
visited.add(startVet);
// 佇列用於實現 BFS
Expand Down
2 changes: 1 addition & 1 deletion zh-hant/codes/dart/chapter_graph/graph_dfs.dart
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ void dfs(
List<Vertex> graphDFS(GraphAdjList graph, Vertex startVet) {
// 頂點走訪序列
List<Vertex> res = [];
// 雜湊表,用於記錄已被訪問過的頂點
// 雜湊集合,用於記錄已被訪問過的頂點
Set<Vertex> visited = {};
dfs(graph, visited, res, startVet);
return res;
Expand Down
2 changes: 1 addition & 1 deletion zh-hant/codes/go/chapter_graph/graph_bfs.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ import (
func graphBFS(g *graphAdjList, startVet Vertex) []Vertex {
// 頂點走訪序列
res := make([]Vertex, 0)
// 雜湊表,用於記錄已被訪問過的頂點
// 雜湊集合,用於記錄已被訪問過的頂點
visited := make(map[Vertex]struct{})
visited[startVet] = struct{}{}
// 佇列用於實現 BFS, 使用切片模擬佇列
Expand Down
2 changes: 1 addition & 1 deletion zh-hant/codes/go/chapter_graph/graph_dfs.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ func dfs(g *graphAdjList, visited map[Vertex]struct{}, res *[]Vertex, vet Vertex
func graphDFS(g *graphAdjList, startVet Vertex) []Vertex {
// 頂點走訪序列
res := make([]Vertex, 0)
// 雜湊表,用於記錄已被訪問過的頂點
// 雜湊集合,用於記錄已被訪問過的頂點
visited := make(map[Vertex]struct{})
dfs(g, visited, &res, startVet)
// 返回頂點走訪序列
Expand Down
2 changes: 1 addition & 1 deletion zh-hant/codes/go/chapter_searching/two_sum.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ func twoSumBruteForce(nums []int, target int) []int {
size := len(nums)
// 兩層迴圈,時間複雜度為 O(n^2)
for i := 0; i < size-1; i++ {
for j := i + 1; i < size; j++ {
for j := i + 1; j < size; j++ {
if nums[i]+nums[j] == target {
return []int{i, j}
}
Expand Down
2 changes: 1 addition & 1 deletion zh-hant/codes/java/chapter_graph/graph_bfs.java
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ public class graph_bfs {
static List<Vertex> graphBFS(GraphAdjList graph, Vertex startVet) {
// 頂點走訪序列
List<Vertex> res = new ArrayList<>();
// 雜湊表,用於記錄已被訪問過的頂點
// 雜湊集合,用於記錄已被訪問過的頂點
Set<Vertex> visited = new HashSet<>();
visited.add(startVet);
// 佇列用於實現 BFS
Expand Down
2 changes: 1 addition & 1 deletion zh-hant/codes/java/chapter_graph/graph_dfs.java
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ static void dfs(GraphAdjList graph, Set<Vertex> visited, List<Vertex> res, Verte
static List<Vertex> graphDFS(GraphAdjList graph, Vertex startVet) {
// 頂點走訪序列
List<Vertex> res = new ArrayList<>();
// 雜湊表,用於記錄已被訪問過的頂點
// 雜湊集合,用於記錄已被訪問過的頂點
Set<Vertex> visited = new HashSet<>();
dfs(graph, visited, res, startVet);
return res;
Expand Down
2 changes: 1 addition & 1 deletion zh-hant/codes/javascript/chapter_graph/graph_bfs.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ const { Vertex } = require('../modules/Vertex');
function graphBFS(graph, startVet) {
// 頂點走訪序列
const res = [];
// 雜湊表,用於記錄已被訪問過的頂點
// 雜湊集合,用於記錄已被訪問過的頂點
const visited = new Set();
visited.add(startVet);
// 佇列用於實現 BFS
Expand Down
2 changes: 1 addition & 1 deletion zh-hant/codes/javascript/chapter_graph/graph_dfs.js
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ function dfs(graph, visited, res, vet) {
function graphDFS(graph, startVet) {
// 頂點走訪序列
const res = [];
// 雜湊表,用於記錄已被訪問過的頂點
// 雜湊集合,用於記錄已被訪問過的頂點
const visited = new Set();
dfs(graph, visited, res, startVet);
return res;
Expand Down
63 changes: 63 additions & 0 deletions zh-hant/codes/javascript/test_all.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
import { bold, brightRed } from 'jsr:@std/fmt/colors';
import { expandGlob } from 'jsr:@std/fs';
import { relative, resolve } from 'jsr:@std/path';

/**
* @typedef {import('jsr:@std/fs').WalkEntry} WalkEntry
* @type {WalkEntry[]}
*/
const entries = [];

for await (const entry of expandGlob(
resolve(import.meta.dirname, './chapter_*/*.js')
)) {
entries.push(entry);
}

/** @type {{ status: Promise<Deno.CommandStatus>; stderr: ReadableStream<Uint8Array>; }[]} */
const processes = [];

for (const file of entries) {
const execute = new Deno.Command('node', {
args: [relative(import.meta.dirname, file.path)],
cwd: import.meta.dirname,
stdin: 'piped',
stdout: 'piped',
stderr: 'piped',
});

const process = execute.spawn();
processes.push({ status: process.status, stderr: process.stderr });
}

const results = await Promise.all(
processes.map(async (item) => {
const status = await item.status;
return { status, stderr: item.stderr };
})
);

/** @type {ReadableStream<Uint8Array>[]} */
const errors = [];

for (const result of results) {
if (!result.status.success) {
errors.push(result.stderr);
}
}

console.log(`Tested ${entries.length} files`);
console.log(`Found exception in ${errors.length} files`);

if (errors.length) {
console.log();

for (const error of errors) {
const reader = error.getReader();
const { value } = await reader.read();
const decoder = new TextDecoder();
console.log(`${bold(brightRed('error'))}: ${decoder.decode(value)}`);
}

throw new Error('Test failed');
}
2 changes: 1 addition & 1 deletion zh-hant/codes/kotlin/chapter_graph/graph_bfs.kt
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ import java.util.*
fun graphBFS(graph: GraphAdjList, startVet: Vertex): MutableList<Vertex?> {
// 頂點走訪序列
val res = mutableListOf<Vertex?>()
// 雜湊表,用於記錄已被訪問過的頂點
// 雜湊集合,用於記錄已被訪問過的頂點
val visited = HashSet<Vertex>()
visited.add(startVet)
// 佇列用於實現 BFS
Expand Down
Loading

0 comments on commit 870e3e5

Please sign in to comment.