mirror of
https://github.com/krahets/hello-algo.git
synced 2026-11-10 16:00:36 +08:00
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:
@@ -12,39 +12,39 @@ from modules import TreeNode, list_to_tree, print_tree
|
||||
|
||||
|
||||
class ArrayBinaryTree:
|
||||
"""Binary tree class represented by array"""
|
||||
"""Binary tree class using an array representation"""
|
||||
|
||||
def __init__(self, arr: list[int | None]):
|
||||
"""Constructor"""
|
||||
self._tree = list(arr)
|
||||
|
||||
def size(self):
|
||||
"""List capacity"""
|
||||
"""Length of the backing list"""
|
||||
return len(self._tree)
|
||||
|
||||
def val(self, i: int) -> int | None:
|
||||
"""Get value of node at index i"""
|
||||
# If index is out of bounds, return None, representing empty position
|
||||
"""Get the value of the node at index i"""
|
||||
# If the index is out of bounds, return None to represent an empty position
|
||||
if i < 0 or i >= self.size():
|
||||
return None
|
||||
return self._tree[i]
|
||||
|
||||
def left(self, i: int) -> int | None:
|
||||
"""Get index of left child node of node at index i"""
|
||||
"""Get the left child index of the node at index i"""
|
||||
return 2 * i + 1
|
||||
|
||||
def right(self, i: int) -> int | None:
|
||||
"""Get index of right child node of node at index i"""
|
||||
"""Get the right child index of the node at index i"""
|
||||
return 2 * i + 2
|
||||
|
||||
def parent(self, i: int) -> int | None:
|
||||
"""Get index of parent node of node at index i"""
|
||||
"""Get the parent index of the node at index i"""
|
||||
return (i - 1) // 2
|
||||
|
||||
def level_order(self) -> list[int]:
|
||||
"""Level-order traversal"""
|
||||
self.res = []
|
||||
# Traverse array directly
|
||||
# Traverse the array directly
|
||||
for i in range(self.size()):
|
||||
if self.val(i) is not None:
|
||||
self.res.append(self.val(i))
|
||||
@@ -85,35 +85,35 @@ class ArrayBinaryTree:
|
||||
return self.res
|
||||
|
||||
|
||||
"""Driver Code"""
|
||||
"""Driver code"""
|
||||
if __name__ == "__main__":
|
||||
# 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, None, 6, 7, 8, 9, None, None, 12, None, None, 15]
|
||||
root = list_to_tree(arr)
|
||||
print("\nInitialize binary tree\n")
|
||||
print("Array representation of binary tree:")
|
||||
print("\nInitialize the binary tree\n")
|
||||
print("Array representation of the binary tree:")
|
||||
print(arr)
|
||||
print("Linked list representation of binary tree:")
|
||||
print("Linked representation of the binary tree:")
|
||||
print_tree(root)
|
||||
|
||||
# Binary tree class represented by array
|
||||
# Binary tree class using an array representation
|
||||
abt = ArrayBinaryTree(arr)
|
||||
|
||||
# Access nodes
|
||||
# Access a node
|
||||
i = 1
|
||||
l, r, p = abt.left(i), abt.right(i), abt.parent(i)
|
||||
print(f"\nCurrent node's index is {i}, value is {abt.val(i)}")
|
||||
print(f"Its left child node's index is {l}, value is {abt.val(l)}")
|
||||
print(f"Its right child node's index is {r}, value is {abt.val(r)}")
|
||||
print(f"Its parent node's index is {p}, value is {abt.val(p)}")
|
||||
print(f"\nCurrent node index: {i}, value: {abt.val(i)}")
|
||||
print(f"Left child index: {l}, value: {abt.val(l)}")
|
||||
print(f"Right child index: {r}, value: {abt.val(r)}")
|
||||
print(f"Parent index: {p}, value: {abt.val(p)}")
|
||||
|
||||
# Traverse tree
|
||||
# Traverse the tree
|
||||
res = abt.level_order()
|
||||
print("\nLevel-order traversal is:", res)
|
||||
print("\nLevel-order traversal:", res)
|
||||
res = abt.pre_order()
|
||||
print("Preorder traversal is:", res)
|
||||
print("Preorder traversal:", res)
|
||||
res = abt.in_order()
|
||||
print("Inorder traversal is:", res)
|
||||
print("Inorder traversal:", res)
|
||||
res = abt.post_order()
|
||||
print("Postorder traversal is:", res)
|
||||
print("Postorder traversal:", res)
|
||||
|
||||
Reference in New Issue
Block a user