1. 程式人生 > >演算法導論20(van Emde Boas 樹)

演算法導論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; }