Retranslate the complete English edition from Simplified Chinese (#2010)

* Retranslate the complete English edition from Simplified Chinese

* Polish three English explanations after reader-focused review
This commit is contained in:
Yudong Jin
2026-10-31 03:35:51 +08:00
committed by GitHub
parent ef46e2ed95
commit 14c314c08c
1502 changed files with 29459 additions and 23517 deletions

View File

@@ -6,34 +6,34 @@
namespace hello_algo.chapter_tree;
/* Binary tree class represented by array */
/* Binary tree class using an array representation */
public class ArrayBinaryTree(List<int?> arr) {
List<int?> tree = new(arr);
/* List capacity */
/* List size */
public int Size() {
return tree.Count;
}
/* Get value of node at index i */
/* Get the value of the node at index i */
public int? Val(int i) {
// If index out of bounds, return null to represent empty position
// If the index is out of bounds, return null to represent an empty position
if (i < 0 || i >= Size())
return null;
return tree[i];
}
/* Get index of left child node of node at index i */
/* Get the left child index of the node at index i */
public int Left(int i) {
return 2 * i + 1;
}
/* Get index of right child node of node at index i */
/* Get the right child index of the node at index i */
public int Right(int i) {
return 2 * i + 2;
}
/* Get index of parent node of node at index i */
/* Get the parent index of the node at index i */
public int Parent(int i) {
return (i - 1) / 2;
}
@@ -41,7 +41,7 @@ public class ArrayBinaryTree(List<int?> arr) {
/* Level-order traversal */
public List<int> LevelOrder() {
List<int> res = [];
// Traverse array directly
// Traverse the array directly
for (int i = 0; i < Size(); i++) {
if (Val(i).HasValue)
res.Add(Val(i)!.Value);
@@ -51,7 +51,7 @@ public class ArrayBinaryTree(List<int?> arr) {
/* Depth-first traversal */
void DFS(int i, string order, List<int> res) {
// If empty position, return
// Return if this position is empty
if (!Val(i).HasValue)
return;
// Preorder traversal
@@ -92,38 +92,38 @@ public class ArrayBinaryTree(List<int?> arr) {
public class array_binary_tree {
[Test]
public void Test() {
// Initialize binary tree
// Here we use a function to generate a binary tree directly from an array
// Initialize the binary tree
// Use a helper function that builds a binary tree directly from an array
List<int?> arr = [1, 2, 3, 4, null, 6, 7, 8, 9, null, null, 12, null, null, 15];
TreeNode? root = TreeNode.ListToTree(arr);
Console.WriteLine("\nInitialize binary tree\n");
Console.WriteLine("Array representation of binary tree:");
Console.WriteLine("\nInitialize the binary tree\n");
Console.WriteLine("Array representation of the binary tree:");
Console.WriteLine(arr.PrintList());
Console.WriteLine("Linked list representation of binary tree:");
Console.WriteLine("Linked representation of the binary tree:");
PrintUtil.PrintTree(root);
// Binary tree class represented by array
// Binary tree class using an array representation
ArrayBinaryTree abt = new(arr);
// Access node
// Access a node
int i = 1;
int l = abt.Left(i);
int r = abt.Right(i);
int p = abt.Parent(i);
Console.WriteLine("\nCurrent node index is " + i + ", value is " + abt.Val(i));
Console.WriteLine("Its left child node index is " + l + ", value is " + (abt.Val(l).HasValue ? abt.Val(l) : "null"));
Console.WriteLine("Its right child node index is " + r + ", value is " + (abt.Val(r).HasValue ? abt.Val(r) : "null"));
Console.WriteLine("Its parent node index is " + p + ", value is " + (abt.Val(p).HasValue ? abt.Val(p) : "null"));
Console.WriteLine("\nCurrent node index: " + i + " with value " + abt.Val(i));
Console.WriteLine("Left child index: " + l + " with value " + (abt.Val(l).HasValue ? abt.Val(l) : "null"));
Console.WriteLine("Right child index: " + r + " with value " + (abt.Val(r).HasValue ? abt.Val(r) : "null"));
Console.WriteLine("Parent index: " + p + " with value " + (abt.Val(p).HasValue ? abt.Val(p) : "null"));
// Traverse tree
// Traverse the tree
List<int> res = abt.LevelOrder();
Console.WriteLine("\nLevel-order traversal is:" + res.PrintList());
Console.WriteLine("\nLevel-order traversal:" + res.PrintList());
res = abt.PreOrder();
Console.WriteLine("Preorder traversal is:" + res.PrintList());
Console.WriteLine("Preorder traversal:" + res.PrintList());
res = abt.InOrder();
Console.WriteLine("Inorder traversal is:" + res.PrintList());
Console.WriteLine("Inorder traversal:" + res.PrintList());
res = abt.PostOrder();
Console.WriteLine("Postorder traversal is:" + res.PrintList());
Console.WriteLine("Postorder traversal:" + res.PrintList());
}
}