|
|
@ -82,14 +82,14 @@ G. M. Adelson-Velsky 和 E. M. Landis 在其 1962 年发表的论文 "An algorit
|
|
|
|
```js title="avl_tree.js"
|
|
|
|
```js title="avl_tree.js"
|
|
|
|
class TreeNode {
|
|
|
|
class TreeNode {
|
|
|
|
val; // 结点值
|
|
|
|
val; // 结点值
|
|
|
|
|
|
|
|
height; //结点高度
|
|
|
|
left; // 左子结点指针
|
|
|
|
left; // 左子结点指针
|
|
|
|
right; // 右子结点指针
|
|
|
|
right; // 右子结点指针
|
|
|
|
height; //结点高度
|
|
|
|
|
|
|
|
constructor(val, left, right, height) {
|
|
|
|
constructor(val, left, right, height) {
|
|
|
|
this.val = val === undefined ? 0 : val;
|
|
|
|
this.val = val === undefined ? 0 : val;
|
|
|
|
|
|
|
|
this.height = height === undefined ? 0 : height;
|
|
|
|
this.left = left === undefined ? null : left;
|
|
|
|
this.left = left === undefined ? null : left;
|
|
|
|
this.right = right === undefined ? null : right;
|
|
|
|
this.right = right === undefined ? null : right;
|
|
|
|
this.height = height === undefined ? 0 : height;
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
```
|
|
|
|
```
|
|
|
@ -97,7 +97,18 @@ G. M. Adelson-Velsky 和 E. M. Landis 在其 1962 年发表的论文 "An algorit
|
|
|
|
=== "TypeScript"
|
|
|
|
=== "TypeScript"
|
|
|
|
|
|
|
|
|
|
|
|
```typescript title="avl_tree.ts"
|
|
|
|
```typescript title="avl_tree.ts"
|
|
|
|
|
|
|
|
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"
|
|
|
@ -228,7 +239,17 @@ G. M. Adelson-Velsky 和 E. M. Landis 在其 1962 年发表的论文 "An algorit
|
|
|
|
=== "TypeScript"
|
|
|
|
=== "TypeScript"
|
|
|
|
|
|
|
|
|
|
|
|
```typescript title="avl_tree.ts"
|
|
|
|
```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"
|
|
|
|
=== "C"
|
|
|
@ -340,7 +361,13 @@ G. M. Adelson-Velsky 和 E. M. Landis 在其 1962 年发表的论文 "An algorit
|
|
|
|
=== "TypeScript"
|
|
|
|
=== "TypeScript"
|
|
|
|
|
|
|
|
|
|
|
|
```typescript title="avl_tree.ts"
|
|
|
|
```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"
|
|
|
|
=== "C"
|
|
|
@ -479,8 +506,8 @@ AVL 树的独特之处在于「旋转 Rotation」的操作,其可 **在不影
|
|
|
|
```js title="avl_tree.js"
|
|
|
|
```js title="avl_tree.js"
|
|
|
|
/* 右旋操作 */
|
|
|
|
/* 右旋操作 */
|
|
|
|
rightRotate(node) {
|
|
|
|
rightRotate(node) {
|
|
|
|
let child = node.left;
|
|
|
|
const child = node.left;
|
|
|
|
let grandChild = child.right;
|
|
|
|
const grandChild = child.right;
|
|
|
|
// 以 child 为原点,将 node 向右旋转
|
|
|
|
// 以 child 为原点,将 node 向右旋转
|
|
|
|
child.right = node;
|
|
|
|
child.right = node;
|
|
|
|
node.left = grandChild;
|
|
|
|
node.left = grandChild;
|
|
|
@ -495,7 +522,19 @@ AVL 树的独特之处在于「旋转 Rotation」的操作,其可 **在不影
|
|
|
|
=== "TypeScript"
|
|
|
|
=== "TypeScript"
|
|
|
|
|
|
|
|
|
|
|
|
```typescript title="avl_tree.ts"
|
|
|
|
```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"
|
|
|
|
=== "C"
|
|
|
@ -624,8 +663,8 @@ AVL 树的独特之处在于「旋转 Rotation」的操作,其可 **在不影
|
|
|
|
```js title="avl_tree.js"
|
|
|
|
```js title="avl_tree.js"
|
|
|
|
/* 左旋操作 */
|
|
|
|
/* 左旋操作 */
|
|
|
|
leftRotate(node) {
|
|
|
|
leftRotate(node) {
|
|
|
|
let child = node.right;
|
|
|
|
const child = node.right;
|
|
|
|
let grandChild = child.left;
|
|
|
|
const grandChild = child.left;
|
|
|
|
// 以 child 为原点,将 node 向左旋转
|
|
|
|
// 以 child 为原点,将 node 向左旋转
|
|
|
|
child.left = node;
|
|
|
|
child.left = node;
|
|
|
|
node.right = grandChild;
|
|
|
|
node.right = grandChild;
|
|
|
@ -640,7 +679,19 @@ AVL 树的独特之处在于「旋转 Rotation」的操作,其可 **在不影
|
|
|
|
=== "TypeScript"
|
|
|
|
=== "TypeScript"
|
|
|
|
|
|
|
|
|
|
|
|
```typescript title="avl_tree.ts"
|
|
|
|
```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"
|
|
|
|
=== "C"
|
|
|
@ -843,7 +894,7 @@ AVL 树的独特之处在于「旋转 Rotation」的操作,其可 **在不影
|
|
|
|
/* 执行旋转操作,使该子树重新恢复平衡 */
|
|
|
|
/* 执行旋转操作,使该子树重新恢复平衡 */
|
|
|
|
rotate(node) {
|
|
|
|
rotate(node) {
|
|
|
|
// 获取结点 node 的平衡因子
|
|
|
|
// 获取结点 node 的平衡因子
|
|
|
|
let balanceFactor = this.balanceFactor(node);
|
|
|
|
const balanceFactor = this.balanceFactor(node);
|
|
|
|
// 左偏树
|
|
|
|
// 左偏树
|
|
|
|
if (balanceFactor > 1) {
|
|
|
|
if (balanceFactor > 1) {
|
|
|
|
if (this.balanceFactor(node.left) >= 0) {
|
|
|
|
if (this.balanceFactor(node.left) >= 0) {
|
|
|
@ -874,7 +925,35 @@ AVL 树的独特之处在于「旋转 Rotation」的操作,其可 **在不影
|
|
|
|
=== "TypeScript"
|
|
|
|
=== "TypeScript"
|
|
|
|
|
|
|
|
|
|
|
|
```typescript title="avl_tree.ts"
|
|
|
|
```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"
|
|
|
|
=== "C"
|
|
|
@ -1092,7 +1171,29 @@ AVL 树的独特之处在于「旋转 Rotation」的操作,其可 **在不影
|
|
|
|
=== "TypeScript"
|
|
|
|
=== "TypeScript"
|
|
|
|
|
|
|
|
|
|
|
|
```typescript title="avl_tree.ts"
|
|
|
|
```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"
|
|
|
|
=== "C"
|
|
|
@ -1333,14 +1434,14 @@ AVL 树的独特之处在于「旋转 Rotation」的操作,其可 **在不影
|
|
|
|
else if (val > node.val) node.right = this.removeHelper(node.right, val);
|
|
|
|
else if (val > node.val) node.right = this.removeHelper(node.right, val);
|
|
|
|
else {
|
|
|
|
else {
|
|
|
|
if (node.left === null || node.right === null) {
|
|
|
|
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 并返回
|
|
|
|
// 子结点数量 = 0 ,直接删除 node 并返回
|
|
|
|
if (child === null) return null;
|
|
|
|
if (child === null) return null;
|
|
|
|
// 子结点数量 = 1 ,直接删除 node
|
|
|
|
// 子结点数量 = 1 ,直接删除 node
|
|
|
|
else node = child;
|
|
|
|
else node = child;
|
|
|
|
} else {
|
|
|
|
} else {
|
|
|
|
// 子结点数量 = 2 ,则将中序遍历的下个结点删除,并用该结点替换当前结点
|
|
|
|
// 子结点数量 = 2 ,则将中序遍历的下个结点删除,并用该结点替换当前结点
|
|
|
|
let temp = this.getInOrderNext(node.right);
|
|
|
|
const temp = this.getInOrderNext(node.right);
|
|
|
|
node.right = this.removeHelper(node.right, temp.val);
|
|
|
|
node.right = this.removeHelper(node.right, temp.val);
|
|
|
|
node.val = temp.val;
|
|
|
|
node.val = temp.val;
|
|
|
|
}
|
|
|
|
}
|
|
|
@ -1351,12 +1452,68 @@ AVL 树的独特之处在于「旋转 Rotation」的操作,其可 **在不影
|
|
|
|
// 返回子树的根节点
|
|
|
|
// 返回子树的根节点
|
|
|
|
return node;
|
|
|
|
return node;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/* 获取中序遍历中的下一个结点(仅适用于 root 有左子结点的情况) */
|
|
|
|
|
|
|
|
getInOrderNext(node) {
|
|
|
|
|
|
|
|
if (node === null) return node;
|
|
|
|
|
|
|
|
// 循环访问左子结点,直到叶结点时为最小结点,跳出
|
|
|
|
|
|
|
|
while (node.left !== null) {
|
|
|
|
|
|
|
|
node = node.left;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
return node;
|
|
|
|
|
|
|
|
}
|
|
|
|
```
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
|
|
=== "TypeScript"
|
|
|
|
=== "TypeScript"
|
|
|
|
|
|
|
|
|
|
|
|
```typescript title="avl_tree.ts"
|
|
|
|
```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"
|
|
|
|
=== "C"
|
|
|
|