Skip to content

Commit 2737357

Browse files
steventimeskrahets
andauthored
translation: refine translation of binary_search_tree.md (krahets#1484)
* translation_refine_bst Signed-off-by: steventimes <stevenyang0316@gmail.com> * Update binary_search_tree.md * Update binary_search_tree.md --------- Signed-off-by: steventimes <stevenyang0316@gmail.com> Co-authored-by: Yudong Jin <krahets@163.com>
1 parent e0d617e commit 2737357

File tree

1 file changed

+15
-15
lines changed

1 file changed

+15
-15
lines changed

en/docs/chapter_tree/binary_search_tree.md

Lines changed: 15 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -9,15 +9,15 @@ As shown in the figure below, a <u>binary search tree</u> satisfies the followin
99

1010
## Operations on a binary search tree
1111

12-
We encapsulate the binary search tree as a class `BinarySearchTree` and declare a member variable `root`, pointing to the tree's root node.
12+
We encapsulate the binary search tree as a class `BinarySearchTree` and declare a member variable `root` pointing to the tree's root node.
1313

1414
### Searching for a node
1515

16-
Given a target node value `num`, one can search according to the properties of the binary search tree. As shown in the figure below, we declare a node `cur` and start from the binary tree's root node `root`, looping to compare the size relationship between the node value `cur.val` and `num`.
16+
Given a target node value `num`, one can search according to the properties of the binary search tree. As shown in the figure below, we declare a node `cur`, start from the binary tree's root node `root`, and loop to compare the size between the node value `cur.val` and `num`.
1717

1818
- If `cur.val < num`, it means the target node is in `cur`'s right subtree, thus execute `cur = cur.right`.
1919
- If `cur.val > num`, it means the target node is in `cur`'s left subtree, thus execute `cur = cur.left`.
20-
- If `cur.val = num`, it means the target node is found, exit the loop and return the node.
20+
- If `cur.val = num`, it means the target node is found, exit the loop, and return the node.
2121

2222
=== "<1>"
2323
![Example of searching for a node in a binary search tree](binary_search_tree.assets/bst_search_step1.png)
@@ -31,7 +31,7 @@ Given a target node value `num`, one can search according to the properties of t
3131
=== "<4>"
3232
![bst_search_step4](binary_search_tree.assets/bst_search_step4.png)
3333

34-
The search operation in a binary search tree works on the same principle as the binary search algorithm, eliminating half of the possibilities in each round. The number of loops is at most the height of the binary tree. When the binary tree is balanced, it uses $O(\log n)$ time. Example code is as follows:
34+
The search operation in a binary search tree works on the same principle as the binary search algorithm, eliminating half of the cases in each round. The number of loops is at most the height of the binary tree. When the binary tree is balanced, it uses $O(\log n)$ time. The example code is as follows:
3535

3636
```src
3737
[file]{binary_search_tree}-[class]{binary_search_tree}-[func]{search}
@@ -41,15 +41,15 @@ The search operation in a binary search tree works on the same principle as the
4141

4242
Given an element `num` to be inserted, to maintain the property of the binary search tree "left subtree < root node < right subtree," the insertion operation proceeds as shown in the figure below.
4343

44-
1. **Finding the insertion position**: Similar to the search operation, start from the root node and loop downwards according to the size relationship between the current node value and `num` until passing through the leaf node (traversing to `None`) then exit the loop.
45-
2. **Insert the node at that position**: Initialize the node `num` and place it where `None` was.
44+
1. **Finding insertion position**: Similar to the search operation, start from the root node, loop downwards according to the size relationship between the current node value and `num`, until the leaf node is passed (traversed to `None`), then exit the loop.
45+
2. **Insert the node at this position**: Initialize the node `num` and place it where `None` was.
4646

4747
![Inserting a node into a binary search tree](binary_search_tree.assets/bst_insert.png)
4848

4949
In the code implementation, note the following two points.
5050

51-
- The binary search tree does not allow duplicate nodes; otherwise, it will violate its definition. Therefore, if the node to be inserted already exists in the tree, the insertion is not performed, and it directly returns.
52-
- To perform the insertion operation, we need to use the node `pre` to save the node from the last loop. This way, when traversing to `None`, we can get its parent node, thus completing the node insertion operation.
51+
- The binary search tree does not allow duplicate nodes to exist; otherwise, its definition would be violated. Therefore, if the node to be inserted already exists in the tree, the insertion is not performed, and the node returns directly.
52+
- To perform the insertion operation, we need to use the node `pre` to save the node from the previous loop. This way, when traversing to `None`, we can get its parent node, thus completing the node insertion operation.
5353

5454
```src
5555
[file]{binary_search_tree}-[class]{binary_search_tree}-[func]{insert}
@@ -59,9 +59,9 @@ Similar to searching for a node, inserting a node uses $O(\log n)$ time.
5959

6060
### Removing a node
6161

62-
First, find the target node in the binary tree, then remove it. Similar to inserting a node, we need to ensure that after the removal operation is completed, the property of the binary search tree "left subtree < root node < right subtree" is still satisfied. Therefore, based on the number of child nodes of the target node, we divide it into 0, 1, and 2 cases, performing the corresponding node removal operations.
62+
First, find the target node in the binary tree, then remove it. Similar to inserting a node, we need to ensure that after the removal operation is completed, the property of the binary search tree "left subtree < root node < right subtree" is still satisfied. Therefore, based on the number of child nodes of the target node, we divide it into three cases: 0, 1, and 2, and perform the corresponding node removal operations.
6363

64-
As shown in the figure below, when the degree of the node to be removed is $0$, it means the node is a leaf node, and it can be directly removed.
64+
As shown in the figure below, when the degree of the node to be removed is $0$, it means the node is a leaf node and can be directly removed.
6565

6666
![Removing a node in a binary search tree (degree 0)](binary_search_tree.assets/bst_remove_case1.png)
6767

@@ -96,17 +96,17 @@ The operation of removing a node also uses $O(\log n)$ time, where finding the n
9696

9797
### In-order traversal is ordered
9898

99-
As shown in the figure below, the in-order traversal of a binary tree follows the "left $\rightarrow$ root $\rightarrow$ right" traversal order, and a binary search tree satisfies the size relationship "left child node $<$ root node $<$ right child node".
99+
As shown in the figure below, the in-order traversal of a binary tree follows the traversal order of "left $\rightarrow$ root $\rightarrow$ right," and a binary search tree satisfies the size relationship of "left child node $<$ root node $<$ right child node."
100100

101-
This means that in-order traversal in a binary search tree always traverses the next smallest node first, thus deriving an important property: **The in-order traversal sequence of a binary search tree is ascending**.
101+
This means that when performing in-order traversal in a binary search tree, the next smallest node will always be traversed first, thus leading to an important property: **The sequence of in-order traversal in a binary search tree is ascending**.
102102

103103
Using the ascending property of in-order traversal, obtaining ordered data in a binary search tree requires only $O(n)$ time, without the need for additional sorting operations, which is very efficient.
104104

105105
![In-order traversal sequence of a binary search tree](binary_search_tree.assets/bst_inorder_traversal.png)
106106

107107
## Efficiency of binary search trees
108108

109-
Given a set of data, we consider using an array or a binary search tree for storage. Observing the table below, the operations on a binary search tree all have logarithmic time complexity, which is stable and efficient. Only in scenarios of high-frequency addition and low-frequency search and removal, arrays are more efficient than binary search trees.
109+
Given a set of data, we consider using an array or a binary search tree for storage. Observing the table below, the operations on a binary search tree all have logarithmic time complexity, which is stable and efficient. Arrays are more efficient than binary search trees only in scenarios involving frequent additions and infrequent searches or removals.
110110

111111
<p align="center"> Table <id> &nbsp; Efficiency comparison between arrays and search trees </p>
112112

@@ -116,9 +116,9 @@ Given a set of data, we consider using an array or a binary search tree for stor
116116
| Insert element | $O(1)$ | $O(\log n)$ |
117117
| Remove element | $O(n)$ | $O(\log n)$ |
118118

119-
In ideal conditions, the binary search tree is "balanced," thus any node can be found within $\log n$ loops.
119+
Ideally, the binary search tree is "balanced," allowing any node can be found within $\log n$ loops.
120120

121-
However, continuously inserting and removing nodes in a binary search tree may lead to the binary tree degenerating into a chain list as shown in the figure below, at which point the time complexity of various operations also degrades to $O(n)$.
121+
However, if we continuously insert and remove nodes in a binary search tree, it may degenerate into a linked list as shown in the figure below, where the time complexity of various operations also degrades to $O(n)$.
122122

123123
![Degradation of a binary search tree](binary_search_tree.assets/bst_degradation.png)
124124

0 commit comments

Comments
 (0)