This commit is contained in:
krahets
2023-04-23 14:58:03 +08:00
parent 881ece517f
commit fe8027e64a
26 changed files with 344 additions and 626 deletions
+20 -43
View File
@@ -315,15 +315,13 @@ G. M. Adelson-Velsky 和 E. M. Landis 在其 1962 年发表的论文 "An algorit
```csharp title="avl_tree.cs"
/* 获取节点高度 */
int height(TreeNode? node)
{
int height(TreeNode? node) {
// 空节点高度为 -1 ,叶节点高度为 0
return node == null ? -1 : node.height;
}
/* 更新节点高度 */
void updateHeight(TreeNode node)
{
void updateHeight(TreeNode node) {
// 节点高度等于最高子树高度 + 1
node.height = Math.Max(height(node.left), height(node.right)) + 1;
}
@@ -460,8 +458,7 @@ G. M. Adelson-Velsky 和 E. M. Landis 在其 1962 年发表的论文 "An algorit
```csharp title="avl_tree.cs"
/* 获取平衡因子 */
int balanceFactor(TreeNode? node)
{
int balanceFactor(TreeNode? node) {
// 空节点平衡因子为 0
if (node == null) return 0;
// 节点平衡因子 = 左子树高度 - 右子树高度
@@ -657,8 +654,7 @@ AVL 树的特点在于「旋转 Rotation」操作,它能够在不影响二叉
```csharp title="avl_tree.cs"
/* 右旋操作 */
TreeNode? rightRotate(TreeNode? node)
{
TreeNode? rightRotate(TreeNode? node) {
TreeNode? child = node.left;
TreeNode? grandChild = child?.right;
// 以 child 为原点,将 node 向右旋转
@@ -854,8 +850,7 @@ AVL 树的特点在于「旋转 Rotation」操作,它能够在不影响二叉
```csharp title="avl_tree.cs"
/* 左旋操作 */
TreeNode? leftRotate(TreeNode? node)
{
TreeNode? leftRotate(TreeNode? node) {
TreeNode? child = node.right;
TreeNode? grandChild = child?.left;
// 以 child 为原点,将 node 向左旋转
@@ -1182,35 +1177,26 @@ AVL 树的特点在于「旋转 Rotation」操作,它能够在不影响二叉
```csharp title="avl_tree.cs"
/* 执行旋转操作,使该子树重新恢复平衡 */
TreeNode? rotate(TreeNode? node)
{
TreeNode? rotate(TreeNode? node) {
// 获取节点 node 的平衡因子
int balanceFactorInt = balanceFactor(node);
// 左偏树
if (balanceFactorInt > 1)
{
if (balanceFactor(node.left) >= 0)
{
if (balanceFactorInt > 1) {
if (balanceFactor(node.left) >= 0) {
// 右旋
return rightRotate(node);
}
else
{
} else {
// 先左旋后右旋
node.left = leftRotate(node?.left);
return rightRotate(node);
}
}
// 右偏树
if (balanceFactorInt < -1)
{
if (balanceFactor(node.right) <= 0)
{
if (balanceFactorInt < -1) {
if (balanceFactor(node.right) <= 0) {
// 左旋
return leftRotate(node);
}
else
{
} else {
// 先右旋后左旋
node.right = rightRotate(node?.right);
return leftRotate(node);
@@ -1491,14 +1477,12 @@ AVL 树的特点在于「旋转 Rotation」操作,它能够在不影响二叉
```csharp title="avl_tree.cs"
/* 插入节点 */
void insert(int val)
{
void insert(int val) {
root = insertHelper(root, val);
}
/* 递归插入节点(辅助方法) */
TreeNode? insertHelper(TreeNode? node, int val)
{
TreeNode? insertHelper(TreeNode? node, int val) {
if (node == null) return new TreeNode(val);
/* 1. 查找插入位置,并插入节点 */
if (val < node.val)
@@ -1904,24 +1888,20 @@ AVL 树的特点在于「旋转 Rotation」操作,它能够在不影响二叉
```csharp title="avl_tree.cs"
/* 删除节点 */
void remove(int val)
{
void remove(int val) {
root = removeHelper(root, val);
}
/* 递归删除节点(辅助方法) */
TreeNode? removeHelper(TreeNode? node, int val)
{
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)
{
else {
if (node.left == null || node.right == null) {
TreeNode? child = node.left != null ? node.left : node.right;
// 子节点数量 = 0 ,直接删除 node 并返回
if (child == null)
@@ -1929,13 +1909,10 @@ AVL 树的特点在于「旋转 Rotation」操作,它能够在不影响二叉
// 子节点数量 = 1 ,直接删除 node
else
node = child;
}
else
{
} else {
// 子节点数量 = 2 ,则将中序遍历的下个节点删除,并用该节点替换当前节点
TreeNode? temp = node.right;
while (temp.left != null)
{
while (temp.left != null) {
temp = temp.left;
}
node.right = removeHelper(node.right, temp.val);