mirror of
https://github.com/krahets/hello-algo.git
synced 2026-04-13 10:09:54 +08:00
build
This commit is contained in:
@@ -194,15 +194,15 @@ 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
|
||||
|
||||
""" 更新结点高度 """
|
||||
def __update_height(self, node: Optional[TreeNode]):
|
||||
""" 更新结点高度 """
|
||||
# 结点高度等于最高子树高度 + 1
|
||||
node.height = max([self.height(node.left), self.height(node.right)]) + 1
|
||||
```
|
||||
@@ -242,7 +242,7 @@ G. M. Adelson-Velsky 和 E. M. Landis 在其 1962 年发表的论文 "An algorit
|
||||
}
|
||||
|
||||
/* 更新结点高度 */
|
||||
updateHeight(node) {
|
||||
#updateHeight(node) {
|
||||
// 结点高度等于最高子树高度 + 1
|
||||
node.height = Math.max(this.height(node.left), this.height(node.right)) + 1;
|
||||
}
|
||||
@@ -354,8 +354,8 @@ 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
|
||||
@@ -517,8 +517,8 @@ 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 向右旋转
|
||||
@@ -553,15 +553,15 @@ AVL 树的独特之处在于「旋转 Rotation」的操作,其可 **在不影
|
||||
|
||||
```javascript title="avl_tree.js"
|
||||
/* 右旋操作 */
|
||||
rightRotate(node) {
|
||||
#rightRotate(node) {
|
||||
const child = node.left;
|
||||
const grandChild = child.right;
|
||||
// 以 child 为原点,将 node 向右旋转
|
||||
child.right = node;
|
||||
node.left = grandChild;
|
||||
// 更新结点高度
|
||||
this.updateHeight(node);
|
||||
this.updateHeight(child);
|
||||
this.#updateHeight(node);
|
||||
this.#updateHeight(child);
|
||||
// 返回旋转后子树的根结点
|
||||
return child;
|
||||
}
|
||||
@@ -701,8 +701,8 @@ 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 向左旋转
|
||||
@@ -737,15 +737,15 @@ AVL 树的独特之处在于「旋转 Rotation」的操作,其可 **在不影
|
||||
|
||||
```javascript title="avl_tree.js"
|
||||
/* 左旋操作 */
|
||||
leftRotate(node) {
|
||||
#leftRotate(node) {
|
||||
const child = node.right;
|
||||
const grandChild = child.left;
|
||||
// 以 child 为原点,将 node 向左旋转
|
||||
child.left = node;
|
||||
node.right = grandChild;
|
||||
// 更新结点高度
|
||||
this.updateHeight(node);
|
||||
this.updateHeight(child);
|
||||
this.#updateHeight(node);
|
||||
this.#updateHeight(child);
|
||||
// 返回旋转后子树的根结点
|
||||
return child;
|
||||
}
|
||||
@@ -940,8 +940,8 @@ AVL 树的独特之处在于「旋转 Rotation」的操作,其可 **在不影
|
||||
=== "Python"
|
||||
|
||||
```python title="avl_tree.py"
|
||||
""" 执行旋转操作,使该子树重新恢复平衡 """
|
||||
def __rotate(self, node: Optional[TreeNode]) -> TreeNode:
|
||||
""" 执行旋转操作,使该子树重新恢复平衡 """
|
||||
# 获取结点 node 的平衡因子
|
||||
balance_factor = self.balance_factor(node)
|
||||
# 左偏树
|
||||
@@ -1005,29 +1005,29 @@ AVL 树的独特之处在于「旋转 Rotation」的操作,其可 **在不影
|
||||
|
||||
```javascript title="avl_tree.js"
|
||||
/* 执行旋转操作,使该子树重新恢复平衡 */
|
||||
rotate(node) {
|
||||
#rotate(node) {
|
||||
// 获取结点 node 的平衡因子
|
||||
const balanceFactor = this.balanceFactor(node);
|
||||
// 左偏树
|
||||
if (balanceFactor > 1) {
|
||||
if (this.balanceFactor(node.left) >= 0) {
|
||||
// 右旋
|
||||
return this.rightRotate(node);
|
||||
return this.#rightRotate(node);
|
||||
} else {
|
||||
// 先左旋后右旋
|
||||
node.left = this.leftRotate(node.left);
|
||||
return this.rightRotate(node);
|
||||
node.left = this.#leftRotate(node.left);
|
||||
return this.#rightRotate(node);
|
||||
}
|
||||
}
|
||||
// 右偏树
|
||||
if (balanceFactor < -1) {
|
||||
if (this.balanceFactor(node.right) <= 0) {
|
||||
// 左旋
|
||||
return this.leftRotate(node);
|
||||
return this.#leftRotate(node);
|
||||
} else {
|
||||
// 先右旋后左旋
|
||||
node.right = this.rightRotate(node.right);
|
||||
return this.leftRotate(node);
|
||||
node.right = this.#rightRotate(node.right);
|
||||
return this.#leftRotate(node);
|
||||
}
|
||||
}
|
||||
// 平衡树,无需旋转,直接返回
|
||||
@@ -1249,13 +1249,13 @@ AVL 树的独特之处在于「旋转 Rotation」的操作,其可 **在不影
|
||||
=== "Python"
|
||||
|
||||
```python title="avl_tree.py"
|
||||
""" 插入结点 """
|
||||
def insert(self, val) -> TreeNode:
|
||||
""" 插入结点 """
|
||||
self.root = self.__insert_helper(self.root, val)
|
||||
return self.root
|
||||
|
||||
""" 递归插入结点(辅助方法)"""
|
||||
def __insert_helper(self, node: Optional[TreeNode], val: int) -> TreeNode:
|
||||
""" 递归插入结点(辅助方法)"""
|
||||
if node is None:
|
||||
return TreeNode(val)
|
||||
# 1. 查找插入位置,并插入结点
|
||||
@@ -1309,20 +1309,20 @@ AVL 树的独特之处在于「旋转 Rotation」的操作,其可 **在不影
|
||||
```javascript title="avl_tree.js"
|
||||
/* 插入结点 */
|
||||
insert(val) {
|
||||
this.root = this.insertHelper(this.root, val);
|
||||
this.root = this.#insertHelper(this.root, val);
|
||||
return this.root;
|
||||
}
|
||||
|
||||
/* 递归插入结点(辅助方法) */
|
||||
insertHelper(node, val) {
|
||||
#insertHelper(node, val) {
|
||||
if (node === null) return new TreeNode(val);
|
||||
/* 1. 查找插入位置,并插入结点 */
|
||||
if (val < node.val) node.left = this.insertHelper(node.left, val);
|
||||
else if (val > node.val) node.right = this.insertHelper(node.right, val);
|
||||
if (val < node.val) node.left = this.#insertHelper(node.left, val);
|
||||
else if (val > node.val) node.right = this.#insertHelper(node.right, val);
|
||||
else return node; // 重复结点不插入,直接返回
|
||||
this.updateHeight(node); // 更新结点高度
|
||||
this.#updateHeight(node); // 更新结点高度
|
||||
/* 2. 执行旋转操作,使该子树重新恢复平衡 */
|
||||
node = this.rotate(node);
|
||||
node = this.#rotate(node);
|
||||
// 返回子树的根结点
|
||||
return node;
|
||||
}
|
||||
@@ -1571,13 +1571,13 @@ AVL 树的独特之处在于「旋转 Rotation」的操作,其可 **在不影
|
||||
=== "Python"
|
||||
|
||||
```python title="avl_tree.py"
|
||||
""" 删除结点 """
|
||||
def remove(self, val: int):
|
||||
""" 删除结点 """
|
||||
root = self.__remove_helper(self.root, val)
|
||||
return root
|
||||
|
||||
""" 递归删除结点(辅助方法) """
|
||||
def __remove_helper(self, node: Optional[TreeNode], val: int) -> Optional[TreeNode]:
|
||||
""" 递归删除结点(辅助方法) """
|
||||
if node is None:
|
||||
return None
|
||||
# 1. 查找结点,并删除之
|
||||
@@ -1603,8 +1603,8 @@ AVL 树的独特之处在于「旋转 Rotation」的操作,其可 **在不影
|
||||
# 2. 执行旋转操作,使该子树重新恢复平衡
|
||||
return self.__rotate(node)
|
||||
|
||||
""" 获取中序遍历中的下一个结点(仅适用于 root 有左子结点的情况) """
|
||||
def __get_inorder_next(self, node: Optional[TreeNode]) -> Optional[TreeNode]:
|
||||
""" 获取中序遍历中的下一个结点(仅适用于 root 有左子结点的情况) """
|
||||
if node is None:
|
||||
return None
|
||||
# 循环访问左子结点,直到叶结点时为最小结点,跳出
|
||||
@@ -1678,16 +1678,16 @@ AVL 树的独特之处在于「旋转 Rotation」的操作,其可 **在不影
|
||||
```javascript title="avl_tree.js"
|
||||
/* 删除结点 */
|
||||
remove(val) {
|
||||
this.root = this.removeHelper(this.root, val);
|
||||
this.root = this.#removeHelper(this.root, val);
|
||||
return this.root;
|
||||
}
|
||||
|
||||
/* 递归删除结点(辅助方法) */
|
||||
removeHelper(node, val) {
|
||||
#removeHelper(node, val) {
|
||||
if (node === null) return null;
|
||||
/* 1. 查找结点,并删除之 */
|
||||
if (val < node.val) node.left = this.removeHelper(node.left, val);
|
||||
else if (val > node.val) node.right = this.removeHelper(node.right, val);
|
||||
if (val < node.val) node.left = this.#removeHelper(node.left, val);
|
||||
else if (val > node.val) node.right = this.#removeHelper(node.right, val);
|
||||
else {
|
||||
if (node.left === null || node.right === null) {
|
||||
const child = node.left !== null ? node.left : node.right;
|
||||
@@ -1697,20 +1697,20 @@ AVL 树的独特之处在于「旋转 Rotation」的操作,其可 **在不影
|
||||
else node = child;
|
||||
} else {
|
||||
// 子结点数量 = 2 ,则将中序遍历的下个结点删除,并用该结点替换当前结点
|
||||
const temp = this.getInOrderNext(node.right);
|
||||
node.right = this.removeHelper(node.right, temp.val);
|
||||
const temp = this.#getInOrderNext(node.right);
|
||||
node.right = this.#removeHelper(node.right, temp.val);
|
||||
node.val = temp.val;
|
||||
}
|
||||
}
|
||||
this.updateHeight(node); // 更新结点高度
|
||||
this.#updateHeight(node); // 更新结点高度
|
||||
/* 2. 执行旋转操作,使该子树重新恢复平衡 */
|
||||
node = this.rotate(node);
|
||||
node = this.#rotate(node);
|
||||
// 返回子树的根结点
|
||||
return node;
|
||||
}
|
||||
|
||||
/* 获取中序遍历中的下一个结点(仅适用于 root 有左子结点的情况) */
|
||||
getInOrderNext(node) {
|
||||
#getInOrderNext(node) {
|
||||
if (node === null) return node;
|
||||
// 循环访问左子结点,直到叶结点时为最小结点,跳出
|
||||
while (node.left !== null) {
|
||||
@@ -1745,7 +1745,7 @@ AVL 树的独特之处在于「旋转 Rotation」的操作,其可 **在不影
|
||||
return null;
|
||||
} else {
|
||||
// 子结点数量 = 1 ,直接删除 node
|
||||
node = child;
|
||||
node = child;
|
||||
}
|
||||
} else {
|
||||
// 子结点数量 = 2 ,则将中序遍历的下个结点删除,并用该结点替换当前结点
|
||||
|
||||
@@ -80,8 +80,8 @@ comments: true
|
||||
=== "Python"
|
||||
|
||||
```python title="binary_search_tree.py"
|
||||
""" 查找结点 """
|
||||
def search(self, num: int) -> Optional[TreeNode]:
|
||||
""" 查找结点 """
|
||||
cur = self.root
|
||||
# 循环查找,越过叶结点后跳出
|
||||
while cur is not None:
|
||||
@@ -308,8 +308,8 @@ comments: true
|
||||
=== "Python"
|
||||
|
||||
```python title="binary_search_tree.py"
|
||||
""" 插入结点 """
|
||||
def insert(self, num: int) -> Optional[TreeNode]:
|
||||
""" 插入结点 """
|
||||
root = self.root
|
||||
# 若树为空,直接提前返回
|
||||
if root is None:
|
||||
@@ -392,7 +392,7 @@ comments: true
|
||||
else cur = cur.left;
|
||||
}
|
||||
// 插入结点 val
|
||||
let node = new Tree.TreeNode(num);
|
||||
let node = new TreeNode(num);
|
||||
if (pre.val < num) pre.right = node;
|
||||
else pre.left = node;
|
||||
return node;
|
||||
@@ -692,8 +692,8 @@ comments: true
|
||||
=== "Python"
|
||||
|
||||
```python title="binary_search_tree.py"
|
||||
""" 删除结点 """
|
||||
def remove(self, num: int) -> Optional[TreeNode]:
|
||||
""" 删除结点 """
|
||||
root = self.root
|
||||
# 若树为空,直接提前返回
|
||||
if root is None:
|
||||
@@ -734,8 +734,8 @@ comments: true
|
||||
cur.val = tmp
|
||||
return cur
|
||||
|
||||
""" 获取中序遍历中的下一个结点(仅适用于 root 有左子结点的情况) """
|
||||
def get_inorder_next(self, root: Optional[TreeNode]) -> Optional[TreeNode]:
|
||||
""" 获取中序遍历中的下一个结点(仅适用于 root 有左子结点的情况) """
|
||||
if root is None:
|
||||
return root
|
||||
# 循环访问左子结点,直到叶结点时为最小结点,跳出
|
||||
|
||||
@@ -69,8 +69,8 @@ comments: true
|
||||
=== "Python"
|
||||
|
||||
```python title="binary_tree_bfs.py"
|
||||
""" 层序遍历 """
|
||||
def level_order(root: Optional[TreeNode]):
|
||||
""" 层序遍历 """
|
||||
# 初始化队列,加入根结点
|
||||
queue = collections.deque()
|
||||
queue.append(root)
|
||||
@@ -120,9 +120,9 @@ comments: true
|
||||
/* 层序遍历 */
|
||||
function levelOrder(root) {
|
||||
// 初始化队列,加入根结点
|
||||
let queue = [root];
|
||||
const queue = [root];
|
||||
// 初始化一个列表,用于保存遍历序列
|
||||
let list = [];
|
||||
const list = [];
|
||||
while (queue.length) {
|
||||
let node = queue.shift(); // 队列出队
|
||||
list.push(node.val); // 保存结点值
|
||||
@@ -337,8 +337,8 @@ comments: true
|
||||
=== "Python"
|
||||
|
||||
```python title="binary_tree_dfs.py"
|
||||
""" 前序遍历 """
|
||||
def pre_order(root: Optional[TreeNode]):
|
||||
""" 前序遍历 """
|
||||
if root is None:
|
||||
return
|
||||
# 访问优先级:根结点 -> 左子树 -> 右子树
|
||||
@@ -346,8 +346,8 @@ comments: true
|
||||
pre_order(root=root.left)
|
||||
pre_order(root=root.right)
|
||||
|
||||
""" 中序遍历 """
|
||||
def in_order(root: Optional[TreeNode]):
|
||||
""" 中序遍历 """
|
||||
if root is None:
|
||||
return
|
||||
# 访问优先级:左子树 -> 根结点 -> 右子树
|
||||
@@ -355,8 +355,8 @@ comments: true
|
||||
res.append(root.val)
|
||||
in_order(root=root.right)
|
||||
|
||||
""" 后序遍历 """
|
||||
def post_order(root: Optional[TreeNode]):
|
||||
""" 后序遍历 """
|
||||
if root is None:
|
||||
return
|
||||
# 访问优先级:左子树 -> 右子树 -> 根结点
|
||||
|
||||
Reference in New Issue
Block a user