跳到正文

个人记录

Abort Vanquishes Luck Tree

limingx约 15 分钟
原文

这是 2026年1月20日 存档的第 1 版,不是当前版本。查看当前版本

文章信息与版本

文章信息

收录
2026年1月20日
更新
2026年2月23日
篇幅
约 15 分钟

版本

  1. v2当前
  2. v1

翻译:运气不好导致爆零,但是这东西叫做 AVL

P1. AVL Tree\texttt{AVL~Tree} 的前置知识 - BST Tree\texttt{BST~Tree}

P1.1. BST Tree\texttt{BST~Tree} 的基础定义

二叉搜索树,本质是一种满足“左小右大”规则的二叉树:

  • 左子树里所有节点的值,都 << 当前节点的值。
  • 右子树里所有节点的值,都 >> 当前节点的值。

P1.2. BST Tree\texttt{BST~Tree} 是什么

这是一种对于查询、插入、删除操作,时间复杂度是 O(logn)O(log_n) 的树形数据结构。原因如下:

  • 排除一半子树,类似二分查找。
    • 比如要找值为 55 的节点,根节点是 33 就查右子树,是 77 就查左子树。

但是上述只是理想情况,如果读入的数据有序,则会退化成链表状,如:

代码
1
 \
  2
   \
    3
     \
      4
       \
        5

这时候查询最后一个元素 55,就得从根节点遍历到叶子,时间复杂度直接跌到 O(n)O(n),令人忍俊不禁。

P2. AVL Tree\texttt{AVL~Tree} 的原理

AVL Tree\texttt{AVL~Tree} 的核心是“自平衡”,而自平衡的关键是两个核心概念:

  • 平衡因子
  • 旋转

P2.1 平衡因子

这是一个定义概念:一棵空树,或者对于树中任意一个节点,其左子树的高度与右子树的高度差(称为平衡因子)的绝对值不超过 11 则为“平衡的”。

综上所述,合法的平衡因子只有三个值:−1、0、1-1、0、1。只要某个节点的高度差绝对值 ≥2\ge2,就说明树“失衡”了,必须通过操作修复,否则白写。

P2.2 失衡的四种情况

插入或删除节点后,树失衡的情况只有四种:

  • 左左型(LL\texttt{LL}):当前节点的高度差 =2=2,且左子节点的高度差 ≥0\ge0(左子树的左子树更高)。
  • 左右型(LR\texttt{LR}):当前节点的高度差 =−2=-2,且左子节点的高度差 =1=1(左子树的右子树更高)。
  • 右右型(RR\texttt{RR}):当前节点的高度差 =−2=-2,且右子节点的高度差 ≥0\ge0(右子树的右子树更高)。
  • 右左型(RL\texttt{RL}):当前节点的高度差 =−2=-2,且右子节点的高度差 =1=1(右子树的左子树更高)。

P2.3 360 90°旋转

旋转操作是 AVL Tree\texttt{AVL~Tree} 修复失衡的核心,本质是“通过调整节点位置,让树的高度重新满足平衡要求”,而且旋转后必须仍然遵守 BST Tree\texttt{BST~Tree} 的“左小右大”规则。

旋转分两种基础操作(右旋转、左旋转)和两种组合操作(左-右旋转、右-左旋转)。

P2.3.0 概念 - BF\texttt{BF}

BF=\texttt{BF} = 左子树高度 −- 右子树高度

P2.3.1 右旋 - LL\texttt{LL} & 左旋 - RR\texttt{RR}

核心思路是“把左子节点扶正,让当前节点变成左子节点的右子树”。假设一个“失衡的树”长这样:

代码
        A (BF=2)
       /
      B (BF=1)
     /
    C (BF=0)

执行如下操作

  1. 取 A\texttt{A} 的左子节点 B\texttt{B} 作为新的根节点。
  2. 把 B\texttt{B} 的右子树(如果有)“挂”到 A\texttt{A} 的左子树位置。
  3. 把 A\texttt{A} 挂载到 B\texttt{B} 的右子树位置。
  4. 更新 A\texttt{A} 和 B\texttt{B} 的高度(旋转后节点高度会变)。

也就是会变成:

texttt
      B (BF=0)
     / \
    C   A (BF=0)

左旋做相反动作

P2.3.2 [左右]旋 LR\texttt{LR} & [右左]旋 RL\texttt{RL}

其实就是组合罢了。

P3. AVL Tree\texttt{AVL~Tree} 的操作

A核心操作有四个:

  • 插入
  • 删除
  • 查询排名
  • 查询第 kk 大元素

所有操作都遵循“先按 BST-Tree\texttt{BST-Tree} 规则执行 → 更新节点高度和大小 → 检查失衡并修复”的三步流程。

定义基础结构体:

C++
struct node{
    int val;        // 节点存储的值
    int num;        // 重复次数(优化重复元素)
    int h;          // 节点高度
    int size;       // 以当前节点为根的子树节点总数(含重复)
    node *lchild;   // 左子节点指针
    node *rchild;   // 右子节点指针
    // 构造函数:初始化新节点
    node(int x):val(x),num(1),h(1),size(1),lchild(nullptr),rchild(nullptr){}
}*root=nullptr; // 树的根节点,初始为空

接下来按照之前的步骤即可

P4. AVL Tree\texttt{AVL~Tree} 的运用 - 实战

来吧看题:。

AC code:

C++
#include<bits/stdc++.h>
#define int long long
using namespace std;
struct node{
    int val;
    int num;
    int h;
    int size;
    node *lchild;
    node *rchild;
    node(int x):val(x),num(1),h(1),size(1),lchild(nullptr),rchild(nullptr){}
}*root=nullptr;
int getheight(node *p){
    if(p==nullptr){
        return 0;
    }
    else return p->h;
}
int getsize(node *p){
    if(p==nullptr){
        return 0;
    }
    else return p->size;
}
int getbalance(node *p){
    if(p==nullptr) return 0;
    else return getheight(p->lchild)-getheight(p->rchild);
}
void update(node *p){
    if(p==nullptr) return;
    p->h=max(getheight(p->lchild),getheight(p->rchild))+1;
    p->size=getsize(p->lchild)+getsize(p->rchild)+p->num;
}
node* rotateleft(node *p){
    node *newroot=p->rchild;
    node *tmp=newroot->lchild;
    newroot->lchild=p;
    p->rchild=tmp;
    update(p);
    update(newroot);
    return newroot;
}
node* rotateright(node *p){
    node *newroot=p->lchild;
    node *tmp=newroot->rchild;
    newroot->rchild=p;
    p->lchild=tmp;
    update(p);
    update(newroot);
    return newroot;
}
node* insert(node *p,int val){
    if(p==nullptr){
        return new node(val);
    }
    if(val<p->val){
        p->lchild=insert(p->lchild,val);
    }
    else if(val>p->val){
        p->rchild=insert(p->rchild,val);
    }
    else{
        p->num++;
        update(p);
        return p;
    }
    update(p);
    int balance=getbalance(p);
    if(balance>1&&val<p->lchild->val){
        return rotateright(p);
    }
    if(balance<-1&&val>p->rchild->val){
        return rotateleft(p);
    }
    if(balance>1&&val>p->lchild->val){
        p->lchild=rotateleft(p->lchild);
        return rotateright(p);
    }
    if(balance<-1&&val<p->rchild->val){
        p->rchild=rotateright(p->rchild);
        return rotateleft(p);
    }
    return p;
}

node* findmin(node *p){
    if(p==nullptr) return nullptr;
    while(p->lchild!=nullptr){
        p=p->lchild;
    }
    return p;
}
node* findmax(node *p){
    if(p==nullptr) return nullptr;
    while(p->rchild!=nullptr){
        p=p->rchild;
    }
    return p;
}
node* remove(node *p,int val){
    if(p==nullptr){
        return nullptr;
    }
    if(val<p->val){
        p->lchild=remove(p->lchild,val);
    }
    else if(val>p->val){
        p->rchild=remove(p->rchild,val);
    }
      else{
        if(p->num>1){
            p->num--;
            update(p);
            return p;
        }
        else{
            if(p->lchild==nullptr||p->rchild==nullptr){
                node *tmp=p->lchild?p->lchild:p->rchild;
                delete p;
                return tmp;
            }
            else{
                node *tmp=findmin(p->rchild);
                p->val=tmp->val;
                p->num=tmp->num;
                tmp->num=1;
                p->rchild=remove(p->rchild,tmp->val);
            }
        }
    }
    update(p);
    int balance=getbalance(p);
    if(balance>1&&getbalance(p->lchild)>=0){
        return rotateright(p);
    }
    if(balance>1&&getbalance(p->lchild)<0){
        p->lchild=rotateleft(p->lchild);
        return rotateright(p);
    }
    if(balance<-1&&getbalance(p->rchild)<=0){
        return rotateleft(p);
    }
    if(balance<-1&&getbalance(p->rchild)>0){
        p->rchild=rotateright(p->rchild);
        return rotateleft(p);
    }
    return p;
}
int getrank(node *p,int val){
    if(p==nullptr){
        return 1;
    }
    if(val<p->val){
        return getrank(p->lchild,val);
    }
    else if(val>p->val){
        return getsize(p->lchild)+p->num+getrank(p->rchild,val);
    }
    else{
        return getsize(p->lchild)+1;
    }
}
int getkth(node *p,int k){
    if(p==nullptr){
        return -1;
    }
    if(getsize(p->lchild)>=k){
        return getkth(p->lchild,k);
    }
    else if(getsize(p->lchild)+p->num<k){
        return getkth(p->rchild,k-getsize(p->lchild)-p->num);
    }
    else{
        return p->val;
    }
}
int getpre(node *p,int val){
    if(p==nullptr){
        return LLONG_MIN;
    }
    if(p->val>=val){
        return getpre(p->lchild,val);
    }
    else{
        int right_pre=getpre(p->rchild,val);
        return right_pre==LLONG_MIN?p->val:right_pre;
    }
}
int getsuc(node *p,int val){
    if(p==nullptr){
        return LLONG_MAX;
    }
    if(p->val<=val){
        return getsuc(p->rchild,val);
    }
    else{
        int left_suc=getsuc(p->lchild,val);
        return left_suc==LLONG_MAX?p->val:left_suc;
    }
}
signed main(){
    ios::sync_with_stdio(false);
    cin.tie(nullptr);
    int n;
    cin>>n;
    while(n--){
        int doing,x;
        cin>>doing>>x;
        if(doing==1){
            root=insert(root,x);
        }
        if(doing==2){
            root=remove(root,x);
        }
        if(doing==3){
            cout<<getrank(root,x)<<endl;
        }
        if(doing==4){
            cout<<getkth(root,x)<<endl;
        }
        if(doing==5){
            cout<<getpre(root,x)<<endl;
        }
        if(doing==6){
            cout<<getsuc(root,x)<<endl;
        }
    }
    return 0;
}