Translate all code to English (#1836)

* Review the EN heading format.

* Fix pythontutor headings.

* Fix pythontutor headings.

* bug fixes

* Fix headings in **/summary.md

* Revisit the CN-to-EN translation for Python code using Claude-4.5

* Revisit the CN-to-EN translation for Java code using Claude-4.5

* Revisit the CN-to-EN translation for Cpp code using Claude-4.5.

* Fix the dictionary.

* Fix cpp code translation for the multipart strings.

* Translate Go code to English.

* Update workflows to test EN code.

* Add EN translation for C.

* Add EN translation for CSharp.

* Add EN translation for Swift.

* Trigger the CI check.

* Revert.

* Update en/hash_map.md

* Add the EN version of Dart code.

* Add the EN version of Kotlin code.

* Add missing code files.

* Add the EN version of JavaScript code.

* Add the EN version of TypeScript code.

* Fix the workflows.

* Add the EN version of Ruby code.

* Add the EN version of Rust code.

* Update the CI check for the English version  code.

* Update Python CI check.

* Fix cmakelists for en/C code.

* Fix Ruby comments
This commit is contained in:
Yudong Jin
2025-12-31 07:44:52 +08:00
committed by GitHub
parent 45e1295241
commit 2778a6f9c7
1284 changed files with 71557 additions and 3275 deletions
@@ -1,10 +1,10 @@
# Array representation of binary trees
# Array Representation of Binary Trees
Under the linked list representation, the storage unit of a binary tree is a node `TreeNode`, and nodes are connected by pointers. The previous section introduced the basic operations of binary trees under the linked list representation.
So, can we use an array to represent a binary tree? The answer is yes.
## Representing perfect binary trees
## Representing Perfect Binary Trees
Let's analyze a simple case first. Given a perfect binary tree, we store all nodes in an array according to the order of level-order traversal, where each node corresponds to a unique array index.
@@ -14,7 +14,7 @@ Based on the characteristics of level-order traversal, we can derive a "mapping
**The mapping formula plays a role similar to the node references (pointers) in linked lists**. Given any node in the array, we can access its left (right) child node using the mapping formula.
## Representing any binary tree
## Representing Any Binary Tree
Perfect binary trees are a special case; in the middle levels of a binary tree, there are typically many `None` values. Since the level-order traversal sequence does not include these `None` values, we cannot infer the number and distribution of `None` values based on this sequence alone. **This means multiple binary tree structures can correspond to the same level-order traversal sequence**.
@@ -151,7 +151,7 @@ The following code implements a binary tree based on array representation, inclu
[file]{array_binary_tree}-[class]{array_binary_tree}-[func]{}
```
## Advantages and limitations
## Advantages and Limitations
The array representation of binary trees has the following advantages:
+15 -15
View File
@@ -1,4 +1,4 @@
# AVL tree *
# Avl Tree *
In the "Binary Search Tree" section, we mentioned that after multiple insertion and removal operations, a binary search tree may degenerate into a linked list. In this case, the time complexity of all operations degrades from $O(\log n)$ to $O(n)$.
@@ -12,11 +12,11 @@ For example, in the perfect binary tree shown in the figure below, after inserti
In 1962, G. M. Adelson-Velsky and E. M. Landis proposed the <u>AVL tree</u> in their paper "An algorithm for the organization of information". The paper described in detail a series of operations ensuring that after continuously adding and removing nodes, the AVL tree does not degenerate, thus keeping the time complexity of various operations at the $O(\log n)$ level. In other words, in scenarios requiring frequent insertions, deletions, searches, and modifications, the AVL tree can always maintain efficient data operation performance, making it very valuable in applications.
## Common terminology in AVL trees
## Common Terminology in Avl Trees
An AVL tree is both a binary search tree and a balanced binary tree, simultaneously satisfying all the properties of these two types of binary trees, hence it is a <u>balanced binary search tree</u>.
### Node height
### Node Height
Since the operations related to AVL trees require obtaining node heights, we need to add a `height` variable to the node class:
@@ -240,7 +240,7 @@ The "node height" refers to the distance from that node to its farthest leaf nod
[file]{avl_tree}-[class]{avl_tree}-[func]{update_height}
```
### Node balance factor
### Node Balance Factor
The <u>balance factor</u> of a node is defined as the height of the node's left subtree minus the height of its right subtree, and the balance factor of a null node is defined as $0$. We also encapsulate the function to obtain the node's balance factor for convenient subsequent use:
@@ -252,13 +252,13 @@ The <u>balance factor</u> of a node is defined as the height of the node's left
Let the balance factor be $f$, then the balance factor of any node in an AVL tree satisfies $-1 \le f \le 1$.
## Rotations in AVL trees
## Rotations in Avl Trees
The characteristic of AVL trees lies in the "rotation" operation, which can restore balance to unbalanced nodes without affecting the inorder traversal sequence of the binary tree. In other words, **rotation operations can both maintain the property of a "binary search tree" and make the tree return to a "balanced binary tree"**.
We call nodes with a balance factor absolute value $> 1$ "unbalanced nodes". Depending on the imbalance situation, rotation operations are divided into four types: right rotation, left rotation, left rotation then right rotation, and right rotation then left rotation. Below we describe these rotation operations in detail.
### Right rotation
### Right Rotation
As shown in the figure below, the value below the node is the balance factor. From bottom to top, the first unbalanced node in the binary tree is "node 3". We focus on the subtree with this unbalanced node as the root, denoting the node as `node` and its left child as `child`, and perform a "right rotation" operation. After the right rotation is completed, the subtree regains balance and still maintains the properties of a binary search tree.
@@ -284,7 +284,7 @@ As shown in the figure below, when the `child` node has a right child (denoted a
[file]{avl_tree}-[class]{avl_tree}-[func]{right_rotate}
```
### Left rotation
### Left Rotation
Correspondingly, if considering the "mirror" of the above unbalanced binary tree, the "left rotation" operation shown in the figure below needs to be performed.
@@ -300,19 +300,19 @@ It can be observed that **right rotation and left rotation operations are mirror
[file]{avl_tree}-[class]{avl_tree}-[func]{left_rotate}
```
### Left rotation then right rotation
### Left Rotation Then Right Rotation
For the unbalanced node 3 in the figure below, using either left rotation or right rotation alone cannot restore the subtree to balance. In this case, a "left rotation" needs to be performed on `child` first, followed by a "right rotation" on `node`.
![Left-right rotation](avl_tree.assets/avltree_left_right_rotate.png)
### Right rotation then left rotation
### Right Rotation Then Left Rotation
As shown in the figure below, for the mirror case of the above unbalanced binary tree, a "right rotation" needs to be performed on `child` first, then a "left rotation" on `node`.
![Right-left rotation](avl_tree.assets/avltree_right_left_rotate.png)
### Choice of rotation
### Choice of Rotation
The four imbalances shown in the figure below correspond one-to-one with the above cases, requiring right rotation, left rotation then right rotation, right rotation then left rotation, and left rotation operations respectively.
@@ -335,9 +335,9 @@ For ease of use, we encapsulate the rotation operations into a function. **With
[file]{avl_tree}-[class]{avl_tree}-[func]{rotate}
```
## Common operations in AVL trees
## Common Operations in Avl Trees
### Node insertion
### Node Insertion
The node insertion operation in AVL trees is similar in principle to that in binary search trees. The only difference is that after inserting a node in an AVL tree, a series of unbalanced nodes may appear on the path from that node to the root. Therefore, **we need to start from this node and perform rotation operations from bottom to top, restoring balance to all unbalanced nodes**. The code is as follows:
@@ -345,7 +345,7 @@ The node insertion operation in AVL trees is similar in principle to that in bin
[file]{avl_tree}-[class]{avl_tree}-[func]{insert_helper}
```
### Node removal
### Node Removal
Similarly, on the basis of the binary search tree's node removal method, rotation operations need to be performed from bottom to top to restore balance to all unbalanced nodes. The code is as follows:
@@ -353,11 +353,11 @@ Similarly, on the basis of the binary search tree's node removal method, rotatio
[file]{avl_tree}-[class]{avl_tree}-[func]{remove_helper}
```
### Node search
### Node Search
The node search operation in AVL trees is consistent with that in binary search trees, and will not be elaborated here.
## Typical applications of AVL trees
## Typical Applications of Avl Trees
- Organizing and storing large-scale data, suitable for scenarios with high-frequency searches and low-frequency insertions and deletions.
- Used to build index systems in databases.
+8 -8
View File
@@ -1,4 +1,4 @@
# Binary search tree
# Binary Search Tree
As shown in the figure below, a <u>binary search tree</u> satisfies the following conditions.
@@ -7,11 +7,11 @@ As shown in the figure below, a <u>binary search tree</u> satisfies the followin
![Binary search tree](binary_search_tree.assets/binary_search_tree.png)
## Operations on a binary search tree
## Operations on a Binary Search Tree
We encapsulate the binary search tree as a class `BinarySearchTree` and declare a member variable `root` pointing to the tree's root node.
### Searching for a node
### Searching for a Node
Given a target node value `num`, we 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 node value `cur.val` with `num`.
@@ -37,7 +37,7 @@ The search operation in a binary search tree works on the same principle as the
[file]{binary_search_tree}-[class]{binary_search_tree}-[func]{search}
```
### Inserting a node
### Inserting a Node
Given an element `num` to be inserted, in order to maintain the property of the binary search tree "left subtree < root node < right subtree," the insertion process is as shown in the figure below.
@@ -57,7 +57,7 @@ In the code implementation, note the following two points:
Similar to searching for a node, inserting a node uses $O(\log n)$ time.
### Removing a node
### Removing a Node
First, find the target node in the binary tree, then remove it. Similar to node insertion, we need to ensure that after the removal operation is completed, the binary search tree's property of "left subtree $<$ root node $<$ right subtree" is still maintained. Therefore, depending on the number of child nodes the target node has, we divide it into 0, 1, and 2 three cases, and execute the corresponding node removal operations.
@@ -94,7 +94,7 @@ The node removal operation also uses $O(\log n)$ time, where finding the node to
[file]{binary_search_tree}-[class]{binary_search_tree}-[func]{remove}
```
### Inorder traversal is ordered
### Inorder Traversal Is Ordered
As shown in the figure below, the inorder traversal of a binary tree follows the "left $\rightarrow$ root $\rightarrow$ right" traversal order, while the binary search tree satisfies the "left child node $<$ root node $<$ right child node" size relationship.
@@ -104,7 +104,7 @@ Using the property of inorder traversal being ascending, we can obtain ordered d
![Inorder traversal sequence of a binary search tree](binary_search_tree.assets/bst_inorder_traversal.png)
## Efficiency of binary search trees
## Efficiency of Binary Search Trees
Given a set of data, we consider using an array or a binary search tree for storage. Observing the table below, all operations in a binary search tree have logarithmic time complexity, providing stable and efficient performance. Arrays are more efficient than binary search trees only in scenarios with high-frequency additions and low-frequency searches and deletions.
@@ -122,7 +122,7 @@ However, if we continuously insert and remove nodes in a binary search tree, it
![Degradation of a binary search tree](binary_search_tree.assets/bst_degradation.png)
## Common applications of binary search trees
## Common Applications of Binary Search Trees
- Used as multi-level indexes in systems to implement efficient search, insertion, and removal operations.
- Serves as the underlying data structure for certain search algorithms.
+13 -13
View File
@@ -1,4 +1,4 @@
# Binary tree
# Binary Tree
A <u>binary tree</u> is a non-linear data structure that represents the derivation relationship between "ancestors" and "descendants" and embodies the divide-and-conquer logic of "one divides into two". Similar to a linked list, the basic unit of a binary tree is a node, and each node contains a value, a reference to its left child node, and a reference to its right child node.
@@ -213,7 +213,7 @@ Each node has two references (pointers), pointing respectively to the <u>left-ch
![Parent Node, child Node, subtree](binary_tree.assets/binary_tree_definition.png)
## Common terminology of binary trees
## Common Terminology of Binary Trees
The commonly used terminology of binary trees is shown in the figure below.
@@ -232,9 +232,9 @@ The commonly used terminology of binary trees is shown in the figure below.
Please note that we usually define "height" and "depth" as "the number of edges traversed", but some questions or textbooks may define them as "the number of nodes traversed". In this case, both height and depth need to be incremented by 1.
## Basic operations of binary trees
## Basic Operations of Binary Trees
### Initializing a binary tree
### Initializing a Binary Tree
Similar to a linked list, the initialization of a binary tree involves first creating the nodes and then establishing the references (pointers) between them.
@@ -461,11 +461,11 @@ Similar to a linked list, the initialization of a binary tree involves first cre
```
??? pythontutor "Code visualization"
??? pythontutor "Code Visualization"
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&cumulative=false&curInstr=3&heapPrimitives=nevernest&mode=display&origin=opt-frontend.js&py=311&rawInputLstJSON=%5B%5D&textReferences=false
### Inserting and removing nodes
### Inserting and Removing Nodes
Similar to a linked list, inserting and removing nodes in a binary tree can be achieved by modifying pointers. The figure below provides an example.
@@ -629,7 +629,7 @@ Similar to a linked list, inserting and removing nodes in a binary tree can be a
```
??? pythontutor "Code visualization"
??? pythontutor "Code Visualization"
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
@@ -637,9 +637,9 @@ Similar to a linked list, inserting and removing nodes in a binary tree can be a
It should be noted that inserting nodes may change the original logical structure of the binary tree, while removing nodes typically involves 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 outcomes.
## Common types of binary trees
## Common Types of Binary Trees
### Perfect binary tree
### Perfect Binary Tree
As shown in the figure below, a <u>perfect binary tree</u> has all levels completely filled with nodes. In a perfect binary tree, leaf nodes have a degree of $0$, while all other nodes have a degree of $2$. If the tree height is $h$, the total number of nodes is $2^{h+1} - 1$, exhibiting a standard exponential relationship that reflects the common phenomenon of cell division in nature.
@@ -649,25 +649,25 @@ As shown in the figure below, a <u>perfect binary tree</u> has all levels comple
![Perfect binary tree](binary_tree.assets/perfect_binary_tree.png)
### Complete binary tree
### Complete Binary Tree
As shown in the figure below, a <u>complete binary tree</u> only allows the bottom level to be incompletely filled, and the nodes at the bottom level must be filled continuously from left to right. Note that a perfect binary tree is also a complete binary tree.
![Complete binary tree](binary_tree.assets/complete_binary_tree.png)
### Full binary tree
### Full Binary Tree
As shown in the figure below, in a <u>full binary tree</u>, all nodes except leaf nodes have two child nodes.
![Full binary tree](binary_tree.assets/full_binary_tree.png)
### Balanced binary tree
### Balanced Binary Tree
As shown in the figure below, in a <u>balanced binary tree</u>, the absolute difference between the height of the left and right subtrees of any node does not exceed 1.
![Balanced binary tree](binary_tree.assets/balanced_binary_tree.png)
## Degeneration of binary trees
## Degeneration of Binary Trees
The figure below shows the ideal and degenerate structures of binary trees. When every level of a binary tree is filled, it reaches the "perfect binary tree" state; when all nodes are biased toward one side, the binary tree degenerates into a "linked list".
@@ -1,10 +1,10 @@
# Binary tree traversal
# Binary Tree Traversal
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.
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. Within each level, it visits nodes from left to right.
@@ -12,7 +12,7 @@ Level-order traversal is essentially <u>breadth-first traversal</u>, also known
![Level-order traversal of a binary tree](binary_tree_traversal.assets/binary_tree_bfs.png)
### Code implementation
### Code Implementation
Breadth-first traversal is typically 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:
@@ -20,12 +20,12 @@ Breadth-first traversal is typically implemented with the help of a "queue". The
[file]{binary_tree_bfs}-[class]{}-[func]{level_order}
```
### 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.
- **Space complexity is $O(n)$**: In the worst case, i.e., a full binary tree, before traversing to the bottom level, the queue contains at most $(n + 1) / 2$ nodes simultaneously, occupying $O(n)$ space.
## Preorder, inorder, and postorder traversal
## Preorder, Inorder, and Postorder Traversal
Correspondingly, preorder, inorder, and postorder traversals all belong to <u>depth-first traversal</u>, also known as <u>depth-first search (DFS)</u>, which embodies a "first go to the end, then backtrack and continue" traversal method.
@@ -33,7 +33,7 @@ The figure below shows how depth-first traversal works on a binary tree. **Depth
![Preorder, inorder, and postorder traversal of a binary tree](binary_tree_traversal.assets/binary_tree_dfs.png)
### Code implementation
### Code Implementation
Depth-first search is usually implemented based on recursion:
@@ -83,7 +83,7 @@ The figure below shows the recursive process of preorder traversal of a binary t
=== "<11>"
![preorder_step11](binary_tree_traversal.assets/preorder_step11.png)
### Complexity analysis
### 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 degenerates into a linked list, the recursion depth reaches $n$, and the system occupies $O(n)$ stack frame space.
+1 -1
View File
@@ -1,6 +1,6 @@
# Summary
### Key review
### Key Review
- A binary tree is a non-linear data structure that embodies the divide-and-conquer logic of "one divides into two". Each binary tree node contains a value and two pointers, which respectively point to its left and right child nodes.
- For a certain node in a binary tree, the tree formed by its left (right) child node and all nodes below is called the left (right) subtree of that node.