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
+6
View File
@@ -0,0 +1,6 @@
add_executable(avl_tree avl_tree.c)
add_executable(binary_tree binary_tree.c)
add_executable(binary_tree_bfs binary_tree_bfs.c)
add_executable(binary_tree_dfs binary_tree_dfs.c)
add_executable(binary_search_tree binary_search_tree.c)
add_executable(array_binary_tree array_binary_tree.c)
+166
View File
@@ -0,0 +1,166 @@
/**
* File: array_binary_tree.c
* Created Time: 2023-07-29
* Author: Gonglja (glj0@outlook.com)
*/
#include "../utils/common.h"
/* Binary tree structure in array representation */
typedef struct {
int *tree;
int size;
} ArrayBinaryTree;
/* Constructor */
ArrayBinaryTree *newArrayBinaryTree(int *arr, int arrSize) {
ArrayBinaryTree *abt = (ArrayBinaryTree *)malloc(sizeof(ArrayBinaryTree));
abt->tree = malloc(sizeof(int) * arrSize);
memcpy(abt->tree, arr, sizeof(int) * arrSize);
abt->size = arrSize;
return abt;
}
/* Destructor */
void delArrayBinaryTree(ArrayBinaryTree *abt) {
free(abt->tree);
free(abt);
}
/* List capacity */
int size(ArrayBinaryTree *abt) {
return abt->size;
}
/* Get value of node at index i */
int val(ArrayBinaryTree *abt, int i) {
// Return INT_MAX if index out of bounds, representing empty position
if (i < 0 || i >= size(abt))
return INT_MAX;
return abt->tree[i];
}
/* Get index of left child node of node at index i */
int left(int i) {
return 2 * i + 1;
}
/* Get index of right child node of node at index i */
int right(int i) {
return 2 * i + 2;
}
/* Get index of parent node of node at index i */
int parent(int i) {
return (i - 1) / 2;
}
/* Level-order traversal */
int *levelOrder(ArrayBinaryTree *abt, int *returnSize) {
int *res = (int *)malloc(sizeof(int) * size(abt));
int index = 0;
// Traverse array directly
for (int i = 0; i < size(abt); i++) {
if (val(abt, i) != INT_MAX)
res[index++] = val(abt, i);
}
*returnSize = index;
return res;
}
/* Depth-first traversal */
void dfs(ArrayBinaryTree *abt, int i, char *order, int *res, int *index) {
// If empty position, return
if (val(abt, i) == INT_MAX)
return;
// Preorder traversal
if (strcmp(order, "pre") == 0)
res[(*index)++] = val(abt, i);
dfs(abt, left(i), order, res, index);
// Inorder traversal
if (strcmp(order, "in") == 0)
res[(*index)++] = val(abt, i);
dfs(abt, right(i), order, res, index);
// Postorder traversal
if (strcmp(order, "post") == 0)
res[(*index)++] = val(abt, i);
}
/* Preorder traversal */
int *preOrder(ArrayBinaryTree *abt, int *returnSize) {
int *res = (int *)malloc(sizeof(int) * size(abt));
int index = 0;
dfs(abt, 0, "pre", res, &index);
*returnSize = index;
return res;
}
/* Inorder traversal */
int *inOrder(ArrayBinaryTree *abt, int *returnSize) {
int *res = (int *)malloc(sizeof(int) * size(abt));
int index = 0;
dfs(abt, 0, "in", res, &index);
*returnSize = index;
return res;
}
/* Postorder traversal */
int *postOrder(ArrayBinaryTree *abt, int *returnSize) {
int *res = (int *)malloc(sizeof(int) * size(abt));
int index = 0;
dfs(abt, 0, "post", res, &index);
*returnSize = index;
return res;
}
/* Driver Code */
int main() {
// Initialize binary tree
// Use INT_MAX to represent NULL
int arr[] = {1, 2, 3, 4, INT_MAX, 6, 7, 8, 9, INT_MAX, INT_MAX, 12, INT_MAX, INT_MAX, 15};
int arrSize = sizeof(arr) / sizeof(arr[0]);
TreeNode *root = arrayToTree(arr, arrSize);
printf("\nInitialize binary tree\n");
printf("Array representation of binary tree:\n");
printArray(arr, arrSize);
printf("Linked list representation of binary tree:\n");
printTree(root);
ArrayBinaryTree *abt = newArrayBinaryTree(arr, arrSize);
// Access node
int i = 1;
int l = left(i), r = right(i), p = parent(i);
printf("\nCurrent node index is %d, value is %d\n", i, val(abt, i));
printf("Its left child index is %d, value is %d\n", l, l < arrSize ? val(abt, l) : INT_MAX);
printf("Its right child index is %d, value is %d\n", r, r < arrSize ? val(abt, r) : INT_MAX);
printf("Its parent node index is %d, value is %d\n", p, p < arrSize ? val(abt, p) : INT_MAX);
// Traverse tree
int returnSize;
int *res;
res = levelOrder(abt, &returnSize);
printf("\nLevel-order traversal: ");
printArray(res, returnSize);
free(res);
res = preOrder(abt, &returnSize);
printf("Pre-order traversal: ");
printArray(res, returnSize);
free(res);
res = inOrder(abt, &returnSize);
printf("In-order traversal: ");
printArray(res, returnSize);
free(res);
res = postOrder(abt, &returnSize);
printf("Post-order traversal: ");
printArray(res, returnSize);
free(res);
// Free memory
delArrayBinaryTree(abt);
return 0;
}
+259
View File
@@ -0,0 +1,259 @@
/**
* File: avl_tree.c
* Created Time: 2023-01-15
* Author: Reanon (793584285@qq.com)
*/
#include "../utils/common.h"
/* AVL tree structure */
typedef struct {
TreeNode *root;
} AVLTree;
/* Constructor */
AVLTree *newAVLTree() {
AVLTree *tree = (AVLTree *)malloc(sizeof(AVLTree));
tree->root = NULL;
return tree;
}
/* Destructor */
void delAVLTree(AVLTree *tree) {
freeMemoryTree(tree->root);
free(tree);
}
/* Get node height */
int height(TreeNode *node) {
// Empty node height is -1, leaf node height is 0
if (node != NULL) {
return node->height;
}
return -1;
}
/* Update node height */
void updateHeight(TreeNode *node) {
int lh = height(node->left);
int rh = height(node->right);
// Node height equals the height of the tallest subtree + 1
if (lh > rh) {
node->height = lh + 1;
} else {
node->height = rh + 1;
}
}
/* Get balance factor */
int balanceFactor(TreeNode *node) {
// Empty node balance factor is 0
if (node == NULL) {
return 0;
}
// Node balance factor = left subtree height - right subtree height
return height(node->left) - height(node->right);
}
/* Right rotation operation */
TreeNode *rightRotate(TreeNode *node) {
TreeNode *child, *grandChild;
child = node->left;
grandChild = child->right;
// Using child as pivot, rotate node to the right
child->right = node;
node->left = grandChild;
// Update node height
updateHeight(node);
updateHeight(child);
// Return root node of subtree after rotation
return child;
}
/* Left rotation operation */
TreeNode *leftRotate(TreeNode *node) {
TreeNode *child, *grandChild;
child = node->right;
grandChild = child->left;
// Using child as pivot, rotate node to the left
child->left = node;
node->right = grandChild;
// Update node height
updateHeight(node);
updateHeight(child);
// Return root node of subtree after rotation
return child;
}
/* Perform rotation operation to restore balance to this subtree */
TreeNode *rotate(TreeNode *node) {
// Get balance factor of node
int bf = balanceFactor(node);
// Left-leaning tree
if (bf > 1) {
if (balanceFactor(node->left) >= 0) {
// Right rotation
return rightRotate(node);
} else {
// First left rotation then right rotation
node->left = leftRotate(node->left);
return rightRotate(node);
}
}
// Right-leaning tree
if (bf < -1) {
if (balanceFactor(node->right) <= 0) {
// Left rotation
return leftRotate(node);
} else {
// First right rotation then left rotation
node->right = rightRotate(node->right);
return leftRotate(node);
}
}
// Balanced tree, no rotation needed, return directly
return node;
}
/* Recursively insert node (helper function) */
TreeNode *insertHelper(TreeNode *node, int val) {
if (node == NULL) {
return newTreeNode(val);
}
/* 1. Find insertion position and insert node */
if (val < node->val) {
node->left = insertHelper(node->left, val);
} else if (val > node->val) {
node->right = insertHelper(node->right, val);
} else {
// Duplicate node not inserted, return directly
return node;
}
// Update node height
updateHeight(node);
/* 2. Perform rotation operation to restore balance to this subtree */
node = rotate(node);
// Return root node of subtree
return node;
}
/* Insert node */
void insert(AVLTree *tree, int val) {
tree->root = insertHelper(tree->root, val);
}
/* Recursively remove node (helper function) */
TreeNode *removeHelper(TreeNode *node, int val) {
TreeNode *child, *grandChild;
if (node == NULL) {
return NULL;
}
/* 1. Find node and delete */
if (val < node->val) {
node->left = removeHelper(node->left, val);
} else if (val > node->val) {
node->right = removeHelper(node->right, val);
} else {
if (node->left == NULL || node->right == NULL) {
child = node->left;
if (node->right != NULL) {
child = node->right;
}
// Number of child nodes = 0, delete node directly and return
if (child == NULL) {
return NULL;
} else {
// Number of child nodes = 1, delete node directly
node = child;
}
} else {
// 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;
}
int tempVal = temp->val;
node->right = removeHelper(node->right, temp->val);
node->val = tempVal;
}
}
// Update node height
updateHeight(node);
/* 2. Perform rotation operation to restore balance to this subtree */
node = rotate(node);
// Return root node of subtree
return node;
}
/* Remove node */
// Cannot use remove keyword here due to stdio.h inclusion
void removeItem(AVLTree *tree, int val) {
TreeNode *root = removeHelper(tree->root, val);
}
/* Search node */
TreeNode *search(AVLTree *tree, int val) {
TreeNode *cur = tree->root;
// Loop search, exit after passing leaf node
while (cur != NULL) {
if (cur->val < val) {
// Target node is in cur's right subtree
cur = cur->right;
} else if (cur->val > val) {
// Target node is in cur's left subtree
cur = cur->left;
} else {
// Found target node, exit loop
break;
}
}
// Found target node, exit loop
return cur;
}
void testInsert(AVLTree *tree, int val) {
insert(tree, val);
printf("\nAfter inserting node %d, AVL tree is \n", val);
printTree(tree->root);
}
void testRemove(AVLTree *tree, int val) {
removeItem(tree, val);
printf("\nAfter removing node %d, AVL tree is \n", val);
printTree(tree->root);
}
/* Driver Code */
int main() {
/* Please pay attention to how the AVL tree maintains balance after inserting nodes */
AVLTree *tree = (AVLTree *)newAVLTree();
/* Insert node */
// Delete nodes
testInsert(tree, 1);
testInsert(tree, 2);
testInsert(tree, 3);
testInsert(tree, 4);
testInsert(tree, 5);
testInsert(tree, 8);
testInsert(tree, 7);
testInsert(tree, 9);
testInsert(tree, 10);
testInsert(tree, 6);
/* Please pay attention to how the AVL tree maintains balance after deleting nodes */
testInsert(tree, 7);
/* Remove node */
// Delete node with degree 1
testRemove(tree, 8); // Delete node with degree 2
testRemove(tree, 5); // Remove node with degree 1
testRemove(tree, 4); // Remove node with degree 2
/* Search node */
TreeNode *node = search(tree, 7);
printf("\nFound node object value = %d \n", node->val);
// Free memory
delAVLTree(tree);
return 0;
}
@@ -0,0 +1,171 @@
/**
* File: binary_search_tree.c
* Created Time: 2023-01-11
* Author: Reanon (793584285@qq.com)
*/
#include "../utils/common.h"
/* Binary search tree structure */
typedef struct {
TreeNode *root;
} BinarySearchTree;
/* Constructor */
BinarySearchTree *newBinarySearchTree() {
// Initialize empty tree
BinarySearchTree *bst = (BinarySearchTree *)malloc(sizeof(BinarySearchTree));
bst->root = NULL;
return bst;
}
/* Destructor */
void delBinarySearchTree(BinarySearchTree *bst) {
freeMemoryTree(bst->root);
free(bst);
}
/* Get binary tree root node */
TreeNode *getRoot(BinarySearchTree *bst) {
return bst->root;
}
/* Search node */
TreeNode *search(BinarySearchTree *bst, int num) {
TreeNode *cur = bst->root;
// Loop search, exit after passing leaf node
while (cur != NULL) {
if (cur->val < num) {
// Target node is in cur's right subtree
cur = cur->right;
} else if (cur->val > num) {
// Target node is in cur's left subtree
cur = cur->left;
} else {
// Found target node, exit loop
break;
}
}
// Return target node
return cur;
}
/* Insert node */
void insert(BinarySearchTree *bst, int num) {
// If tree is empty, initialize root node
if (bst->root == NULL) {
bst->root = newTreeNode(num);
return;
}
TreeNode *cur = bst->root, *pre = NULL;
// Loop search, exit after passing leaf node
while (cur != NULL) {
// Found duplicate node, return directly
if (cur->val == num) {
return;
}
pre = cur;
if (cur->val < num) {
// Insertion position is in cur's right subtree
cur = cur->right;
} else {
// Insertion position is in cur's left subtree
cur = cur->left;
}
}
// Insert node
TreeNode *node = newTreeNode(num);
if (pre->val < num) {
pre->right = node;
} else {
pre->left = node;
}
}
/* Remove node */
// Cannot use remove keyword here due to stdio.h inclusion
void removeItem(BinarySearchTree *bst, int num) {
// If tree is empty, return directly
if (bst->root == NULL)
return;
TreeNode *cur = bst->root, *pre = NULL;
// Loop search, exit after passing leaf node
while (cur != NULL) {
// Found node to delete, exit loop
if (cur->val == num)
break;
pre = cur;
if (cur->val < num) {
// Node to delete is in right subtree of root
cur = cur->right;
} else {
// Node to delete is in left subtree of root
cur = cur->left;
}
}
// If no node to delete, return directly
if (cur == NULL)
return;
// Check if node to delete has children
if (cur->left == NULL || cur->right == NULL) {
/* Number of child nodes = 0 or 1 */
// When number of child nodes = 0 / 1, child = nullptr / that child node
TreeNode *child = cur->left != NULL ? cur->left : cur->right;
// Delete node cur
if (pre->left == cur) {
pre->left = child;
} else {
pre->right = child;
}
// Free memory
free(cur);
} else {
/* Number of child nodes = 2 */
// Get next node of cur in inorder traversal
TreeNode *tmp = cur->right;
while (tmp->left != NULL) {
tmp = tmp->left;
}
int tmpVal = tmp->val;
// Recursively delete node tmp
removeItem(bst, tmp->val);
// Replace cur with tmp
cur->val = tmpVal;
}
}
/* Driver Code */
int main() {
/* Initialize binary search tree */
int nums[] = {8, 4, 12, 2, 6, 10, 14, 1, 3, 5, 7, 9, 11, 13, 15};
BinarySearchTree *bst = newBinarySearchTree();
for (int i = 0; i < sizeof(nums) / sizeof(int); i++) {
insert(bst, nums[i]);
}
printf("Initialized binary tree is\n");
printTree(getRoot(bst));
/* Search node */
TreeNode *node = search(bst, 7);
printf("Found node object value = %d\n", node->val);
/* Insert node */
insert(bst, 16);
printf("After inserting node 16, binary tree is\n");
printTree(getRoot(bst));
/* Remove node */
removeItem(bst, 1);
printf("After removing node 1, binary tree is\n");
printTree(getRoot(bst));
removeItem(bst, 2);
printf("After removing node 2, binary tree is\n");
printTree(getRoot(bst));
removeItem(bst, 4);
printf("After removing node 4, binary tree is\n");
printTree(getRoot(bst));
// Free memory
delBinarySearchTree(bst);
return 0;
}
+43
View File
@@ -0,0 +1,43 @@
/**
* File: binary_tree.c
* Created Time: 2023-01-11
* Author: Reanon (793584285@qq.com)
*/
#include "../utils/common.h"
/* Driver Code */
int main() {
/* Initialize binary tree */
// Initialize nodes
TreeNode *n1 = newTreeNode(1);
TreeNode *n2 = newTreeNode(2);
TreeNode *n3 = newTreeNode(3);
TreeNode *n4 = newTreeNode(4);
TreeNode *n5 = newTreeNode(5);
// Build references (pointers) between nodes
n1->left = n2;
n1->right = n3;
n2->left = n4;
n2->right = n5;
printf("Initialize binary tree\n");
printTree(n1);
/* Insert node P between n1 -> n2 */
TreeNode *P = newTreeNode(0);
// Delete node
n1->left = P;
P->left = n2;
printf("After inserting node P\n");
printTree(n1);
// Remove node P
n1->left = n2;
// Free memory
free(P);
printf("After removing node P\n");
printTree(n1);
freeMemoryTree(n1);
return 0;
}
+73
View File
@@ -0,0 +1,73 @@
/**
* File: binary_tree_bfs.c
* Created Time: 2023-01-11
* Author: Reanon (793584285@qq.com)
*/
#include "../utils/common.h"
#define MAX_SIZE 100
/* Level-order traversal */
int *levelOrder(TreeNode *root, int *size) {
/* Auxiliary queue */
int front, rear;
int index, *arr;
TreeNode *node;
TreeNode **queue;
/* Auxiliary queue */
queue = (TreeNode **)malloc(sizeof(TreeNode *) * MAX_SIZE);
// Queue pointer
front = 0, rear = 0;
// Add root node
queue[rear++] = root;
// Initialize a list to save the traversal sequence
/* Auxiliary array */
arr = (int *)malloc(sizeof(int) * MAX_SIZE);
// Array pointer
index = 0;
while (front < rear) {
// Dequeue
node = queue[front++];
// Save node value
arr[index++] = node->val;
if (node->left != NULL) {
// Left child node enqueue
queue[rear++] = node->left;
}
if (node->right != NULL) {
// Right child node enqueue
queue[rear++] = node->right;
}
}
// Update array length value
*size = index;
arr = realloc(arr, sizeof(int) * (*size));
// Free auxiliary array space
free(queue);
return arr;
}
/* Driver Code */
int main() {
/* Initialize binary tree */
// Here we use a function to generate a binary tree directly from an array
int nums[] = {1, 2, 3, 4, 5, 6, 7};
int size = sizeof(nums) / sizeof(int);
TreeNode *root = arrayToTree(nums, size);
printf("Initialize binary tree\n");
printTree(root);
/* Level-order traversal */
// Need to pass array length
int *arr = levelOrder(root, &size);
printf("Level-order traversal node print sequence = ");
printArray(arr, size);
// Free memory
freeMemoryTree(root);
free(arr);
return 0;
}
+75
View File
@@ -0,0 +1,75 @@
/**
* File: binary_tree_dfs.c
* Created Time: 2023-01-11
* Author: Reanon (793584285@qq.com)
*/
#include "../utils/common.h"
#define MAX_SIZE 100
// Auxiliary array for storing traversal sequence
int arr[MAX_SIZE];
/* Preorder traversal */
void preOrder(TreeNode *root, int *size) {
if (root == NULL)
return;
// Visit priority: root node -> left subtree -> right subtree
arr[(*size)++] = root->val;
preOrder(root->left, size);
preOrder(root->right, size);
}
/* Inorder traversal */
void inOrder(TreeNode *root, int *size) {
if (root == NULL)
return;
// Visit priority: left subtree -> root node -> right subtree
inOrder(root->left, size);
arr[(*size)++] = root->val;
inOrder(root->right, size);
}
/* Postorder traversal */
void postOrder(TreeNode *root, int *size) {
if (root == NULL)
return;
// Visit priority: left subtree -> right subtree -> root node
postOrder(root->left, size);
postOrder(root->right, size);
arr[(*size)++] = root->val;
}
/* Driver Code */
int main() {
/* Initialize binary tree */
// Here we use a function to generate a binary tree directly from an array
int nums[] = {1, 2, 3, 4, 5, 6, 7};
int size = sizeof(nums) / sizeof(int);
TreeNode *root = arrayToTree(nums, size);
printf("Initialize binary tree\n");
printTree(root);
/* Preorder traversal */
// Initialize auxiliary array
size = 0;
preOrder(root, &size);
printf("Pre-order traversal node print sequence = ");
printArray(arr, size);
/* Inorder traversal */
size = 0;
inOrder(root, &size);
printf("In-order traversal node print sequence = ");
printArray(arr, size);
/* Postorder traversal */
size = 0;
postOrder(root, &size);
printf("Post-order traversal node print sequence = ");
printArray(arr, size);
freeMemoryTree(root);
return 0;
}