mirror of
https://github.com/krahets/hello-algo.git
synced 2026-04-09 13:51:48 +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:
@@ -26,7 +26,7 @@ class BinarySearchTree:
|
||||
def search(self, num: int) -> TreeNode | None:
|
||||
"""Search node"""
|
||||
cur = self._root
|
||||
# Loop find, break after passing leaf nodes
|
||||
# Loop search, exit after passing leaf node
|
||||
while cur is not None:
|
||||
# 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
|
||||
elif cur.val > num:
|
||||
cur = cur.left
|
||||
# Found target node, break loop
|
||||
# Found target node, exit loop
|
||||
else:
|
||||
break
|
||||
return cur
|
||||
@@ -45,10 +45,10 @@ class BinarySearchTree:
|
||||
if self._root is None:
|
||||
self._root = TreeNode(num)
|
||||
return
|
||||
# Loop find, break after passing leaf nodes
|
||||
# Loop search, exit after passing leaf node
|
||||
cur, pre = self._root, None
|
||||
while cur is not None:
|
||||
# Found duplicate node, thus return
|
||||
# Found duplicate node, return directly
|
||||
if cur.val == num:
|
||||
return
|
||||
pre = cur
|
||||
@@ -66,47 +66,47 @@ class BinarySearchTree:
|
||||
pre.left = node
|
||||
|
||||
def remove(self, num: int):
|
||||
"""Remove node"""
|
||||
# If tree is empty, return
|
||||
"""Delete node"""
|
||||
# If tree is empty, return directly
|
||||
if self._root is None:
|
||||
return
|
||||
# Loop find, break after passing leaf nodes
|
||||
# Loop search, exit after passing leaf node
|
||||
cur, pre = self._root, None
|
||||
while cur is not None:
|
||||
# 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 is None:
|
||||
return
|
||||
|
||||
# Number of child nodes = 0 or 1
|
||||
if cur.left is None or cur.right is None:
|
||||
# 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
|
||||
child = cur.left or cur.right
|
||||
# Remove node cur
|
||||
# Delete node cur
|
||||
if cur != self._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
|
||||
self._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
|
||||
tmp: TreeNode = cur.right
|
||||
while tmp.left is not None:
|
||||
tmp = tmp.left
|
||||
# Recursively remove node tmp
|
||||
# Recursively delete node tmp
|
||||
self.remove(tmp.val)
|
||||
# Replace cur with tmp
|
||||
cur.val = tmp.val
|
||||
@@ -117,7 +117,7 @@ if __name__ == "__main__":
|
||||
# Initialize binary search tree
|
||||
bst = BinarySearchTree()
|
||||
nums = [8, 4, 12, 2, 6, 10, 14, 1, 3, 5, 7, 9, 11, 13, 15]
|
||||
# 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
|
||||
for num in nums:
|
||||
bst.insert(num)
|
||||
print("\nInitialized binary tree is\n")
|
||||
@@ -129,18 +129,18 @@ if __name__ == "__main__":
|
||||
|
||||
# Insert node
|
||||
bst.insert(16)
|
||||
print("\nAfter inserting node 16, the binary tree is\n")
|
||||
print("\nAfter inserting node 16, binary tree is\n")
|
||||
print_tree(bst.get_root())
|
||||
|
||||
# Remove node
|
||||
# Delete node
|
||||
bst.remove(1)
|
||||
print("\nAfter removing node 1, the binary tree is\n")
|
||||
print("\nAfter deleting node 1, binary tree is\n")
|
||||
print_tree(bst.get_root())
|
||||
|
||||
bst.remove(2)
|
||||
print("\nAfter removing node 2, the binary tree is\n")
|
||||
print("\nAfter deleting node 2, binary tree is\n")
|
||||
print_tree(bst.get_root())
|
||||
|
||||
bst.remove(4)
|
||||
print("\nAfter removing node 4, the binary tree is\n")
|
||||
print("\nAfter deleting node 4, binary tree is\n")
|
||||
print_tree(bst.get_root())
|
||||
|
||||
Reference in New Issue
Block a user