mirror of
https://github.com/krahets/hello-algo.git
synced 2025-11-07 07:36:48 +08:00
Graph dev
This commit is contained in:
31
docs/chapter_graph/graph_traversal.md
Normal file
31
docs/chapter_graph/graph_traversal.md
Normal file
@ -0,0 +1,31 @@
|
||||
---
|
||||
comments: true
|
||||
---
|
||||
|
||||
# 图的遍历
|
||||
|
||||
与遍历树类似,遍历图也需要通过搜索算法来实现,并也可根据遍历顺序来分为「广度优先遍历 Breadth-First Traversal」和「深度优先遍历 Depth-First Travsersal」,简称分别为 BFS 和 DFS 。
|
||||
|
||||
!!! tip 「树」与「图」的关系
|
||||
|
||||
本质上,可以把树看作是图的一种特例,即树是一种限制条件下的图。
|
||||
|
||||
## 广度优先遍历
|
||||
|
||||
广度优先遍历(BFS)代表一种优先遍历最近的顶点、一层层向外扩张的遍历方式。具体来看,从某个顶点出发,则优先遍历该顶点的所有邻接顶点,随后遍历下个顶点的所有邻接顶点,以此类推……
|
||||
|
||||
(图)
|
||||
|
||||
BFS 常借助「队列」来实现,队列具有“先入先出”的性质,这与 BFS 的“由近及远”的遍历方式是异曲同工的。具体地,在每轮迭代中弹出队首顶点且访问之,并将该顶点的所有邻接顶点加入到队列尾部,直到所有顶点访问完成即可。
|
||||
|
||||
为了防止重复遍历顶点,我们需要借助一个 HashSet 来记录哪些结点已被访问,从而避免走“回头路”。
|
||||
|
||||
```java
|
||||
|
||||
```
|
||||
|
||||
|
||||
|
||||
## 深度优先遍历
|
||||
|
||||
深度优先遍历(DFS)代表一种优先走到底,无路可走再回头的遍历方式。从某个顶点出发,首先不断地通过指针向下一个顶点遍历,直到走到头开始回溯,再继续走到底 + 回溯,以此类推……
|
||||
@ -4,7 +4,9 @@ comments: true
|
||||
|
||||
# 7.2. 二叉树遍历
|
||||
|
||||
非线性数据结构的遍历操作比线性数据结构更加复杂,往往需要使用搜索算法来实现。常见的二叉树遍历方式有层序遍历、前序遍历、中序遍历、后序遍历。
|
||||
从物理结构角度看,树是一种基于链表的数据结构,因此遍历方式也是通过指针(即引用)逐个遍历结点。同时,树还是一种非线性数据结构,这导致遍历树比遍历链表更加复杂,需要使用搜索算法来实现。
|
||||
|
||||
常见的二叉树遍历方式有层序遍历、前序遍历、中序遍历、后序遍历。
|
||||
|
||||
## 7.2.1. 层序遍历
|
||||
|
||||
@ -102,9 +104,9 @@ comments: true
|
||||
|
||||
```java title="binary_tree_dfs.java"
|
||||
[class]{binary_tree_dfs}-[func]{preOrder}
|
||||
|
||||
|
||||
[class]{binary_tree_dfs}-[func]{inOrder}
|
||||
|
||||
|
||||
[class]{binary_tree_dfs}-[func]{postOrder}
|
||||
```
|
||||
|
||||
@ -112,9 +114,9 @@ comments: true
|
||||
|
||||
```cpp title="binary_tree_dfs.cpp"
|
||||
[class]{}-[func]{preOrder}
|
||||
|
||||
|
||||
[class]{}-[func]{inOrder}
|
||||
|
||||
|
||||
[class]{}-[func]{postOrder}
|
||||
```
|
||||
|
||||
@ -122,9 +124,9 @@ comments: true
|
||||
|
||||
```python title="binary_tree_dfs.py"
|
||||
[class]{}-[func]{pre_order}
|
||||
|
||||
|
||||
[class]{}-[func]{in_order}
|
||||
|
||||
|
||||
[class]{}-[func]{post_order}
|
||||
```
|
||||
|
||||
@ -132,9 +134,9 @@ comments: true
|
||||
|
||||
```go title="binary_tree_dfs.go"
|
||||
[class]{}-[func]{preOrder}
|
||||
|
||||
|
||||
[class]{}-[func]{inOrder}
|
||||
|
||||
|
||||
[class]{}-[func]{postOrder}
|
||||
```
|
||||
|
||||
@ -142,9 +144,9 @@ comments: true
|
||||
|
||||
```javascript title="binary_tree_dfs.js"
|
||||
[class]{}-[func]{preOrder}
|
||||
|
||||
|
||||
[class]{}-[func]{inOrder}
|
||||
|
||||
|
||||
[class]{}-[func]{postOrder}
|
||||
```
|
||||
|
||||
@ -152,9 +154,9 @@ comments: true
|
||||
|
||||
```typescript title="binary_tree_dfs.ts"
|
||||
[class]{}-[func]{preOrder}
|
||||
|
||||
|
||||
[class]{}-[func]{inOrder}
|
||||
|
||||
|
||||
[class]{}-[func]{postOrder}
|
||||
```
|
||||
|
||||
@ -162,9 +164,9 @@ comments: true
|
||||
|
||||
```c title="binary_tree_dfs.c"
|
||||
[class]{}-[func]{preOrder}
|
||||
|
||||
|
||||
[class]{}-[func]{inOrder}
|
||||
|
||||
|
||||
[class]{}-[func]{postOrder}
|
||||
```
|
||||
|
||||
@ -172,9 +174,9 @@ comments: true
|
||||
|
||||
```csharp title="binary_tree_dfs.cs"
|
||||
[class]{binary_tree_dfs}-[func]{preOrder}
|
||||
|
||||
|
||||
[class]{binary_tree_dfs}-[func]{inOrder}
|
||||
|
||||
|
||||
[class]{binary_tree_dfs}-[func]{postOrder}
|
||||
```
|
||||
|
||||
@ -182,9 +184,9 @@ comments: true
|
||||
|
||||
```swift title="binary_tree_dfs.swift"
|
||||
[class]{}-[func]{preOrder}
|
||||
|
||||
|
||||
[class]{}-[func]{inOrder}
|
||||
|
||||
|
||||
[class]{}-[func]{postOrder}
|
||||
```
|
||||
|
||||
@ -192,9 +194,9 @@ comments: true
|
||||
|
||||
```zig title="binary_tree_dfs.zig"
|
||||
[class]{}-[func]{preOrder}
|
||||
|
||||
|
||||
[class]{}-[func]{inOrder}
|
||||
|
||||
|
||||
[class]{}-[func]{postOrder}
|
||||
```
|
||||
|
||||
|
||||
Reference in New Issue
Block a user