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,37 +7,37 @@ Author: Xuan Khoa Tu Nguyen (ngxktuzkai2000@gmail.com)
require_relative '../utils/tree_node'
require_relative '../utils/print_util'
### Array representation of binary tree class ###
### Binary tree class using an array representation ###
class ArrayBinaryTree
### Constructor ###
def initialize(arr)
@tree = arr.to_a
end
### List capacity ###
### Length of the array representation ###
def size
@tree.length
end
### Get value of node at index i ###
### Get the value of the node at index i ###
def val(i)
# Return nil if index out of bounds, representing empty position
# If the index is out of bounds, return nil to indicate an empty position
return if i < 0 || i >= size
@tree[i]
end
### Get left child index of node at index i ###
### Get the left child index of the node at index i ###
def left(i)
2 * i + 1
end
### Get right child index of node at index i ###
### Get the right child index of the node at index i ###
def right(i)
2 * i + 2
end
### Get parent node index of node at index i ###
### Get the parent index of the node at index i ###
def parent(i)
(i - 1) / 2
end
@@ -46,7 +46,7 @@ class ArrayBinaryTree
def level_order
@res = []
# Traverse array directly
# Traverse the array directly
for i in 0...size
@res << val(i) unless val(i).nil?
end
@@ -67,21 +67,21 @@ class ArrayBinaryTree
@res << val(i) if order == :post
end
### Pre-order traversal ###
### Preorder traversal ###
def pre_order
@res = []
dfs(0, :pre)
@res
end
### In-order traversal ###
### Inorder traversal ###
def in_order
@res = []
dfs(0, :in)
@res
end
### Post-order traversal ###
### Postorder traversal ###
def post_order
@res = []
dfs(0, :post)
@@ -89,36 +89,36 @@ class ArrayBinaryTree
end
end
### Driver Code ###
### Driver code ###
if __FILE__ == $0
# 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
arr = [1, 2, 3, 4, nil, 6, 7, 8, 9, nil, nil, 12, nil, nil, 15]
root = arr_to_tree(arr)
puts "\nInitialize binary tree\n\n"
puts 'Array representation of binary tree:'
puts "\nInitialize the binary tree\n\n"
puts 'Array representation of the binary tree:'
pp arr
puts 'Linked list representation of binary tree:'
puts 'Linked representation of the binary tree:'
print_tree(root)
# Binary tree class represented by array
# Binary tree class using an array representation
abt = ArrayBinaryTree.new(arr)
# Access node
# Access a node
i = 1
l, r, _p = abt.left(i), abt.right(i), abt.parent(i)
puts "\nCurrent node index is #{i}, value is #{abt.val(i).inspect}"
puts "Left child index is #{l}, value is #{abt.val(l).inspect}"
puts "Right child index is #{r}, value is #{abt.val(r).inspect}"
puts "Parent node index is #{_p}, value is #{abt.val(_p).inspect}"
puts "\nCurrent node: index = #{i}, value = #{abt.val(i).inspect}"
puts "Left child: index = #{l}, value = #{abt.val(l).inspect}"
puts "Right child: index = #{r}, value = #{abt.val(r).inspect}"
puts "Parent: index = #{_p}, value = #{abt.val(_p).inspect}"
# Traverse tree
# Traverse the tree
res = abt.level_order
puts "\nLevel-order traversal is: #{res}"
puts "\nLevel-order traversal: #{res}"
res = abt.pre_order
puts "Pre-order traversal is: #{res}"
puts "Preorder traversal: #{res}"
res = abt.in_order
puts "In-order traversal is: #{res}"
puts "Inorder traversal: #{res}"
res = abt.post_order
puts "Post-order traversal is: #{res}"
puts "Postorder traversal: #{res}"
end