This commit is contained in:
krahets
2023-02-07 17:17:14 +08:00
parent ecbf2d1560
commit 1ca5c731f7
19 changed files with 501 additions and 192 deletions

View File

@@ -28,7 +28,7 @@ G. M. Adelson-Velsky 和 E. M. Landis 在其 1962 年发表的论文 "An algorit
=== "Java"
```java title="avl_tree.java"
```java title=""
/* AVL 树结点类 */
class TreeNode {
public int val; // 结点值
@@ -41,7 +41,7 @@ G. M. Adelson-Velsky 和 E. M. Landis 在其 1962 年发表的论文 "An algorit
=== "C++"
```cpp title="avl_tree.cpp"
```cpp title=""
/* AVL 树结点类 */
struct TreeNode {
int val{}; // 结点值
@@ -55,7 +55,7 @@ G. M. Adelson-Velsky 和 E. M. Landis 在其 1962 年发表的论文 "An algorit
=== "Python"
```python title="avl_tree.py"
```python title=""
""" AVL 树结点类 """
class TreeNode:
def __init__(self, val=None, left=None, right=None):
@@ -67,7 +67,7 @@ G. M. Adelson-Velsky 和 E. M. Landis 在其 1962 年发表的论文 "An algorit
=== "Go"
```go title="avl_tree.go"
```go title=""
/* AVL 树结点类 */
type TreeNode struct {
Val int // 结点值
@@ -79,36 +79,47 @@ G. M. Adelson-Velsky 和 E. M. Landis 在其 1962 年发表的论文 "An algorit
=== "JavaScript"
```js title="avl_tree.js"
```js title=""
class TreeNode {
val; // 结点值
height; //结点高度
left; // 左子结点指针
right; // 右子结点指针
height; //结点高度
constructor(val, left, right, height) {
this.val = val === undefined ? 0 : val;
this.height = height === undefined ? 0 : height;
this.left = left === undefined ? null : left;
this.right = right === undefined ? null : right;
this.height = height === undefined ? 0 : height;
}
}
```
=== "TypeScript"
```typescript title="avl_tree.ts"
```typescript title=""
class TreeNode {
val: number; // 结点值
height: number; // 结点高度
left: TreeNode | null; // 左子结点指针
right: TreeNode | null; // 右子结点指针
constructor(val?: number, height?: number, left?: TreeNode | null, right?: TreeNode | null) {
this.val = val === undefined ? 0 : val;
this.height = height === undefined ? 0 : height;
this.left = left === undefined ? null : left;
this.right = right === undefined ? null : right;
}
}
```
=== "C"
```c title="avl_tree.c"
```c title=""
```
=== "C#"
```csharp title="avl_tree.cs"
```csharp title=""
/* AVL 树结点类 */
class TreeNode {
public int val; // 结点值
@@ -121,7 +132,7 @@ G. M. Adelson-Velsky 和 E. M. Landis 在其 1962 年发表的论文 "An algorit
=== "Swift"
```swift title="avl_tree.swift"
```swift title=""
/* AVL 树结点类 */
class TreeNode {
var val: Int // 结点值
@@ -138,7 +149,7 @@ G. M. Adelson-Velsky 和 E. M. Landis 在其 1962 年发表的论文 "An algorit
=== "Zig"
```zig title="avl_tree.zig"
```zig title=""
```
@@ -236,7 +247,17 @@ G. M. Adelson-Velsky 和 E. M. Landis 在其 1962 年发表的论文 "An algorit
=== "TypeScript"
```typescript title="avl_tree.ts"
/* 获取结点高度 */
height(node: TreeNode): number {
// 空结点高度为 -1 ,叶结点高度为 0
return node === null ? -1 : node.height;
}
/* 更新结点高度 */
updateHeight(node: TreeNode): void {
// 结点高度等于最高子树高度 + 1
node.height = Math.max(this.height(node.left), this.height(node.right)) + 1;
}
```
=== "C"
@@ -292,8 +313,8 @@ G. M. Adelson-Velsky 和 E. M. Landis 在其 1962 年发表的论文 "An algorit
=== "Java"
```java title="avl_tree.java"
/* 获取结点平衡因子 */
public int balanceFactor(TreeNode node) {
/* 获取平衡因子 */
int balanceFactor(TreeNode node) {
// 空结点平衡因子为 0
if (node == null) return 0;
// 结点平衡因子 = 左子树高度 - 右子树高度
@@ -354,7 +375,13 @@ G. M. Adelson-Velsky 和 E. M. Landis 在其 1962 年发表的论文 "An algorit
=== "TypeScript"
```typescript title="avl_tree.ts"
/* 获取平衡因子 */
balanceFactor(node: TreeNode): number {
// 空结点平衡因子为 0
if (node === null) return 0;
// 结点平衡因子 = 左子树高度 - 右子树高度
return this.height(node.left) - this.height(node.right);
}
```
=== "C"
@@ -408,7 +435,7 @@ AVL 树的独特之处在于「旋转 Rotation」的操作其可 **在不影
### Case 1 - 右旋
如下图所示(结点下方为「平衡因子」),从底至顶看,二叉树中首个失衡结点是 **结点 3**。我们聚焦在以该失衡结点为根结点的子树上,将该结点记为 `node` ,将其左子点记为 `child` ,执行「右旋」操作。完成右旋后,该子树已经恢复平衡,并且仍然为二叉搜索树。
如下图所示(结点下方为「平衡因子」),从底至顶看,二叉树中首个失衡结点是 **结点 3**。我们聚焦在以该失衡结点为根结点的子树上,将该结点记为 `node` ,将其左子点记为 `child` ,执行「右旋」操作。完成右旋后,该子树已经恢复平衡,并且仍然为二叉搜索树。
=== "Step 1"
![right_rotate_step1](avl_tree.assets/right_rotate_step1.png)
@@ -441,7 +468,7 @@ AVL 树的独特之处在于「旋转 Rotation」的操作其可 **在不影
// 更新结点高度
updateHeight(node);
updateHeight(child);
// 返回旋转后子树的根
// 返回旋转后子树的根
return child;
}
```
@@ -459,7 +486,7 @@ AVL 树的独特之处在于「旋转 Rotation」的操作其可 **在不影
// 更新结点高度
updateHeight(node);
updateHeight(child);
// 返回旋转后子树的根
// 返回旋转后子树的根
return child;
}
```
@@ -477,7 +504,7 @@ AVL 树的独特之处在于「旋转 Rotation」的操作其可 **在不影
# 更新结点高度
self.__update_height(node)
self.__update_height(child)
# 返回旋转后子树的根
# 返回旋转后子树的根
return child
```
@@ -494,7 +521,7 @@ AVL 树的独特之处在于「旋转 Rotation」的操作其可 **在不影
// 更新结点高度
updateHeight(node)
updateHeight(child)
// 返回旋转后子树的根
// 返回旋转后子树的根
return child
}
```
@@ -504,15 +531,15 @@ AVL 树的独特之处在于「旋转 Rotation」的操作其可 **在不影
```js title="avl_tree.js"
/* 右旋操作 */
rightRotate(node) {
let child = node.left;
let grandChild = child.right;
const child = node.left;
const grandChild = child.right;
// 以 child 为原点,将 node 向右旋转
child.right = node;
node.left = grandChild;
// 更新结点高度
this.updateHeight(node);
this.updateHeight(child);
// 返回旋转后子树的根
// 返回旋转后子树的根
return child;
}
```
@@ -520,7 +547,19 @@ AVL 树的独特之处在于「旋转 Rotation」的操作其可 **在不影
=== "TypeScript"
```typescript title="avl_tree.ts"
/* 右旋操作 */
rightRotate(node: TreeNode): TreeNode {
const child = node.left;
const grandChild = child.right;
// 以 child 为原点,将 node 向右旋转
child.right = node;
node.left = grandChild;
// 更新结点高度
this.updateHeight(node);
this.updateHeight(child);
// 返回旋转后子树的根结点
return child;
}
```
=== "C"
@@ -543,7 +582,7 @@ AVL 树的独特之处在于「旋转 Rotation」的操作其可 **在不影
// 更新结点高度
updateHeight(node);
updateHeight(child);
// 返回旋转后子树的根
// 返回旋转后子树的根
return child;
}
```
@@ -561,7 +600,7 @@ AVL 树的独特之处在于「旋转 Rotation」的操作其可 **在不影
// 更新结点高度
updateHeight(node: node)
updateHeight(node: child)
// 返回旋转后子树的根
// 返回旋转后子树的根
return child
}
```
@@ -588,7 +627,7 @@ AVL 树的独特之处在于「旋转 Rotation」的操作其可 **在不影
```java title="avl_tree.java"
/* 左旋操作 */
private TreeNode leftRotate(TreeNode node) {
TreeNode leftRotate(TreeNode node) {
TreeNode child = node.right;
TreeNode grandChild = child.left;
// 以 child 为原点,将 node 向左旋转
@@ -597,7 +636,7 @@ AVL 树的独特之处在于「旋转 Rotation」的操作其可 **在不影
// 更新结点高度
updateHeight(node);
updateHeight(child);
// 返回旋转后子树的根
// 返回旋转后子树的根
return child;
}
```
@@ -615,7 +654,7 @@ AVL 树的独特之处在于「旋转 Rotation」的操作其可 **在不影
// 更新结点高度
updateHeight(node);
updateHeight(child);
// 返回旋转后子树的根
// 返回旋转后子树的根
return child;
}
```
@@ -633,7 +672,7 @@ AVL 树的独特之处在于「旋转 Rotation」的操作其可 **在不影
# 更新结点高度
self.__update_height(node)
self.__update_height(child)
# 返回旋转后子树的根
# 返回旋转后子树的根
return child
```
@@ -650,7 +689,7 @@ AVL 树的独特之处在于「旋转 Rotation」的操作其可 **在不影
// 更新结点高度
updateHeight(node)
updateHeight(child)
// 返回旋转后子树的根
// 返回旋转后子树的根
return child
}
```
@@ -660,15 +699,15 @@ AVL 树的独特之处在于「旋转 Rotation」的操作其可 **在不影
```js title="avl_tree.js"
/* 左旋操作 */
leftRotate(node) {
let child = node.right;
let grandChild = child.left;
const child = node.right;
const grandChild = child.left;
// 以 child 为原点,将 node 向左旋转
child.left = node;
node.right = grandChild;
// 更新结点高度
this.updateHeight(node);
this.updateHeight(child);
// 返回旋转后子树的根
// 返回旋转后子树的根
return child;
}
```
@@ -676,7 +715,19 @@ AVL 树的独特之处在于「旋转 Rotation」的操作其可 **在不影
=== "TypeScript"
```typescript title="avl_tree.ts"
/* 左旋操作 */
leftRotate(node: TreeNode): TreeNode {
const child = node.right;
const grandChild = child.left;
// 以 child 为原点,将 node 向左旋转
child.left = node;
node.right = grandChild;
// 更新结点高度
this.updateHeight(node);
this.updateHeight(child);
// 返回旋转后子树的根结点
return child;
}
```
=== "C"
@@ -701,7 +752,7 @@ AVL 树的独特之处在于「旋转 Rotation」的操作其可 **在不影
// 更新结点高度
updateHeight(node);
updateHeight(child);
// 返回旋转后子树的根
// 返回旋转后子树的根
return child;
}
```
@@ -719,7 +770,7 @@ AVL 树的独特之处在于「旋转 Rotation」的操作其可 **在不影
// 更新结点高度
updateHeight(node: node)
updateHeight(node: child)
// 返回旋转后子树的根
// 返回旋转后子树的根
return child
}
@@ -902,7 +953,7 @@ AVL 树的独特之处在于「旋转 Rotation」的操作其可 **在不影
/* 执行旋转操作,使该子树重新恢复平衡 */
rotate(node) {
// 获取结点 node 的平衡因子
let balanceFactor = this.balanceFactor(node);
const balanceFactor = this.balanceFactor(node);
// 左偏树
if (balanceFactor > 1) {
if (this.balanceFactor(node.left) >= 0) {
@@ -933,7 +984,35 @@ AVL 树的独特之处在于「旋转 Rotation」的操作其可 **在不影
=== "TypeScript"
```typescript title="avl_tree.ts"
/* 执行旋转操作,使该子树重新恢复平衡 */
rotate(node: TreeNode): TreeNode {
// 获取结点 node 的平衡因子
const balanceFactor = this.balanceFactor(node);
// 左偏树
if (balanceFactor > 1) {
if (this.balanceFactor(node.left) >= 0) {
// 右旋
return this.rightRotate(node);
} else {
// 先左旋后右旋
node.left = this.leftRotate(node.left);
return this.rightRotate(node);
}
}
// 右偏树
if (balanceFactor < -1) {
if (this.balanceFactor(node.right) <= 0) {
// 左旋
return this.leftRotate(node);
} else {
// 先右旋后左旋
node.right = this.rightRotate(node.right);
return this.leftRotate(node);
}
}
// 平衡树,无需旋转,直接返回
return node;
}
```
=== "C"
@@ -1049,11 +1128,11 @@ AVL 树的独特之处在于「旋转 Rotation」的操作其可 **在不影
else if (val > node.val)
node.right = insertHelper(node.right, val);
else
return node; // 重复结点不插入,直接返回
updateHeight(node); // 更新结点高度
return node; // 重复结点不插入,直接返回
updateHeight(node); // 更新结点高度
/* 2. 执行旋转操作,使该子树重新恢复平衡 */
node = rotate(node);
// 返回子树的根
// 返回子树的根
return node;
}
```
@@ -1080,7 +1159,7 @@ AVL 树的独特之处在于「旋转 Rotation」的操作其可 **在不影
updateHeight(node); // 更新结点高度
/* 2. 执行旋转操作,使该子树重新恢复平衡 */
node = rotate(node);
// 返回子树的根
// 返回子树的根
return node;
}
```
@@ -1137,7 +1216,7 @@ AVL 树的独特之处在于「旋转 Rotation」的操作其可 **在不影
updateHeight(node)
/* 2. 执行旋转操作,使该子树重新恢复平衡 */
node = rotate(node)
// 返回子树的根
// 返回子树的根
return node
}
```
@@ -1161,7 +1240,7 @@ AVL 树的独特之处在于「旋转 Rotation」的操作其可 **在不影
this.updateHeight(node); // 更新结点高度
/* 2. 执行旋转操作,使该子树重新恢复平衡 */
node = this.rotate(node);
// 返回子树的根
// 返回子树的根
return node;
}
```
@@ -1169,7 +1248,29 @@ AVL 树的独特之处在于「旋转 Rotation」的操作其可 **在不影
=== "TypeScript"
```typescript title="avl_tree.ts"
/* 插入结点 */
insert(val: number): TreeNode {
this.root = this.insertHelper(this.root, val);
return this.root;
}
/* 递归插入结点(辅助函数) */
insertHelper(node: TreeNode, val: number): TreeNode {
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);
} else {
return node; // 重复结点不插入,直接返回
}
this.updateHeight(node); // 更新结点高度
/* 2. 执行旋转操作,使该子树重新恢复平衡 */
node = this.rotate(node);
// 返回子树的根结点
return node;
}
```
=== "C"
@@ -1204,7 +1305,7 @@ AVL 树的独特之处在于「旋转 Rotation」的操作其可 **在不影
updateHeight(node); // 更新结点高度
/* 2. 执行旋转操作,使该子树重新恢复平衡 */
node = rotate(node);
// 返回子树的根
// 返回子树的根
return node;
}
```
@@ -1236,7 +1337,7 @@ AVL 树的独特之处在于「旋转 Rotation」的操作其可 **在不影
updateHeight(node: node) // 更新结点高度
/* 2. 执行旋转操作,使该子树重新恢复平衡 */
node = rotate(node: node)
// 返回子树的根
// 返回子树的根
return node
}
```
@@ -1284,10 +1385,20 @@ AVL 树的独特之处在于「旋转 Rotation」的操作其可 **在不影
node.val = temp.val;
}
}
updateHeight(node); // 更新结点高度
updateHeight(node); // 更新结点高度
/* 2. 执行旋转操作,使该子树重新恢复平衡 */
node = rotate(node);
// 返回子树的根
// 返回子树的根
return node;
}
/* 获取中序遍历中的下一个结点(仅适用于 root 有左子结点的情况) */
TreeNode getInOrderNext(TreeNode node) {
if (node == null) return node;
// 循环访问左子结点,直到叶结点时为最小结点,跳出
while (node.left != null) {
node = node.left;
}
return node;
}
```
@@ -1332,7 +1443,7 @@ AVL 树的独特之处在于「旋转 Rotation」的操作其可 **在不影
updateHeight(node); // 更新结点高度
/* 2. 执行旋转操作,使该子树重新恢复平衡 */
node = rotate(node);
// 返回子树的根
// 返回子树的根
return node;
}
```
@@ -1416,7 +1527,7 @@ AVL 树的独特之处在于「旋转 Rotation」的操作其可 **在不影
updateHeight(node)
/* 2. 执行旋转操作,使该子树重新恢复平衡 */
node = rotate(node)
// 返回子树的根
// 返回子树的根
return node
}
```
@@ -1438,14 +1549,14 @@ AVL 树的独特之处在于「旋转 Rotation」的操作其可 **在不影
else if (val > node.val) node.right = this.removeHelper(node.right, val);
else {
if (node.left === null || node.right === null) {
let child = node.left !== null ? node.left : node.right;
const child = node.left !== null ? node.left : node.right;
// 子结点数量 = 0 ,直接删除 node 并返回
if (child === null) return null;
// 子结点数量 = 1 ,直接删除 node
else node = child;
} else {
// 子结点数量 = 2 ,则将中序遍历的下个结点删除,并用该结点替换当前结点
let temp = this.getInOrderNext(node.right);
const temp = this.getInOrderNext(node.right);
node.right = this.removeHelper(node.right, temp.val);
node.val = temp.val;
}
@@ -1453,7 +1564,27 @@ AVL 树的独特之处在于「旋转 Rotation」的操作其可 **在不影
this.updateHeight(node); // 更新结点高度
/* 2. 执行旋转操作,使该子树重新恢复平衡 */
node = this.rotate(node);
// 返回子树的根
// 返回子树的根
return node;
}
/* 获取中序遍历中的下一个结点(仅适用于 root 有左子结点的情况) */
getInOrderNext(node) {
if (node === null) return node;
// 循环访问左子结点,直到叶结点时为最小结点,跳出
while (node.left !== null) {
node = node.left;
}
return node;
}
/* 获取中序遍历中的下一个结点(仅适用于 root 有左子结点的情况) */
getInOrderNext(node) {
if (node === null) return node;
// 循环访问左子结点,直到叶结点时为最小结点,跳出
while (node.left !== null) {
node = node.left;
}
return node;
}
```
@@ -1461,7 +1592,53 @@ AVL 树的独特之处在于「旋转 Rotation」的操作其可 **在不影
=== "TypeScript"
```typescript title="avl_tree.ts"
/* 删除结点 */
remove(val: number): TreeNode {
this.root = this.removeHelper(this.root, val);
return this.root;
}
/* 递归删除结点(辅助函数) */
removeHelper(node: TreeNode, val: number): TreeNode {
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);
} else {
if (node.left === null || node.right === null) {
const child = node.left !== null ? node.left : node.right;
// 子结点数量 = 0 ,直接删除 node 并返回
if (child === null) {
return null;
} else {
// 子结点数量 = 1 ,直接删除 node
node = child;
}
} else {
// 子结点数量 = 2 ,则将中序遍历的下个结点删除,并用该结点替换当前结点
const temp = this.getInOrderNext(node.right);
node.right = this.removeHelper(node.right, temp.val);
node.val = temp.val;
}
}
this.updateHeight(node); // 更新结点高度
/* 2. 执行旋转操作,使该子树重新恢复平衡 */
node = this.rotate(node);
// 返回子树的根结点
return node;
}
/* 获取中序遍历中的下一个结点(仅适用于 root 有左子结点的情况) */
getInOrderNext(node: TreeNode): TreeNode {
if (node === null) return node;
// 循环访问左子结点,直到叶结点时为最小结点,跳出
while (node.left !== null) {
node = node.left;
}
return node;
}
```
=== "C"
@@ -1512,7 +1689,7 @@ AVL 树的独特之处在于「旋转 Rotation」的操作其可 **在不影
updateHeight(node); // 更新结点高度
/* 2. 执行旋转操作,使该子树重新恢复平衡 */
node = rotate(node);
// 返回子树的根
// 返回子树的根
return node;
}
```
@@ -1559,7 +1736,7 @@ AVL 树的独特之处在于「旋转 Rotation」的操作其可 **在不影
updateHeight(node: node) // 更新结点高度
/* 2. 执行旋转操作,使该子树重新恢复平衡 */
node = rotate(node: node)
// 返回子树的根
// 返回子树的根
return node
}
```

View File

@@ -556,8 +556,6 @@ comments: true
// 删除结点 cur
if (pre.left == cur) pre.left = child;
else pre.right = child;
// 释放内存
delete cur;
}
// 子结点数量 = 2
else {
@@ -573,7 +571,7 @@ comments: true
}
/* 获取中序遍历中的下一个结点(仅适用于 root 有左子结点的情况) */
public TreeNode getInOrderNext(TreeNode root) {
TreeNode getInOrderNext(TreeNode root) {
if (root == null) return root;
// 循环访问左子结点,直到叶结点时为最小结点,跳出
while (root.left != null) {

View File

@@ -197,7 +197,7 @@ comments: true
```python title="binary_tree.py"
""" 初始化二叉树 """
# 初始化
# 初始化
n1 = TreeNode(val=1)
n2 = TreeNode(val=2)
n3 = TreeNode(val=3)
@@ -343,7 +343,7 @@ comments: true
# 在 n1 -> n2 中间插入结点 P
n1.left = p
p.left = n2
# 删除点 P
# 删除点 P
n1.left = n2
```

View File

@@ -52,7 +52,7 @@ comments: true
while (!queue.empty()) {
TreeNode* node = queue.front();
queue.pop(); // 队列出队
vec.push_back(node->val); // 保存结点
vec.push_back(node->val); // 保存结点
if (node->left != nullptr)
queue.push(node->left); // 左子结点入队
if (node->right != nullptr)
@@ -74,7 +74,7 @@ comments: true
res = []
while queue:
node = queue.popleft() # 队列出队
res.append(node.val) # 保存点值
res.append(node.val) # 保存点值
if node.left is not None:
queue.append(node.left) # 左子结点入队
if node.right is not None:
@@ -95,7 +95,7 @@ comments: true
for queue.Len() > 0 {
// poll
node := queue.Remove(queue.Front()).(*TreeNode)
// 保存结点
// 保存结点
nums = append(nums, node.Val)
if node.Left != nil {
// 左子结点入队
@@ -120,12 +120,12 @@ comments: true
// 初始化一个列表,用于保存遍历序列
let list = [];
while (queue.length) {
let node = queue.shift(); // 队列出队
list.push(node.val); // 保存结点
let node = queue.shift(); // 队列出队
list.push(node.val); // 保存结点
if (node.left)
queue.push(node.left); // 左子结点入队
queue.push(node.left); // 左子结点入队
if (node.right)
queue.push(node.right); // 右子结点入队
queue.push(node.right); // 右子结点入队
}
return list;
}
@@ -142,7 +142,7 @@ comments: true
const list: number[] = [];
while (queue.length) {
let node = queue.shift() as TreeNode; // 队列出队
list.push(node.val); // 保存结点
list.push(node.val); // 保存结点
if (node.left) {
queue.push(node.left); // 左子结点入队
}
@@ -196,7 +196,7 @@ comments: true
var list: [Int] = []
while !queue.isEmpty {
let node = queue.removeFirst() // 队列出队
list.append(node.val) // 保存结点
list.append(node.val) // 保存结点
if let left = node.left {
queue.append(left) // 左子结点入队
}
@@ -245,7 +245,7 @@ comments: true
preOrder(root.left);
preOrder(root.right);
}
/* 中序遍历 */
void inOrder(TreeNode root) {
if (root == null) return;
@@ -254,7 +254,7 @@ comments: true
list.add(root.val);
inOrder(root.right);
}
/* 后序遍历 */
void postOrder(TreeNode root) {
if (root == null) return;