功能如下
1. 添加学生
2. 删除学生
3. 修改学生信息
4. 获得学生信息
5. 获得所有学生信息
B树创建优势
- 查找非常快
2.任何一个学生只需要查询几步(譬如2的32次方的学生,每次只需要32次遍历)
B树缺点
1.严重浪费空间,有点符合动态规划的:以空间换时间
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
typedef struct stu{
char name[20];
}stu;
typedef struct Node{
struct Node *pre,*L,*R;
int id;
struct stu *info;
}biTree;
biTree* init(){
biTree *root=(biTree*)malloc(sizeof(biTree));
root->pre=NULL;
root->L=NULL;
root->R=NULL;
return root;
}
void binary(int x,int b[]){
int i,j=0,temp,count=0;
memset(b,0,32*sizeof(int));
if(x==0){
b[0]=0;
}
while(x!=0){
b[count]=x%2;
x/=2;
count++;
}
i=31;
while(j<=i){
temp=b[i];b[i]=b[j];b[j]=temp;
i--;j++;
}
}
void creat(biTree *root,int id){
biTree *bt,*pre;
int b[32];
bt=root;
binary(id,b);
for (int i = 0; i < 32; ++i){
if(b[i]==0){
if(bt->L!=NULL){
bt=bt->L;
}else{
pre=bt;
bt=(biTree*)malloc(sizeof(biTree));
pre->L=bt;
bt->pre=pre;
bt->L=NULL;
bt->R=NULL;
}
}else if(b[i]==1){
if(bt->R!=NULL){
bt=bt->R;
}else{
pre=bt;
bt=(biTree*)malloc(sizeof(biTree));
pre->R=bt;
bt->pre=pre;
bt->L=NULL;
bt->R=NULL;
}
}
}
bt->id=id;
bt->info=(stu*)malloc(sizeof(stu));
printf("\n\t\t\tplease input the name:");
scanf("%s",bt->info->name);
}
biTree* getnode(biTree *root,int id){
int b[32];
biTree *bt=root;
binary(id,b);
if(bt==NULL){
return NULL;
}
for (int i = 0; i < 32; ++i){
if(bt==NULL){
return NULL;
}
if(b[i]==0){
bt=bt->L;
if(bt==NULL){
return NULL;
}
}else if(b[i]==1){
bt=bt->R;
if(bt==NULL){
return NULL;
}
}
}
return bt;
}
int main(){
biTree *root;
biTree *stu,*pre;
int code,studentId,i,b[32];
int a[1000];
char name[20];
memset(a,0,sizeof(a));
root=init();
while(1){
printf("\n\t\t\t==============student system===============");
printf("\n\t\t\t 1.add student");
printf("\n\t\t\t 2.del student");
printf("\n\t\t\t 3.modify student");
printf("\n\t\t\t 4.get student information");
printf("\n\t\t\t 5.get all student information");
printf("\n\t\t\tplease input the choice:");
scanf("%d",&code);
switch(code){
case 1:
printf("\n\t\t\tplease input studentId you want to add:");
scanf("%d",&studentId);
if(a[studentId]==0){
a[studentId]=1;
creat(root,studentId);
printf("\n\t\t\tadd success");
break;
}
else if(a[studentId]==1){
printf("\n\t\t\tthe student is already exit!");
break;
}
break;
case 2:
printf("\n\t\t\tplease input the studentId you want del:");
scanf("%d",&studentId);
binary(studentId,b);
stu=getnode(root,studentId);
if(stu==NULL){
printf("\n\t\t\tthe student don't exit");
break;
}
pre=stu->pre;
if(b[31]==0){
pre->L=NULL;
}else{
pre->R=NULL;
}
free(stu);
a[studentId]=0;
printf("\n\t\t\tdel success");
break;
case 3:
printf("\n\t\t\tplease input the studentId you want modify:");
scanf("%d",&studentId);
stu=getnode(root,studentId);
if(stu==NULL){
printf("\n\t\t\tthe student don't exit");
break;
}
printf("\n\t\t\tid: %d,name: %s",stu->id,stu->info->name);
printf("\n\t\t\tplease input the new name:");
scanf("%s",stu->info->name);
printf("\n\t\t\tmodify success!");
break;
case 4:
printf("\n\t\t\tplease input the studentId you want search:");
scanf("%d",&studentId);
stu=getnode(root,studentId);
if(stu==NULL){
printf("\n\t\t\tthe student don't exit!");
break;
}
printf("\n\t\t\tid: %d,name: %s",stu->id,stu->info->name);
break;
case 5:
for(int i=0;i<1000;i++){
if(a[i]!=0){
stu=getnode(root,i);
printf("\n\t\t\tid: %d ,name: %s",stu->id,stu->info->name);
}
}
break;
}
}
return 0;
}