Compare commits

..

3 commits

Author SHA1 Message Date
Anurag Pandey
4b5add01dd
Merge f4d398d01b into 40d13cb81f 2024-09-25 13:58:18 +08:00
Spark
40d13cb81f
doc: translate chapter_tree/binary_tree_traversal.md (#1510)
Some checks failed
JavaScript / build (macos-latest) (push) Has been cancelled
JavaScript / build (ubuntu-latest) (push) Has been cancelled
JavaScript / build (windows-latest) (push) Has been cancelled
* Update binary_tree_traversal.md

* Update binary_tree_traversal.md with review
2024-09-24 18:00:36 +08:00
bongbongbakudan
c12d01a752
Update my_list.js (#1511)
修改insert注释
2024-09-24 17:57:34 +08:00
2 changed files with 9 additions and 9 deletions

View file

@ -70,7 +70,7 @@ class MyList {
remove(index) { remove(index) {
if (index < 0 || index >= this.#size) throw new Error('索引越界'); if (index < 0 || index >= this.#size) throw new Error('索引越界');
let num = this.#arr[index]; let num = this.#arr[index];
// 将索引 index 之后的元素都向前移动一位 // 将索引 index 之后的元素都向前移动一位
for (let j = index; j < this.#size - 1; j++) { for (let j = index; j < this.#size - 1; j++) {
this.#arr[j] = this.#arr[j + 1]; this.#arr[j] = this.#arr[j + 1];
} }

View file

@ -1,14 +1,14 @@
# Binary tree traversal # 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. From a physical structure perspective, 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.
Common traversal methods for binary trees include level-order traversal, pre-order traversal, in-order traversal, and post-order traversal, among others. The common traversal methods for binary trees include level-order traversal, pre-order traversal, in-order traversal, and post-order traversal.
## Level-order traversal ## Level-order traversal
As shown in the figure below, <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. As shown in the figure below, <u>level-order traversal</u> traverses the binary tree from top to bottom, layer by layer. Within each level, it visits nodes from left to right.
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. Level-order traversal is essentially a type of <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.
![Level-order traversal of a binary tree](binary_tree_traversal.assets/binary_tree_bfs.png) ![Level-order traversal of a binary tree](binary_tree_traversal.assets/binary_tree_bfs.png)
@ -22,14 +22,14 @@ Breadth-first traversal is usually implemented with the help of a "queue". The q
### Complexity analysis ### Complexity analysis
- **Time complexity is $O(n)$**: All nodes are visited once, using $O(n)$ time, where $n$ is the number of nodes. - **Time complexity is $O(n)$**: All nodes are visited once, taking $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. - **Space complexity is $O(n)$**: In the worst case, i.e., a full binary tree, before traversing to the bottom level, the queue can contain at most $(n + 1) / 2$ nodes simultaneously, occupying $O(n)$ space.
## Preorder, in-order, and post-order traversal ## Preorder, in-order, and post-order traversal
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. 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.
The figure below 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. The figure below shows the working principle of performing a depth-first traversal on a binary tree. **Depth-first traversal is like "walking" around the entire binary tree**, encountering three positions at each node, corresponding to pre-order, in-order, and post-order traversal.
![Preorder, in-order, and post-order traversal of a binary search tree](binary_tree_traversal.assets/binary_tree_dfs.png) ![Preorder, in-order, and post-order traversal of a binary search tree](binary_tree_traversal.assets/binary_tree_dfs.png)
@ -86,4 +86,4 @@ The figure below shows the recursive process of pre-order traversal of a binary
### Complexity analysis ### Complexity analysis
- **Time complexity is $O(n)$**: All nodes are visited once, using $O(n)$ time. - **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. - **Space complexity is $O(n)$**: In the worst case, i.e., the tree degenerates into a linked list, the recursion depth reaches $n$, the system occupies $O(n)$ stack frame space.