算法导论Van Emde Boas树

#include<iostream>
#include<math.h>
#define NIL 9999
#define MAX 100
using namespace std;

struct Van_Emde_Boas {//veb树节点 
    int u;
	int min; 
	int max;
	Van_Emde_Boas *summary=NULL;
	Van_Emde_Boas *cluster[MAX];
	
};

//创建vbn树 
void vEB_TREE_CREATE(Van_Emde_Boas *V,int u){
	V->u=u;
	int min=NIL; 
	int max=NIL;
	for(int j=0;j<MAX;j++){
		V->cluster[j]=NULL;
	}
	if(u!=2) {
		int su=(int)sqrt((double)u);
        V->summary=new Van_Emde_Boas;
        vEB_TREE_CREATE(V->summary,su);
        for(int i=0;i<su;i++) {
            V->cluster[i]=new Van_Emde_Boas;
            vEB_TREE_CREATE(V->cluster[i],su);
        }
	}
} 

//交换函数
void swap(int *a,int *b) {
	int temp=*a;
	*a=*b;
	*b=temp;
} 

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

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

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

//返回vbn树中的最小值 
int vEB_TREE_MINIMUM(Van_Emde_Boas *V) {
	return V->min;
}

//返回vbn树中的最大值 
int vEB_TREE_MAXIMUM(Van_Emde_Boas *V) {
	return V->max;
}

//判断一个值是否在集合中
bool vEB_TREE_MEMBER(Van_Emde_Boas *V,int x,int u) {
	if(x==V->min || x==V->max) {
		return true;
	} else {
		if(V->u==2){
			return false;
		} else {
			return vEB_TREE_MEMBER(V->cluster[high(x,u)],low(x,u),u);
		}
	}
}

//查找后继
int vEB_TREE_SUCCESSOR(Van_Emde_Boas *V,int x,int u) {
	if(V->u==2) {
		if(x==0 && V->max==1) {
			return 1;
		} else {
			return NIL;
		}
	} else {
		if(V->min!=NIL && x<V->min) {
			return V->min;
		} else {
			int max_low=vEB_TREE_MAXIMUM(V->cluster[high(x,u)]);
			if(max_low!=NIL && low(x,u)<max_low) {
				int offset=vEB_TREE_SUCCESSOR(V->cluster[high(x,u)],low(x,u),u);
				return index(high(x,u),offset,u);
			} else {
				int succ_cluster=vEB_TREE_SUCCESSOR(V->summary,high(x,u),u);
				if(succ_cluster==NIL) {
					return NIL;
				} else {
					int offset=vEB_TREE_MINIMUM(V->cluster[succ_cluster]);
					return index(succ_cluster,offset,u);
				}
			}
		}
	}
}


//查找前驱 
int vEB_TREE_PREDECESSOR(Van_Emde_Boas *V,int x,int u) {
	if(V->u==2) {
		if(x==1 && V->min==0) {
			return 0;
		} else {
			return NIL;
		}
	} else {
		if(V->max!=NIL && x>V->max) {
			return V->max;
		} else {
			int min_low=vEB_TREE_MINIMUM(V->cluster[high(x,u)]);
			if(min_low!=NIL && low(x,u)>min_low) {
				int offset=vEB_TREE_PREDECESSOR(V->cluster[high(x,u)],low(x,u),u);
				return index(high(x,u),offset,u);
			} else {
				int pred_cluster=vEB_TREE_PREDECESSOR(V->summary,high(x,u),u);
				if(pred_cluster==NIL) {
					if(V->min!=NIL &&x>V->max) {
						return V->min;
					} else {
						return NIL;	
					}
				} else {
					int offset=vEB_TREE_MINIMUM(V->cluster[pred_cluster]);
					return index(pred_cluster,offset,u);
				}
			}
		}
	}
}

//插入一个空树

void vEB_EMPTY_TREE_INSERT(Van_Emde_Boas *V,int x) {
	V->min=x;
	V->max=x;
} 

//插入一个 元素
void vEB_TREE_INSERT(Van_Emde_Boas *V,int x,int u) {
	if(V->min==NIL) {
		vEB_EMPTY_TREE_INSERT(V,x);
	} else {
		//第一种情况 
		if(x<V->min) {
			swap(&x,&V->min);
		}
		
		//第二种情况
		if(V->u>2) {
			if(vEB_TREE_MINIMUM(V->cluster[high(x,u)])==NIL) {
				vEB_TREE_INSERT(V->summary,high(x,u),u);
				vEB_EMPTY_TREE_INSERT(V->cluster[high(x,u)],low(x,u));
			} else {
				vEB_TREE_INSERT(V->cluster[high(x,u)],low(x,u),u);
			}
		} 
		
		//第三种情况
		if(x>V->max) {
			V->max=x;
		}
	}
} 

//删除一个元素
void vEB_TREE_DELETE(Van_Emde_Boas *V,int x,int u) {
	if(V->min==V->max) {
		V->min=NIL;
		V->max=NIL;
	} else {
		if(V->u==2) {
			if(x==0) {
				V->min=1;
			} else {
				V->min=0;
				V->max=V->min;
			}
		} else {
			if(x==V->min) {
				int first_cluster=vEB_TREE_MINIMUM(V->summary);
				x=index(first_cluster,vEB_TREE_MINIMUM(V->cluster[first_cluster]),u);
				V->min=x;
				vEB_TREE_DELETE(V->cluster[high(x,u)],low(x,u),u);
				if(vEB_TREE_MINIMUM(V->cluster[high(x,u)])==NIL) {
					vEB_TREE_DELETE(V->summary,high(x,u),u);
					if(x==V->max) {
						int summary_max=vEB_TREE_MAXIMUM(V->summary);
						if(summary_max==NIL) {
							V->max=V->min;
						} else {
							V->max=index(summary_max,vEB_TREE_MAXIMUM(V->cluster[summary_max]),u);
						}
					}
				} else {
					if(x==V->max) {
						V->max=index(high(x,u),vEB_TREE_MAXIMUM(V->cluster[high(x,u)]),u);
					}
				}
			}
		}
	}
}


//测试函数 
void vEB_TREE_INSERT(Van_Emde_Boas *V,int u){
	int x;
	int a[]={2,3,4,5,7,14,15};
	vEB_TREE_CREATE(V,u);
	for(int i=0;i<7;i++){
		vEB_TREE_INSERT(V,a[i],u);
	}
	cout<<"请输入你要查询的数据:";
	cin>>x; 
	if(vEB_TREE_MEMBER(V,x,u)){
		cout<<"该数据存在VEB树中"<<endl; 
	} else {
		cout<<"该数据不存在VEB树中"<<endl; 
	}
	cout<<"请输入你要删除的数据:";
	cin>>x; 
    vEB_TREE_DELETE(V,x,u);
	cout<<"请输入你要查询的数据:";
	cin>>x; 
	if(vEB_TREE_MEMBER(V,x,u)){
		cout<<"该数据存在VEB树中"<<endl; 
	} else {
		cout<<"该数据不存在VEB树中"<<endl; 
	}
} 

int main() {
	int u;
	cin>>u;
	Van_Emde_Boas *V=new Van_Emde_Boas;
	vEB_TREE_INSERT(V,u);
	return 0;
}

 

  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 4
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值