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,127 @@
/**
* File: array_binary_tree.kt
* Created Time: 2024-01-25
* Author: curtishd (1023632660@qq.com)
*/
package chapter_tree
import utils.TreeNode
import utils.printTree
/* Binary tree class represented by array */
class ArrayBinaryTree(private val tree: MutableList<Int?>) {
/* List capacity */
fun size(): Int {
return tree.size
}
/* Get value of node at index i */
fun _val(i: Int): Int? {
// If index out of bounds, return null to represent empty position
if (i < 0 || i >= size()) return null
return tree[i]
}
/* Get index of left child node of node at index i */
fun left(i: Int): Int {
return 2 * i + 1
}
/* Get index of right child node of node at index i */
fun right(i: Int): Int {
return 2 * i + 2
}
/* Get index of parent node of node at index i */
fun parent(i: Int): Int {
return (i - 1) / 2
}
/* Level-order traversal */
fun levelOrder(): MutableList<Int?> {
val res = mutableListOf<Int?>()
// Traverse array directly
for (i in 0..<size()) {
if (_val(i) != null)
res.add(_val(i))
}
return res
}
/* Depth-first traversal */
fun dfs(i: Int, order: String, res: MutableList<Int?>) {
// If empty position, return
if (_val(i) == null)
return
// Preorder traversal
if ("pre" == order)
res.add(_val(i))
dfs(left(i), order, res)
// Inorder traversal
if ("in" == order)
res.add(_val(i))
dfs(right(i), order, res)
// Postorder traversal
if ("post" == order)
res.add(_val(i))
}
/* Preorder traversal */
fun preOrder(): MutableList<Int?> {
val res = mutableListOf<Int?>()
dfs(0, "pre", res)
return res
}
/* Inorder traversal */
fun inOrder(): MutableList<Int?> {
val res = mutableListOf<Int?>()
dfs(0, "in", res)
return res
}
/* Postorder traversal */
fun postOrder(): MutableList<Int?> {
val res = mutableListOf<Int?>()
dfs(0, "post", res)
return res
}
}
/* Driver Code */
fun main() {
// Initialize binary tree
// Here we use a function to generate binary tree directly from list
val arr = mutableListOf(1, 2, 3, 4, null, 6, 7, 8, 9, null, null, 12, null, null, 15)
val root = TreeNode.listToTree(arr)
println("\nInitialize binary tree\n")
println("Array representation of binary tree:")
println(arr)
println("Linked list representation of binary tree:")
printTree(root)
// Binary tree class represented by array
val abt = ArrayBinaryTree(arr)
// Access node
val i = 1
val l = abt.left(i)
val r = abt.right(i)
val p = abt.parent(i)
println("Current node index is $i, value is ${abt._val(i)}")
println("Its left child index is $l, value is ${abt._val(l)}")
println("Its right child index is $r, value is ${abt._val(r)}")
println("Its parent node index is $p, value is ${abt._val(p)}")
// Traverse tree
var res = abt.levelOrder()
println("\nLevel-order traversal is: $res")
res = abt.preOrder()
println("Pre-order traversal is: $res")
res = abt.inOrder()
println("In-order traversal is: $res")
res = abt.postOrder()
println("Post-order traversal is: $res")
}
+223
View File
@@ -0,0 +1,223 @@
/**
* File: avl_tree.kt
* Created Time: 2024-01-25
* Author: curtishd (1023632660@qq.com)
*/
package chapter_tree
import utils.TreeNode
import utils.printTree
import kotlin.math.max
/* AVL tree */
class AVLTree {
var root: TreeNode? = null // Root node
/* Get node height */
fun height(node: TreeNode?): Int {
// Empty node height is -1, leaf node height is 0
return node?.height ?: -1
}
/* Update node height */
private fun updateHeight(node: TreeNode?) {
// Node height equals the height of the tallest subtree + 1
node?.height = max(height(node?.left), height(node?.right)) + 1
}
/* Get balance factor */
fun balanceFactor(node: TreeNode?): Int {
// 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 */
private fun rightRotate(node: TreeNode?): TreeNode {
val child = node!!.left
val 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 */
private fun leftRotate(node: TreeNode?): TreeNode {
val child = node!!.right
val 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 */
private fun rotate(node: TreeNode): TreeNode {
// Get balance factor of node
val balanceFactor = balanceFactor(node)
// Left-leaning tree
if (balanceFactor > 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 (balanceFactor < -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
}
/* Insert node */
fun insert(_val: Int) {
root = insertHelper(root, _val)
}
/* Recursively insert node (helper method) */
private fun insertHelper(n: TreeNode?, _val: Int): TreeNode {
if (n == null)
return TreeNode(_val)
var node = n
/* 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
return node // Duplicate node not inserted, return directly
updateHeight(node) // Update node height
/* 2. Perform rotation operation to restore balance to this subtree */
node = rotate(node)
// Return root node of subtree
return node
}
/* Remove node */
fun remove(_val: Int) {
root = removeHelper(root, _val)
}
/* Recursively delete node (helper method) */
private fun removeHelper(n: TreeNode?, _val: Int): TreeNode? {
var node = n ?: 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) {
val child = if (node.left != null)
node.left
else
node.right
// Number of child nodes = 0, delete node directly and return
if (child == null)
return null
// 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 != null) {
temp = temp.left
}
node.right = removeHelper(node.right, temp._val)
node._val = temp._val
}
}
updateHeight(node) // Update node height
/* 2. Perform rotation operation to restore balance to this subtree */
node = rotate(node)
// Return root node of subtree
return node
}
/* Search node */
fun search(_val: Int): TreeNode? {
var cur = root
// Loop search, exit after passing leaf node
while (cur != null) {
// Target node is in cur's right subtree
cur = if (cur._val < _val)
cur.right!!
// Target node is in cur's left subtree
else if (cur._val > _val)
cur.left
// Found target node, exit loop
else
break
}
// Return target node
return cur
}
}
fun testInsert(tree: AVLTree, _val: Int) {
tree.insert(_val)
println("\nAfter inserting node $_val, AVL tree is")
printTree(tree.root)
}
fun testRemove(tree: AVLTree, _val: Int) {
tree.remove(_val)
println("\nAfter deleting node $_val, AVL tree is")
printTree(tree.root)
}
/* Driver Code */
fun main() {
/* Please pay attention to how the AVL tree maintains balance after inserting nodes */
val avlTree = AVLTree()
/* Insert node */
// Delete nodes
testInsert(avlTree, 1)
testInsert(avlTree, 2)
testInsert(avlTree, 3)
testInsert(avlTree, 4)
testInsert(avlTree, 5)
testInsert(avlTree, 8)
testInsert(avlTree, 7)
testInsert(avlTree, 9)
testInsert(avlTree, 10)
testInsert(avlTree, 6)
/* Please pay attention to how the AVL tree maintains balance after deleting nodes */
testInsert(avlTree, 7)
/* Remove node */
// 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 */
val node = avlTree.search(7)
println("\n Found node object is $node, node value = ${node?._val}")
}
@@ -0,0 +1,157 @@
/**
* File: binary_search_tree.kt
* Created Time: 2024-01-25
* Author: curtishd (1023632660@qq.com)
*/
package chapter_tree
import utils.TreeNode
import utils.printTree
/* Binary search tree */
class BinarySearchTree {
// Initialize empty tree
private var root: TreeNode? = null
/* Get binary tree root node */
fun getRoot(): TreeNode? {
return root
}
/* Search node */
fun search(num: Int): TreeNode? {
var cur = root
// Loop search, exit after passing leaf node
while (cur != null) {
// Target node is in cur's right subtree
cur = if (cur._val < num)
cur.right
// Target node is in cur's left subtree
else if (cur._val > num)
cur.left
// Found target node, exit loop
else
break
}
// Return target node
return cur
}
/* Insert node */
fun insert(num: Int) {
// If tree is empty, initialize root node
if (root == null) {
root = TreeNode(num)
return
}
var cur = root
var pre: TreeNode? = null
// Loop search, exit after passing leaf node
while (cur != null) {
// Found duplicate node, return directly
if (cur._val == num)
return
pre = cur
// Insertion position is in cur's right subtree
cur = if (cur._val < num)
cur.right
// Insertion position is in cur's left subtree
else
cur.left
}
// Insert node
val node = TreeNode(num)
if (pre?._val!! < num)
pre.right = node
else
pre.left = node
}
/* Remove node */
fun remove(num: Int) {
// If tree is empty, return directly
if (root == null)
return
var cur = root
var pre: TreeNode? = null
// Loop search, exit after passing leaf node
while (cur != null) {
// Found node to delete, exit loop
if (cur._val == num)
break
pre = cur
// Node to delete is in cur's right subtree
cur = if (cur._val < num)
cur.right
// Node to delete is in cur's left subtree
else
cur.left
}
// 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 number of child nodes = 0 / 1, child = null / that child node
val child = if (cur.left != null)
cur.left
else
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 != null) {
tmp = tmp.left
}
// Recursively delete node tmp
remove(tmp._val)
// Replace cur with tmp
cur._val = tmp._val
}
}
}
/* Driver Code */
fun main() {
/* Initialize binary search tree */
val bst = BinarySearchTree()
// Please note that different insertion orders will generate different binary trees, this sequence can generate a perfect binary tree
val nums = intArrayOf(8, 4, 12, 2, 6, 10, 14, 1, 3, 5, 7, 9, 11, 13, 15)
for (num in nums) {
bst.insert(num)
}
println("\nInitialized binary tree is\n")
printTree(bst.getRoot())
/* Search node */
val node = bst.search(7)
println("Found node object is $node, node value = ${node?._val}")
/* Insert node */
bst.insert(16)
println("\nAfter inserting node 16, binary tree is\n")
printTree(bst.getRoot())
/* Remove node */
bst.remove(1)
println("\nAfter removing node 1, binary tree is\n")
printTree(bst.getRoot())
bst.remove(2)
println("\nAfter removing node 2, binary tree is\n")
printTree(bst.getRoot())
bst.remove(4)
println("\nAfter removing node 4, binary tree is\n")
printTree(bst.getRoot())
}
@@ -0,0 +1,40 @@
/**
* File: binary_tree.kt
* Created Time: 2024-01-25
* Author: curtishd (1023632660@qq.com)
*/
package chapter_tree
import utils.TreeNode
import utils.printTree
/* Driver Code */
fun main() {
/* Initialize binary tree */
// Initialize nodes
val n1 = TreeNode(1)
val n2 = TreeNode(2)
val n3 = TreeNode(3)
val n4 = TreeNode(4)
val n5 = TreeNode(5)
// Build references (pointers) between nodes
n1.left = n2
n1.right = n3
n2.left = n4
n2.right = n5
println("\nInitialize binary tree\n")
printTree(n1)
/* Insert node P between n1 -> n2 */
val P = TreeNode(0)
// Delete node
n1.left = P
P.left = n2
println("\nAfter inserting node P\n")
printTree(n1)
// Remove node P
n1.left = n2
println("\nAfter removing node P\n")
printTree(n1)
}
@@ -0,0 +1,42 @@
/**
* File: binary_tree_bfs.kt
* Created Time: 2024-01-25
* Author: curtishd (1023632660@qq.com)
*/
package chapter_tree
import utils.TreeNode
import utils.printTree
import java.util.*
/* Level-order traversal */
fun levelOrder(root: TreeNode?): MutableList<Int> {
// Initialize queue, add root node
val queue = LinkedList<TreeNode?>()
queue.add(root)
// Initialize a list to save the traversal sequence
val list = mutableListOf<Int>()
while (queue.isNotEmpty()) {
val node = queue.poll() // Dequeue
list.add(node?._val!!) // Save node value
if (node.left != null)
queue.offer(node.left) // Left child node enqueue
if (node.right != null)
queue.offer(node.right) // Right child node enqueue
}
return list
}
/* Driver Code */
fun main() {
/* Initialize binary tree */
// Here we use a function to generate binary tree directly from list
val root = TreeNode.listToTree(mutableListOf(1, 2, 3, 4, 5, 6, 7))
println("\nInitialize binary tree\n")
printTree(root)
/* Level-order traversal */
val list = levelOrder(root)
println("\nLevel-order traversal node print sequence = $list")
}
@@ -0,0 +1,64 @@
/**
* File: binary_tree_dfs.kt
* Created Time: 2024-01-25
* Author: curtishd (1023632660@qq.com)
*/
package chapter_tree
import utils.TreeNode
import utils.printTree
// Initialize list for storing traversal sequence
var list = mutableListOf<Int>()
/* Preorder traversal */
fun preOrder(root: TreeNode?) {
if (root == null) return
// Visit priority: root node -> left subtree -> right subtree
list.add(root._val)
preOrder(root.left)
preOrder(root.right)
}
/* Inorder traversal */
fun inOrder(root: TreeNode?) {
if (root == null) return
// Visit priority: left subtree -> root node -> right subtree
inOrder(root.left)
list.add(root._val)
inOrder(root.right)
}
/* Postorder traversal */
fun postOrder(root: TreeNode?) {
if (root == null) return
// Visit priority: left subtree -> right subtree -> root node
postOrder(root.left)
postOrder(root.right)
list.add(root._val)
}
/* Driver Code */
fun main() {
/* Initialize binary tree */
// Here we use a function to generate binary tree directly from list
val root = TreeNode.listToTree(mutableListOf(1, 2, 3, 4, 5, 6, 7))
println("\nInitialize binary tree\n")
printTree(root)
/* Preorder traversal */
list.clear()
preOrder(root)
println("\nPre-order traversal node print sequence = $list")
/* Inorder traversal */
list.clear()
inOrder(root)
println("\nIn-order traversal node print sequence = $list")
/* Postorder traversal */
list.clear()
postOrder(root)
println("\nPost-order traversal node print sequence = $list")
}