算法导论20(van Emde Boas 树)

#include<iostream>
using namespace std;

struct node
{
    int u,min,max;
    node *summary;
    node **cluster;
};

void vEBTreeCreate(node *root,int u)
{
    root->u=u;
    root->min=root->max=-1;
    if(u==2)
    {
        root->summary=NULL;
        root->cluster=NULL;
    }
    else
    {
        int v=(int)sqrt((double)u);
        root->summary=new node;
        vEBTreeCreate(root->summary,v);
        for(int i=0;i<v;++i)
        {
            root->cluster[i]=new node;
            vEBTreeCreate(root->cluster[i],v);
        }
    }
}

int vEBTreeMinimum(node *root)
{
    return root->min;
}

int vEBTreeMaximum(node *root)
{
    return root->max;
}

int high(int x,int u)
{
    return (int)floor(x/sqrt((double)u));
}

int low(int x,int u)
{
    return x%(int)sqrt((double)u);
}

int index(int x,int y,int u)
{
    return x*(int)sqrt((double)u)+y;
}

bool vEBTreeMember(node *root,int x)
{
    if(x==root->min||x==root->max)return true;
    else if(root->u==2)return false;
    else return vEBTreeMember(root->cluster[high(x,root->u)],low(x,root->u));
}

int vEBTreeSuccessor(node *root,int x)
{
    if(root->u==2)
    {
        if(x==0||root->max==1)return 1;
        else return -1;
    }
    else if(root->min!=-1&&x<root->min)return root->min;
    else
    {
        int maxLow=vEBTreeMaximum(root->cluster[high(x,root->u)]);
        if(maxLow!=-1&&low(x,root->u)<maxLow)
        {
            int offset=vEBTreeSuccessor(root->cluster[high(x,root->u)],low(x,root->u));
            return index(high(x,root->u),offset,root->u);
        }
        else
        {
            int succCluster=vEBTreeSuccessor(root->summary,high(x,root->u));
            if(succCluster==-1)return -1;
            else
            {
                int offset=vEBTreeMinimum(root->cluster[succCluster]);
                return index(succCluster,offset,root->u);
            }
        }
    }
}

int vEBTreePredecessor(node *root,int x)
{
    if(root->u==2)
    {
        if(x==0||root->min==0)return 0;
        else return -1;
    }
    else if(root->max!=-1&&x>root->max)return root->max;
    else
    {
        int minLow=vEBTreeMinimum(root->cluster[high(x,root->u)]);
        if(minLow!=-1&&low(x,root->u)>minLow)
        {
            int offset=vEBTreePredecessor(root->cluster[high(x,root->u)],low(x,root->u));
            return index(high(x,root->u),offset,root->u);
        }
        else
        {
            int predCluster=vEBTreePredecessor(root->summary,high(x,root->u));
            if(predCluster==-1)
            {
                if(root->min!=-1&&x>root->min)return root->min;
                else return -1;
            }
            else
            {
                int offset=vEBTreeMaximum(root->cluster[predCluster]);
                return index(predCluster,offset,root->u);
            }
        }
    }
}

void vEBEmptyTreeInsert(node *root,int x)
{
    root->min=root->max=x;
}

void vEBTreeInsert(node *root,int x)
{
    if(root->min==-1)vEBEmptyTreeInsert(root,x);
    else 
    {
        if(x<root->min)
        {
            int tmp=x;
            x=root->min;
            root->min=tmp;
        }
        if(root->u>2)
        {
            if(vEBTreeMinimum(root->cluster[high(x,root->u)]))
            {
                vEBTreeInsert(root->summary,high(x,root->u));
                vEBEmptyTreeInsert(root->cluster[high(x,root->u)],low(x,root->u));
            }
            else vEBTreeInsert(root->cluster[high(x,root->u)],low(x,root->u));
        }
        if(x>root->max)root->max=x;
    }
}

void vEBTreeDelete(node *root,int x)
{
    if(root->min==root->max)root->min=root->max=-1;
    else if(root->u==2)
    {
        if(x==0)root->min=1;
        else root->min=0;
        root->max=root->min;
    }
    else if(x==root->min)
    {
        int firstCluster=vEBTreeMinimum(root->summary);
        x=index(firstCluster,vEBTreeMinimum(root->cluster[firstCluster]),root->u);
        root->min=x;
        vEBTreeDelete(root->cluster[high(x,root->u)],low(x,root->u));
        if(vEBTreeMinimum(root->cluster[high(x,root->u)])==-1)
        {
            vEBTreeDelete(root->summary,high(x,root->u));
            if(x==root->max)
            {
                int summaryMax=vEBTreeMaximum(root->summary);
                if(summaryMax==-1)root->max=root->min;
                else root->max=index(summaryMax,vEBTreeMaximum(root->cluster[summaryMax]),root->u);
            }
        }
        else if(x==root->max)root->max=index(high(x,root->u),vEBTreeMaximum(root->cluster[high(x,root->u)]),root->u);
    }
}

int main()
{
    node *root=new node;
    vEBTreeCreate(root,16);
    return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值