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
@@ -0,0 +1,141 @@
/**
* File: array_binary_tree.swift
* Created Time: 2023-07-23
* Author: nuomi1 (nuomi1@qq.com)
*/
import utils
/* Binary tree class represented by array */
class ArrayBinaryTree {
private var tree: [Int?]
/* Constructor */
init(arr: [Int?]) {
tree = arr
}
/* List capacity */
func size() -> Int {
tree.count
}
/* Get value of node at index i */
func val(i: Int) -> Int? {
// If index out of bounds, return null to represent empty position
if i < 0 || i >= size() {
return nil
}
return tree[i]
}
/* Get index of left child node of node at index i */
func left(i: Int) -> Int {
2 * i + 1
}
/* Get index of right child node of node at index i */
func right(i: Int) -> Int {
2 * i + 2
}
/* Get index of parent node of node at index i */
func parent(i: Int) -> Int {
(i - 1) / 2
}
/* Level-order traversal */
func levelOrder() -> [Int] {
var res: [Int] = []
// Traverse array directly
for i in 0 ..< size() {
if let val = val(i: i) {
res.append(val)
}
}
return res
}
/* Depth-first traversal */
private func dfs(i: Int, order: String, res: inout [Int]) {
// If empty position, return
guard let val = val(i: i) else {
return
}
// Preorder traversal
if order == "pre" {
res.append(val)
}
dfs(i: left(i: i), order: order, res: &res)
// Inorder traversal
if order == "in" {
res.append(val)
}
dfs(i: right(i: i), order: order, res: &res)
// Postorder traversal
if order == "post" {
res.append(val)
}
}
/* Preorder traversal */
func preOrder() -> [Int] {
var res: [Int] = []
dfs(i: 0, order: "pre", res: &res)
return res
}
/* Inorder traversal */
func inOrder() -> [Int] {
var res: [Int] = []
dfs(i: 0, order: "in", res: &res)
return res
}
/* Postorder traversal */
func postOrder() -> [Int] {
var res: [Int] = []
dfs(i: 0, order: "post", res: &res)
return res
}
}
@main
enum _ArrayBinaryTree {
/* Driver Code */
static func main() {
// Initialize binary tree
// Here we use a function to generate a binary tree directly from an array
let arr = [1, 2, 3, 4, nil, 6, 7, 8, 9, nil, nil, 12, nil, nil, 15]
let root = TreeNode.listToTree(arr: arr)
print("\nInitialize binary tree\n")
print("Array representation of binary tree:")
print(arr)
print("Linked list representation of binary tree:")
PrintUtil.printTree(root: root)
// Binary tree class represented by array
let abt = ArrayBinaryTree(arr: arr)
// Access node
let i = 1
let l = abt.left(i: i)
let r = abt.right(i: i)
let p = abt.parent(i: i)
print("\nCurrent node index is \(i), value is \(abt.val(i: i) as Any)")
print("Its left child index is \(l), value is \(abt.val(i: l) as Any)")
print("Its right child index is \(r), value is \(abt.val(i: r) as Any)")
print("Its parent node index is \(p), value is \(abt.val(i: p) as Any)")
// Traverse tree
var res = abt.levelOrder()
print("\nLevel-order traversal is: \(res)")
res = abt.preOrder()
print("Pre-order traversal is: \(res)")
res = abt.inOrder()
print("In-order traversal is: \(res)")
res = abt.postOrder()
print("Post-order traversal is: \(res)")
}
}
+230
View File
@@ -0,0 +1,230 @@
/**
* File: avl_tree.swift
* Created Time: 2023-01-28
* Author: nuomi1 (nuomi1@qq.com)
*/
import utils
/* AVL tree */
class AVLTree {
fileprivate var root: TreeNode? // Root node
init() {}
/* Get node height */
func height(node: TreeNode?) -> Int {
// Empty node height is -1, leaf node height is 0
node?.height ?? -1
}
/* Update node height */
private func updateHeight(node: TreeNode?) {
// Node height equals the height of the tallest subtree + 1
node?.height = max(height(node: node?.left), height(node: node?.right)) + 1
}
/* Get balance factor */
func balanceFactor(node: TreeNode?) -> Int {
// Empty node balance factor is 0
guard let node = node else { return 0 }
// Node balance factor = left subtree height - right subtree height
return height(node: node.left) - height(node: node.right)
}
/* Right rotation operation */
private func rightRotate(node: TreeNode?) -> TreeNode? {
let child = node?.left
let grandChild = child?.right
// Using child as pivot, rotate node to the right
child?.right = node
node?.left = grandChild
// Update node height
updateHeight(node: node)
updateHeight(node: child)
// Return root node of subtree after rotation
return child
}
/* Left rotation operation */
private func leftRotate(node: TreeNode?) -> TreeNode? {
let child = node?.right
let grandChild = child?.left
// Using child as pivot, rotate node to the left
child?.left = node
node?.right = grandChild
// Update node height
updateHeight(node: node)
updateHeight(node: child)
// Return root node of subtree after rotation
return child
}
/* Perform rotation operation to restore balance to this subtree */
private func rotate(node: TreeNode?) -> TreeNode? {
// Get balance factor of node
let balanceFactor = balanceFactor(node: node)
// Left-leaning tree
if balanceFactor > 1 {
if self.balanceFactor(node: node?.left) >= 0 {
// Right rotation
return rightRotate(node: node)
} else {
// First left rotation then right rotation
node?.left = leftRotate(node: node?.left)
return rightRotate(node: node)
}
}
// Right-leaning tree
if balanceFactor < -1 {
if self.balanceFactor(node: node?.right) <= 0 {
// Left rotation
return leftRotate(node: node)
} else {
// First right rotation then left rotation
node?.right = rightRotate(node: node?.right)
return leftRotate(node: node)
}
}
// Balanced tree, no rotation needed, return directly
return node
}
/* Insert node */
func insert(val: Int) {
root = insertHelper(node: root, val: val)
}
/* Recursively insert node (helper method) */
private func insertHelper(node: TreeNode?, val: Int) -> TreeNode? {
var node = node
if node == nil {
return TreeNode(x: val)
}
/* 1. Find insertion position and insert node */
if val < node!.val {
node?.left = insertHelper(node: node?.left, val: val)
} else if val > node!.val {
node?.right = insertHelper(node: node?.right, val: val)
} else {
return node // Duplicate node not inserted, return directly
}
updateHeight(node: node) // Update node height
/* 2. Perform rotation operation to restore balance to this subtree */
node = rotate(node: node)
// Return root node of subtree
return node
}
/* Remove node */
func remove(val: Int) {
root = removeHelper(node: root, val: val)
}
/* Recursively delete node (helper method) */
private func removeHelper(node: TreeNode?, val: Int) -> TreeNode? {
var node = node
if node == nil {
return nil
}
/* 1. Find node and delete */
if val < node!.val {
node?.left = removeHelper(node: node?.left, val: val)
} else if val > node!.val {
node?.right = removeHelper(node: node?.right, val: val)
} else {
if node?.left == nil || node?.right == nil {
let child = node?.left ?? node?.right
// Number of child nodes = 0, delete node directly and return
if child == nil {
return nil
}
// Number of child nodes = 1, delete node directly
else {
node = child
}
} else {
// Number of child nodes = 2, delete the next node in inorder traversal and replace current node with it
var temp = node?.right
while temp?.left != nil {
temp = temp?.left
}
node?.right = removeHelper(node: node?.right, val: temp!.val)
node?.val = temp!.val
}
}
updateHeight(node: node) // Update node height
/* 2. Perform rotation operation to restore balance to this subtree */
node = rotate(node: node)
// Return root node of subtree
return node
}
/* Search node */
func search(val: Int) -> TreeNode? {
var cur = root
while cur != nil {
// Target node is in cur's right subtree
if cur!.val < val {
cur = cur?.right
}
// Target node is in cur's left subtree
else if cur!.val > val {
cur = cur?.left
}
// Found target node, exit loop
else {
break
}
}
// Return target node
return cur
}
}
@main
enum _AVLTree {
static func testInsert(tree: AVLTree, val: Int) {
tree.insert(val: val)
print("\nAfter inserting node \(val), AVL tree is")
PrintUtil.printTree(root: tree.root)
}
static func testRemove(tree: AVLTree, val: Int) {
tree.remove(val: val)
print("\nAfter deleting node \(val), AVL tree is")
PrintUtil.printTree(root: tree.root)
}
/* Driver Code */
static func main() {
/* Please pay attention to how the AVL tree maintains balance after inserting nodes */
let avlTree = AVLTree()
/* Insert node */
// Delete nodes
testInsert(tree: avlTree, val: 1)
testInsert(tree: avlTree, val: 2)
testInsert(tree: avlTree, val: 3)
testInsert(tree: avlTree, val: 4)
testInsert(tree: avlTree, val: 5)
testInsert(tree: avlTree, val: 8)
testInsert(tree: avlTree, val: 7)
testInsert(tree: avlTree, val: 9)
testInsert(tree: avlTree, val: 10)
testInsert(tree: avlTree, val: 6)
/* Please pay attention to how the AVL tree maintains balance after deleting nodes */
testInsert(tree: avlTree, val: 7)
/* Remove node */
// Delete node with degree 1
testRemove(tree: avlTree, val: 8) // Delete node with degree 2
testRemove(tree: avlTree, val: 5) // Remove node with degree 1
testRemove(tree: avlTree, val: 4) // Remove node with degree 2
/* Search node */
let node = avlTree.search(val: 7)
print("\nFound node object is \(node!), node value = \(node!.val)")
}
}
@@ -0,0 +1,173 @@
/**
* File: binary_search_tree.swift
* Created Time: 2023-01-26
* Author: nuomi1 (nuomi1@qq.com)
*/
import utils
/* Binary search tree */
class BinarySearchTree {
private var root: TreeNode?
/* Constructor */
init() {
// Initialize empty tree
root = nil
}
/* Get binary tree root node */
func getRoot() -> TreeNode? {
root
}
/* Search node */
func search(num: Int) -> TreeNode? {
var cur = root
// Loop search, exit after passing leaf node
while cur != nil {
// Target node is in cur's right subtree
if cur!.val < num {
cur = cur?.right
}
// Target node is in cur's left subtree
else if cur!.val > num {
cur = cur?.left
}
// Found target node, exit loop
else {
break
}
}
// Return target node
return cur
}
/* Insert node */
func insert(num: Int) {
// If tree is empty, initialize root node
if root == nil {
root = TreeNode(x: num)
return
}
var cur = root
var pre: TreeNode?
// Loop search, exit after passing leaf node
while cur != nil {
// Found duplicate node, return directly
if cur!.val == num {
return
}
pre = cur
// Insertion position is in cur's right subtree
if cur!.val < num {
cur = cur?.right
}
// Insertion position is in cur's left subtree
else {
cur = cur?.left
}
}
// Insert node
let node = TreeNode(x: num)
if pre!.val < num {
pre?.right = node
} else {
pre?.left = node
}
}
/* Remove node */
func remove(num: Int) {
// If tree is empty, return directly
if root == nil {
return
}
var cur = root
var pre: TreeNode?
// Loop search, exit after passing leaf node
while cur != nil {
// Found node to delete, exit loop
if cur!.val == num {
break
}
pre = cur
// Node to delete is in cur's right subtree
if cur!.val < num {
cur = cur?.right
}
// Node to delete is in cur's left subtree
else {
cur = cur?.left
}
}
// If no node to delete, return directly
if cur == nil {
return
}
// Number of child nodes = 0 or 1
if cur?.left == nil || cur?.right == nil {
// When number of child nodes = 0 / 1, child = null / that child node
let child = cur?.left ?? cur?.right
// Delete node cur
if cur !== root {
if pre?.left === cur {
pre?.left = child
} else {
pre?.right = child
}
} else {
// If deleted node is root node, reassign root node
root = child
}
}
// Number of child nodes = 2
else {
// Get next node of cur in inorder traversal
var tmp = cur?.right
while tmp?.left != nil {
tmp = tmp?.left
}
// Recursively delete node tmp
remove(num: tmp!.val)
// Replace cur with tmp
cur?.val = tmp!.val
}
}
}
@main
enum _BinarySearchTree {
/* Driver Code */
static func main() {
/* Initialize binary search tree */
let bst = BinarySearchTree()
// Please note that different insertion orders will generate different binary trees, this sequence can generate a perfect binary tree
let nums = [8, 4, 12, 2, 6, 10, 14, 1, 3, 5, 7, 9, 11, 13, 15]
for num in nums {
bst.insert(num: num)
}
print("\nInitialized binary tree is\n")
PrintUtil.printTree(root: bst.getRoot())
/* Search node */
let node = bst.search(num: 7)
print("\nFound node object is \(node!), node value = \(node!.val)")
/* Insert node */
bst.insert(num: 16)
print("\nAfter inserting node 16, binary tree is\n")
PrintUtil.printTree(root: bst.getRoot())
/* Remove node */
bst.remove(num: 1)
print("\nAfter removing node 1, binary tree is\n")
PrintUtil.printTree(root: bst.getRoot())
bst.remove(num: 2)
print("\nAfter removing node 2, binary tree is\n")
PrintUtil.printTree(root: bst.getRoot())
bst.remove(num: 4)
print("\nAfter removing node 4, binary tree is\n")
PrintUtil.printTree(root: bst.getRoot())
}
}
@@ -0,0 +1,40 @@
/**
* File: binary_tree.swift
* Created Time: 2023-01-18
* Author: nuomi1 (nuomi1@qq.com)
*/
import utils
@main
enum BinaryTree {
/* Driver Code */
static func main() {
/* Initialize binary tree */
// Initialize nodes
let n1 = TreeNode(x: 1)
let n2 = TreeNode(x: 2)
let n3 = TreeNode(x: 3)
let n4 = TreeNode(x: 4)
let n5 = TreeNode(x: 5)
// Build references (pointers) between nodes
n1.left = n2
n1.right = n3
n2.left = n4
n2.right = n5
print("\nInitialize binary tree\n")
PrintUtil.printTree(root: n1)
/* Insert node P between n1 -> n2 */
let P = TreeNode(x: 0)
// Delete node
n1.left = P
P.left = n2
print("\nAfter inserting node P\n")
PrintUtil.printTree(root: n1)
// Remove node P
n1.left = n2
print("\nAfter removing node P\n")
PrintUtil.printTree(root: n1)
}
}
@@ -0,0 +1,42 @@
/**
* File: binary_tree_bfs.swift
* Created Time: 2023-01-18
* Author: nuomi1 (nuomi1@qq.com)
*/
import utils
/* Level-order traversal */
func levelOrder(root: TreeNode) -> [Int] {
// Initialize queue, add root node
var queue: [TreeNode] = [root]
// Initialize a list to save the traversal sequence
var list: [Int] = []
while !queue.isEmpty {
let node = queue.removeFirst() // Dequeue
list.append(node.val) // Save node value
if let left = node.left {
queue.append(left) // Left child node enqueue
}
if let right = node.right {
queue.append(right) // Right child node enqueue
}
}
return list
}
@main
enum BinaryTreeBFS {
/* Driver Code */
static func main() {
/* Initialize binary tree */
// Here we use a function to generate a binary tree directly from an array
let node = TreeNode.listToTree(arr: [1, 2, 3, 4, 5, 6, 7])!
print("\nInitialize binary tree\n")
PrintUtil.printTree(root: node)
/* Level-order traversal */
let list = levelOrder(root: node)
print("\nLevel-order traversal node print sequence = \(list)")
}
}
@@ -0,0 +1,70 @@
/**
* File: binary_tree_dfs.swift
* Created Time: 2023-01-18
* Author: nuomi1 (nuomi1@qq.com)
*/
import utils
// Initialize list for storing traversal sequence
var list: [Int] = []
/* Preorder traversal */
func preOrder(root: TreeNode?) {
guard let root = root else {
return
}
// Visit priority: root node -> left subtree -> right subtree
list.append(root.val)
preOrder(root: root.left)
preOrder(root: root.right)
}
/* Inorder traversal */
func inOrder(root: TreeNode?) {
guard let root = root else {
return
}
// Visit priority: left subtree -> root node -> right subtree
inOrder(root: root.left)
list.append(root.val)
inOrder(root: root.right)
}
/* Postorder traversal */
func postOrder(root: TreeNode?) {
guard let root = root else {
return
}
// Visit priority: left subtree -> right subtree -> root node
postOrder(root: root.left)
postOrder(root: root.right)
list.append(root.val)
}
@main
enum BinaryTreeDFS {
/* Driver Code */
static func main() {
/* Initialize binary tree */
// Here we use a function to generate a binary tree directly from an array
let root = TreeNode.listToTree(arr: [1, 2, 3, 4, 5, 6, 7])!
print("\nInitialize binary tree\n")
PrintUtil.printTree(root: root)
/* Preorder traversal */
list.removeAll()
preOrder(root: root)
print("\nPre-order traversal node print sequence = \(list)")
/* Inorder traversal */
list.removeAll()
inOrder(root: root)
print("\nIn-order traversal node print sequence = \(list)")
/* Postorder traversal */
list.removeAll()
postOrder(root: root)
print("\nPost-order traversal node print sequence = \(list)")
}
}