1. Add build script for Java.

2. Add height limitation for code blocks in extra.css.
3. Fix "节点" to "结点".
This commit is contained in:
krahets
2023-02-07 04:43:52 +08:00
parent b14568151c
commit ecbf2d1560
54 changed files with 457 additions and 1633 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,7 +79,7 @@ G. M. Adelson-Velsky 和 E. M. Landis 在其 1962 年发表的论文 "An algorit
=== "JavaScript"
```js title="avl_tree.js"
```js title=""
class TreeNode {
val; // 结点值
height; //结点高度
@@ -96,7 +96,7 @@ G. M. Adelson-Velsky 和 E. M. Landis 在其 1962 年发表的论文 "An algorit
=== "TypeScript"
```typescript title="avl_tree.ts"
```typescript title=""
class TreeNode {
val: number; // 结点值
height: number; // 结点高度
@@ -113,13 +113,13 @@ G. M. Adelson-Velsky 和 E. M. Landis 在其 1962 年发表的论文 "An algorit
=== "C"
```c title="avl_tree.c"
```c title=""
```
=== "C#"
```csharp title="avl_tree.cs"
```csharp title=""
/* AVL 树结点类 */
class TreeNode {
public int val; // 结点值
@@ -132,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 // 结点值
@@ -149,7 +149,7 @@ G. M. Adelson-Velsky 和 E. M. Landis 在其 1962 年发表的论文 "An algorit
=== "Zig"
```zig title="avl_tree.zig"
```zig title=""
```
@@ -158,17 +158,9 @@ G. M. Adelson-Velsky 和 E. M. Landis 在其 1962 年发表的论文 "An algorit
=== "Java"
```java title="avl_tree.java"
/* 获取结点高度 */
int height(TreeNode node) {
// 空结点高度为 -1 ,叶结点高度为 0
return node == null ? -1 : node.height;
}
[class]{AVLTree}-[func]{height}
/* 更新结点高度 */
void updateHeight(TreeNode node) {
// 结点高度等于最高子树高度 + 1
node.height = Math.max(height(node.left), height(node.right)) + 1;
}
[class]{AVLTree}-[func]{updateHeight}
```
=== "C++"
@@ -305,13 +297,7 @@ G. M. Adelson-Velsky 和 E. M. Landis 在其 1962 年发表的论文 "An algorit
=== "Java"
```java title="avl_tree.java"
/* 获取结点平衡因子 */
public int balanceFactor(TreeNode node) {
// 空结点平衡因子为 0
if (node == null) return 0;
// 结点平衡因子 = 左子树高度 - 右子树高度
return height(node.left) - height(node.right);
}
[class]{AVLTree}-[func]{balanceFactor}
```
=== "C++"
@@ -421,7 +407,7 @@ AVL 树的独特之处在于「旋转 Rotation」的操作其可 **在不影
### Case 1 - 右旋
如下图所示(结点下方为「平衡因子」),从底至顶看,二叉树中首个失衡结点是 **结点 3**。我们聚焦在以该失衡结点为根结点的子树上,将该结点记为 `node` ,将其左子点记为 `child` ,执行「右旋」操作。完成右旋后,该子树已经恢复平衡,并且仍然为二叉搜索树。
如下图所示(结点下方为「平衡因子」),从底至顶看,二叉树中首个失衡结点是 **结点 3**。我们聚焦在以该失衡结点为根结点的子树上,将该结点记为 `node` ,将其左子点记为 `child` ,执行「右旋」操作。完成右旋后,该子树已经恢复平衡,并且仍然为二叉搜索树。
=== "Step 1"
![right_rotate_step1](avl_tree.assets/right_rotate_step1.png)
@@ -444,19 +430,7 @@ AVL 树的独特之处在于「旋转 Rotation」的操作其可 **在不影
=== "Java"
```java title="avl_tree.java"
/* 右旋操作 */
TreeNode rightRotate(TreeNode node) {
TreeNode child = node.left;
TreeNode grandChild = child.right;
// 以 child 为原点,将 node 向右旋转
child.right = node;
node.left = grandChild;
// 更新结点高度
updateHeight(node);
updateHeight(child);
// 返回旋转后子树的根节点
return child;
}
[class]{AVLTree}-[func]{rightRotate}
```
=== "C++"
@@ -472,7 +446,7 @@ AVL 树的独特之处在于「旋转 Rotation」的操作其可 **在不影
// 更新结点高度
updateHeight(node);
updateHeight(child);
// 返回旋转后子树的根
// 返回旋转后子树的根
return child;
}
```
@@ -496,7 +470,7 @@ AVL 树的独特之处在于「旋转 Rotation」的操作其可 **在不影
// 更新结点高度
updateHeight(node)
updateHeight(child)
// 返回旋转后子树的根
// 返回旋转后子树的根
return child
}
```
@@ -514,7 +488,7 @@ AVL 树的独特之处在于「旋转 Rotation」的操作其可 **在不影
// 更新结点高度
this.updateHeight(node);
this.updateHeight(child);
// 返回旋转后子树的根
// 返回旋转后子树的根
return child;
}
```
@@ -532,7 +506,7 @@ AVL 树的独特之处在于「旋转 Rotation」的操作其可 **在不影
// 更新结点高度
this.updateHeight(node);
this.updateHeight(child);
// 返回旋转后子树的根
// 返回旋转后子树的根
return child;
}
```
@@ -557,7 +531,7 @@ AVL 树的独特之处在于「旋转 Rotation」的操作其可 **在不影
// 更新结点高度
updateHeight(node);
updateHeight(child);
// 返回旋转后子树的根
// 返回旋转后子树的根
return child;
}
```
@@ -575,7 +549,7 @@ AVL 树的独特之处在于「旋转 Rotation」的操作其可 **在不影
// 更新结点高度
updateHeight(node: node)
updateHeight(node: child)
// 返回旋转后子树的根
// 返回旋转后子树的根
return child
}
```
@@ -601,19 +575,7 @@ AVL 树的独特之处在于「旋转 Rotation」的操作其可 **在不影
=== "Java"
```java title="avl_tree.java"
/* 左旋操作 */
private TreeNode leftRotate(TreeNode node) {
TreeNode child = node.right;
TreeNode grandChild = child.left;
// 以 child 为原点,将 node 向左旋转
child.left = node;
node.right = grandChild;
// 更新结点高度
updateHeight(node);
updateHeight(child);
// 返回旋转后子树的根节点
return child;
}
[class]{AVLTree}-[func]{leftRotate}
```
=== "C++"
@@ -629,7 +591,7 @@ AVL 树的独特之处在于「旋转 Rotation」的操作其可 **在不影
// 更新结点高度
updateHeight(node);
updateHeight(child);
// 返回旋转后子树的根
// 返回旋转后子树的根
return child;
}
```
@@ -653,7 +615,7 @@ AVL 树的独特之处在于「旋转 Rotation」的操作其可 **在不影
// 更新结点高度
updateHeight(node)
updateHeight(child)
// 返回旋转后子树的根
// 返回旋转后子树的根
return child
}
```
@@ -671,7 +633,7 @@ AVL 树的独特之处在于「旋转 Rotation」的操作其可 **在不影
// 更新结点高度
this.updateHeight(node);
this.updateHeight(child);
// 返回旋转后子树的根
// 返回旋转后子树的根
return child;
}
```
@@ -689,7 +651,7 @@ AVL 树的独特之处在于「旋转 Rotation」的操作其可 **在不影
// 更新结点高度
this.updateHeight(node);
this.updateHeight(child);
// 返回旋转后子树的根
// 返回旋转后子树的根
return child;
}
```
@@ -716,7 +678,7 @@ AVL 树的独特之处在于「旋转 Rotation」的操作其可 **在不影
// 更新结点高度
updateHeight(node);
updateHeight(child);
// 返回旋转后子树的根
// 返回旋转后子树的根
return child;
}
```
@@ -734,7 +696,7 @@ AVL 树的独特之处在于「旋转 Rotation」的操作其可 **在不影
// 更新结点高度
updateHeight(node: node)
updateHeight(node: child)
// 返回旋转后子树的根
// 返回旋转后子树的根
return child
}
@@ -782,35 +744,7 @@ AVL 树的独特之处在于「旋转 Rotation」的操作其可 **在不影
=== "Java"
```java title="avl_tree.java"
/* 执行旋转操作,使该子树重新恢复平衡 */
TreeNode rotate(TreeNode node) {
// 获取结点 node 的平衡因子
int balanceFactor = balanceFactor(node);
// 左偏树
if (balanceFactor > 1) {
if (balanceFactor(node.left) >= 0) {
// 右旋
return rightRotate(node);
} else {
// 先左旋后右旋
node.left = leftRotate(node.left);
return rightRotate(node);
}
}
// 右偏树
if (balanceFactor < -1) {
if (balanceFactor(node.right) <= 0) {
// 左旋
return leftRotate(node);
} else {
// 先右旋后左旋
node.right = rightRotate(node.right);
return leftRotate(node);
}
}
// 平衡树,无需旋转,直接返回
return node;
}
[class]{AVLTree}-[func]{rotate}
```
=== "C++"
@@ -1054,28 +988,9 @@ AVL 树的独特之处在于「旋转 Rotation」的操作其可 **在不影
=== "Java"
```java title="avl_tree.java"
/* 插入结点 */
TreeNode insert(int val) {
root = insertHelper(root, val);
return root;
}
[class]{AVLTree}-[func]{insert}
/* 递归插入结点(辅助函数) */
TreeNode insertHelper(TreeNode node, int val) {
if (node == null) return new TreeNode(val);
/* 1. 查找插入位置,并插入结点 */
if (val < node.val)
node.left = insertHelper(node.left, val);
else if (val > node.val)
node.right = insertHelper(node.right, val);
else
return node; // 重复结点不插入,直接返回
updateHeight(node); // 更新结点高度
/* 2. 执行旋转操作,使该子树重新恢复平衡 */
node = rotate(node);
// 返回子树的根节点
return node;
}
[class]{AVLTree}-[func]{insertHelper}
```
=== "C++"
@@ -1100,7 +1015,7 @@ AVL 树的独特之处在于「旋转 Rotation」的操作其可 **在不影
updateHeight(node); // 更新结点高度
/* 2. 执行旋转操作,使该子树重新恢复平衡 */
node = rotate(node);
// 返回子树的根
// 返回子树的根
return node;
}
```
@@ -1139,7 +1054,7 @@ AVL 树的独特之处在于「旋转 Rotation」的操作其可 **在不影
updateHeight(node)
/* 2. 执行旋转操作,使该子树重新恢复平衡 */
node = rotate(node)
// 返回子树的根
// 返回子树的根
return node
}
```
@@ -1163,7 +1078,7 @@ AVL 树的独特之处在于「旋转 Rotation」的操作其可 **在不影
this.updateHeight(node); // 更新结点高度
/* 2. 执行旋转操作,使该子树重新恢复平衡 */
node = this.rotate(node);
// 返回子树的根
// 返回子树的根
return node;
}
```
@@ -1191,7 +1106,7 @@ AVL 树的独特之处在于「旋转 Rotation」的操作其可 **在不影
this.updateHeight(node); // 更新结点高度
/* 2. 执行旋转操作,使该子树重新恢复平衡 */
node = this.rotate(node);
// 返回子树的根
// 返回子树的根
return node;
}
```
@@ -1228,7 +1143,7 @@ AVL 树的独特之处在于「旋转 Rotation」的操作其可 **在不影
updateHeight(node); // 更新结点高度
/* 2. 执行旋转操作,使该子树重新恢复平衡 */
node = rotate(node);
// 返回子树的根
// 返回子树的根
return node;
}
```
@@ -1260,7 +1175,7 @@ AVL 树的独特之处在于「旋转 Rotation」的操作其可 **在不影
updateHeight(node: node) // 更新结点高度
/* 2. 执行旋转操作,使该子树重新恢复平衡 */
node = rotate(node: node)
// 返回子树的根
// 返回子树的根
return node
}
```
@@ -1278,42 +1193,11 @@ AVL 树的独特之处在于「旋转 Rotation」的操作其可 **在不影
=== "Java"
```java title="avl_tree.java"
/* 删除结点 */
TreeNode remove(int val) {
root = removeHelper(root, val);
return root;
}
[class]{AVLTree}-[func]{remove}
/* 递归删除结点(辅助函数) */
TreeNode removeHelper(TreeNode node, int val) {
if (node == null) return null;
/* 1. 查找结点,并删除之 */
if (val < node.val)
node.left = removeHelper(node.left, val);
else if (val > node.val)
node.right = removeHelper(node.right, val);
else {
if (node.left == null || node.right == null) {
TreeNode child = node.left != null ? node.left : node.right;
// 子结点数量 = 0 ,直接删除 node 并返回
if (child == null)
return null;
// 子结点数量 = 1 ,直接删除 node
else
node = child;
} else {
// 子结点数量 = 2 ,则将中序遍历的下个结点删除,并用该结点替换当前结点
TreeNode temp = getInOrderNext(node.right);
node.right = removeHelper(node.right, temp.val);
node.val = temp.val;
}
}
updateHeight(node); // 更新结点高度
/* 2. 执行旋转操作,使该子树重新恢复平衡 */
node = rotate(node);
// 返回子树的根节点
return node;
}
[class]{AVLTree}-[func]{removeHelper}
[class]{AVLTree}-[func]{getInOrderNext}
```
=== "C++"
@@ -1356,7 +1240,7 @@ AVL 树的独特之处在于「旋转 Rotation」的操作其可 **在不影
updateHeight(node); // 更新结点高度
/* 2. 执行旋转操作,使该子树重新恢复平衡 */
node = rotate(node);
// 返回子树的根
// 返回子树的根
return node;
}
```
@@ -1412,7 +1296,7 @@ AVL 树的独特之处在于「旋转 Rotation」的操作其可 **在不影
updateHeight(node)
/* 2. 执行旋转操作,使该子树重新恢复平衡 */
node = rotate(node)
// 返回子树的根
// 返回子树的根
return node
}
```
@@ -1449,7 +1333,17 @@ 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;
}
@@ -1501,7 +1395,7 @@ AVL 树的独特之处在于「旋转 Rotation」的操作其可 **在不影
this.updateHeight(node); // 更新结点高度
/* 2. 执行旋转操作,使该子树重新恢复平衡 */
node = this.rotate(node);
// 返回子树的根
// 返回子树的根
return node;
}
@@ -1564,7 +1458,7 @@ AVL 树的独特之处在于「旋转 Rotation」的操作其可 **在不影
updateHeight(node); // 更新结点高度
/* 2. 执行旋转操作,使该子树重新恢复平衡 */
node = rotate(node);
// 返回子树的根
// 返回子树的根
return node;
}
```
@@ -1611,7 +1505,7 @@ AVL 树的独特之处在于「旋转 Rotation」的操作其可 **在不影
updateHeight(node: node) // 更新结点高度
/* 2. 执行旋转操作,使该子树重新恢复平衡 */
node = rotate(node: node)
// 返回子树的根
// 返回子树的根
return node
}
```