mirror of
https://github.com/krahets/hello-algo.git
synced 2026-04-05 03:30:30 +08:00
1. Add the building util of Python
for the markdown docs. 2. Update the deploy.sh
This commit is contained in:
125
docs/chapter_tree/avl_tree.md
Normal file → Executable file
125
docs/chapter_tree/avl_tree.md
Normal file → Executable file
@@ -179,17 +179,9 @@ G. M. Adelson-Velsky 和 E. M. Landis 在其 1962 年发表的论文 "An algorit
|
||||
=== "Python"
|
||||
|
||||
```python title="avl_tree.py"
|
||||
""" 获取结点高度 """
|
||||
def height(self, node: Optional[TreeNode]) -> int:
|
||||
# 空结点高度为 -1 ,叶结点高度为 0
|
||||
if node is not None:
|
||||
return node.height
|
||||
return -1
|
||||
[class]{AVLTree}-[func]{height}
|
||||
|
||||
""" 更新结点高度 """
|
||||
def __update_height(self, node: Optional[TreeNode]):
|
||||
# 结点高度等于最高子树高度 + 1
|
||||
node.height = max([self.height(node.left), self.height(node.right)]) + 1
|
||||
[class]{AVLTree}-[func]{__update_height}
|
||||
```
|
||||
|
||||
=== "Go"
|
||||
@@ -316,13 +308,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:
|
||||
# 空结点平衡因子为 0
|
||||
if node is None:
|
||||
return 0
|
||||
# 结点平衡因子 = 左子树高度 - 右子树高度
|
||||
return self.height(node.left) - self.height(node.right)
|
||||
[class]{AVLTree}-[func]{balance_factor}
|
||||
```
|
||||
|
||||
=== "Go"
|
||||
@@ -467,18 +453,7 @@ AVL 树的独特之处在于「旋转 Rotation」的操作,其可 **在不影
|
||||
=== "Python"
|
||||
|
||||
```python title="avl_tree.py"
|
||||
""" 右旋操作 """
|
||||
def __right_rotate(self, node: Optional[TreeNode]) -> TreeNode:
|
||||
child = node.left
|
||||
grand_child = child.right
|
||||
# 以 child 为原点,将 node 向右旋转
|
||||
child.right = node
|
||||
node.left = grand_child
|
||||
# 更新结点高度
|
||||
self.__update_height(node)
|
||||
self.__update_height(child)
|
||||
# 返回旋转后子树的根节点
|
||||
return child
|
||||
[class]{AVLTree}-[func]{__right_rotate}
|
||||
```
|
||||
|
||||
=== "Go"
|
||||
@@ -623,18 +598,7 @@ AVL 树的独特之处在于「旋转 Rotation」的操作,其可 **在不影
|
||||
=== "Python"
|
||||
|
||||
```python title="avl_tree.py"
|
||||
""" 左旋操作 """
|
||||
def __left_rotate(self, node: Optional[TreeNode]) -> TreeNode:
|
||||
child = node.right
|
||||
grand_child = child.left
|
||||
# 以 child 为原点,将 node 向左旋转
|
||||
child.left = node
|
||||
node.right = grand_child
|
||||
# 更新结点高度
|
||||
self.__update_height(node)
|
||||
self.__update_height(child)
|
||||
# 返回旋转后子树的根节点
|
||||
return child
|
||||
[class]{AVLTree}-[func]{__left_rotate}
|
||||
```
|
||||
|
||||
=== "Go"
|
||||
@@ -835,30 +799,7 @@ AVL 树的独特之处在于「旋转 Rotation」的操作,其可 **在不影
|
||||
=== "Python"
|
||||
|
||||
```python title="avl_tree.py"
|
||||
""" 执行旋转操作,使该子树重新恢复平衡 """
|
||||
def __rotate(self, node: Optional[TreeNode]) -> TreeNode:
|
||||
# 获取结点 node 的平衡因子
|
||||
balance_factor = self.balance_factor(node)
|
||||
# 左偏树
|
||||
if balance_factor > 1:
|
||||
if self.balance_factor(node.left) >= 0:
|
||||
# 右旋
|
||||
return self.__right_rotate(node)
|
||||
else:
|
||||
# 先左旋后右旋
|
||||
node.left = self.__left_rotate(node.left)
|
||||
return self.__right_rotate(node)
|
||||
# 右偏树
|
||||
elif balance_factor < -1:
|
||||
if self.balance_factor(node.right) <= 0:
|
||||
# 左旋
|
||||
return self.__left_rotate(node)
|
||||
else:
|
||||
# 先右旋后左旋
|
||||
node.right = self.__right_rotate(node.right)
|
||||
return self.__left_rotate(node)
|
||||
# 平衡树,无需旋转,直接返回
|
||||
return node
|
||||
[class]{AVLTree}-[func]{__rotate}
|
||||
```
|
||||
|
||||
=== "Go"
|
||||
@@ -1088,27 +1029,9 @@ AVL 树的独特之处在于「旋转 Rotation」的操作,其可 **在不影
|
||||
=== "Python"
|
||||
|
||||
```python title="avl_tree.py"
|
||||
""" 插入结点 """
|
||||
def insert(self, val) -> TreeNode:
|
||||
self.root = self.__insert_helper(self.root, val)
|
||||
return self.root
|
||||
[class]{AVLTree}-[func]{insert}
|
||||
|
||||
""" 递归插入结点(辅助函数)"""
|
||||
def __insert_helper(self, node: Optional[TreeNode], val: int) -> TreeNode:
|
||||
if node is None:
|
||||
return TreeNode(val)
|
||||
# 1. 查找插入位置,并插入结点
|
||||
if val < node.val:
|
||||
node.left = self.__insert_helper(node.left, val)
|
||||
elif val > node.val:
|
||||
node.right = self.__insert_helper(node.right, val)
|
||||
else:
|
||||
# 重复结点不插入,直接返回
|
||||
return node
|
||||
# 更新结点高度
|
||||
self.__update_height(node)
|
||||
# 2. 执行旋转操作,使该子树重新恢复平衡
|
||||
return self.__rotate(node)
|
||||
[class]{AVLTree}-[func]{__insert_helper}
|
||||
```
|
||||
|
||||
=== "Go"
|
||||
@@ -1340,37 +1263,9 @@ AVL 树的独特之处在于「旋转 Rotation」的操作,其可 **在不影
|
||||
=== "Python"
|
||||
|
||||
```python title="avl_tree.py"
|
||||
""" 删除结点 """
|
||||
def remove(self, val: int):
|
||||
root = self.__remove_helper(self.root, val)
|
||||
return root
|
||||
[class]{AVLTree}-[func]{remove}
|
||||
|
||||
""" 递归删除结点(辅助函数) """
|
||||
def __remove_helper(self, node: Optional[TreeNode], val: int) -> Optional[TreeNode]:
|
||||
if node is None:
|
||||
return None
|
||||
# 1. 查找结点,并删除之
|
||||
if val < node.val:
|
||||
node.left = self.__remove_helper(node.left, val)
|
||||
elif val > node.val:
|
||||
node.right = self.__remove_helper(node.right, val)
|
||||
else:
|
||||
if node.left is None or node.right is None:
|
||||
child = node.left or node.right
|
||||
# 子结点数量 = 0 ,直接删除 node 并返回
|
||||
if child is None:
|
||||
return None
|
||||
# 子结点数量 = 1 ,直接删除 node
|
||||
else:
|
||||
node = child
|
||||
else: # 子结点数量 = 2 ,则将中序遍历的下个结点删除,并用该结点替换当前结点
|
||||
temp = self.__get_inorder_next(node.right)
|
||||
node.right = self.__remove_helper(node.right, temp.val)
|
||||
node.val = temp.val
|
||||
# 更新结点高度
|
||||
self.__update_height(node)
|
||||
# 2. 执行旋转操作,使该子树重新恢复平衡
|
||||
return self.__rotate(node)
|
||||
[class]{AVLTree}-[func]{__remove_helper}
|
||||
```
|
||||
|
||||
=== "Go"
|
||||
|
||||
101
docs/chapter_tree/binary_search_tree.md
Normal file → Executable file
101
docs/chapter_tree/binary_search_tree.md
Normal file → Executable file
@@ -78,21 +78,7 @@ comments: true
|
||||
=== "Python"
|
||||
|
||||
```python title="binary_search_tree.py"
|
||||
""" 查找结点 """
|
||||
def search(self, num: int) -> Optional[TreeNode]:
|
||||
cur = self.root
|
||||
# 循环查找,越过叶结点后跳出
|
||||
while cur is not None:
|
||||
# 目标结点在 cur 的右子树中
|
||||
if cur.val < num:
|
||||
cur = cur.right
|
||||
# 目标结点在 cur 的左子树中
|
||||
elif cur.val > num:
|
||||
cur = cur.left
|
||||
# 找到目标结点,跳出循环
|
||||
else:
|
||||
break
|
||||
return cur
|
||||
[class]{BinarySearchTree}-[func]{search}
|
||||
```
|
||||
|
||||
=== "Go"
|
||||
@@ -286,36 +272,7 @@ comments: true
|
||||
=== "Python"
|
||||
|
||||
```python title="binary_search_tree.py"
|
||||
""" 插入结点 """
|
||||
def insert(self, num: int) -> Optional[TreeNode]:
|
||||
root = self.root
|
||||
# 若树为空,直接提前返回
|
||||
if root is None:
|
||||
return None
|
||||
|
||||
cur = root
|
||||
pre = None
|
||||
|
||||
# 循环查找,越过叶结点后跳出
|
||||
while cur is not None:
|
||||
# 找到重复结点,直接返回
|
||||
if cur.val == num:
|
||||
return None
|
||||
pre = cur
|
||||
# 插入位置在 cur 的右子树中
|
||||
if cur.val < num:
|
||||
cur = cur.right
|
||||
# 插入位置在 cur 的左子树中
|
||||
else:
|
||||
cur = cur.left
|
||||
|
||||
# 插入结点 val
|
||||
node = TreeNode(num)
|
||||
if pre.val < num:
|
||||
pre.right = node
|
||||
else:
|
||||
pre.left = node
|
||||
return node
|
||||
[class]{BinarySearchTree}-[func]{insert}
|
||||
```
|
||||
|
||||
=== "Go"
|
||||
@@ -640,59 +597,9 @@ comments: true
|
||||
=== "Python"
|
||||
|
||||
```python title="binary_search_tree.py"
|
||||
""" 删除结点 """
|
||||
def remove(self, num: int) -> Optional[TreeNode]:
|
||||
root = self.root
|
||||
# 若树为空,直接提前返回
|
||||
if root is None:
|
||||
return None
|
||||
|
||||
cur = root
|
||||
pre = None
|
||||
|
||||
# 循环查找,越过叶结点后跳出
|
||||
while cur is not None:
|
||||
# 找到待删除结点,跳出循环
|
||||
if cur.val == num:
|
||||
break
|
||||
pre = cur
|
||||
if cur.val < num: # 待删除结点在 cur 的右子树中
|
||||
cur = cur.right
|
||||
else: # 待删除结点在 cur 的左子树中
|
||||
cur = cur.left
|
||||
|
||||
# 若无待删除结点,则直接返回
|
||||
if cur is None:
|
||||
return None
|
||||
|
||||
# 子结点数量 = 0 or 1
|
||||
if cur.left is None or cur.right is None:
|
||||
# 当子结点数量 = 0 / 1 时, child = null / 该子结点
|
||||
child = cur.left or cur.right
|
||||
# 删除结点 cur
|
||||
if pre.left == cur:
|
||||
pre.left = child
|
||||
else:
|
||||
pre.right = child
|
||||
# 子结点数量 = 2
|
||||
else:
|
||||
# 获取中序遍历中 cur 的下一个结点
|
||||
nex = self.get_inorder_next(cur.right)
|
||||
tmp = nex.val
|
||||
# 递归删除结点 nex
|
||||
self.remove(nex.val)
|
||||
# 将 nex 的值复制给 cur
|
||||
cur.val = tmp
|
||||
return cur
|
||||
[class]{BinarySearchTree}-[func]{remove}
|
||||
|
||||
""" 获取中序遍历中的下一个结点(仅适用于 root 有左子结点的情况) """
|
||||
def get_inorder_next(self, root: Optional[TreeNode]) -> Optional[TreeNode]:
|
||||
if root is None:
|
||||
return root
|
||||
# 循环访问左子结点,直到叶结点时为最小结点,跳出
|
||||
while root.left is not None:
|
||||
root = root.left
|
||||
return root
|
||||
[class]{BinarySearchTree}-[func]{get_inorder_next}
|
||||
```
|
||||
|
||||
=== "Go"
|
||||
|
||||
43
docs/chapter_tree/binary_tree_traversal.md
Normal file → Executable file
43
docs/chapter_tree/binary_tree_traversal.md
Normal file → Executable file
@@ -65,21 +65,7 @@ comments: true
|
||||
=== "Python"
|
||||
|
||||
```python title="binary_tree_bfs.py"
|
||||
""" 层序遍历 """
|
||||
def hier_order(root: Optional[TreeNode]):
|
||||
# 初始化队列,加入根结点
|
||||
queue = collections.deque()
|
||||
queue.append(root)
|
||||
# 初始化一个列表,用于保存遍历序列
|
||||
res = []
|
||||
while queue:
|
||||
node = queue.popleft() # 队列出队
|
||||
res.append(node.val) # 保存节点值
|
||||
if node.left is not None:
|
||||
queue.append(node.left) # 左子结点入队
|
||||
if node.right is not None:
|
||||
queue.append(node.right) # 右子结点入队
|
||||
return res
|
||||
[class]{}-[func]{hier_order}
|
||||
```
|
||||
|
||||
=== "Go"
|
||||
@@ -299,32 +285,11 @@ comments: true
|
||||
=== "Python"
|
||||
|
||||
```python title="binary_tree_dfs.py"
|
||||
""" 前序遍历 """
|
||||
def pre_order(root: Optional[TreeNode]):
|
||||
if root is None:
|
||||
return
|
||||
# 访问优先级:根结点 -> 左子树 -> 右子树
|
||||
res.append(root.val)
|
||||
pre_order(root=root.left)
|
||||
pre_order(root=root.right)
|
||||
[class]{}-[func]{pre_order}
|
||||
|
||||
""" 中序遍历 """
|
||||
def in_order(root: Optional[TreeNode]):
|
||||
if root is None:
|
||||
return
|
||||
# 访问优先级:左子树 -> 根结点 -> 右子树
|
||||
in_order(root=root.left)
|
||||
res.append(root.val)
|
||||
in_order(root=root.right)
|
||||
[class]{}-[func]{in_order}
|
||||
|
||||
""" 后序遍历 """
|
||||
def post_order(root: Optional[TreeNode]):
|
||||
if root is None:
|
||||
return
|
||||
# 访问优先级:左子树 -> 右子树 -> 根结点
|
||||
post_order(root=root.left)
|
||||
post_order(root=root.right)
|
||||
res.append(root.val)
|
||||
[class]{}-[func]{post_order}
|
||||
```
|
||||
|
||||
=== "Go"
|
||||
|
||||
Reference in New Issue
Block a user