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

@@ -9,7 +9,7 @@ package chapter_tree;
import utils.*;
import java.util.*;
/* Binary tree class represented by array */
/* Binary tree class using an array representation */
class ArrayBinaryTree {
private List<Integer> tree;
@@ -18,30 +18,30 @@ class ArrayBinaryTree {
tree = new ArrayList<>(arr);
}
/* List capacity */
/* List size */
public int size() {
return tree.size();
}
/* Get value of node at index i */
/* Get the value of the node at index i */
public Integer 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.get(i);
}
/* Get index of left child node of node at index i */
/* Get the left child index of the node at index i */
public Integer 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 Integer 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 Integer parent(int i) {
return (i - 1) / 2;
}
@@ -49,7 +49,7 @@ class ArrayBinaryTree {
/* Level-order traversal */
public List<Integer> levelOrder() {
List<Integer> res = new ArrayList<>();
// Traverse array directly
// Traverse the array directly
for (int i = 0; i < size(); i++) {
if (val(i) != null)
res.add(val(i));
@@ -59,7 +59,7 @@ class ArrayBinaryTree {
/* Depth-first traversal */
private void dfs(Integer i, String order, List<Integer> res) {
// If empty position, return
// Return if this position is empty
if (val(i) == null)
return;
// Preorder traversal
@@ -99,38 +99,38 @@ class ArrayBinaryTree {
public class array_binary_tree {
public static void main(String[] args) {
// 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<Integer> arr = Arrays.asList(1, 2, 3, 4, null, 6, 7, 8, 9, null, null, 12, null, null, 15);
TreeNode root = TreeNode.listToTree(arr);
System.out.println("\nInitialize binary tree\n");
System.out.println("Array representation of binary tree:");
System.out.println("\nInitialize the binary tree\n");
System.out.println("Array representation of the binary tree:");
System.out.println(arr);
System.out.println("Linked list representation of binary tree:");
System.out.println("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 ArrayBinaryTree(arr);
// Access node
// Access a node
int i = 1;
Integer l = abt.left(i);
Integer r = abt.right(i);
Integer p = abt.parent(i);
System.out.println("\nCurrent node index is " + i + ", value is " + abt.val(i));
System.out.println("Its left child node index is " + l + ", value is " + (l == null ? "null" : abt.val(l)));
System.out.println("Its right child node index is " + r + ", value is " + (r == null ? "null" : abt.val(r)));
System.out.println("Its parent node index is " + p + ", value is " + (p == null ? "null" : abt.val(p)));
System.out.println("\nCurrent node index: " + i + " with value " + abt.val(i));
System.out.println("Left child index: " + l + " with value " + (l == null ? "null" : abt.val(l)));
System.out.println("Right child index: " + r + " with value " + (r == null ? "null" : abt.val(r)));
System.out.println("Parent index: " + p + " with value " + (p == null ? "null" : abt.val(p)));
// Traverse tree
// Traverse the tree
List<Integer> res = abt.levelOrder();
System.out.println("\nLevel-order traversal is:" + res);
System.out.println("\nLevel-order traversal:" + res);
res = abt.preOrder();
System.out.println("Preorder traversal is:" + res);
System.out.println("Preorder traversal:" + res);
res = abt.inOrder();
System.out.println("Inorder traversal is:" + res);
System.out.println("Inorder traversal:" + res);
res = abt.postOrder();
System.out.println("Postorder traversal is:" + res);
System.out.println("Postorder traversal:" + res);
}
}