替罪羊树——平衡二叉树

  替罪羊树是一种优雅的暴力,它通过设立\large \alpha值来使得总体的时间复杂度趋向于O(Nlog(N))

  替罪羊树是一棵平衡二叉树,但是众所周知,如果我们按照一棵二叉搜索树BST的道理来直接插入或者删除点的话,很容易使得它不够平衡,变得瘦瘦高高的,我们不喜欢这种瘦瘦高高,更喜欢平摊高度的矮矮胖胖。如何平摊高度,这是替罪羊树要处理的东西。

暴力重建!

  有多暴力?直接把这棵子树拉出来,利用O(N)的方法,直接全体重建,所以如果贪婪的每次一插入点就需要重建的话,复杂度自然是O(N^2)的,于是估摸出一个数,给它取个好听的名字——\large {\color{Red} \alpha}便应运而生了,它是平衡因子。

所谓平衡?

  什么时候是不平衡的,我们定义二叉树有左右子结点,如果说左右子树的有效点的权重任一存在大于了目前子树的有效点的权重乘以\large \alpha,那么,我们说它不平衡,把它拉出来重建!

有效点?

  什么点是有效的?自然是存在的点,那为什么会有无效的点?因为删除操作,我们不能直接把点删除掉,因为会影响到它的后继点的继承,所以我们会偷懒,如何偷懒?直接给它打上删除标记,然后不管它了。所以,子树中可能存在这样的名存实亡的无效点。

  那么,如果无效点多了,那么如果搜索的时候碰到了很多的无效点,自然浪费的很!所以当无效点大于了有效点的30%或者40%的时候,我们可以再把这棵子树拉出来重建,因为重建是只对有效点的重建。


  大致上我们已经捋清了整体的框架,接下去就是讲一些细节上的,代码上的操作了。

插入操作

void add(int x)
{
    if(!root)
    {
        build(x, root = kk(), 0);
        return;
    }
    int p = fid(x, root);
    if(x == tree[p].x)
    {
        tree[p].sum++;
        if(tree[p].tf) { tree[p].tf = false; updata(p, 1, 0, 1); }
        else updata(p, 0, 0, 1);
    }
    else if(x < tree[p].x) { build(x, tree[p].lc = kk(), p); updata(p, 1, 1, 1); }
    else { build(x, tree[p].rc = kk(), p); updata(p, 1, 1, 1); }
    find_rebuild(root, x);
}

如果现在是一棵空树,那么root不存在,此时直接给开个新树。

否则,我们需要找到值x需要插入的位置,标明它需要插在哪个结点的下面,这是基础的二叉搜索树的操作。

build建立新点操作

void build(int x,int y,int fa)
{
    tree[y].lc = tree[y].rc = 0; tree[y].fa = fa;
    tree[y].tf = false;
    tree[y].x = x;
    tree[y].sum = tree[y].siz = tree[y].trsize = tree[y].whsize = 1;
}

Find,找到应当插入的位置

int fid(int x, int now)
{
    if(x < tree[now].x && tree[now].lc) return fid(x, tree[now].lc);
    if(x > tree[now].x && tree[now].rc) return fid(x, tree[now].rc);
    return now;
}

删除操作

void del(int x)
{
    int p = fid(x, root);
    tree[p].sum--;
    if(!tree[p].sum) { tree[p].tf = true; updata(p, -1, 0, -1); }
    else updata(p, 0, 0, -1);
    find_rebuild(root, x);
}

我们会发现,插入和删除两个操作里面都有一个神奇的find_rebuild,这到底是个什么玩意儿呢?

find_rebuild找到重建点,并重建子树

void find_rebuild(int now,int x)
{
    if(1. * tree[tree[now].lc].siz > 1. * tree[now].siz * alpha ||
       1. * tree[tree[now].rc].siz > 1. * tree[now].siz * alpha ||
       1. * (tree[now].siz - tree[now].trsize) > 0.4 * tree[now].siz) { rebuild(now); return; }
    if(tree[now].x ^ x) find_rebuild(x < tree[now].x ? tree[now].lc : tree[now].rc, x);
}

  这个就是我们上面说到的重建的规则,如果子树的有效点的数量大于了总数的数量乘以\large \alpha,那么就需要重建,如果无效点的数量大于了总有效点的40%也是需要重建的。

rebuild重建操作

void rebuild(int x)
{
    tt = 0;
    dfs_rebuild(x);
    if(x == root) root = readd(1, tt, 0);
    else
    {
        updata(tree[x].fa, 0, -(tree[x].siz - tree[x].trsize), 0);
        if(tree[tree[x].fa].lc == x)tree[tree[x].fa].lc = readd(1, tt, tree[x].fa);
        else tree[tree[x].fa].rc = readd(1, tt, tree[x].fa);
    }
}

readd建树操作(典型分治思想)

int readd(int l,int r,int fa)
{
    if(l > r)return 0;
    int mid = HalF;
    int id = kk();
    tree[id].fa = fa;
    tree[id].sum = shulie[mid].sum;
    tree[id].x = shulie[mid].x;
    tree[id].lc = readd(l, mid - 1, id);
    tree[id].rc = readd(mid + 1, r, id);
    tree[id].whsize = tree[tree[id].lc].whsize + tree[tree[id].rc].whsize + shulie[mid].sum;
    tree[id].siz = tree[id].trsize = r - l + 1;
    tree[id].tf = false;
    return id;
}

中序遍历(得到需要重建子树的所有结点、升序)

void dfs_rebuild(int x)
{
    if(!x)return;
    dfs_rebuild(tree[x].lc);
    if(!tree[x].tf) { shulie[++tt].x = tree[x].x; shulie[tt].sum = tree[x].sum; }
    ck[++t] = x;
    dfs_rebuild(tree[x].rc);
}

更新操作(pushup)

  每次删除点或者是加入点,都会影响到根到该点链上的所有的点,我们还需要更新他们的值。

void updata(int x, int y, int z, int k)
{
    if(!x)return;
    tree[x].trsize += y;
    tree[x].siz += z;
    tree[x].whsize += k;
    updata(tree[x].fa, y, z, k);
}

节约空间、回收再利用

inline int kk()
{
    if(t > 0) return ck[t--];
    else return ++len;
}

定义的变量名讲解

  • lc、rc:左右儿子
  • x:值
  • sum:值为x的点的个数
  • siz:以它为根的子树有多少个结点
  • trsize:以它为根的子树有多少个有效结点
  • whsize:以它为根的结点有多少个数(所有的sum之和)
  • fa:父亲结点
  • tf(bool类型):删除标记
  • ck[ ]:回收站,回收被删除的点
  • t:记录有多少点被回收了

例题 普通平衡树

#include <iostream>
#include <cstdio>
#include <cmath>
#include <string>
#include <cstring>
#include <algorithm>
#include <limits>
#include <vector>
#include <stack>
#include <queue>
#include <set>
#include <map>
#include <bitset>
//#include <unordered_map>
//#include <unordered_set>
#define lowbit(x) ( x&(-x) )
#define pi 3.141592653589793
#define e 2.718281828459045
#define INF 0x3f3f3f3f
#define eps 1e-8
#define HalF (l + r)>>1
#define lsn rt<<1
#define rsn rt<<1|1
#define Lson lsn, l, mid
#define Rson rsn, mid+1, r
#define QL Lson, ql, qr
#define QR Rson, ql, qr
#define myself rt, l, r
#define MP(a, b) make_pair(a, b)
using namespace std;
typedef unsigned long long ull;
typedef unsigned int uit;
typedef long long ll;
const int maxN = 1e5 + 7;
const double alpha = 0.75;
struct node
{
    int lc, rc, x, sum, siz, trsize, whsize, fa;
    bool tf;
    node() { lc = rc = x = sum = siz = trsize = whsize = fa = 0; tf = false; }
} tree[maxN];
int len = 0, N, root = 0;
int ck[maxN], t = 0;
void build(int x,int y,int fa)
{
    tree[y].lc = tree[y].rc = 0; tree[y].fa = fa;
    tree[y].tf = false;
    tree[y].x = x;
    tree[y].sum = tree[y].siz = tree[y].trsize = tree[y].whsize = 1;
}
inline int kk()
{
    if(t > 0) return ck[t--];
    else return ++len;
}
void updata(int x, int y, int z, int k)
{
    if(!x)return;
    tree[x].trsize += y;
    tree[x].siz += z;
    tree[x].whsize += k;
    updata(tree[x].fa, y, z, k);
}
int fid(int x, int now)
{
    if(x < tree[now].x && tree[now].lc) return fid(x, tree[now].lc);
    if(x > tree[now].x && tree[now].rc) return fid(x, tree[now].rc);
    return now;
}
struct sl
{
    int x, sum;
    sl(int a=0, int b=0):x(a), sum(b) {}
} shulie[maxN];
int tt;
void dfs_rebuild(int x)
{
    if(!x)return;
    dfs_rebuild(tree[x].lc);
    if(!tree[x].tf) { shulie[++tt].x = tree[x].x; shulie[tt].sum = tree[x].sum; }
    ck[++t] = x;
    dfs_rebuild(tree[x].rc);
}
int readd(int l,int r,int fa)
{
    if(l > r)return 0;
    int mid = HalF;
    int id = kk();
    tree[id].fa = fa;
    tree[id].sum = shulie[mid].sum;
    tree[id].x = shulie[mid].x;
    tree[id].lc = readd(l, mid - 1, id);
    tree[id].rc = readd(mid + 1, r, id);
    tree[id].whsize = tree[tree[id].lc].whsize + tree[tree[id].rc].whsize + shulie[mid].sum;
    tree[id].siz = tree[id].trsize = r - l + 1;
    tree[id].tf = false;
    return id;
}
void rebuild(int x)
{
    tt = 0;
    dfs_rebuild(x);
    if(x == root) root = readd(1, tt, 0);
    else
    {
        updata(tree[x].fa, 0, -(tree[x].siz - tree[x].trsize), 0);
        if(tree[tree[x].fa].lc == x)tree[tree[x].fa].lc = readd(1, tt, tree[x].fa);
        else tree[tree[x].fa].rc = readd(1, tt, tree[x].fa);
    }
}
void find_rebuild(int now,int x)
{
    if(1. * tree[tree[now].lc].siz > 1. * tree[now].siz * alpha ||
       1. * tree[tree[now].rc].siz > 1. * tree[now].siz * alpha ||
       1. * (tree[now].siz - tree[now].trsize) > 0.4 * tree[now].siz) { rebuild(now); return; }
    if(tree[now].x ^ x) find_rebuild(x < tree[now].x ? tree[now].lc : tree[now].rc, x);
}
void add(int x)
{
    if(!root)
    {
        build(x, root = kk(), 0);
        return;
    }
    int p = fid(x, root);
    if(x == tree[p].x)
    {
        tree[p].sum++;
        if(tree[p].tf) { tree[p].tf = false; updata(p, 1, 0, 1); }
        else updata(p, 0, 0, 1);
    }
    else if(x < tree[p].x) { build(x, tree[p].lc = kk(), p); updata(p, 1, 1, 1); }
    else { build(x, tree[p].rc = kk(), p); updata(p, 1, 1, 1); }
    find_rebuild(root, x);
}
void del(int x)
{
    int p = fid(x, root);
    tree[p].sum--;
    if(!tree[p].sum) { tree[p].tf = true; updata(p, -1, 0, -1); }
    else updata(p, 0, 0, -1);
    find_rebuild(root, x);
}
int fid_rank(int x)
{
    int ans = 0, now = root;
    while(now && (tree[now].x ^ x))
    {
        if(x < tree[now].x) now = tree[now].lc;
        else
        {
            ans += tree[tree[now].lc].whsize + tree[now].sum;
            now = tree[now].rc;
        }
    }
    ans += tree[tree[now].lc].whsize;
    return ans + 1;
}
void rank_to_X(int kth)
{
    int now = root;
    while(true)
    {
        if(kth <= tree[tree[now].lc].whsize) now = tree[now].lc;
        else if(kth > tree[tree[now].lc].whsize + tree[now].sum)
        {
            kth -= tree[tree[now].lc].whsize + tree[now].sum;
            now = tree[now].rc;
        }
        else { printf("%d\n", tree[now].x); return; }
    }
}
int main()
{
    scanf("%d", &N);
    for(int i=1, op, x; i<=N; i++)
    {
        scanf("%d%d", &op, &x);
        if(op == 1) add(x);
        else if(op == 2) del(x);
        else if(op == 3) printf("%d\n", fid_rank(x));
        else if(op == 4) rank_to_X(x);
        else if(op == 5) rank_to_X(fid_rank(x) - 1);
        else rank_to_X(fid_rank(x + 1));
    }
    return 0;
}
发布了884 篇原创文章 · 获赞 1057 · 访问量 12万+

猜你喜欢

转载自blog.csdn.net/qq_41730082/article/details/105111852