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

@@ -7,7 +7,7 @@
const { arrToTree } = require('../modules/TreeNode');
const { printTree } = require('../modules/PrintUtil');
/* Binary tree class represented by array */
/* Binary tree class using an array representation */
class ArrayBinaryTree {
#tree;
@@ -16,29 +16,29 @@ class ArrayBinaryTree {
this.#tree = arr;
}
/* List capacity */
/* Array length */
size() {
return this.#tree.length;
}
/* Get value of node at index i */
/* Get the value of the node at index i */
val(i) {
// If index out of bounds, return null to represent empty position
// If the index is out of bounds, return null to indicate an empty position
if (i < 0 || i >= this.size()) return null;
return this.#tree[i];
}
/* Get index of left child node of node at index i */
/* Get the left child index of the node at index i */
left(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 */
right(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 */
parent(i) {
return Math.floor((i - 1) / 2); // Floor division
}
@@ -46,7 +46,7 @@ class ArrayBinaryTree {
/* Level-order traversal */
levelOrder() {
let res = [];
// Traverse array directly
// Traverse the array directly
for (let i = 0; i < this.size(); i++) {
if (this.val(i) !== null) res.push(this.val(i));
}
@@ -55,7 +55,7 @@ class ArrayBinaryTree {
/* Depth-first traversal */
#dfs(i, order, res) {
// If empty position, return
// Return if this position is empty
if (this.val(i) === null) return;
// Preorder traversal
if (order === 'pre') res.push(this.val(i));
@@ -89,9 +89,9 @@ class ArrayBinaryTree {
}
}
/* Driver Code */
// Initialize binary tree
// Here we use a function to generate a binary tree directly from an array
/* Driver code */
// Initialize the binary tree
// Use a helper function that builds a binary tree directly from an array
const arr = Array.of(
1,
2,
@@ -111,37 +111,37 @@ const arr = Array.of(
);
const root = arrToTree(arr);
console.log('\nInitialize binary tree\n');
console.log('Array representation of binary tree:');
console.log('\nInitialize the binary tree\n');
console.log('Array representation of the binary tree:');
console.log(arr);
console.log('Linked list representation of binary tree:');
console.log('Linked representation of the binary tree:');
printTree(root);
// Binary tree class represented by array
// Binary tree class using an array representation
const abt = new ArrayBinaryTree(arr);
// Access node
// Access a node
const i = 1;
const l = abt.left(i);
const r = abt.right(i);
const p = abt.parent(i);
console.log('\nCurrent node index is ' + i + ', value is ' + abt.val(i));
console.log('\nCurrent node index: ' + i + ' with value ' + abt.val(i));
console.log(
'Its left child node index is ' + l + ', value is ' + (l === null ? 'null' : abt.val(l))
'Left child index: ' + l + ' with value ' + (l === null ? 'null' : abt.val(l))
);
console.log(
'Its right child node index is ' + r + ', value is ' + (r === null ? 'null' : abt.val(r))
'Right child index: ' + r + ' with value ' + (r === null ? 'null' : abt.val(r))
);
console.log(
'Its parent node index is ' + p + ', value is ' + (p === null ? 'null' : abt.val(p))
'Parent index: ' + p + ' with value ' + (p === null ? 'null' : abt.val(p))
);
// Traverse tree
// Traverse the tree
let res = abt.levelOrder();
console.log('\nLevel-order traversal is:' + res);
console.log('\nLevel-order traversal:' + res);
res = abt.preOrder();
console.log('Preorder traversal is:' + res);
console.log('Preorder traversal:' + res);
res = abt.inOrder();
console.log('Inorder traversal is:' + res);
console.log('Inorder traversal:' + res);
res = abt.postOrder();
console.log('Postorder traversal is:' + res);
console.log('Postorder traversal:' + res);

View File

@@ -7,133 +7,133 @@
const { TreeNode } = require('../modules/TreeNode');
const { printTree } = require('../modules/PrintUtil');
/* AVL tree */
/* AVL tree*/
class AVLTree {
/* Constructor */
constructor() {
this.root = null; // Root node
this.root = null; //Root node
}
/* Get node height */
/* Get the node height */
height(node) {
// Empty node height is -1, leaf node height is 0
// A null node has height -1; a leaf has height 0
return node === null ? -1 : node.height;
}
/* Update node height */
/* Update the node height */
#updateHeight(node) {
// Node height equals the height of the tallest subtree + 1
// The node height equals the height of its taller subtree + 1
node.height =
Math.max(this.height(node.left), this.height(node.right)) + 1;
}
/* Get balance factor */
/* Get the balance factor */
balanceFactor(node) {
// Empty node balance factor is 0
// A null node has balance factor 0
if (node === null) return 0;
// Node balance factor = left subtree height - right subtree height
return this.height(node.left) - this.height(node.right);
}
/* Right rotation operation */
/* Right rotation */
#rightRotate(node) {
const child = node.left;
const grandChild = child.right;
// Using child as pivot, rotate node to the right
// Rotate node to the right around child
child.right = node;
node.left = grandChild;
// Update node height
// Update the node height
this.#updateHeight(node);
this.#updateHeight(child);
// Return root node of subtree after rotation
// Return the root of the rotated subtree
return child;
}
/* Left rotation operation */
/* Left rotation */
#leftRotate(node) {
const child = node.right;
const grandChild = child.left;
// Using child as pivot, rotate node to the left
// Rotate node to the left around child
child.left = node;
node.right = grandChild;
// Update node height
// Update the node height
this.#updateHeight(node);
this.#updateHeight(child);
// Return root node of subtree after rotation
// Return the root of the rotated subtree
return child;
}
/* Perform rotation operation to restore balance to this subtree */
/* Perform rotations to restore balance in this subtree */
#rotate(node) {
// Get balance factor of node
// Get the balance factor of node
const balanceFactor = this.balanceFactor(node);
// Left-leaning tree
// Left-heavy tree
if (balanceFactor > 1) {
if (this.balanceFactor(node.left) >= 0) {
// Right rotation
// Rotate right
return this.#rightRotate(node);
} else {
// First left rotation then right rotation
// Rotate left, then right
node.left = this.#leftRotate(node.left);
return this.#rightRotate(node);
}
}
// Right-leaning tree
// Right-heavy tree
if (balanceFactor < -1) {
if (this.balanceFactor(node.right) <= 0) {
// Left rotation
// Rotate left
return this.#leftRotate(node);
} else {
// First right rotation then left rotation
// Rotate right, then left
node.right = this.#rightRotate(node.right);
return this.#leftRotate(node);
}
}
// Balanced tree, no rotation needed, return directly
// The tree is balanced; return without rotating
return node;
}
/* Insert node */
/* Insert a node */
insert(val) {
this.root = this.#insertHelper(this.root, val);
}
/* Recursively insert node (helper method) */
/* Insert a node recursively (helper method) */
#insertHelper(node, val) {
if (node === null) return new TreeNode(val);
/* 1. Find insertion position and insert node */
/* 1. Find the insertion position and insert the node */
if (val < node.val) node.left = this.#insertHelper(node.left, val);
else if (val > node.val)
node.right = this.#insertHelper(node.right, val);
else return node; // Duplicate node not inserted, return directly
this.#updateHeight(node); // Update node height
/* 2. Perform rotation operation to restore balance to this subtree */
else return node; // Return without inserting a duplicate node
this.#updateHeight(node); // Update the node height
/* 2. Perform rotations to restore balance in this subtree */
node = this.#rotate(node);
// Return root node of subtree
// Return the root of the subtree
return node;
}
/* Remove node */
/* Remove a node */
remove(val) {
this.root = this.#removeHelper(this.root, val);
}
/* Recursively delete node (helper method) */
/* Remove a node recursively (helper method) */
#removeHelper(node, val) {
if (node === null) return null;
/* 1. Find node and delete */
/* 1. Find and remove the node */
if (val < node.val) node.left = this.#removeHelper(node.left, val);
else if (val > node.val)
node.right = this.#removeHelper(node.right, val);
else {
if (node.left === null || node.right === null) {
const child = node.left !== null ? node.left : node.right;
// Number of child nodes = 0, delete node directly and return
// Number of children = 0: remove node and return
if (child === null) return null;
// Number of child nodes = 1, delete node directly
// Number of children = 1: remove node directly
else node = child;
} else {
// Number of child nodes = 2, delete the next node in inorder traversal and replace current node with it
// Number of children = 2: remove the inorder successor and use its value to replace the current node value
let temp = node.right;
while (temp.left !== null) {
temp = temp.left;
@@ -142,47 +142,47 @@ class AVLTree {
node.val = temp.val;
}
}
this.#updateHeight(node); // Update node height
/* 2. Perform rotation operation to restore balance to this subtree */
this.#updateHeight(node); // Update the node height
/* 2. Perform rotations to restore balance in this subtree */
node = this.#rotate(node);
// Return root node of subtree
// Return the root of the subtree
return node;
}
/* Search node */
/* Find a node */
search(val) {
let cur = this.root;
// Loop search, exit after passing leaf node
// Search in a loop until reaching a null reference beyond a leaf
while (cur !== null) {
// Target node is in cur's right subtree
// The target node lies in the right subtree of cur
if (cur.val < val) cur = cur.right;
// Target node is in cur's left subtree
// The target node lies in the left subtree of cur
else if (cur.val > val) cur = cur.left;
// Found target node, exit loop
// The target node was found; exit the loop
else break;
}
// Return target node
// Return the target node
return cur;
}
}
function testInsert(tree, val) {
tree.insert(val);
console.log('\nInsert node ' + val + ', AVL tree is');
console.log('\nAfter inserting node ' + val + ' the AVL tree is:');
printTree(tree.root);
}
function testRemove(tree, val) {
tree.remove(val);
console.log('\nRemove node ' + val + ', AVL tree is');
console.log('\nAfter removing node ' + val + ' the AVL tree is:');
printTree(tree.root);
}
/* Driver Code */
/* Please pay attention to how the AVL tree maintains balance after inserting nodes */
/* Driver code */
/* Initialize an empty AVL tree */
const avlTree = new AVLTree();
/* Insert node */
// Delete nodes
/* Insert a node */
// Observe how the AVL tree maintains balance after a node is inserted
testInsert(avlTree, 1);
testInsert(avlTree, 2);
testInsert(avlTree, 3);
@@ -194,15 +194,15 @@ testInsert(avlTree, 9);
testInsert(avlTree, 10);
testInsert(avlTree, 6);
/* Please pay attention to how the AVL tree maintains balance after deleting nodes */
/* Insert a duplicate node */
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
/* Remove a node */
// Observe how the AVL tree maintains balance after a node is removed
testRemove(avlTree, 8); // Remove a node with 0 children
testRemove(avlTree, 5); // Remove a node with 1 child
testRemove(avlTree, 4); // Remove a node with 2 children
/* Search node */
/* Look up a node */
const node = avlTree.search(7);
console.log('\nFound node object is', node, ', node value = ' + node.val);
console.log('\nFound node object:', node, ', node value = ' + node.val);

View File

@@ -11,129 +11,129 @@ const { printTree } = require('../modules/PrintUtil');
class BinarySearchTree {
/* Constructor */
constructor() {
// Initialize empty tree
// Initialize an empty tree
this.root = null;
}
/* Get binary tree root node */
/* Get the root of the binary tree */
getRoot() {
return this.root;
}
/* Search node */
/* Find a node */
search(num) {
let cur = this.root;
// Loop search, exit after passing leaf node
// Search in a loop until reaching a null reference beyond a leaf
while (cur !== null) {
// Target node is in cur's right subtree
// The target node lies in the right subtree of cur
if (cur.val < num) cur = cur.right;
// Target node is in cur's left subtree
// The target node lies in the left subtree of cur
else if (cur.val > num) cur = cur.left;
// Found target node, exit loop
// The target node was found; exit the loop
else break;
}
// Return target node
// Return the target node
return cur;
}
/* Insert node */
/* Insert a node */
insert(num) {
// If tree is empty, initialize root node
// If the tree is empty, initialize the root node
if (this.root === null) {
this.root = new TreeNode(num);
return;
}
let cur = this.root,
pre = null;
// Loop search, exit after passing leaf node
// Search in a loop until reaching a null reference beyond a leaf
while (cur !== null) {
// Found duplicate node, return directly
// Return immediately if a duplicate node is found
if (cur.val === num) return;
pre = cur;
// Insertion position is in cur's right subtree
// The insertion position lies in the right subtree of cur
if (cur.val < num) cur = cur.right;
// Insertion position is in cur's left subtree
// The insertion position lies in the left subtree of cur
else cur = cur.left;
}
// Insert node
// Insert a node
const node = new TreeNode(num);
if (pre.val < num) pre.right = node;
else pre.left = node;
}
/* Remove node */
/* Remove a node */
remove(num) {
// If tree is empty, return directly
// Return immediately if the tree is empty
if (this.root === null) return;
let cur = this.root,
pre = null;
// Loop search, exit after passing leaf node
// Search in a loop until reaching a null reference beyond a leaf
while (cur !== null) {
// Found node to delete, exit loop
// The node to remove was found; exit the loop
if (cur.val === num) break;
pre = cur;
// Node to delete is in cur's right subtree
// The node to remove lies in the right subtree of cur
if (cur.val < num) cur = cur.right;
// Node to delete is in cur's left subtree
// The node to remove lies in the left subtree of cur
else cur = cur.left;
}
// If no node to delete, return directly
// Return immediately if the node to remove does not exist
if (cur === null) return;
// Number of child nodes = 0 or 1
// Number of children = 0 or 1
if (cur.left === null || cur.right === null) {
// When number of child nodes = 0 / 1, child = null / that child node
// With 0 / 1 children, child is null / the child node, respectively
const child = cur.left !== null ? cur.left : cur.right;
// Delete node cur
// Remove node cur
if (cur !== this.root) {
if (pre.left === cur) pre.left = child;
else pre.right = child;
} else {
// If deleted node is root node, reassign root node
// If the removed node is the root, assign the new root
this.root = child;
}
}
// Number of child nodes = 2
// Number of children = 2
else {
// Get next node of cur in inorder traversal
// Get the inorder successor of cur
let tmp = cur.right;
while (tmp.left !== null) {
tmp = tmp.left;
}
// Recursively delete node tmp
// Recursively remove node tmp
this.remove(tmp.val);
// Replace cur with tmp
// Replace the value of cur with the value of tmp
cur.val = tmp.val;
}
}
}
/* Driver Code */
/* Initialize binary search tree */
/* Driver code */
/* Initialize the binary search tree */
const bst = new BinarySearchTree();
// Please note that different insertion orders will generate different binary trees, this sequence can generate a perfect binary tree
// Different insertion orders produce different binary trees; this sequence produces a perfect binary tree
const nums = [8, 4, 12, 2, 6, 10, 14, 1, 3, 5, 7, 9, 11, 13, 15];
for (const num of nums) {
bst.insert(num);
}
console.log('\nInitialized binary tree is\n');
console.log('\nThe initialized binary tree is:\n');
printTree(bst.getRoot());
/* Search node */
/* Find a node */
const node = bst.search(7);
console.log('\nFound node object is ' + node + ', node value = ' + node.val);
console.log('\nFound node object: ' + node + ', node value = ' + node.val);
/* Insert node */
/* Insert a node */
bst.insert(16);
console.log('\nAfter inserting node 16, binary tree is\n');
console.log('\nBinary tree after inserting node 16:\n');
printTree(bst.getRoot());
/* Remove node */
/* Remove a node */
bst.remove(1);
console.log('\nAfter removing node 1, binary tree is\n');
console.log('\nBinary tree after removing node 1:\n');
printTree(bst.getRoot());
bst.remove(2);
console.log('\nAfter removing node 2, binary tree is\n');
console.log('\nBinary tree after removing node 2:\n');
printTree(bst.getRoot());
bst.remove(4);
console.log('\nAfter removing node 4, binary tree is\n');
console.log('\nBinary tree after removing node 4:\n');
printTree(bst.getRoot());

View File

@@ -7,29 +7,29 @@
const { TreeNode } = require('../modules/TreeNode');
const { printTree } = require('../modules/PrintUtil');
/* Initialize binary tree */
// Initialize nodes
/* Initialize the binary tree */
// Initialize the nodes
let n1 = new TreeNode(1),
n2 = new TreeNode(2),
n3 = new TreeNode(3),
n4 = new TreeNode(4),
n5 = new TreeNode(5);
// Build references (pointers) between nodes
// Set up references (pointers) between the nodes
n1.left = n2;
n1.right = n3;
n2.left = n4;
n2.right = n5;
console.log('\nInitialize binary tree\n');
console.log('\nInitialize the binary tree\n');
printTree(n1);
/* Insert node P between n1 -> n2 */
/* Insert and remove nodes */
const P = new TreeNode(0);
// Delete node
// Insert node P between n1 and n2 in n1 -> n2
n1.left = P;
P.left = n2;
console.log('\nAfter inserting node P\n');
console.log('\nAfter inserting node P:\n');
printTree(n1);
// Remove node P
n1.left = n2;
console.log('\nAfter removing node P\n');
console.log('\nAfter removing node P:\n');
printTree(n1);

View File

@@ -9,26 +9,26 @@ const { printTree } = require('../modules/PrintUtil');
/* Level-order traversal */
function levelOrder(root) {
// Initialize queue, add root node
// Initialize a queue and enqueue the root
const queue = [root];
// Initialize a list to save the traversal sequence
// Initialize a list to store the traversal sequence
const list = [];
while (queue.length) {
let node = queue.shift(); // Dequeue
list.push(node.val); // Save node value
if (node.left) queue.push(node.left); // Left child node enqueue
if (node.right) queue.push(node.right); // Right child node enqueue
let node = queue.shift(); // Dequeue from the queue
list.push(node.val); // Store the node value
if (node.left) queue.push(node.left); // Enqueue the left child
if (node.right) queue.push(node.right); // Enqueue the right child
}
return list;
}
/* Driver Code */
/* Initialize binary tree */
// Here we use a function to generate a binary tree directly from an array
/* Driver code */
/* Initialize the binary tree */
// Use a helper function that builds a binary tree directly from an array
const root = arrToTree([1, 2, 3, 4, 5, 6, 7]);
console.log('\nInitialize binary tree\n');
console.log('\nInitialize the binary tree\n');
printTree(root);
/* Level-order traversal */
const list = levelOrder(root);
console.log('\nLevel-order traversal node print sequence = ' + list);
console.log('\nNode sequence from level-order traversal = ' + list);

View File

@@ -7,13 +7,13 @@
const { arrToTree } = require('../modules/TreeNode');
const { printTree } = require('../modules/PrintUtil');
// Initialize list for storing traversal sequence
// Initialize a list to store the traversal sequence
const list = [];
/* Preorder traversal */
function preOrder(root) {
if (root === null) return;
// Visit priority: root node -> left subtree -> right subtree
// Visit order: root -> left subtree -> right subtree
list.push(root.val);
preOrder(root.left);
preOrder(root.right);
@@ -22,7 +22,7 @@ function preOrder(root) {
/* Inorder traversal */
function inOrder(root) {
if (root === null) return;
// Visit priority: left subtree -> root node -> right subtree
// Visit order: left subtree -> root -> right subtree
inOrder(root.left);
list.push(root.val);
inOrder(root.right);
@@ -31,30 +31,30 @@ function inOrder(root) {
/* Postorder traversal */
function postOrder(root) {
if (root === null) return;
// Visit priority: left subtree -> right subtree -> root node
// Visit order: left subtree -> right subtree -> root
postOrder(root.left);
postOrder(root.right);
list.push(root.val);
}
/* Driver Code */
/* Initialize binary tree */
// Here we use a function to generate a binary tree directly from an array
/* Driver code */
/* Initialize the binary tree */
// Use a helper function that builds a binary tree directly from an array
const root = arrToTree([1, 2, 3, 4, 5, 6, 7]);
console.log('\nInitialize binary tree\n');
console.log('\nInitialize the binary tree\n');
printTree(root);
/* Preorder traversal */
list.length = 0;
preOrder(root);
console.log('\nPreorder traversal node print sequence = ' + list);
console.log('\nNode sequence from preorder traversal = ' + list);
/* Inorder traversal */
list.length = 0;
inOrder(root);
console.log('\nInorder traversal node print sequence = ' + list);
console.log('\nNode sequence from inorder traversal = ' + list);
/* Postorder traversal */
list.length = 0;
postOrder(root);
console.log('\nPostorder traversal node print sequence = ' + list);
console.log('\nNode sequence from postorder traversal = ' + list);