diff --git a/codes/python/chapter_tree/avl_tree.py b/codes/python/chapter_tree/avl_tree.py index 04218e46a..83929981d 100644 --- a/codes/python/chapter_tree/avl_tree.py +++ b/codes/python/chapter_tree/avl_tree.py @@ -5,30 +5,28 @@ Author: a16su (lpluls001@gmail.com) """ import sys, os.path as osp -import typing - sys.path.append(osp.dirname(osp.dirname(osp.abspath(__file__)))) from include import * class AVLTree: - def __init__(self, root: typing.Optional[TreeNode] = None): + def __init__(self, root: Optional[TreeNode] = None): self.root = root """ 获取结点高度 """ - def height(self, node: typing.Optional[TreeNode]) -> int: + def height(self, node: Optional[TreeNode]) -> int: # 空结点高度为 -1 ,叶结点高度为 0 if node is not None: return node.height return -1 """ 更新结点高度 """ - def __update_height(self, node: TreeNode): + def __update_height(self, node: Optional[TreeNode]): # 结点高度等于最高子树高度 + 1 node.height = max([self.height(node.left), self.height(node.right)]) + 1 """ 获取平衡因子 """ - def balance_factor(self, node: TreeNode) -> int: + def balance_factor(self, node: Optional[TreeNode]) -> int: # 空结点平衡因子为 0 if node is None: return 0 @@ -36,7 +34,7 @@ class AVLTree: return self.height(node.left) - self.height(node.right) """ 右旋操作 """ - def __right_rotate(self, node: TreeNode) -> TreeNode: + def __right_rotate(self, node: Optional[TreeNode]) -> TreeNode: child = node.left grand_child = child.right # 以 child 为原点,将 node 向右旋转 @@ -49,7 +47,7 @@ class AVLTree: return child """ 左旋操作 """ - def __left_rotate(self, node: TreeNode) -> TreeNode: + def __left_rotate(self, node: Optional[TreeNode]) -> TreeNode: child = node.right grand_child = child.left # 以 child 为原点,将 node 向左旋转 @@ -62,7 +60,7 @@ class AVLTree: return child """ 执行旋转操作,使该子树重新恢复平衡 """ - def __rotate(self, node: TreeNode) -> TreeNode: + def __rotate(self, node: Optional[TreeNode]) -> TreeNode: # 获取结点 node 的平衡因子 balance_factor = self.balance_factor(node) # 左偏树 @@ -92,7 +90,7 @@ class AVLTree: return self.root """ 递归插入结点(辅助函数)""" - def __insert_helper(self, node: typing.Optional[TreeNode], val: int) -> TreeNode: + def __insert_helper(self, node: Optional[TreeNode], val: int) -> TreeNode: if node is None: return TreeNode(val) # 1. 查找插入位置,并插入结点 @@ -114,7 +112,7 @@ class AVLTree: return root """ 递归删除结点(辅助函数) """ - def __remove_helper(self, node: typing.Optional[TreeNode], val: int) -> typing.Optional[TreeNode]: + def __remove_helper(self, node: Optional[TreeNode], val: int) -> Optional[TreeNode]: if node is None: return None # 1. 查找结点,并删除之 @@ -141,7 +139,7 @@ class AVLTree: return self.__rotate(node) """ 获取中序遍历中的下一个结点(仅适用于 root 有左子结点的情况) """ - def __get_inorder_next(self, node: typing.Optional[TreeNode]) -> typing.Optional[TreeNode]: + def __get_inorder_next(self, node: Optional[TreeNode]) -> Optional[TreeNode]: if node is None: return None # 循环访问左子结点,直到叶结点时为最小结点,跳出 diff --git a/codes/python/chapter_tree/binary_search_tree.py b/codes/python/chapter_tree/binary_search_tree.py index 7633b6288..2a8272761 100644 --- a/codes/python/chapter_tree/binary_search_tree.py +++ b/codes/python/chapter_tree/binary_search_tree.py @@ -5,20 +5,18 @@ Author: a16su (lpluls001@gmail.com) """ import sys, os.path as osp -import typing - sys.path.append(osp.dirname(osp.dirname(osp.abspath(__file__)))) from include import * """ 二叉搜索树 """ class BinarySearchTree: - def __init__(self, nums: typing.List[int]) -> None: + def __init__(self, nums: List[int]) -> None: nums.sort() self.__root = self.build_tree(nums, 0, len(nums) - 1) """ 构建二叉搜索树 """ - def build_tree(self, nums: typing.List[int], start_index: int, end_index: int) -> typing.Optional[TreeNode]: + def build_tree(self, nums: List[int], start_index: int, end_index: int) -> Optional[TreeNode]: if start_index > end_index: return None @@ -31,11 +29,11 @@ class BinarySearchTree: return root @property - def root(self) -> typing.Optional[TreeNode]: + def root(self) -> Optional[TreeNode]: return self.__root """ 查找结点 """ - def search(self, num: int) -> typing.Optional[TreeNode]: + def search(self, num: int) -> Optional[TreeNode]: cur = self.root # 循环查找,越过叶结点后跳出 while cur is not None: @@ -51,7 +49,7 @@ class BinarySearchTree: return cur """ 插入结点 """ - def insert(self, num: int) -> typing.Optional[TreeNode]: + def insert(self, num: int) -> Optional[TreeNode]: root = self.root # 若树为空,直接提前返回 if root is None: @@ -81,7 +79,7 @@ class BinarySearchTree: return node """ 删除结点 """ - def remove(self, num: int) -> typing.Optional[TreeNode]: + def remove(self, num: int) -> Optional[TreeNode]: root = self.root # 若树为空,直接提前返回 if root is None: @@ -126,7 +124,7 @@ class BinarySearchTree: return cur """ 获取中序遍历中的下一个结点(仅适用于 root 有左子结点的情况) """ - def get_inorder_next(self, root: typing.Optional[TreeNode]) -> typing.Optional[TreeNode]: + def get_inorder_next(self, root: Optional[TreeNode]) -> Optional[TreeNode]: if root is None: return root # 循环访问左子结点,直到叶结点时为最小结点,跳出 diff --git a/codes/python/chapter_tree/binary_tree_bfs.py b/codes/python/chapter_tree/binary_tree_bfs.py index 225a8bd6e..fb3e93059 100644 --- a/codes/python/chapter_tree/binary_tree_bfs.py +++ b/codes/python/chapter_tree/binary_tree_bfs.py @@ -5,14 +5,12 @@ Author: a16su (lpluls001@gmail.com) """ import sys, os.path as osp -import typing - sys.path.append(osp.dirname(osp.dirname(osp.abspath(__file__)))) from include import * """ 层序遍历 """ -def hier_order(root: TreeNode): +def hier_order(root: Optional[TreeNode]): # 初始化队列,加入根结点 queue = collections.deque() queue.append(root) diff --git a/codes/python/chapter_tree/binary_tree_dfs.py b/codes/python/chapter_tree/binary_tree_dfs.py index a1d46e697..b0efb14c8 100644 --- a/codes/python/chapter_tree/binary_tree_dfs.py +++ b/codes/python/chapter_tree/binary_tree_dfs.py @@ -5,8 +5,6 @@ Author: a16su (lpluls001@gmail.com) """ import sys, os.path as osp -import typing - sys.path.append(osp.dirname(osp.dirname(osp.abspath(__file__)))) from include import * @@ -14,7 +12,7 @@ from include import * res = [] """ 前序遍历 """ -def pre_order(root: typing.Optional[TreeNode]): +def pre_order(root: Optional[TreeNode]): if root is None: return # 访问优先级:根结点 -> 左子树 -> 右子树 @@ -23,7 +21,7 @@ def pre_order(root: typing.Optional[TreeNode]): pre_order(root=root.right) """ 中序遍历 """ -def in_order(root: typing.Optional[TreeNode]): +def in_order(root: Optional[TreeNode]): if root is None: return # 访问优先级:左子树 -> 根结点 -> 右子树 @@ -32,7 +30,7 @@ def in_order(root: typing.Optional[TreeNode]): in_order(root=root.right) """ 后序遍历 """ -def post_order(root: typing.Optional[TreeNode]): +def post_order(root: Optional[TreeNode]): if root is None: return # 访问优先级:左子树 -> 右子树 -> 根结点 diff --git a/codes/python/include/__init__.py b/codes/python/include/__init__.py index 31ed69ace..9d4e90bee 100644 --- a/codes/python/include/__init__.py +++ b/codes/python/include/__init__.py @@ -4,7 +4,7 @@ import queue import random import functools import collections -from typing import List +from typing import Optional, List, Dict, DefaultDict, OrderedDict, Set, Deque from .linked_list import ListNode, list_to_linked_list, linked_list_to_list, get_list_node from .binary_tree import TreeNode, list_to_tree, tree_to_list, get_tree_node from .print_util import print_matrix, print_linked_list, print_tree, print_dict \ No newline at end of file diff --git a/docs/chapter_tree/avl_tree.md b/docs/chapter_tree/avl_tree.md index 8fc2313c7..9944f29e8 100644 --- a/docs/chapter_tree/avl_tree.md +++ b/docs/chapter_tree/avl_tree.md @@ -134,14 +134,14 @@ G. M. Adelson-Velsky 和 E. M. Landis 在其 1962 年发表的论文 "An algorit ```python title="avl_tree.py" """ 获取结点高度 """ - def height(self, node: typing.Optional[TreeNode]) -> int: + def height(self, node: Optional[TreeNode]) -> int: # 空结点高度为 -1 ,叶结点高度为 0 if node is not None: return node.height return -1 """ 更新结点高度 """ - def __update_height(self, node: TreeNode): + def __update_height(self, node: Optional[TreeNode]): # 结点高度等于最高子树高度 + 1 node.height = max([self.height(node.left), self.height(node.right)]) + 1 ``` @@ -239,7 +239,7 @@ G. M. Adelson-Velsky 和 E. M. Landis 在其 1962 年发表的论文 "An algorit ```python title="avl_tree.py" """ 获取平衡因子 """ - def balance_factor(self, node: TreeNode) -> int: + def balance_factor(self, node: Optional[TreeNode]) -> int: # 空结点平衡因子为 0 if node is None: return 0 @@ -355,7 +355,7 @@ AVL 树的独特之处在于「旋转 Rotation」的操作,其可 **在不影 ```python title="avl_tree.py" """ 右旋操作 """ - def __right_rotate(self, node: TreeNode) -> TreeNode: + def __right_rotate(self, node: Optional[TreeNode]) -> TreeNode: child = node.left grand_child = child.right # 以 child 为原点,将 node 向右旋转 @@ -470,7 +470,7 @@ AVL 树的独特之处在于「旋转 Rotation」的操作,其可 **在不影 ```python title="avl_tree.py" """ 左旋操作 """ - def __left_rotate(self, node: TreeNode) -> TreeNode: + def __left_rotate(self, node: Optional[TreeNode]) -> TreeNode: child = node.right grand_child = child.left # 以 child 为原点,将 node 向左旋转 @@ -621,7 +621,7 @@ AVL 树的独特之处在于「旋转 Rotation」的操作,其可 **在不影 ```python title="avl_tree.py" """ 执行旋转操作,使该子树重新恢复平衡 """ - def __rotate(self, node: TreeNode) -> TreeNode: + def __rotate(self, node: Optional[TreeNode]) -> TreeNode: # 获取结点 node 的平衡因子 balance_factor = self.balance_factor(node) # 左偏树 @@ -796,7 +796,7 @@ AVL 树的独特之处在于「旋转 Rotation」的操作,其可 **在不影 return self.root """ 递归插入结点(辅助函数)""" - def __insert_helper(self, node: typing.Optional[TreeNode], val: int) -> TreeNode: + def __insert_helper(self, node: Optional[TreeNode], val: int) -> TreeNode: if node is None: return TreeNode(val) # 1. 查找插入位置,并插入结点 @@ -957,7 +957,7 @@ AVL 树的独特之处在于「旋转 Rotation」的操作,其可 **在不影 return root """ 递归删除结点(辅助函数) """ - def __remove_helper(self, node: typing.Optional[TreeNode], val: int) -> typing.Optional[TreeNode]: + def __remove_helper(self, node: Optional[TreeNode], val: int) -> Optional[TreeNode]: if node is None: return None # 1. 查找结点,并删除之 diff --git a/docs/chapter_tree/binary_search_tree.md b/docs/chapter_tree/binary_search_tree.md index e3ddfaaac..a4c5a5d2a 100644 --- a/docs/chapter_tree/binary_search_tree.md +++ b/docs/chapter_tree/binary_search_tree.md @@ -83,7 +83,7 @@ comments: true ```python title="binary_search_tree.py" """ 查找结点 """ - def search(self, num: int) -> typing.Optional[TreeNode]: + def search(self, num: int) -> Optional[TreeNode]: cur = self.root # 循环查找,越过叶结点后跳出 while cur is not None: @@ -265,7 +265,7 @@ comments: true ```python title="binary_search_tree.py" """ 插入结点 """ - def insert(self, num: int) -> typing.Optional[TreeNode]: + def insert(self, num: int) -> Optional[TreeNode]: root = self.root # 若树为空,直接提前返回 if root is None: @@ -560,7 +560,7 @@ comments: true ```python title="binary_search_tree.py" """ 删除结点 """ - def remove(self, num: int) -> typing.Optional[TreeNode]: + def remove(self, num: int) -> Optional[TreeNode]: root = self.root # 若树为空,直接提前返回 if root is None: diff --git a/docs/chapter_tree/binary_tree_traversal.md b/docs/chapter_tree/binary_tree_traversal.md index c64077072..650a15297 100644 --- a/docs/chapter_tree/binary_tree_traversal.md +++ b/docs/chapter_tree/binary_tree_traversal.md @@ -66,7 +66,7 @@ comments: true ```python title="binary_tree_bfs.py" """ 层序遍历 """ - def hier_order(root: TreeNode): + def hier_order(root: Optional[TreeNode]): # 初始化队列,加入根结点 queue = collections.deque() queue.append(root) @@ -277,7 +277,7 @@ comments: true ```python title="binary_tree_dfs.py" """ 前序遍历 """ - def pre_order(root: typing.Optional[TreeNode]): + def pre_order(root: Optional[TreeNode]): if root is None: return # 访问优先级:根结点 -> 左子树 -> 右子树 @@ -286,7 +286,7 @@ comments: true pre_order(root=root.right) """ 中序遍历 """ - def in_order(root: typing.Optional[TreeNode]): + def in_order(root: Optional[TreeNode]): if root is None: return # 访问优先级:左子树 -> 根结点 -> 右子树 @@ -295,7 +295,7 @@ comments: true in_order(root=root.right) """ 后序遍历 """ - def post_order(root: typing.Optional[TreeNode]): + def post_order(root: Optional[TreeNode]): if root is None: return # 访问优先级:左子树 -> 右子树 -> 根结点