mirror of
https://github.com/krahets/hello-algo.git
synced 2026-04-24 10:33:34 +08:00
build
This commit is contained in:
@@ -35,9 +35,9 @@ G. M. Adelson-Velsky 和 E. M. Landis 在其 1962 年发表的论文 "An algorit
|
||||
```java title=""
|
||||
/* AVL 树结点类 */
|
||||
class TreeNode {
|
||||
public int val; // 结点值
|
||||
public int height; // 结点高度
|
||||
public TreeNode left; // 左子结点
|
||||
public int val; // 结点值
|
||||
public int height; // 结点高度
|
||||
public TreeNode left; // 左子结点
|
||||
public TreeNode right; // 右子结点
|
||||
public TreeNode(int x) { val = x; }
|
||||
}
|
||||
@@ -48,10 +48,10 @@ G. M. Adelson-Velsky 和 E. M. Landis 在其 1962 年发表的论文 "An algorit
|
||||
```cpp title=""
|
||||
/* AVL 树结点类 */
|
||||
struct TreeNode {
|
||||
int val{}; // 结点值
|
||||
int height = 0; // 结点高度
|
||||
TreeNode *left{}; // 左子结点
|
||||
TreeNode *right{}; // 右子结点
|
||||
int val{}; // 结点值
|
||||
int height = 0; // 结点高度
|
||||
TreeNode *left{}; // 左子结点
|
||||
TreeNode *right{}; // 右子结点
|
||||
TreeNode() = default;
|
||||
explicit TreeNode(int x) : val(x){}
|
||||
};
|
||||
@@ -62,11 +62,11 @@ G. M. Adelson-Velsky 和 E. M. Landis 在其 1962 年发表的论文 "An algorit
|
||||
```python title=""
|
||||
""" AVL 树结点类 """
|
||||
class TreeNode:
|
||||
def __init__(self, val=None, left=None, right=None):
|
||||
self.val = val # 结点值
|
||||
self.height = 0 # 结点高度
|
||||
self.left = left # 左子结点引用
|
||||
self.right = right # 右子结点引用
|
||||
def __init__(self, val: int):
|
||||
self.val: int = val # 结点值
|
||||
self.height: int = 0 # 结点高度
|
||||
self.left: Optional[TreeNode] = None # 左子结点引用
|
||||
self.right: Optional[TreeNode] = None # 右子结点引用
|
||||
```
|
||||
|
||||
=== "Go"
|
||||
@@ -517,7 +517,7 @@ AVL 树的独特之处在于「旋转 Rotation」的操作,其可 **在不影
|
||||
=== "Python"
|
||||
|
||||
```python title="avl_tree.py"
|
||||
def __right_rotate(self, node: Optional[TreeNode]) -> TreeNode:
|
||||
def __right_rotate(self, node: Optional[TreeNode]) -> Optional[TreeNode]:
|
||||
""" 右旋操作 """
|
||||
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]) -> TreeNode:
|
||||
def __left_rotate(self, node: Optional[TreeNode]) -> Optional[TreeNode]:
|
||||
""" 左旋操作 """
|
||||
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]) -> TreeNode:
|
||||
def __rotate(self, node: Optional[TreeNode]) -> Optional[TreeNode]:
|
||||
""" 执行旋转操作,使该子树重新恢复平衡 """
|
||||
# 获取结点 node 的平衡因子
|
||||
balance_factor = self.balance_factor(node)
|
||||
@@ -1251,8 +1251,8 @@ AVL 树的独特之处在于「旋转 Rotation」的操作,其可 **在不影
|
||||
```python title="avl_tree.py"
|
||||
def insert(self, val) -> TreeNode:
|
||||
""" 插入结点 """
|
||||
self.root = self.__insert_helper(self.root, val)
|
||||
return self.root
|
||||
self.__root = self.__insert_helper(self.__root, val)
|
||||
return self.__root
|
||||
|
||||
def __insert_helper(self, node: Optional[TreeNode], val: int) -> TreeNode:
|
||||
""" 递归插入结点(辅助方法)"""
|
||||
@@ -1571,10 +1571,10 @@ AVL 树的独特之处在于「旋转 Rotation」的操作,其可 **在不影
|
||||
=== "Python"
|
||||
|
||||
```python title="avl_tree.py"
|
||||
def remove(self, val: int):
|
||||
def remove(self, val: int) -> Optional[TreeNode]:
|
||||
""" 删除结点 """
|
||||
root = self.__remove_helper(self.root, val)
|
||||
return root
|
||||
self.__root = self.__remove_helper(self.__root, val)
|
||||
return self.__root
|
||||
|
||||
def __remove_helper(self, node: Optional[TreeNode], val: int) -> Optional[TreeNode]:
|
||||
""" 递归删除结点(辅助方法) """
|
||||
|
||||
@@ -82,7 +82,7 @@ comments: true
|
||||
```python title="binary_search_tree.py"
|
||||
def search(self, num: int) -> Optional[TreeNode]:
|
||||
""" 查找结点 """
|
||||
cur = self.root
|
||||
cur: Optional[TreeNode] = self.__root
|
||||
# 循环查找,越过叶结点后跳出
|
||||
while cur is not None:
|
||||
# 目标结点在 cur 的右子树中
|
||||
@@ -310,13 +310,12 @@ comments: true
|
||||
```python title="binary_search_tree.py"
|
||||
def insert(self, num: int) -> Optional[TreeNode]:
|
||||
""" 插入结点 """
|
||||
root = self.root
|
||||
# 若树为空,直接提前返回
|
||||
if root is None:
|
||||
if self.__root is None:
|
||||
return None
|
||||
|
||||
# 循环查找,越过叶结点后跳出
|
||||
cur, pre = root, None
|
||||
cur, pre = self.__root, None
|
||||
while cur is not None:
|
||||
# 找到重复结点,直接返回
|
||||
if cur.val == num:
|
||||
@@ -694,13 +693,12 @@ comments: true
|
||||
```python title="binary_search_tree.py"
|
||||
def remove(self, num: int) -> Optional[TreeNode]:
|
||||
""" 删除结点 """
|
||||
root = self.root
|
||||
# 若树为空,直接提前返回
|
||||
if root is None:
|
||||
if self.__root is None:
|
||||
return None
|
||||
|
||||
# 循环查找,越过叶结点后跳出
|
||||
cur, pre = root, None
|
||||
cur, pre = self.__root, None
|
||||
while cur is not None:
|
||||
# 找到待删除结点,跳出循环
|
||||
if cur.val == num:
|
||||
@@ -726,8 +724,8 @@ comments: true
|
||||
# 子结点数量 = 2
|
||||
else:
|
||||
# 获取中序遍历中 cur 的下一个结点
|
||||
nex = self.get_inorder_next(cur.right)
|
||||
tmp = nex.val
|
||||
nex: TreeNode = self.get_inorder_next(cur.right)
|
||||
tmp: int = nex.val
|
||||
# 递归删除结点 nex
|
||||
self.remove(nex.val)
|
||||
# 将 nex 的值复制给 cur
|
||||
|
||||
@@ -9,7 +9,7 @@ comments: true
|
||||
=== "Java"
|
||||
|
||||
```java title=""
|
||||
/* 链表结点类 */
|
||||
/* 二叉树结点类 */
|
||||
class TreeNode {
|
||||
int val; // 结点值
|
||||
TreeNode left; // 左子结点指针
|
||||
@@ -21,7 +21,7 @@ comments: true
|
||||
=== "C++"
|
||||
|
||||
```cpp title=""
|
||||
/* 链表结点结构体 */
|
||||
/* 二叉树结点结构体 */
|
||||
struct TreeNode {
|
||||
int val; // 结点值
|
||||
TreeNode *left; // 左子结点指针
|
||||
@@ -33,18 +33,18 @@ comments: true
|
||||
=== "Python"
|
||||
|
||||
```python title=""
|
||||
""" 链表结点类 """
|
||||
""" 二叉树结点类 """
|
||||
class TreeNode:
|
||||
def __init__(self, val=None, left=None, right=None):
|
||||
self.val = val # 结点值
|
||||
self.left = left # 左子结点指针
|
||||
self.right = right # 右子结点指针
|
||||
def __init__(self, val: int):
|
||||
self.val: int = val # 结点值
|
||||
self.left: Optional[TreeNode] = None # 左子结点指针
|
||||
self.right: Optional[TreeNode] = None # 右子结点指针
|
||||
```
|
||||
|
||||
=== "Go"
|
||||
|
||||
```go title=""
|
||||
/* 链表结点结构体 */
|
||||
/* 二叉树结点结构体 */
|
||||
type TreeNode struct {
|
||||
Val int
|
||||
Left *TreeNode
|
||||
@@ -63,7 +63,7 @@ comments: true
|
||||
=== "JavaScript"
|
||||
|
||||
```javascript title=""
|
||||
/* 链表结点类 */
|
||||
/* 二叉树结点类 */
|
||||
function TreeNode(val, left, right) {
|
||||
this.val = (val === undefined ? 0 : val); // 结点值
|
||||
this.left = (left === undefined ? null : left); // 左子结点指针
|
||||
@@ -74,7 +74,7 @@ comments: true
|
||||
=== "TypeScript"
|
||||
|
||||
```typescript title=""
|
||||
/* 链表结点类 */
|
||||
/* 二叉树结点类 */
|
||||
class TreeNode {
|
||||
val: number;
|
||||
left: TreeNode | null;
|
||||
@@ -97,7 +97,7 @@ comments: true
|
||||
=== "C#"
|
||||
|
||||
```csharp title=""
|
||||
/* 链表结点类 */
|
||||
/* 二叉树结点类 */
|
||||
class TreeNode {
|
||||
int val; // 结点值
|
||||
TreeNode? left; // 左子结点指针
|
||||
@@ -109,7 +109,7 @@ comments: true
|
||||
=== "Swift"
|
||||
|
||||
```swift title=""
|
||||
/* 链表结点类 */
|
||||
/* 二叉树结点类 */
|
||||
class TreeNode {
|
||||
var val: Int // 结点值
|
||||
var left: TreeNode? // 左子结点指针
|
||||
|
||||
@@ -69,20 +69,20 @@ comments: true
|
||||
=== "Python"
|
||||
|
||||
```python title="binary_tree_bfs.py"
|
||||
def level_order(root: Optional[TreeNode]):
|
||||
def level_order(root: Optional[TreeNode]) -> List[int]:
|
||||
""" 层序遍历 """
|
||||
# 初始化队列,加入根结点
|
||||
queue = collections.deque()
|
||||
queue: Deque[TreeNode] = collections.deque()
|
||||
queue.append(root)
|
||||
# 初始化一个列表,用于保存遍历序列
|
||||
res = []
|
||||
res: List[int] = []
|
||||
while queue:
|
||||
node = queue.popleft() # 队列出队
|
||||
res.append(node.val) # 保存结点值
|
||||
node: TreeNode = queue.popleft() # 队列出队
|
||||
res.append(node.val) # 保存结点值
|
||||
if node.left is not None:
|
||||
queue.append(node.left) # 左子结点入队
|
||||
queue.append(node.left) # 左子结点入队
|
||||
if node.right is not None:
|
||||
queue.append(node.right) # 右子结点入队
|
||||
queue.append(node.right) # 右子结点入队
|
||||
return res
|
||||
```
|
||||
|
||||
@@ -337,7 +337,7 @@ comments: true
|
||||
=== "Python"
|
||||
|
||||
```python title="binary_tree_dfs.py"
|
||||
def pre_order(root: Optional[TreeNode]):
|
||||
def pre_order(root: Optional[TreeNode]) -> 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]):
|
||||
def in_order(root: Optional[TreeNode]) -> 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]):
|
||||
def post_order(root: Optional[TreeNode]) -> None:
|
||||
""" 后序遍历 """
|
||||
if root is None:
|
||||
return
|
||||
|
||||
Reference in New Issue
Block a user