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:
@@ -1,8 +1,8 @@
|
||||
# Follow the PEP 585 - Type Hinting Generics In Standard Collections
|
||||
# Follow PEP 585: Type Hinting Generics in Standard Collections
|
||||
# https://peps.python.org/pep-0585/
|
||||
from __future__ import annotations
|
||||
|
||||
# Import common libs here to simplify the code by `from module import *`
|
||||
# Import common utilities here to simplify code with `from modules import *`
|
||||
from .list_node import (
|
||||
ListNode,
|
||||
list_to_linked_list,
|
||||
|
||||
@@ -10,7 +10,7 @@ class ListNode:
|
||||
|
||||
def __init__(self, val: int):
|
||||
self.val: int = val # Node value
|
||||
self.next: ListNode | None = None # Reference to next node
|
||||
self.next: ListNode | None = None # Reference to the next node
|
||||
|
||||
|
||||
def list_to_linked_list(arr: list[int]) -> ListNode | None:
|
||||
|
||||
@@ -9,7 +9,7 @@ from .list_node import ListNode, linked_list_to_list
|
||||
|
||||
|
||||
def print_matrix(mat: list[list[int]]):
|
||||
"""Print matrix"""
|
||||
"""Print a matrix"""
|
||||
s = []
|
||||
for arr in mat:
|
||||
s.append(" " + str(arr))
|
||||
@@ -17,7 +17,7 @@ def print_matrix(mat: list[list[int]]):
|
||||
|
||||
|
||||
def print_linked_list(head: ListNode | None):
|
||||
"""Print linked list"""
|
||||
"""Print a linked list"""
|
||||
arr: list[int] = linked_list_to_list(head)
|
||||
print(" -> ".join([str(a) for a in arr]))
|
||||
|
||||
@@ -39,7 +39,7 @@ def print_tree(
|
||||
root: TreeNode | None, prev: Trunk | None = None, is_right: bool = False
|
||||
):
|
||||
"""
|
||||
Print binary tree
|
||||
Print a binary tree
|
||||
This tree printer is borrowed from TECHIE DELIGHT
|
||||
https://www.techiedelight.com/c-program-print-binary-tree/
|
||||
"""
|
||||
@@ -68,13 +68,13 @@ def print_tree(
|
||||
|
||||
|
||||
def print_dict(hmap: dict):
|
||||
"""Print dictionary"""
|
||||
"""Print a dictionary"""
|
||||
for key, value in hmap.items():
|
||||
print(key, "->", value)
|
||||
|
||||
|
||||
def print_heap(heap: list[int]):
|
||||
"""Print heap"""
|
||||
"""Print a heap"""
|
||||
print("Array representation of the heap:", heap)
|
||||
print("Tree representation of the heap:")
|
||||
root: TreeNode | None = list_to_tree(heap)
|
||||
|
||||
@@ -13,14 +13,14 @@ class TreeNode:
|
||||
def __init__(self, val: int = 0):
|
||||
self.val: int = val # Node value
|
||||
self.height: int = 0 # Node height
|
||||
self.left: TreeNode | None = None # Reference to left child node
|
||||
self.right: TreeNode | None = None # Reference to right child node
|
||||
self.left: TreeNode | None = None # Reference to the left child
|
||||
self.right: TreeNode | None = None # Reference to the right child
|
||||
|
||||
# For the serialization encoding rules, please refer to:
|
||||
# https://www.hello-algo.com/chapter_tree/array_representation_of_tree/
|
||||
# Array representation of binary tree:
|
||||
# For the serialization rules, see:
|
||||
# https://www.hello-algo.com/en/chapter_tree/array_representation_of_tree/
|
||||
# Array representation of the binary tree:
|
||||
# [1, 2, 3, 4, None, 6, 7, 8, 9, None, None, 12, None, None, 15]
|
||||
# Linked list representation of binary tree:
|
||||
# Linked representation of the binary tree:
|
||||
# /——— 15
|
||||
# /——— 7
|
||||
# /——— 3
|
||||
@@ -35,10 +35,10 @@ class TreeNode:
|
||||
|
||||
def list_to_tree_dfs(arr: list[int], i: int) -> TreeNode | None:
|
||||
"""Deserialize a list into a binary tree: recursion"""
|
||||
# If the index exceeds the array length, or the corresponding element is None, return None
|
||||
# Return None if the index is out of bounds or the corresponding element is None
|
||||
if i < 0 or i >= len(arr) or arr[i] is None:
|
||||
return None
|
||||
# Build the current node
|
||||
# Create the current node
|
||||
root = TreeNode(arr[i])
|
||||
# Recursively build the left and right subtrees
|
||||
root.left = list_to_tree_dfs(arr, 2 * i + 1)
|
||||
|
||||
@@ -11,10 +11,10 @@ class Vertex:
|
||||
|
||||
|
||||
def vals_to_vets(vals: list[int]) -> list["Vertex"]:
|
||||
"""Input value list vals, return vertex list vets"""
|
||||
"""Given the list of values vals, return the list of vertices vets"""
|
||||
return [Vertex(val) for val in vals]
|
||||
|
||||
|
||||
def vets_to_vals(vets: list["Vertex"]) -> list[int]:
|
||||
"""Input vertex list vets, return value list vals"""
|
||||
"""Given the list of vertices vets, return the list of values vals"""
|
||||
return [vet.val for vet in vets]
|
||||
|
||||
Reference in New Issue
Block a user