mirror of
https://github.com/krahets/hello-algo.git
synced 2026-06-15 14:48:05 +08:00
Re-translate the Japanese version (#1871)
* Retranslate Japanese docs with GPT-5.4 * Retranslate Japanese code with GPT-5.4
This commit is contained in:
@@ -1,8 +1,8 @@
|
||||
# PEP 585に従う - 標準コレクションでの型ヒント
|
||||
# Follow the PEP 585 - Type Hinting Generics In Standard Collections
|
||||
# https://peps.python.org/pep-0585/
|
||||
from __future__ import annotations
|
||||
|
||||
# 共通ライブラリをここでインポートして、`from module import *`でコードを簡潔にする
|
||||
# Import common libs here to simplify the code by `from module import *`
|
||||
from .list_node import (
|
||||
ListNode,
|
||||
list_to_linked_list,
|
||||
@@ -16,4 +16,4 @@ from .print_util import (
|
||||
print_tree,
|
||||
print_dict,
|
||||
print_heap,
|
||||
)
|
||||
)
|
||||
|
||||
@@ -6,15 +6,15 @@ Author: krahets (krahets@163.com)
|
||||
|
||||
|
||||
class ListNode:
|
||||
"""連結リストのノードクラス"""
|
||||
"""連結リストノードクラス"""
|
||||
|
||||
def __init__(self, val: int):
|
||||
self.val: int = val # ノードの値
|
||||
self.val: int = val # ノード値
|
||||
self.next: ListNode | None = None # 後続ノードへの参照
|
||||
|
||||
|
||||
def list_to_linked_list(arr: list[int]) -> ListNode | None:
|
||||
"""リストを連結リストにデシリアライズ"""
|
||||
"""リストを連結リストにデシリアライズする"""
|
||||
dum = head = ListNode(0)
|
||||
for a in arr:
|
||||
node = ListNode(a)
|
||||
@@ -29,4 +29,4 @@ def linked_list_to_list(head: ListNode | None) -> list[int]:
|
||||
while head:
|
||||
arr.append(head.val)
|
||||
head = head.next
|
||||
return arr
|
||||
return arr
|
||||
|
||||
@@ -9,7 +9,7 @@ from .list_node import ListNode, linked_list_to_list
|
||||
|
||||
|
||||
def print_matrix(mat: list[list[int]]):
|
||||
"""行列を出力"""
|
||||
"""行列を出力する"""
|
||||
s = []
|
||||
for arr in mat:
|
||||
s.append(" " + str(arr))
|
||||
@@ -39,10 +39,10 @@ def print_tree(
|
||||
root: TreeNode | None, prev: Trunk | None = None, is_right: bool = False
|
||||
):
|
||||
"""
|
||||
二分木を出力
|
||||
この木プリンタはTECHIE DELIGHTから借用
|
||||
https://www.techiedelight.com/c-program-print-binary-tree/
|
||||
"""
|
||||
二分木を出力
|
||||
This tree printer is borrowed from TECHIE DELIGHT
|
||||
https://www.techiedelight.com/c-program-print-binary-tree/
|
||||
"""
|
||||
if root is None:
|
||||
return
|
||||
|
||||
@@ -75,7 +75,7 @@ def print_dict(hmap: dict):
|
||||
|
||||
def print_heap(heap: list[int]):
|
||||
"""ヒープを出力"""
|
||||
print("ヒープの配列表現:", heap)
|
||||
print("ヒープの木表現:")
|
||||
print("ヒープの配列表現:", heap)
|
||||
print("ヒープの木構造表示:")
|
||||
root: TreeNode | None = list_to_tree(heap)
|
||||
print_tree(root)
|
||||
print_tree(root)
|
||||
|
||||
@@ -8,51 +8,51 @@ from collections import deque
|
||||
|
||||
|
||||
class TreeNode:
|
||||
"""二分木のノードクラス"""
|
||||
"""二分木ノードクラス"""
|
||||
|
||||
def __init__(self, val: int = 0):
|
||||
self.val: int = val # ノードの値
|
||||
self.val: int = val # ノード値
|
||||
self.height: int = 0 # ノードの高さ
|
||||
self.left: TreeNode | None = None # 左の子ノードへの参照
|
||||
self.right: TreeNode | None = None # 右の子ノードへの参照
|
||||
self.left: TreeNode | None = None # 左子ノードへの参照
|
||||
self.right: TreeNode | None = None # 右子ノードへの参照
|
||||
|
||||
# シリアライゼーションのエンコーディングルールについては、以下を参照:
|
||||
# シリアライズの符号化規則は以下を参照:
|
||||
# https://www.hello-algo.com/chapter_tree/array_representation_of_tree/
|
||||
# 二分木の配列表現:
|
||||
# [1, 2, 3, 4, None, 6, 7, 8, 9, None, None, 12, None, None, 15]
|
||||
# 二分木の連結リスト表現:
|
||||
# /——— 15
|
||||
# /——— 7
|
||||
# /——— 3
|
||||
# | \——— 6
|
||||
# | \——— 12
|
||||
# /——— 15
|
||||
# /——— 7
|
||||
# /——— 3
|
||||
# | \——— 6
|
||||
# | \——— 12
|
||||
# ——— 1
|
||||
# \——— 2
|
||||
# | /——— 9
|
||||
# \——— 4
|
||||
# \——— 8
|
||||
# \——— 2
|
||||
# | /——— 9
|
||||
# \——— 4
|
||||
# \——— 8
|
||||
|
||||
|
||||
def list_to_tree_dfs(arr: list[int], i: int) -> TreeNode | None:
|
||||
"""リストを二分木にデシリアライズ: 再帰的"""
|
||||
# インデックスが配列の境界外、または対応する要素がNoneの場合、Noneを返す
|
||||
"""リストを二分木にデシリアライズする: 再帰"""
|
||||
# 添字が配列長を超えるか、対応する要素が None なら、None を返す
|
||||
if i < 0 or i >= len(arr) or arr[i] is None:
|
||||
return None
|
||||
# 現在のノードを構築
|
||||
# 現在のノードを構築する
|
||||
root = TreeNode(arr[i])
|
||||
# 左右の部分木を再帰的に構築
|
||||
# 左右の部分木を再帰的に構築する
|
||||
root.left = list_to_tree_dfs(arr, 2 * i + 1)
|
||||
root.right = list_to_tree_dfs(arr, 2 * i + 2)
|
||||
return root
|
||||
|
||||
|
||||
def list_to_tree(arr: list[int]) -> TreeNode | None:
|
||||
"""リストを二分木にデシリアライズ"""
|
||||
"""リストを二分木にデシリアライズする"""
|
||||
return list_to_tree_dfs(arr, 0)
|
||||
|
||||
|
||||
def tree_to_list_dfs(root: TreeNode, i: int, res: list[int]) -> list[int]:
|
||||
"""二分木をリストにシリアライズ: 再帰的"""
|
||||
"""二分木をリストにシリアライズする: 再帰"""
|
||||
if root is None:
|
||||
return
|
||||
if i >= len(res):
|
||||
@@ -63,7 +63,7 @@ def tree_to_list_dfs(root: TreeNode, i: int, res: list[int]) -> list[int]:
|
||||
|
||||
|
||||
def tree_to_list(root: TreeNode | None) -> list[int]:
|
||||
"""二分木をリストにシリアライズ"""
|
||||
"""二分木をリストにシリアライズする"""
|
||||
res = []
|
||||
tree_to_list_dfs(root, 0, res)
|
||||
return res
|
||||
return res
|
||||
|
||||
@@ -11,10 +11,10 @@ class Vertex:
|
||||
|
||||
|
||||
def vals_to_vets(vals: list[int]) -> list["Vertex"]:
|
||||
"""値のリストvalsを入力し、頂点のリストvetsを返す"""
|
||||
"""値リスト vals を入力し、頂点リスト vets を返す"""
|
||||
return [Vertex(val) for val in vals]
|
||||
|
||||
|
||||
def vets_to_vals(vets: list["Vertex"]) -> list[int]:
|
||||
"""頂点のリストvetsを入力し、値のリストvalsを返す"""
|
||||
return [vet.val for vet in vets]
|
||||
"""頂点リスト vets を入力し、値リスト vals を返す"""
|
||||
return [vet.val for vet in vets]
|
||||
|
||||
Reference in New Issue
Block a user