替罪羊树学习笔记

替罪羊树基于一种暴力重构的操作来保证平衡,具体来说,就是定义一个平衡因子alphaalpha ,当某个节点x的某棵子树的x.ch.size>x.size*alphax.ch.size>x.sizealpha *时便将这棵以x为根的子树拍扁重构。

替罪羊树的基本操作和普通二叉树差不多的,神奇的就在于它的拍扁重构。

假如这有一棵树(你tm告诉我这是树?)


虽然不知道它是咋长成这样的,但是显然这种结构是要维护的。

于是,我们把它拍扁重构!

完事以后树的形态变成了这样


这就是一颗很完美的二叉查找树了。可以理解为,当当前满足一定条件时就以它为跟将它的左右子树重新建一颗平衡的二叉查找树。那么,这个条件是什么呢?

正如开头所说,我们可以定义一个常数a(一般为0.7),当当前节点大小小于某个子节点与a的乘积时,就把树拍扁重构,这个a的取值可以人为控制,但太大了容易造成二叉树左右节点不平衡而影响查询效率,太小了将进行过多的拍扁重构也会影响效率,因此一般a取0.7。

基本操作:

check判断当前点是否合法:

int check(int p)
{
    if(a[p].w==0)
        return 0;
    if ((a[a[p].lc].s+a[a[p].lc].w>=double(alpha*(a[p].w+a[p].s)))||
        (a[a[p].rc].s+a[a[p].rc].w>=double(alpha*(a[p].w+a[p].s))))return 1;
    return 0;
}

查找节点

int getN(int x)
{
    int now=root;
    while(a[now].n!=x&&a[now].s!=0)
    {
        if(a[now].n>=x)
	    now=a[now].lc;
        else now=a[now].rc;
    }
    return now;
}

拍扁重构:先dfs一遍,找出每个节点在新树中的编号,并将相关信息储存。

void dfs(int p)
{
    h[++top]=p;
    if(a[p].lc!=0)
	dfs(a[p].lc);
    if(a[p].w!=0)
	d[++tott].n=a[p].n,
    d[tott].w=a[p].w;
    a[p].s=0;
    if(a[p].rc!=0)
	dfs(a[p].rc);
}

可以看出,这是按从左往右的顺序遍历的,这样就可以保证新树的大小顺序和原树是一样的。之后修改根节点的信息,并从根节点向下修改其子节点的信息。

代码:

void make(int l,int r,int p,int fa)
{
    int mid=(l+r)/2;
    a[p].fa=fa;
    a[p].w=d[mid].w;
    a[p].n=d[mid].n;
    if(mid-1>=l)
	a[p].lc=getnewp(),make(l,mid-1,a[p].lc,p);
    else a[p].lc=0;
    if(mid+1<=r)
	a[p].rc=getnewp(),make(mid+1,r,a[p].rc,p);
    else a[p].rc=0;
    a[p].s=a[a[p].lc].w+a[a[p].lc].s+a[a[p].rc].w+a[a[p].rc].s;
}
void rebuild(int p)
{
    if(p==0)
	return;
    tott=0;
    dfs(p);
    int now=getnewp();
    a[now].fa=a[p].fa;
    if(p==root)	
	root=now;
    int mid=(1+tott)/2;
    if(d[mid].n>a[a[p].fa].n)	
	a[a[p].fa].rc=now;
    else a[a[p].fa].lc=now;
    make(1,tott,now,a[p].fa);
}

插入:替罪羊树的插入和一般二叉查找树相同,但当操作完成后,往上回溯时要依次判断每个节点是否合法,记录离根节点最近的一个点并拍扁重构,注意把插入结点到根的路径上所有点的子树大小加1。

代码:

void insr(int x,bool rb)
{
    if(root==0)
    {
        root=1;
        a[1].n=x;
        a[1].w++;
        return;
    }
    int now=root;
    while(a[now].s!=0)
    {
        if(x==a[now].n)
        {
            a[now].w++;
            return;
        }
        if(x>a[now].n&&a[now].rc==0)
            break;
        if(x<a[now].n&&a[now].lc==0)
            break;
        a[now].s++;
        if(x>a[now].n)
            now=a[now].rc;
        else if(x<a[now].n)
            now=a[now].lc;
    }
    if(x==a[now].n)
    {
        a[now].w++;
        return;
    }
    a[now].s++;
    int tmp=now;
    if(x>a[now].n)
        now=a[now].rc=++tot;
    else if(x<a[now].n)
        now=a[now].lc=++tot;
    a[now].w++;
    a[now].n=x;
    if(tmp!=now)
        a[now].fa=tmp;
    int chk=0;
    while(now!=root)
    {
        now=a[now].fa;
        if(check(now))
            chk=now;
    }
    if(rb)
        rebuild(chk);
}

删除:删除操作基本没啥技术含量,找到要删的点把他的个数减1就好了,注意还要把它到跟上的路径所有点的子树大小减1。

代码:

void delt(int p)
{
    a[p].w--;
    int chk=0;
    while(p!=root)
    {
        p=a[p].fa;
        a[p].s--;
    }
}

查找某一节点的排名:找到该点在序列中的编号并往上跳,如果改点为父亲节点的右儿子,直接加上父亲结点的左儿子的大小和个数,注意查到根节点时要把答案加1(想想为什么)。

代码:

int XgetRk(int x)
{
    int p=getN(x),ans;
    ans=a[a[p].lc].s+a[a[p].lc].w;
    while(p!=root)
	{
        if(a[a[p].fa].rc==p)
			ans+=a[a[a[p].fa].lc].s+a[a[a[p].fa].lc].w+a[a[p].fa].w;
        p=a[p].fa;
    }
    return ans+1;
}

查找某节点的值:跟正常操作一样,直接上代码。

代码:

int RkgetX(int x)
{
    int now=root;
    while(true)
	{
        int lcS=a[a[now].lc].s+a[a[now].lc].w;
        if(x<=lcS)now=a[now].lc;
        else if(x>lcS&&x<=a[now].w+lcS)
			return a[now].n;
        else x-=lcS+a[now].w,now=a[now].rc;
        if(x==0)
			return a[now].fa;
    }
}

查前驱后继:这个操作比较神奇。以前驱为例,先插入一个值为x的节点,查找他的排名tmp,再把这个节点删了,查找tmp-1点的值。

代码:

int suc(int x)
{
    insr(x+1,0);
    int tmp=XgetRk(x+1),nx=getN(x+1);
    delt(nx);
    return RkgetX(tmp);
}
int pre(int x)
{
    insr(x,0);
    int tmp=XgetRk(x);
    delt(getN(x));
    return RkgetX(tmp-1);
}

整体代码(洛谷3369)

#include<iostream>
#include<cstdio>
#define alpha 0.7
using namespace std;
struct ScapeGoatTree{
    int lc,rc,n,w,s,fa;
}a[100010];
struct REbuild{
    int n,w;
}d[100010];
void rebuild(int p);
int tot=1,tott=0,top=0,h[100010],n,T,X,root;
int check(int p)
{
    if(a[p].w==0)
		return 0;
    if ((a[a[p].lc].s+a[a[p].lc].w>=double(alpha*(a[p].w+a[p].s)))||
        (a[a[p].rc].s+a[a[p].rc].w>=double(alpha*(a[p].w+a[p].s))))return 1;
    return 0;
}
int getN(int x)
{
    int now=root;
    while(a[now].n!=x&&a[now].s!=0)
	{
        if(a[now].n>=x)
			now=a[now].lc;
        else now=a[now].rc;
    }
    return now;
}
void insr(int x,bool rb)
{
    if(root==0)
	{
        root=1;
        a[1].n=x;
        a[1].w++;
        return;
    }
    int now=root;
    while(a[now].s!=0)
	{
        if(x==a[now].n)
		{
            a[now].w++;
            return;
        }
        if(x>a[now].n&&a[now].rc==0)break;
        if(x<a[now].n&&a[now].lc==0)break;
        a[now].s++;
        if(x>a[now].n)now=a[now].rc;
        else if(x<a[now].n)now=a[now].lc;
    }

    if(x==a[now].n)
	{
        a[now].w++;
        return;
    }
    a[now].s++;
    int tmp=now;
    if(x>a[now].n)
		now=a[now].rc=++tot;
    else if(x<a[now].n)
		now=a[now].lc=++tot;
    a[now].w++;
    a[now].n=x;
    if(tmp!=now)
		a[now].fa=tmp;
    int chk=0;
    while(now!=root)
	{
        now=a[now].fa;
        if(check(now))
			chk=now;
    }
    if(rb)
		rebuild(chk);
}
int getnewp()
{
    if(top>0)
	{
        top--;
        return h[top+1];
    }
    else return ++tot;
}
void dfs(int p)
{
    h[++top]=p;
    if(a[p].lc!=0)
		dfs(a[p].lc);
    if(a[p].w!=0)
		d[++tott].n=a[p].n,
    d[tott].w=a[p].w;
    a[p].s=0;
    if(a[p].rc!=0)
		dfs(a[p].rc);
}
void make(int l,int r,int p,int fa)
{
    int mid=(l+r)/2;
    a[p].fa=fa;
    a[p].w=d[mid].w;
    a[p].n=d[mid].n;
    if(mid-1>=l)
		a[p].lc=getnewp(),make(l,mid-1,a[p].lc,p);
	else a[p].lc=0;
    if(mid+1<=r)
		a[p].rc=getnewp(),make(mid+1,r,a[p].rc,p);
	else a[p].rc=0;
    a[p].s=a[a[p].lc].w+a[a[p].lc].s+a[a[p].rc].w+a[a[p].rc].s;
}
void rebuild(int p)
{
    if(p==0)
		return;
    tott=0;
    dfs(p);
    int now=getnewp();
    a[now].fa=a[p].fa;
    if(p==root)	
		root=now;
    int mid=(1+tott)/2;
    if(d[mid].n>a[a[p].fa].n)	
		a[a[p].fa].rc=now;
    else a[a[p].fa].lc=now;
    make(1,tott,now,a[p].fa);
}
void delt(int p)
{
    a[p].w--;
    int chk=0;
    while(p!=root)
	{
        p=a[p].fa;
        a[p].s--;
    }
}
int XgetRk(int x);
int RkgetX(int x);
int suc(int x)
{
    insr(x+1,0);
    int tmp=XgetRk(x+1),nx=getN(x+1);
    delt(nx);
    return RkgetX(tmp);
}
int pre(int x)
{
    insr(x,0);
    int tmp=XgetRk(x);
    delt(getN(x));
    return RkgetX(tmp-1);
}
int XgetRk(int x)
{
    int p=getN(x),ans;
    ans=a[a[p].lc].s+a[a[p].lc].w;
    while(p!=root)
	{
        if(a[a[p].fa].rc==p)
			ans+=a[a[a[p].fa].lc].s+a[a[a[p].fa].lc].w+a[a[p].fa].w;
        p=a[p].fa;
    }
    return ans+1;
}
int RkgetX(int x)
{
    int now=root;
    while(true)
	{
        int lcS=a[a[now].lc].s+a[a[now].lc].w;
        if(x<=lcS)now=a[now].lc;
        else if(x>lcS&&x<=a[now].w+lcS)
			return a[now].n;
        else x-=lcS+a[now].w,now=a[now].rc;
        if(x==0)
			return a[now].fa;
    }
}
int main(){
    scanf("%d",&n);
    for(int i=1;i<=n;i++)
	{
        if(i%1000==0)
		{
            int ttott;
            ttott++;
        }
        scanf("%d%d",&T,&X);                                                        
        if(T==1)insr(X,1);  
        else if(T==2)delt(getN(X));
        else if(T==3)printf("%d\n",XgetRk(X));
        else if(T==4)printf("%d\n",RkgetX(X));
        else if(T==5)printf("%d\n",pre(X));
        else if(T==6)printf("%d\n",suc(X)); 
    }   
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
好的,以下是用 C++ 实现替罪羊的代码。替罪羊是一种平衡二叉,可以支持插入、删除、查找等操作,并且可以在插入或删除节点时自动平衡的结构。 ```C++ #include <iostream> #include <algorithm> using namespace std; template <typename T> struct TreeNode { T val; int size; TreeNode<T> *left, *right, *parent; TreeNode(T val = 0, TreeNode<T> *parent = nullptr, TreeNode<T> *left = nullptr, TreeNode<T> *right = nullptr, int size = 1) : val(val), parent(parent), left(left), right(right), size(size) {} }; // 计算节点的大小 template <typename T> int getSize(TreeNode<T>* node) { return node ? node->size : 0; } // 更新节点的大小 template <typename T> void updateSize(TreeNode<T>* node) { if (node) { node->size = 1 + getSize(node->left) + getSize(node->right); } } // 向左旋转 template <typename T> void rotateLeft(TreeNode<T>* node) { TreeNode<T>* temp = node->right; if (temp) { node->right = temp->left; if (temp->left) { temp->left->parent = node; } temp->parent = node->parent; if (!node->parent) { node->parent = temp; } else if (node == node->parent->left) { node->parent->left = temp; } else { node->parent->right = temp; } temp->left = node; node->parent = temp; updateSize(node); updateSize(temp); } } // 向右旋转 template <typename T> void rotateRight(TreeNode<T>* node) { TreeNode<T>* temp = node->left; if (temp) { node->left = temp->right; if (temp->right) { temp->right->parent = node; } temp->parent = node->parent; if (!node->parent) { node->parent = temp; } else if (node == node->parent->right) { node->parent->right = temp; } else { node->parent->left = temp; } temp->right = node; node->parent = temp; updateSize(node); updateSize(temp); } } // 插入节点 template <typename T> TreeNode<T>* insert(TreeNode<T>* root, T val) { if (!root) { return new TreeNode<T>(val); } if (val < root->val) { root->left = insert(root->left, val); root->left->parent = root; } else { root->right = insert(root->right, val); root->right->parent = root; } updateSize(root); int lSize = getSize(root->left); int rSize = getSize(root->right); if (lSize > 2 * rSize || rSize > 2 * lSize) { int size = lSize + rSize + 1; TreeNode<T>* node = root; TreeNode<T>* parent = nullptr; while (node) { if (2 * getSize(node->left) < size && 2 * getSize(node->right) < size) { break; } parent = node; if (getSize(node->left) > getSize(node->right)) { node = node->left; } else { node = node->right; } } if (!parent) { root = node; } else if (node == parent->left) { parent->left = node; } else { parent->right = node; } node->parent = parent; while (node != root) { node = node->parent; updateSize(node); } } return root; } // 删除节点 template <typename T> TreeNode<T>* remove(TreeNode<T>* root, T val) { TreeNode<T>* node = root; while (node && node->val != val) { if (val < node->val) { node = node->left; } else { node = node->right; } } if (!node) { return root; } if (node->left && node->right) { TreeNode<T>* temp = node->right; while (temp->left) { temp = temp->left; } node->val = temp->val; node = temp; } TreeNode<T>* parent = node->parent; TreeNode<T>* child = node->left ? node->left : node->right; if (child) { child->parent = parent; } if (!parent) { root = child; } else if (node == parent->left) { parent->left = child; } else { parent->right = child; } while (parent) { updateSize(parent); int lSize = getSize(parent->left); int rSize = getSize(parent->right); if (lSize > 2 * rSize || rSize > 2 * lSize) { int size = lSize + rSize + 1; node = parent; parent = node->parent; if (!parent) { root = node; } else if (node == parent->left) { parent->left = node; } else { parent->right = node; } node->parent = parent; while (node != root) { node = node->parent; updateSize(node); } break; } node = parent; parent = node->parent; } delete node; return root; } // 查找节点 template <typename T> TreeNode<T>* find(TreeNode<T>* root, T val) { TreeNode<T>* node = root; while (node && node->val != val) { if (val < node->val) { node = node->left; } else { node = node->right; } } return node; } // 中序遍历 template <typename T> void inorder(TreeNode<T>* root) { if (root) { inorder(root->left); cout << root->val << " "; inorder(root->right); } } int main() { TreeNode<int>* root = nullptr; root = insert(root, 5); root = insert(root, 3); root = insert(root, 7); root = insert(root, 2); root = insert(root, 4); root = insert(root, 6); root = insert(root, 8); cout << "Inorder traversal of the constructed tree: "; inorder(root); cout << endl; root = remove(root, 3); root = remove(root, 6); cout << "Inorder traversal after deletion of 3 and 6: "; inorder(root); cout << endl; return 0; } ``` 在上面的代码中,我们使用了模板来支持不同类型的节点值。我们首先定义了一个 TreeNode 结构体,其中包含节点的值、大小、左子、右子和父节点等信息。然后我们实现了一些辅助函数,如 getSize、updateSize、rotateLeft 和 rotateRight 等函数,这些函数可以帮助我们更新节点的信息并且实现了左旋和右旋操作。 接下来,我们实现了 insert、remove 和 find 等操作。insert 操作用于插入节点,如果的不平衡程度超过了阈值,我们就需要进行重构操作。remove 操作用于删除节点,如果删除节点后的不平衡程度超过了阈值,我们也需要进行重构操作。find 操作用于查找节点,返回节点的指针。 最后,我们实现了一个简单的测试用例,插入一些节点并删除一些节点,最后输出中序遍历的结果。 希望这份代码可以帮助你更好地理解和实现替罪羊

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值