1 二叉排序树
2 散列表
3 二分查找
#include <bits/stdc++.h>
using namespace std;
typedef struct Node{
Node *ls,*rs;
int val;
}Node;
typedef struct BSTree{
Node *root;
BSTree(){root=NULL;}
public:
Node* newnode(){
Node *p=(Node*)malloc(sizeof(Node));
p->ls=p->rs=NULL;
return p;
}
string Search(int x,Node *a){
if(a->val==x){
return " ";
}
if(a->val>x) return "0"+Search(x,a->ls);
return "1"+Search(x,a->rs);
}
Node* Insert(int x,Node *a){
if(a==NULL){
a=newnode();
a->val=x;
}else if(a->val>x){
a->ls=Insert(x,a->ls);
}else{
a->rs=Insert(x,a->rs);
}
return a;
}
Node* Delete_Search(int x,Node *a){
if(((a->ls!=NULL)&&(a->ls->val==x))||((a->rs!=NULL)&&(a->rs->val==x))) return a;
if(a->val>x) return Delete_Search(x,a->ls);
return Delete_Search(x,a->rs);
}
void Delete(int x){
Node *rt=NULL,*p=NULL,*fa=NULL,*re=NULL;
if(root->val==x){
if(root->ls==NULL&&root->rs==NULL){
free(root);root=NULL;return ;
}else{
re=root;
}
}else{
rt=Delete_Search(x,root);
if(rt->val>x){
re=rt->ls;
if(re->ls==NULL&&re->rs==NULL){free(re);rt->ls=NULL;return ;}
}else{
re=rt->rs;
if(re->ls==NULL&&re->rs==NULL){free(re);rt->rs=NULL;return ;}
}
}
if(re->ls!=NULL){
p=re->ls;
fa=re;
if(p->rs==NULL){
fa->ls=p->ls;
re->val=p->val;
}else{
while(p->rs!=NULL){fa=p;p=p->rs;}
fa->rs=p->ls;
re->val=p->val;
}
free(p);
}else{
p=re->rs;
fa=re;
if(p->ls==NULL){
fa->rs=p->rs;
re->val=p->val;
}else{
while(p->ls!=NULL){fa=p;p=p->ls;}
fa->ls=p->rs;
re->val=p->val;
}
free(p);
}
return ;
}
void build(int a[],int n){
for(int i=0;i<n;i++)
root=Insert(a[i],root);
}
void show(Node *a){
if(a==NULL) return ;
show(a->ls);
cout<<a->val<<' ';
show(a->rs);
}
}BSTree;
int erfen(int a[],int len,int aim){
int r=len*2;
int l=1;
for(int i=1;i<=100;i++){
int mid=(r+l)/2;
if(a[mid]==aim) return mid;
else if(a[mid]>aim){
r=mid-1;
}
else
l=mid+1;
}
return 0;
}
int hash_table(){
int a[13];
int table[20];
puts("我的k是取得7");
memset(table,0,sizeof(table));
cout<<"随机产生插入数据"<<endl;
for(int i=0;i<13;i++) a[i]=rand()%97;
for(int i=0;i<13;i++) cout<<a[i]<<' ';
for(int i=0;i<13;i++){
if(!table[a[i]%7])
table[a[i]%7]=a[i];
else{ int s=a[i]%7+1;
while(s<20){
if(!table[s])
{table[s]=a[i];break;}
s++;
}
}
}
puts("下面输入散列表序列");
for(int i=0;i<20;i++){
printf("%d ",table[i]);
}
cout<<endl;
vector<int>G[7];
for(int i=0;i<13;i++){
G[a[i]%7].push_back(a[i]);
}
puts("下面输入散列链表序列");
for(int i=0;i<=6;i++){
printf("编号:%d ",i);
for(int j=0;j<G[i].size();j++){
printf("%d ",G[i][j]);
}
cout<<endl;
}
cout<<endl;
}
int main()
{
srand((unsigned long long)time(0));
int a[13];
cout<<"随机产生插入数据"<<endl;
for(int i=0;i<13;i++) a[i]=rand()%97;
for(int i=0;i<13;i++) cout<<a[i]<<' ';
cout<<endl;
cout<<"插入建树"<<endl;
BSTree b;
b.build(a,13);
b.show(b.root);
cout<<endl;
cout<<"随机查询一个点并用01串表示其路径"<<endl;
int temp=rand()%13;
cout<<a[temp]<<' '<<b.Search(a[temp],b.root)<<endl;
cout<<"删除这个节点"<<endl;
b.Delete(a[temp]);
b.show(b.root);
cout<<endl;
a[1]=2;
for(int i=2;i<=13;i++){
a[i]=a[i-1]+3;
}
a[14]=1000;
for(int i=1;i<=14;i++){
int wz=erfen(a,13,a[i]);
if(wz!=0)
printf("%d在第%d位上\n",a[i],wz);
else
puts("0");}
hash_table();
return 0;
}