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

View File

@@ -9,7 +9,7 @@ package chapter_tree;
import utils.*;
import java.util.*;
/* Array-based binary tree class */
/* Binary tree class represented by array */
class ArrayBinaryTree {
private List<Integer> tree;
@@ -23,25 +23,25 @@ class ArrayBinaryTree {
return tree.size();
}
/* Get the value of the node at index i */
/* Get value of node at index i */
public Integer val(int i) {
// If the index is out of bounds, return null, representing an empty spot
// If index out of bounds, return null to represent empty position
if (i < 0 || i >= size())
return null;
return tree.get(i);
}
/* Get the index of the left child of the node at index i */
/* Get index of left child node of node at index i */
public Integer left(int i) {
return 2 * i + 1;
}
/* Get the index of the right child of the node at index i */
/* Get index of right child node of node at index i */
public Integer right(int i) {
return 2 * i + 2;
}
/* Get the index of the parent of the node at index i */
/* Get index of parent node of node at index i */
public Integer parent(int i) {
return (i - 1) / 2;
}
@@ -49,7 +49,7 @@ class ArrayBinaryTree {
/* Level-order traversal */
public List<Integer> levelOrder() {
List<Integer> res = new ArrayList<>();
// Traverse array
// Traverse array directly
for (int i = 0; i < size(); i++) {
if (val(i) != null)
res.add(val(i));
@@ -59,37 +59,37 @@ class ArrayBinaryTree {
/* Depth-first traversal */
private void dfs(Integer i, String order, List<Integer> res) {
// If it is an empty spot, return
// If empty position, return
if (val(i) == null)
return;
// Pre-order traversal
// Preorder traversal
if ("pre".equals(order))
res.add(val(i));
dfs(left(i), order, res);
// In-order traversal
// Inorder traversal
if ("in".equals(order))
res.add(val(i));
dfs(right(i), order, res);
// Post-order traversal
// Postorder traversal
if ("post".equals(order))
res.add(val(i));
}
/* Pre-order traversal */
/* Preorder traversal */
public List<Integer> preOrder() {
List<Integer> res = new ArrayList<>();
dfs(0, "pre", res);
return res;
}
/* In-order traversal */
/* Inorder traversal */
public List<Integer> inOrder() {
List<Integer> res = new ArrayList<>();
dfs(0, "in", res);
return res;
}
/* Post-order traversal */
/* Postorder traversal */
public List<Integer> postOrder() {
List<Integer> res = new ArrayList<>();
dfs(0, "post", res);
@@ -100,17 +100,17 @@ class ArrayBinaryTree {
public class array_binary_tree {
public static void main(String[] args) {
// Initialize binary tree
// Use a specific function to convert an array into a binary tree
// Here we use a function to generate a binary tree directly from an array
List<Integer> arr = Arrays.asList(1, 2, 3, 4, null, 6, 7, 8, 9, null, null, 12, null, null, 15);
TreeNode root = TreeNode.listToTree(arr);
System.out.println("\nInitialize binary tree\n");
System.out.println("Array representation of the binary tree:");
System.out.println("Array representation of binary tree:");
System.out.println(arr);
System.out.println("Linked list representation of the binary tree:");
System.out.println("Linked list representation of binary tree:");
PrintUtil.printTree(root);
// Array-based binary tree class
// Binary tree class represented by array
ArrayBinaryTree abt = new ArrayBinaryTree(arr);
// Access node
@@ -118,19 +118,19 @@ public class array_binary_tree {
Integer l = abt.left(i);
Integer r = abt.right(i);
Integer p = abt.parent(i);
System.out.println("\nThe current node's index is " + i + ", value = " + abt.val(i));
System.out.println("Its left child's index is " + l + ", value = " + (l == null ? "null" : abt.val(l)));
System.out.println("Its right child's index is " + r + ", value = " + (r == null ? "null" : abt.val(r)));
System.out.println("Its parent's index is " + p + ", value = " + (p == null ? "null" : abt.val(p)));
System.out.println("\nCurrent node index is " + i + ", value is " + abt.val(i));
System.out.println("Its left child node index is " + l + ", value is " + (l == null ? "null" : abt.val(l)));
System.out.println("Its right child node index is " + r + ", value is " + (r == null ? "null" : abt.val(r)));
System.out.println("Its parent node index is " + p + ", value is " + (p == null ? "null" : abt.val(p)));
// Traverse tree
List<Integer> res = abt.levelOrder();
System.out.println("\nLevel-order traversal is:" + res);
res = abt.preOrder();
System.out.println("Pre-order traversal is:" + res);
System.out.println("Preorder traversal is:" + res);
res = abt.inOrder();
System.out.println("In-order traversal is:" + res);
System.out.println("Inorder traversal is:" + res);
res = abt.postOrder();
System.out.println("Post-order traversal is:" + res);
System.out.println("Postorder traversal is:" + res);
}
}

View File

@@ -37,13 +37,13 @@ class AVLTree {
private TreeNode rightRotate(TreeNode node) {
TreeNode child = node.left;
TreeNode grandChild = child.right;
// Rotate node to the right around child
// Using child as pivot, rotate node to the right
child.right = node;
node.left = grandChild;
// Update node height
updateHeight(node);
updateHeight(child);
// Return the root of the subtree after rotation
// Return root node of subtree after rotation
return child;
}
@@ -51,19 +51,19 @@ class AVLTree {
private TreeNode leftRotate(TreeNode node) {
TreeNode child = node.right;
TreeNode grandChild = child.left;
// Rotate node to the left around child
// Using child as pivot, rotate node to the left
child.left = node;
node.right = grandChild;
// Update node height
updateHeight(node);
updateHeight(child);
// Return the root of the subtree after rotation
// Return root node of subtree after rotation
return child;
}
/* Perform rotation operation to restore balance to the subtree */
/* Perform rotation operation to restore balance to this subtree */
private TreeNode rotate(TreeNode node) {
// Get the balance factor of node
// Get balance factor of node
int balanceFactor = balanceFactor(node);
// Left-leaning tree
if (balanceFactor > 1) {
@@ -87,7 +87,7 @@ class AVLTree {
return leftRotate(node);
}
}
// Balanced tree, no rotation needed, return
// Balanced tree, no rotation needed, return directly
return node;
}
@@ -106,11 +106,11 @@ class AVLTree {
else if (val > node.val)
node.right = insertHelper(node.right, val);
else
return node; // Do not insert duplicate nodes, return
return node; // Duplicate node not inserted, return directly
updateHeight(node); // Update node height
/* 2. Perform rotation operation to restore balance to the subtree */
/* 2. Perform rotation operation to restore balance to this subtree */
node = rotate(node);
// Return the root node of the subtree
// Return root node of subtree
return node;
}
@@ -119,11 +119,11 @@ class AVLTree {
root = removeHelper(root, val);
}
/* Recursively remove node (helper method) */
/* Recursively delete node (helper method) */
private TreeNode removeHelper(TreeNode node, int val) {
if (node == null)
return null;
/* 1. Find and remove the node */
/* 1. Find node and delete */
if (val < node.val)
node.left = removeHelper(node.left, val);
else if (val > node.val)
@@ -131,14 +131,14 @@ class AVLTree {
else {
if (node.left == null || node.right == null) {
TreeNode child = node.left != null ? node.left : node.right;
// Number of child nodes = 0, remove node and return
// Number of child nodes = 0, delete node directly and return
if (child == null)
return null;
// Number of child nodes = 1, remove node
// Number of child nodes = 1, delete node directly
else
node = child;
} else {
// Number of child nodes = 2, remove the next node in in-order traversal and replace the current node with it
// Number of child nodes = 2, delete the next node in inorder traversal and replace current node with it
TreeNode temp = node.right;
while (temp.left != null) {
temp = temp.left;
@@ -148,16 +148,16 @@ class AVLTree {
}
}
updateHeight(node); // Update node height
/* 2. Perform rotation operation to restore balance to the subtree */
/* 2. Perform rotation operation to restore balance to this subtree */
node = rotate(node);
// Return the root node of the subtree
// Return root node of subtree
return node;
}
/* Search node */
public TreeNode search(int val) {
TreeNode cur = root;
// Loop find, break after passing leaf nodes
// Loop search, exit after passing leaf node
while (cur != null) {
// Target node is in cur's right subtree
if (cur.val < val)
@@ -165,7 +165,7 @@ class AVLTree {
// Target node is in cur's left subtree
else if (cur.val > val)
cur = cur.left;
// Found target node, break loop
// Found target node, exit loop
else
break;
}
@@ -177,22 +177,22 @@ class AVLTree {
public class avl_tree {
static void testInsert(AVLTree tree, int val) {
tree.insert(val);
System.out.println("\nAfter inserting node " + val + ", the AVL tree is ");
System.out.println("\nAfter inserting node " + val + ", AVL tree is");
PrintUtil.printTree(tree.root);
}
static void testRemove(AVLTree tree, int val) {
tree.remove(val);
System.out.println("\nAfter removing node " + val + ", the AVL tree is ");
System.out.println("\nAfter removing node " + val + ", AVL tree is");
PrintUtil.printTree(tree.root);
}
public static void main(String[] args) {
/* Initialize empty AVL tree */
/* Please pay attention to how the AVL tree maintains balance after inserting nodes */
AVLTree avlTree = new AVLTree();
/* Insert node */
// Notice how the AVL tree maintains balance after inserting nodes
// Delete nodes
testInsert(avlTree, 1);
testInsert(avlTree, 2);
testInsert(avlTree, 3);
@@ -204,17 +204,17 @@ public class avl_tree {
testInsert(avlTree, 10);
testInsert(avlTree, 6);
/* Insert duplicate node */
/* Please pay attention to how the AVL tree maintains balance after deleting nodes */
testInsert(avlTree, 7);
/* Remove node */
// Notice how the AVL tree maintains balance after removing nodes
testRemove(avlTree, 8); // Remove node with degree 0
// Delete node with degree 1
testRemove(avlTree, 8); // Delete node with degree 2
testRemove(avlTree, 5); // Remove node with degree 1
testRemove(avlTree, 4); // Remove node with degree 2
/* Search node */
TreeNode node = avlTree.search(7);
System.out.println("\nThe found node object is " + node + ", node value = " + node.val);
System.out.println("\nFound node object is " + node + ", node value = " + node.val);
}
}

View File

@@ -26,7 +26,7 @@ class BinarySearchTree {
/* Search node */
public TreeNode search(int num) {
TreeNode cur = root;
// Loop find, break after passing leaf nodes
// Loop search, exit after passing leaf node
while (cur != null) {
// Target node is in cur's right subtree
if (cur.val < num)
@@ -34,7 +34,7 @@ class BinarySearchTree {
// Target node is in cur's left subtree
else if (cur.val > num)
cur = cur.left;
// Found target node, break loop
// Found target node, exit loop
else
break;
}
@@ -50,9 +50,9 @@ class BinarySearchTree {
return;
}
TreeNode cur = root, pre = null;
// Loop find, break after passing leaf nodes
// Loop search, exit after passing leaf node
while (cur != null) {
// Found duplicate node, thus return
// Found duplicate node, return directly
if (cur.val == num)
return;
pre = cur;
@@ -73,49 +73,49 @@ class BinarySearchTree {
/* Remove node */
public void remove(int num) {
// If tree is empty, return
// If tree is empty, return directly
if (root == null)
return;
TreeNode cur = root, pre = null;
// Loop find, break after passing leaf nodes
// Loop search, exit after passing leaf node
while (cur != null) {
// Found node to be removed, break loop
// Found node to delete, exit loop
if (cur.val == num)
break;
pre = cur;
// Node to be removed is in cur's right subtree
// Node to delete is in cur's right subtree
if (cur.val < num)
cur = cur.right;
// Node to be removed is in cur's left subtree
// Node to delete is in cur's left subtree
else
cur = cur.left;
}
// If no node to be removed, return
// If no node to delete, return directly
if (cur == null)
return;
// Number of child nodes = 0 or 1
if (cur.left == null || cur.right == null) {
// When the number of child nodes = 0/1, child = null/that child node
// When number of child nodes = 0 / 1, child = null / that child node
TreeNode child = cur.left != null ? cur.left : cur.right;
// Remove node cur
// Delete node cur
if (cur != root) {
if (pre.left == cur)
pre.left = child;
else
pre.right = child;
} else {
// If the removed node is the root, reassign the root
// If deleted node is root node, reassign root node
root = child;
}
}
// Number of child nodes = 2
else {
// Get the next node in in-order traversal of cur
// Get next node of cur in inorder traversal
TreeNode tmp = cur.right;
while (tmp.left != null) {
tmp = tmp.left;
}
// Recursively remove node tmp
// Recursively delete node tmp
remove(tmp.val);
// Replace cur with tmp
cur.val = tmp.val;
@@ -127,7 +127,7 @@ public class binary_search_tree {
public static void main(String[] args) {
/* Initialize binary search tree */
BinarySearchTree bst = new BinarySearchTree();
// Note that different insertion orders can result in various tree structures. This particular sequence creates a perfect binary tree
// Please note that different insertion orders will generate different binary trees, this sequence can generate a perfect binary tree
int[] nums = { 8, 4, 12, 2, 6, 10, 14, 1, 3, 5, 7, 9, 11, 13, 15 };
for (int num : nums) {
bst.insert(num);
@@ -137,22 +137,22 @@ public class binary_search_tree {
/* Search node */
TreeNode node = bst.search(7);
System.out.println("\nThe found node object is " + node + ", node value = " + node.val);
System.out.println("\nFound node object is " + node + ", node value = " + node.val);
/* Insert node */
bst.insert(16);
System.out.println("\nAfter inserting node 16, the binary tree is\n");
System.out.println("\nAfter inserting node 16, binary tree is\n");
PrintUtil.printTree(bst.getRoot());
/* Remove node */
bst.remove(1);
System.out.println("\nAfter removing node 1, the binary tree is\n");
System.out.println("\nAfter removing node 1, binary tree is\n");
PrintUtil.printTree(bst.getRoot());
bst.remove(2);
System.out.println("\nAfter removing node 2, the binary tree is\n");
System.out.println("\nAfter removing node 2, binary tree is\n");
PrintUtil.printTree(bst.getRoot());
bst.remove(4);
System.out.println("\nAfter removing node 4, the binary tree is\n");
System.out.println("\nAfter removing node 4, binary tree is\n");
PrintUtil.printTree(bst.getRoot());
}
}

View File

@@ -11,13 +11,13 @@ import utils.*;
public class binary_tree {
public static void main(String[] args) {
/* Initialize binary tree */
// Initialize node
// Initialize nodes
TreeNode n1 = new TreeNode(1);
TreeNode n2 = new TreeNode(2);
TreeNode n3 = new TreeNode(3);
TreeNode n4 = new TreeNode(4);
TreeNode n5 = new TreeNode(5);
// Construct node references (pointers)
// Build references (pointers) between nodes
n1.left = n2;
n1.right = n3;
n2.left = n4;
@@ -25,9 +25,9 @@ public class binary_tree {
System.out.println("\nInitialize binary tree\n");
PrintUtil.printTree(n1);
/* Insert and remove nodes */
/* Insert node P between n1 -> n2 */
TreeNode P = new TreeNode(0);
// Insert node P between n1 -> n2
// Delete node
n1.left = P;
P.left = n2;
System.out.println("\nAfter inserting node P\n");

View File

@@ -15,28 +15,28 @@ public class binary_tree_bfs {
// Initialize queue, add root node
Queue<TreeNode> queue = new LinkedList<>();
queue.add(root);
// Initialize a list to store the traversal sequence
// Initialize a list to save the traversal sequence
List<Integer> list = new ArrayList<>();
while (!queue.isEmpty()) {
TreeNode node = queue.poll(); // Queue dequeues
TreeNode node = queue.poll(); // Dequeue
list.add(node.val); // Save node value
if (node.left != null)
queue.offer(node.left); // Left child node enqueues
queue.offer(node.left); // Left child node enqueue
if (node.right != null)
queue.offer(node.right); // Right child node enqueues
queue.offer(node.right); // Right child node enqueue
}
return list;
}
public static void main(String[] args) {
/* Initialize binary tree */
// Use a specific function to convert an array into a binary tree
// Here we use a function to generate a binary tree directly from an array
TreeNode root = TreeNode.listToTree(Arrays.asList(1, 2, 3, 4, 5, 6, 7));
System.out.println("\nInitialize binary tree\n");
PrintUtil.printTree(root);
/* Level-order traversal */
List<Integer> list = levelOrder(root);
System.out.println("\nPrint sequence of nodes from level-order traversal = " + list);
System.out.println("\nLevel-order traversal node print sequence = " + list);
}
}

View File

@@ -10,10 +10,10 @@ import utils.*;
import java.util.*;
public class binary_tree_dfs {
// Initialize the list for storing traversal sequences
// Initialize list for storing traversal sequence
static ArrayList<Integer> list = new ArrayList<>();
/* Pre-order traversal */
/* Preorder traversal */
static void preOrder(TreeNode root) {
if (root == null)
return;
@@ -23,7 +23,7 @@ public class binary_tree_dfs {
preOrder(root.right);
}
/* In-order traversal */
/* Inorder traversal */
static void inOrder(TreeNode root) {
if (root == null)
return;
@@ -33,7 +33,7 @@ public class binary_tree_dfs {
inOrder(root.right);
}
/* Post-order traversal */
/* Postorder traversal */
static void postOrder(TreeNode root) {
if (root == null)
return;
@@ -45,24 +45,24 @@ public class binary_tree_dfs {
public static void main(String[] args) {
/* Initialize binary tree */
// Use a specific function to convert an array into a binary tree
// Here we use a function to generate a binary tree directly from an array
TreeNode root = TreeNode.listToTree(Arrays.asList(1, 2, 3, 4, 5, 6, 7));
System.out.println("\nInitialize binary tree\n");
PrintUtil.printTree(root);
/* Pre-order traversal */
/* Preorder traversal */
list.clear();
preOrder(root);
System.out.println("\nPrint sequence of nodes from pre-order traversal = " + list);
System.out.println("\nPreorder traversal node print sequence = " + list);
/* In-order traversal */
/* Inorder traversal */
list.clear();
inOrder(root);
System.out.println("\nPrint sequence of nodes from in-order traversal = " + list);
System.out.println("\nInorder traversal node print sequence = " + list);
/* Post-order traversal */
/* Postorder traversal */
list.clear();
postOrder(root);
System.out.println("\nPrint sequence of nodes from post-order traversal = " + list);
System.out.println("\nPostorder traversal node print sequence = " + list);
}
}