mirror of
https://github.com/krahets/hello-algo.git
synced 2026-04-04 19:20:52 +08:00
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:
@@ -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);
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user