mirror of
https://github.com/krahets/hello-algo.git
synced 2026-04-01 17:53:18 +08:00
build
This commit is contained in:
@@ -194,14 +194,14 @@ G. M. Adelson-Velsky 和 E. M. Landis 在其 1962 年发表的论文 "An algorit
|
||||
=== "Python"
|
||||
|
||||
```python title="avl_tree.py"
|
||||
def height(self, node: Optional[TreeNode]) -> int:
|
||||
def height(self, node: TreeNode | None) -> int:
|
||||
""" 获取结点高度 """
|
||||
# 空结点高度为 -1 ,叶结点高度为 0
|
||||
if node is not None:
|
||||
return node.height
|
||||
return -1
|
||||
|
||||
def __update_height(self, node: Optional[TreeNode]):
|
||||
def __update_height(self, node: TreeNode | None):
|
||||
""" 更新结点高度 """
|
||||
# 结点高度等于最高子树高度 + 1
|
||||
node.height = max([self.height(node.left), self.height(node.right)]) + 1
|
||||
@@ -354,7 +354,7 @@ G. M. Adelson-Velsky 和 E. M. Landis 在其 1962 年发表的论文 "An algorit
|
||||
=== "Python"
|
||||
|
||||
```python title="avl_tree.py"
|
||||
def balance_factor(self, node: Optional[TreeNode]) -> int:
|
||||
def balance_factor(self, node: TreeNode | None) -> int:
|
||||
""" 获取平衡因子 """
|
||||
# 空结点平衡因子为 0
|
||||
if node is None:
|
||||
@@ -517,7 +517,7 @@ AVL 树的独特之处在于「旋转 Rotation」的操作,其可 **在不影
|
||||
=== "Python"
|
||||
|
||||
```python title="avl_tree.py"
|
||||
def __right_rotate(self, node: Optional[TreeNode]) -> Optional[TreeNode]:
|
||||
def __right_rotate(self, node: TreeNode | None) -> TreeNode | None:
|
||||
""" 右旋操作 """
|
||||
child = node.left
|
||||
grand_child = child.right
|
||||
@@ -701,7 +701,7 @@ AVL 树的独特之处在于「旋转 Rotation」的操作,其可 **在不影
|
||||
=== "Python"
|
||||
|
||||
```python title="avl_tree.py"
|
||||
def __left_rotate(self, node: Optional[TreeNode]) -> Optional[TreeNode]:
|
||||
def __left_rotate(self, node: TreeNode | None) -> TreeNode | None:
|
||||
""" 左旋操作 """
|
||||
child = node.right
|
||||
grand_child = child.left
|
||||
@@ -940,7 +940,7 @@ AVL 树的独特之处在于「旋转 Rotation」的操作,其可 **在不影
|
||||
=== "Python"
|
||||
|
||||
```python title="avl_tree.py"
|
||||
def __rotate(self, node: Optional[TreeNode]) -> Optional[TreeNode]:
|
||||
def __rotate(self, node: TreeNode | None) -> TreeNode | None:
|
||||
""" 执行旋转操作,使该子树重新恢复平衡 """
|
||||
# 获取结点 node 的平衡因子
|
||||
balance_factor = self.balance_factor(node)
|
||||
@@ -1255,7 +1255,7 @@ AVL 树的独特之处在于「旋转 Rotation」的操作,其可 **在不影
|
||||
self.__root = self.__insert_helper(self.__root, val)
|
||||
return self.__root
|
||||
|
||||
def __insert_helper(self, node: Optional[TreeNode], val: int) -> TreeNode:
|
||||
def __insert_helper(self, node: TreeNode | None, val: int) -> TreeNode:
|
||||
""" 递归插入结点(辅助方法)"""
|
||||
if node is None:
|
||||
return TreeNode(val)
|
||||
@@ -1575,12 +1575,12 @@ AVL 树的独特之处在于「旋转 Rotation」的操作,其可 **在不影
|
||||
=== "Python"
|
||||
|
||||
```python title="avl_tree.py"
|
||||
def remove(self, val: int) -> Optional[TreeNode]:
|
||||
def remove(self, val: int) -> TreeNode | None:
|
||||
""" 删除结点 """
|
||||
self.__root = self.__remove_helper(self.__root, val)
|
||||
return self.__root
|
||||
|
||||
def __remove_helper(self, node: Optional[TreeNode], val: int) -> Optional[TreeNode]:
|
||||
def __remove_helper(self, node: TreeNode | None, val: int) -> TreeNode | None:
|
||||
""" 递归删除结点(辅助方法) """
|
||||
if node is None:
|
||||
return None
|
||||
@@ -1607,7 +1607,7 @@ AVL 树的独特之处在于「旋转 Rotation」的操作,其可 **在不影
|
||||
# 2. 执行旋转操作,使该子树重新恢复平衡
|
||||
return self.__rotate(node)
|
||||
|
||||
def __get_inorder_next(self, node: Optional[TreeNode]) -> Optional[TreeNode]:
|
||||
def __get_inorder_next(self, node: TreeNode | None) -> TreeNode | None:
|
||||
""" 获取中序遍历中的下一个结点(仅适用于 root 有左子结点的情况) """
|
||||
if node is None:
|
||||
return None
|
||||
|
||||
@@ -80,9 +80,9 @@ comments: true
|
||||
=== "Python"
|
||||
|
||||
```python title="binary_search_tree.py"
|
||||
def search(self, num: int) -> Optional[TreeNode]:
|
||||
def search(self, num: int) -> TreeNode | None:
|
||||
""" 查找结点 """
|
||||
cur: Optional[TreeNode] = self.__root
|
||||
cur: TreeNode | None = self.__root
|
||||
# 循环查找,越过叶结点后跳出
|
||||
while cur is not None:
|
||||
# 目标结点在 cur 的右子树中
|
||||
@@ -308,7 +308,7 @@ comments: true
|
||||
=== "Python"
|
||||
|
||||
```python title="binary_search_tree.py"
|
||||
def insert(self, num: int) -> Optional[TreeNode]:
|
||||
def insert(self, num: int) -> TreeNode | None:
|
||||
""" 插入结点 """
|
||||
# 若树为空,直接提前返回
|
||||
if self.__root is None:
|
||||
@@ -691,7 +691,7 @@ comments: true
|
||||
=== "Python"
|
||||
|
||||
```python title="binary_search_tree.py"
|
||||
def remove(self, num: int) -> Optional[TreeNode]:
|
||||
def remove(self, num: int) -> TreeNode | None:
|
||||
""" 删除结点 """
|
||||
# 若树为空,直接提前返回
|
||||
if self.__root is None:
|
||||
@@ -732,7 +732,7 @@ comments: true
|
||||
cur.val = tmp
|
||||
return cur
|
||||
|
||||
def get_inorder_next(self, root: Optional[TreeNode]) -> Optional[TreeNode]:
|
||||
def get_inorder_next(self, root: TreeNode | None) -> TreeNode | None:
|
||||
""" 获取中序遍历中的下一个结点(仅适用于 root 有左子结点的情况) """
|
||||
if root is None:
|
||||
return root
|
||||
|
||||
@@ -69,13 +69,13 @@ comments: true
|
||||
=== "Python"
|
||||
|
||||
```python title="binary_tree_bfs.py"
|
||||
def level_order(root: Optional[TreeNode]) -> List[int]:
|
||||
def level_order(root: TreeNode | None) -> list[int]:
|
||||
""" 层序遍历 """
|
||||
# 初始化队列,加入根结点
|
||||
queue: Deque[TreeNode] = collections.deque()
|
||||
queue: deque[TreeNode] = deque()
|
||||
queue.append(root)
|
||||
# 初始化一个列表,用于保存遍历序列
|
||||
res: List[int] = []
|
||||
res: list[int] = []
|
||||
while queue:
|
||||
node: TreeNode = queue.popleft() # 队列出队
|
||||
res.append(node.val) # 保存结点值
|
||||
@@ -337,7 +337,7 @@ comments: true
|
||||
=== "Python"
|
||||
|
||||
```python title="binary_tree_dfs.py"
|
||||
def pre_order(root: Optional[TreeNode]) -> None:
|
||||
def pre_order(root: TreeNode | None) -> None:
|
||||
""" 前序遍历 """
|
||||
if root is None:
|
||||
return
|
||||
@@ -346,7 +346,7 @@ comments: true
|
||||
pre_order(root=root.left)
|
||||
pre_order(root=root.right)
|
||||
|
||||
def in_order(root: Optional[TreeNode]) -> None:
|
||||
def in_order(root: TreeNode | None) -> None:
|
||||
""" 中序遍历 """
|
||||
if root is None:
|
||||
return
|
||||
@@ -355,7 +355,7 @@ comments: true
|
||||
res.append(root.val)
|
||||
in_order(root=root.right)
|
||||
|
||||
def post_order(root: Optional[TreeNode]) -> None:
|
||||
def post_order(root: TreeNode | None) -> None:
|
||||
""" 后序遍历 """
|
||||
if root is None:
|
||||
return
|
||||
|
||||
Reference in New Issue
Block a user