hello-algo/en/docs/chapter_tree/binary_tree_traversal.md

432 lines
12 KiB
Markdown
Raw Normal View History

2024-04-02 19:00:01 +08:00
---
comments: true
---
# 7.2   Binary tree traversal
From the perspective of physical structure, a tree is a data structure based on linked lists, hence its traversal method involves accessing nodes one by one through pointers. However, a tree is a non-linear data structure, which makes traversing a tree more complex than traversing a linked list, requiring the assistance of search algorithms to achieve.
2024-05-07 16:35:22 +08:00
Common traversal methods for binary trees include level-order traversal, pre-order traversal, in-order traversal, and post-order traversal, among others.
2024-04-02 19:00:01 +08:00
## 7.2.1   Level-order traversal
2024-05-02 01:46:14 +08:00
As shown in Figure 7-9, <u>level-order traversal</u> traverses the binary tree from top to bottom, layer by layer, and accesses nodes in each layer in a left-to-right order.
2024-04-02 19:00:01 +08:00
2024-05-02 01:46:14 +08:00
Level-order traversal essentially belongs to <u>breadth-first traversal</u>, also known as <u>breadth-first search (BFS)</u>, which embodies a "circumferentially outward expanding" layer-by-layer traversal method.
2024-04-02 19:00:01 +08:00
![Level-order traversal of a binary tree](binary_tree_traversal.assets/binary_tree_bfs.png){ class="animation-figure" }
<p align="center"> Figure 7-9 &nbsp; Level-order traversal of a binary tree </p>
### 1. &nbsp; Code implementation
Breadth-first traversal is usually implemented with the help of a "queue". The queue follows the "first in, first out" rule, while breadth-first traversal follows the "layer-by-layer progression" rule, the underlying ideas of the two are consistent. The implementation code is as follows:
=== "Python"
```python title="binary_tree_bfs.py"
def level_order(root: TreeNode | None) -> list[int]:
2024-05-06 05:27:10 +08:00
"""Level-order traversal"""
# Initialize queue, add root node
2024-04-02 19:00:01 +08:00
queue: deque[TreeNode] = deque()
queue.append(root)
2024-05-06 05:27:10 +08:00
# Initialize a list to store the traversal sequence
2024-04-02 19:00:01 +08:00
res = []
while queue:
2024-05-06 05:27:10 +08:00
node: TreeNode = queue.popleft() # Queue dequeues
res.append(node.val) # Save node value
2024-04-02 19:00:01 +08:00
if node.left is not None:
2024-05-06 05:27:10 +08:00
queue.append(node.left) # Left child node enqueues
2024-04-02 19:00:01 +08:00
if node.right is not None:
2024-05-06 05:27:10 +08:00
queue.append(node.right) # Right child node enqueues
2024-04-02 19:00:01 +08:00
return res
```
=== "C++"
```cpp title="binary_tree_bfs.cpp"
2024-05-06 14:40:36 +08:00
/* Level-order traversal */
vector<int> levelOrder(TreeNode *root) {
// Initialize queue, add root node
queue<TreeNode *> queue;
queue.push(root);
// Initialize a list to store the traversal sequence
vector<int> vec;
while (!queue.empty()) {
TreeNode *node = queue.front();
queue.pop(); // Queue dequeues
vec.push_back(node->val); // Save node value
if (node->left != nullptr)
queue.push(node->left); // Left child node enqueues
if (node->right != nullptr)
queue.push(node->right); // Right child node enqueues
}
return vec;
}
2024-04-02 19:00:01 +08:00
```
=== "Java"
```java title="binary_tree_bfs.java"
2024-05-06 05:27:10 +08:00
/* Level-order traversal */
2024-04-02 19:00:01 +08:00
List<Integer> levelOrder(TreeNode root) {
2024-05-06 05:27:10 +08:00
// Initialize queue, add root node
2024-04-02 19:00:01 +08:00
Queue<TreeNode> queue = new LinkedList<>();
queue.add(root);
2024-05-06 05:27:10 +08:00
// Initialize a list to store the traversal sequence
2024-04-02 19:00:01 +08:00
List<Integer> list = new ArrayList<>();
while (!queue.isEmpty()) {
2024-05-06 05:27:10 +08:00
TreeNode node = queue.poll(); // Queue dequeues
list.add(node.val); // Save node value
2024-04-02 19:00:01 +08:00
if (node.left != null)
2024-05-06 05:27:10 +08:00
queue.offer(node.left); // Left child node enqueues
2024-04-02 19:00:01 +08:00
if (node.right != null)
2024-05-06 05:27:10 +08:00
queue.offer(node.right); // Right child node enqueues
2024-04-02 19:00:01 +08:00
}
return list;
}
```
=== "C#"
```csharp title="binary_tree_bfs.cs"
2024-05-06 05:27:10 +08:00
[class]{binary_tree_bfs}-[func]{LevelOrder}
2024-04-02 19:00:01 +08:00
```
=== "Go"
```go title="binary_tree_bfs.go"
2024-05-06 05:27:10 +08:00
[class]{}-[func]{levelOrder}
2024-04-02 19:00:01 +08:00
```
=== "Swift"
```swift title="binary_tree_bfs.swift"
2024-05-06 05:27:10 +08:00
[class]{}-[func]{levelOrder}
2024-04-02 19:00:01 +08:00
```
=== "JS"
```javascript title="binary_tree_bfs.js"
2024-05-06 05:27:10 +08:00
[class]{}-[func]{levelOrder}
2024-04-02 19:00:01 +08:00
```
=== "TS"
```typescript title="binary_tree_bfs.ts"
2024-05-06 05:27:10 +08:00
[class]{}-[func]{levelOrder}
2024-04-02 19:00:01 +08:00
```
=== "Dart"
```dart title="binary_tree_bfs.dart"
2024-05-06 05:27:10 +08:00
[class]{}-[func]{levelOrder}
2024-04-02 19:00:01 +08:00
```
=== "Rust"
```rust title="binary_tree_bfs.rs"
2024-05-06 05:27:10 +08:00
[class]{}-[func]{level_order}
2024-04-02 19:00:01 +08:00
```
=== "C"
```c title="binary_tree_bfs.c"
2024-05-06 05:27:10 +08:00
[class]{}-[func]{levelOrder}
2024-04-02 19:00:01 +08:00
```
=== "Kotlin"
```kotlin title="binary_tree_bfs.kt"
2024-05-06 05:27:10 +08:00
[class]{}-[func]{levelOrder}
2024-04-02 19:00:01 +08:00
```
=== "Ruby"
```ruby title="binary_tree_bfs.rb"
2024-05-06 05:27:10 +08:00
[class]{}-[func]{level_order}
2024-04-02 19:00:01 +08:00
```
=== "Zig"
```zig title="binary_tree_bfs.zig"
2024-05-06 05:27:10 +08:00
[class]{}-[func]{levelOrder}
2024-04-02 19:00:01 +08:00
```
### 2. &nbsp; Complexity analysis
- **Time complexity is $O(n)$**: All nodes are visited once, using $O(n)$ time, where $n$ is the number of nodes.
- **Space complexity is $O(n)$**: In the worst case, i.e., a full binary tree, before traversing to the lowest level, the queue can contain at most $(n + 1) / 2$ nodes at the same time, occupying $O(n)$ space.
2024-05-07 16:35:22 +08:00
## 7.2.2 &nbsp; Preorder, in-order, and post-order traversal
2024-04-02 19:00:01 +08:00
2024-05-07 16:35:22 +08:00
Correspondingly, pre-order, in-order, and post-order traversal all belong to <u>depth-first traversal</u>, also known as <u>depth-first search (DFS)</u>, which embodies a "proceed to the end first, then backtrack and continue" traversal method.
2024-04-02 19:00:01 +08:00
2024-05-07 16:35:22 +08:00
Figure 7-10 shows the working principle of performing a depth-first traversal on a binary tree. **Depth-first traversal is like walking around the perimeter of the entire binary tree**, encountering three positions at each node, corresponding to pre-order traversal, in-order traversal, and post-order traversal.
2024-04-02 19:00:01 +08:00
2024-05-07 16:35:22 +08:00
![Preorder, in-order, and post-order traversal of a binary search tree](binary_tree_traversal.assets/binary_tree_dfs.png){ class="animation-figure" }
2024-04-02 19:00:01 +08:00
2024-05-07 16:35:22 +08:00
<p align="center"> Figure 7-10 &nbsp; Preorder, in-order, and post-order traversal of a binary search tree </p>
2024-04-02 19:00:01 +08:00
### 1. &nbsp; Code implementation
Depth-first search is usually implemented based on recursion:
=== "Python"
```python title="binary_tree_dfs.py"
def pre_order(root: TreeNode | None):
2024-05-06 05:27:10 +08:00
"""Pre-order traversal"""
2024-04-02 19:00:01 +08:00
if root is None:
return
2024-05-06 05:27:10 +08:00
# Visit priority: root node -> left subtree -> right subtree
2024-04-02 19:00:01 +08:00
res.append(root.val)
pre_order(root=root.left)
pre_order(root=root.right)
def in_order(root: TreeNode | None):
2024-05-06 05:27:10 +08:00
"""In-order traversal"""
2024-04-02 19:00:01 +08:00
if root is None:
return
2024-05-06 05:27:10 +08:00
# Visit priority: left subtree -> root node -> right subtree
2024-04-02 19:00:01 +08:00
in_order(root=root.left)
res.append(root.val)
in_order(root=root.right)
def post_order(root: TreeNode | None):
2024-05-06 05:27:10 +08:00
"""Post-order traversal"""
2024-04-02 19:00:01 +08:00
if root is None:
return
2024-05-06 05:27:10 +08:00
# Visit priority: left subtree -> right subtree -> root node
2024-04-02 19:00:01 +08:00
post_order(root=root.left)
post_order(root=root.right)
res.append(root.val)
```
=== "C++"
```cpp title="binary_tree_dfs.cpp"
2024-05-06 14:40:36 +08:00
/* Pre-order traversal */
void preOrder(TreeNode *root) {
if (root == nullptr)
return;
// Visit priority: root node -> left subtree -> right subtree
vec.push_back(root->val);
preOrder(root->left);
preOrder(root->right);
}
2024-04-02 19:00:01 +08:00
2024-05-06 14:40:36 +08:00
/* In-order traversal */
void inOrder(TreeNode *root) {
if (root == nullptr)
return;
// Visit priority: left subtree -> root node -> right subtree
inOrder(root->left);
vec.push_back(root->val);
inOrder(root->right);
}
2024-04-02 19:00:01 +08:00
2024-05-06 14:40:36 +08:00
/* Post-order traversal */
void postOrder(TreeNode *root) {
if (root == nullptr)
return;
// Visit priority: left subtree -> right subtree -> root node
postOrder(root->left);
postOrder(root->right);
vec.push_back(root->val);
}
2024-04-02 19:00:01 +08:00
```
=== "Java"
```java title="binary_tree_dfs.java"
2024-05-06 05:27:10 +08:00
/* Pre-order traversal */
2024-04-02 19:00:01 +08:00
void preOrder(TreeNode root) {
if (root == null)
return;
2024-05-06 05:27:10 +08:00
// Visit priority: root node -> left subtree -> right subtree
2024-04-02 19:00:01 +08:00
list.add(root.val);
preOrder(root.left);
preOrder(root.right);
}
2024-05-06 05:27:10 +08:00
/* In-order traversal */
2024-04-02 19:00:01 +08:00
void inOrder(TreeNode root) {
if (root == null)
return;
2024-05-06 05:27:10 +08:00
// Visit priority: left subtree -> root node -> right subtree
2024-04-02 19:00:01 +08:00
inOrder(root.left);
list.add(root.val);
inOrder(root.right);
}
2024-05-06 05:27:10 +08:00
/* Post-order traversal */
2024-04-02 19:00:01 +08:00
void postOrder(TreeNode root) {
if (root == null)
return;
2024-05-06 05:27:10 +08:00
// Visit priority: left subtree -> right subtree -> root node
2024-04-02 19:00:01 +08:00
postOrder(root.left);
postOrder(root.right);
list.add(root.val);
}
```
=== "C#"
```csharp title="binary_tree_dfs.cs"
2024-05-06 05:27:10 +08:00
[class]{binary_tree_dfs}-[func]{PreOrder}
2024-04-02 19:00:01 +08:00
2024-05-06 05:27:10 +08:00
[class]{binary_tree_dfs}-[func]{InOrder}
2024-04-02 19:00:01 +08:00
2024-05-06 05:27:10 +08:00
[class]{binary_tree_dfs}-[func]{PostOrder}
2024-04-02 19:00:01 +08:00
```
=== "Go"
```go title="binary_tree_dfs.go"
2024-05-06 05:27:10 +08:00
[class]{}-[func]{preOrder}
2024-04-02 19:00:01 +08:00
2024-05-06 05:27:10 +08:00
[class]{}-[func]{inOrder}
2024-04-02 19:00:01 +08:00
2024-05-06 05:27:10 +08:00
[class]{}-[func]{postOrder}
2024-04-02 19:00:01 +08:00
```
=== "Swift"
```swift title="binary_tree_dfs.swift"
2024-05-06 05:27:10 +08:00
[class]{}-[func]{preOrder}
2024-04-02 19:00:01 +08:00
2024-05-06 05:27:10 +08:00
[class]{}-[func]{inOrder}
2024-04-02 19:00:01 +08:00
2024-05-06 05:27:10 +08:00
[class]{}-[func]{postOrder}
2024-04-02 19:00:01 +08:00
```
=== "JS"
```javascript title="binary_tree_dfs.js"
2024-05-06 05:27:10 +08:00
[class]{}-[func]{preOrder}
2024-04-02 19:00:01 +08:00
2024-05-06 05:27:10 +08:00
[class]{}-[func]{inOrder}
2024-04-02 19:00:01 +08:00
2024-05-06 05:27:10 +08:00
[class]{}-[func]{postOrder}
2024-04-02 19:00:01 +08:00
```
=== "TS"
```typescript title="binary_tree_dfs.ts"
2024-05-06 05:27:10 +08:00
[class]{}-[func]{preOrder}
2024-04-02 19:00:01 +08:00
2024-05-06 05:27:10 +08:00
[class]{}-[func]{inOrder}
2024-04-02 19:00:01 +08:00
2024-05-06 05:27:10 +08:00
[class]{}-[func]{postOrder}
2024-04-02 19:00:01 +08:00
```
=== "Dart"
```dart title="binary_tree_dfs.dart"
2024-05-06 05:27:10 +08:00
[class]{}-[func]{preOrder}
2024-04-02 19:00:01 +08:00
2024-05-06 05:27:10 +08:00
[class]{}-[func]{inOrder}
2024-04-02 19:00:01 +08:00
2024-05-06 05:27:10 +08:00
[class]{}-[func]{postOrder}
2024-04-02 19:00:01 +08:00
```
=== "Rust"
```rust title="binary_tree_dfs.rs"
2024-05-06 05:27:10 +08:00
[class]{}-[func]{pre_order}
2024-04-02 19:00:01 +08:00
2024-05-06 05:27:10 +08:00
[class]{}-[func]{in_order}
2024-04-02 19:00:01 +08:00
2024-05-06 05:27:10 +08:00
[class]{}-[func]{post_order}
2024-04-02 19:00:01 +08:00
```
=== "C"
```c title="binary_tree_dfs.c"
2024-05-06 05:27:10 +08:00
[class]{}-[func]{preOrder}
2024-04-02 19:00:01 +08:00
2024-05-06 05:27:10 +08:00
[class]{}-[func]{inOrder}
2024-04-02 19:00:01 +08:00
2024-05-06 05:27:10 +08:00
[class]{}-[func]{postOrder}
2024-04-02 19:00:01 +08:00
```
=== "Kotlin"
```kotlin title="binary_tree_dfs.kt"
2024-05-06 05:27:10 +08:00
[class]{}-[func]{preOrder}
2024-04-02 19:00:01 +08:00
2024-05-06 05:27:10 +08:00
[class]{}-[func]{inOrder}
2024-04-02 19:00:01 +08:00
2024-05-06 05:27:10 +08:00
[class]{}-[func]{postOrder}
2024-04-02 19:00:01 +08:00
```
=== "Ruby"
```ruby title="binary_tree_dfs.rb"
2024-05-06 05:27:10 +08:00
[class]{}-[func]{pre_order}
[class]{}-[func]{in_order}
[class]{}-[func]{post_order}
2024-04-02 19:00:01 +08:00
```
=== "Zig"
```zig title="binary_tree_dfs.zig"
2024-05-06 05:27:10 +08:00
[class]{}-[func]{preOrder}
2024-04-02 19:00:01 +08:00
2024-05-06 05:27:10 +08:00
[class]{}-[func]{inOrder}
2024-04-02 19:00:01 +08:00
2024-05-06 05:27:10 +08:00
[class]{}-[func]{postOrder}
2024-04-02 19:00:01 +08:00
```
!!! tip
Depth-first search can also be implemented based on iteration, interested readers can study this on their own.
2024-05-07 16:35:22 +08:00
Figure 7-11 shows the recursive process of pre-order traversal of a binary tree, which can be divided into two opposite parts: "recursion" and "return".
2024-04-02 19:00:01 +08:00
1. "Recursion" means starting a new method, the program accesses the next node in this process.
2. "Return" means the function returns, indicating the current node has been fully accessed.
=== "<1>"
2024-05-07 16:35:22 +08:00
![The recursive process of pre-order traversal](binary_tree_traversal.assets/preorder_step1.png){ class="animation-figure" }
2024-04-02 19:00:01 +08:00
=== "<2>"
![preorder_step2](binary_tree_traversal.assets/preorder_step2.png){ class="animation-figure" }
=== "<3>"
![preorder_step3](binary_tree_traversal.assets/preorder_step3.png){ class="animation-figure" }
=== "<4>"
![preorder_step4](binary_tree_traversal.assets/preorder_step4.png){ class="animation-figure" }
=== "<5>"
![preorder_step5](binary_tree_traversal.assets/preorder_step5.png){ class="animation-figure" }
=== "<6>"
![preorder_step6](binary_tree_traversal.assets/preorder_step6.png){ class="animation-figure" }
=== "<7>"
![preorder_step7](binary_tree_traversal.assets/preorder_step7.png){ class="animation-figure" }
=== "<8>"
![preorder_step8](binary_tree_traversal.assets/preorder_step8.png){ class="animation-figure" }
=== "<9>"
![preorder_step9](binary_tree_traversal.assets/preorder_step9.png){ class="animation-figure" }
=== "<10>"
![preorder_step10](binary_tree_traversal.assets/preorder_step10.png){ class="animation-figure" }
=== "<11>"
![preorder_step11](binary_tree_traversal.assets/preorder_step11.png){ class="animation-figure" }
2024-05-07 16:35:22 +08:00
<p align="center"> Figure 7-11 &nbsp; The recursive process of pre-order traversal </p>
2024-04-02 19:00:01 +08:00
### 2. &nbsp; Complexity analysis
- **Time complexity is $O(n)$**: All nodes are visited once, using $O(n)$ time.
- **Space complexity is $O(n)$**: In the worst case, i.e., the tree degrades into a linked list, the recursion depth reaches $n$, the system occupies $O(n)$ stack frame space.