| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 3073 人关注过本帖, 1 人收藏
标题:[原创]AVL树
只看楼主 加入收藏
xtxei2468
Rank: 1
等 级:新手上路
帖 子:14
专家分:0
注 册:2006-1-4
收藏(1)
 问题点数:0 回复次数:10 
[原创]AVL树

#include<stdio.h>
#include<stdlib.h>
#include<malloc.h>
#include<string.h>
#include<iostream.h>

#define LH +1
#define EH 0
#define RH -1

typedef struct node
{
char studentname[50];
char studentnumber[20];
char sex[20];
char major[70];
char classnumber[40];
int bf;
struct node *lchild,*rchild;
}node,*Bitree;

void CreateFace()
{
cout<<endl<<endl<<endl;
cout<<" ************************************** "<<endl;
cout<<" * 欢迎进入学生信息系统 * "<<endl;
cout<<" ************************************** "<<endl;
cout<<" 请选择您要进行的操作 "<<endl;
cout<<" 0.新建学生记录。"<<endl;
cout<<" 1.撤消学生记录。"<<endl;
cout<<" 2.向学生记录中插入一个学生信息。"<<endl;
cout<<" 3.从学生记录中删除一个学生信息。"<<endl;
cout<<" 4.在学生记录中,按照姓名+学号关键字查找指定的学生信息。"<<endl;
cout<<" 5.在学生记录中,按照指定的条件搜索学生信息。"<<endl;
cout<<" 6.按照姓名+学号关键字,有序输出学生记录中所有的学生信息。"<<endl;
cout<<" 7.打印当前学生记录的存储状态。"<<endl;
cout<<" 8.系统帮助及说明。"<<endl;
cout<<" 9.退出系统。"<<endl<<endl<<endl;
cout<<" 建议在进行操作前,查看“8.系统帮助及说明。”!"<<endl<<endl<<endl<<endl;
cout<<"如果您要进行某一项操作,请按下该操作前所对应的数字键!"<<endl;
cout<<"您的选择是:";
}


void ShowHelp()
{
char j;
cout<<endl<<endl<<endl<<endl<<endl;
cout<<" &&&&&&&&&&&&&&&&&&&&&&&&&&&&&&系统帮助及说明&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&"<<endl;
cout<<" 关于学生姓名:学生姓名中不能含有除字母以外的任何其它字符,否则系统会报错,"<<endl;
cout<<" 且不予录入,并提示重新输入。在任何功能下的输入,如果用户输"<<endl;
cout<<" 户合法,那么系统会自动将用户输入的字母串的首字母转换为大写,"<<endl;
cout<<" 其余的转换为小写,之后再进行存储录入。如果用户的输入原本就"<<endl;
cout<<" 符合这样的规范,那么系统将不予改动,直接进行录入。在记录中,"<<endl;
cout<<" 相同的学生姓名可以有多个。"<<endl;
cout<<" 关于学号:学号只能是8位数字组成的字符串,如果用户输入违法,系统会报错,"<<endl;
cout<<" 且不予录入,并提示重新输入。在学生记录中,学号是没有重复的,"<<endl;
cout<<" 如果在插入和记录学生信息时,输入的学号在记录中已经存在,则"<<endl;
cout<<" 系统会给出提示,不予录入,并要求重新输入学生信息。"<<endl;
cout<<" 关于性别:用户只能输入“male”或“female”,否则系统将不予录入,并提"<<endl;
cout<<" 示重新输入。"<<endl;
cout<<" 关于专业:可以录入除空格以外的任何其它字符,但空格以后的字符,无论是"<<endl;
cout<<" 什么,系统都不予录入。"<<endl;
cout<<" 关于班级:其要求同专业。"<<endl;
cout<<" 新建学生记录:系统中只能有一个学生记录,不能同时存在多个,如果原来已经存"<<endl;
cout<<" 在学生记录,那么又要新建一个学生记录时,则会销毁原来的学生"<<endl;
cout<<" 记录。"<<endl;
cout<<" 删除学生信息:删除一个学生信息的关键字只能是学生姓名+学号,不能是其它的。"<<endl;
cout<<endl;
cout<<"按“任意字符键+回车键”继续!"<<endl;
cin>>j;
cout<<endl<<endl<<endl<<endl<<endl<<endl;
cout<<" 插入学生信息:该操作能够进行下去的前提是存在一个学生记录,也就是说如果在"<<endl;
cout<<" 学生记录为空的情况下,该操作不能进行,并提示用户新建一个学"<<endl;
cout<<" 生记录,可以在新建的过程中记录要插入的学生信息,当学生记录"<<endl;
cout<<" 不为空(记录中至少要有一个学生信息)的情况下,才可以进行插"<<endl;
cout<<" 入操作。"<<endl;
cout<<" 记录的存储状态:指的是将记录按某种方式打印出来的一种效果,这种效果类似一"<<endl;
cout<<" 棵树的形状,其根结点在左面,右子树在上面,左子树在下面。"<<endl;
cout<<" 树的每一个结点包含学生姓名和学号,姓名在上,学号在下。"<<endl;
cout<<" 提示:每一个操作的结束都以把该操作的第一个关键字赋值为‘#’为结束"<<endl;
cout<<" 标志"<<endl;
cout<<" 注意:本系统中的“学生姓名”,“学号”,“性别”,“专业”和“班级”";
cout<<" 5个关键字中都不能含有空格,如果输入了空格,那么空格及空格以"<<endl;
cout<<" 后的字符都不能被系统录入!系统中的“任意键”指的是一个任意字"<<endl;
cout<<" 符键,而不是多个,所以在操作时,只能按一个键,而不要连续按"<<endl;
cout<<" 多个键,否则可能会造成系统错误和不稳定!请用户在操作时注意!"<<endl;
cout<<" 警告:在进行任何一个操作时,只要用户不是恶意地非法输入,任何可能"<<endl;
cout<<" 的和可预见的非法输入,系统都是可以控制和排除的,并不影响系"<<endl;
cout<<" 统的稳定和正常运行,但如果用户恶意地进行违法输入,如输入关"<<endl;
cout<<" 键字信息时,输入了远超过正常长度的字符串,则有可能造成系统"<<endl;
cout<<" 溢出,系统无法正常运行,甚至死机!"<<endl;
cout<<endl;
cout<<"按“任意字符键+回车键”返回主界面!"<<endl;
cin>>j;
}

void InitBitree(Bitree &T)
{
T=NULL;
}

void Rrotate(Bitree &p)
{
Bitree lc;
lc=p->lchild;
p->lchild=lc->rchild;
lc->rchild=p;
p=lc;
}

void Lrotate(Bitree &p)
{
Bitree rc;
rc=p->rchild;
p->rchild=rc->lchild;
rc->lchild=p;
p=rc;
}

void LeftBalance(Bitree &T)
{
Bitree lc,rd;
lc=T->lchild;
switch(lc->bf)
{
case LH:
T->bf=EH;
lc->bf=EH;
Rrotate(T);
break;
case RH:
rd=lc->rchild;
switch(rd->bf)
{
case LH:
T->bf=RH;
lc->bf=EH;
break;
case EH:
T->bf=lc->bf=EH;
break;
case RH:
T->bf=EH;
lc->bf=LH;
break;
}
rd->bf=EH;
Lrotate(T->lchild);
Rrotate(T);
}
}

void RightBalance(Bitree &T)
{

Bitree rc,ld;
rc=T->rchild;
switch(rc->bf)
{
case RH:
T->bf=EH;
rc->bf=EH;
Lrotate(T);
break;
case LH:
ld=rc->lchild;
switch(ld->bf)
{
case LH:
T->bf=RH;
rc->bf=EH;
break;
case EH:
T->bf=rc->bf=EH;
break;
case RH:
T->bf = EH;
rc->bf=LH;
break;
}
ld->bf=EH;
Rrotate(T->rchild);
Lrotate(T);
}
}

搜索更多相关主题的帖子: AVL 
2006-01-04 20:00
xtxei2468
Rank: 1
等 级:新手上路
帖 子:14
专家分:0
注 册:2006-1-4
收藏
得分:0 

int cmp(Bitree a,Bitree b)
{
if(strcmp(a->studentname,b->studentname)>0)return 1;
else if(strcmp(a->studentname,b->studentname)<0)return -1;
else
{
if(strcmp(a->studentnumber,b->studentnumber)>0)return 1;
else if(strcmp(a->studentnumber,b->studentnumber)<0)return -1;
else return 0;
}
}


int testname(Bitree &T,Bitree a) //如果树中存在有相同学号的学生信息返回1,否则返回0
{
if(T==NULL)return 0;
else if(strcmp(T->studentname,a->studentname)==0)return 1;
else if(testname(T->lchild,a)==0)return testname(T->rchild,a);
else return 1;
}


int testnumber(Bitree &T,Bitree a) //如果树中存在有相同学号的学生信息返回1,否则返回0
{
if(T==NULL)return 0;
else if(strcmp(T->studentnumber,a->studentnumber)==0)return 1;
else if(testnumber(T->lchild,a)==0)return testnumber(T->rchild,a);
else return 1;
}


int testsex(Bitree &T,Bitree a) //如果树中存在有相同学号的学生信息返回1,否则返回0
{
if(T==NULL)return 0;
else if(strcmp(T->sex,a->sex)==0)return 1;
else if(testsex(T->lchild,a)==0)return testsex(T->rchild,a);
else return 1;
}


int testmajor(Bitree &T,Bitree a) //如果树中存在有相同学号的学生信息返回1,否则返回0
{
if(T==NULL)return 0;
else if(strcmp(T->major,a->major)==0)return 1;
else if(testmajor(T->lchild,a)==0)return testmajor(T->rchild,a);
else return 1;
}


int testclass(Bitree &T,Bitree a) //如果树中存在有相同学号的学生信息返回1,否则返回0
{
if(T==NULL)return 0;
else if(strcmp(T->classnumber,a->classnumber)==0)return 1;
else if(testclass(T->lchild,a)==0)return testclass(T->rchild,a);
else return 1;
}


int InsertAVL(Bitree &T,Bitree &a,int &taller)
{
if(!T)
{
T=a;
T->lchild=T->rchild=NULL;
a=NULL;
T->bf=EH;
taller=true;
}
else
{
if(cmp(a,T)==0)
{
taller=false;
cout<<"插入失败!记录中已存在该同学的信息!"<<endl;
return 0;
}
if(cmp(a,T)<0)
{
if(!InsertAVL(T->lchild,a,taller))return 0;
if(taller)
switch(T->bf)
{
case LH:
LeftBalance(T);
taller=false;
break;
case EH:
T->bf=LH;
taller=true;
break;
case RH:
T->bf=EH;
taller=false;
break;
}
}
else
{
if(!InsertAVL(T->rchild,a,taller))return 0;
if(taller)
switch(T->bf)
{
case LH:
T->bf=EH;
taller=false;
break;
case EH:
T->bf=RH;
taller=true;
break;
case RH:
RightBalance(T);
taller=false;
break;
}
}
}
return 1;
}


吾不能变心以从俗兮 故将愁苦而终穷
2006-01-04 20:00
xtxei2468
Rank: 1
等 级:新手上路
帖 子:14
专家分:0
注 册:2006-1-4
收藏
得分:0 

/************* 左平衡旋转处理1 **************/
void LeftBalance1(Bitree &p,int &shorter)
{
Bitree p1,p2;
if(p->bf==1)
{ p->bf=0;
shorter=1;
}
else if(p->bf==0)
{ p->bf=-1;
shorter=0;
}
else
{ p1=p->rchild;
if(p1->bf==0)
{ p->rchild=p1->lchild;
p1->lchild=p;
p1->bf=1;
p->bf=-1;
p=p1;
shorter=0;
}
else if(p1->bf==-1)
{
p->rchild=p1->lchild;
p1->lchild=p;
p1->bf=p->bf=0;
p=p1;
shorter=1;
}
else
{
p2=p1->lchild;
p1->lchild=p2->rchild;
p2->rchild=p1;
p->rchild=p2->lchild;
p2->lchild=p;
if(p2->bf==0)
{
p->bf=0;
p1->bf=0;
}
else if(p2->bf==-1)
{
p->bf=1;
p1->bf=0;
}
else
{
p->bf=0;
p1->bf=-1;
}
p2->bf=0;
p=p2;
shorter=1;

}
}

}
/************ 右平衡旋转处理1****************/

void RightBalance1(Bitree &p,int &shorter)
{
Bitree p1,p2;
if(p->bf==-1)
{ p->bf=0;
shorter=1;
}
else if(p->bf==0)
{ p->bf=1;
shorter=0;
}
else
{ p1=p->lchild;
if(p1->bf==0)
{ p->lchild=p1->rchild;
p1->rchild=p;
p1->bf=-1;
p->bf=1;
p=p1;
shorter=0;
}
else if(p1->bf==1)
{
p->lchild=p1->rchild;
p1->rchild=p;
p1->bf=p->bf=0;
p=p1;
shorter=1;
}
else
{
p2=p1->rchild;
p1->rchild=p2->lchild;
p2->lchild=p1;
p->lchild=p2->rchild;
p2->rchild=p;
if(p2->bf==0)
{
p->bf=0;
p1->bf=0;
}
else if(p2->bf==1)
{
p->bf=-1;
p1->bf=0;
}
else
{
p->bf=0;
p1->bf=1;
}
p2->bf=0;
p=p2;
shorter=1;

}
}

}


吾不能变心以从俗兮 故将愁苦而终穷
2006-01-04 20:01
xtxei2468
Rank: 1
等 级:新手上路
帖 子:14
专家分:0
注 册:2006-1-4
收藏
得分:0 

/************ 删除结点****************/
void Delete(Bitree q,Bitree &r,int &shorter)
{
if(r->rchild==NULL)
{
strcpy(q->studentname,r->studentname);
strcpy(q->studentnumber,r->studentnumber);
strcpy(q->sex,r->sex);
strcpy(q->major,r->major);
strcpy(q->classnumber,r->classnumber);
q=r;
r=r->lchild;
free(q);
shorter=1;
}
else
{
Delete(q,r->rchild,shorter);
if(shorter==1)
RightBalance1(r,shorter);
}
}


int DeleteAVL(Bitree &p,Bitree a,int &shorter)
{
int k;
Bitree q;
if(p==NULL) {cout<<"删除失败!记录中不存在要删除的学生信息!"<<endl<<endl; return 0;}
else if(cmp(a,p)<0)
{
k=DeleteAVL(p->lchild,a,shorter);
if(shorter==1)
LeftBalance1(p,shorter);
return k;
}
else if(cmp(a,p)>0)
{
k=DeleteAVL(p->rchild,a,shorter);
if(shorter==1)
RightBalance1(p,shorter);
return k;
}
else
{
q=p;
if(p->rchild==NULL)
{p=p->lchild;
free(q);
shorter=1;
}
else if(p->lchild==NULL)
{p=p->rchild;
free(q);
shorter=1;
}
else
{
Delete(q,q->lchild,shorter);
if(shorter==1)
LeftBalance1(p,shorter);
p=q;

}
return 1;
}
}


void destroy(Bitree &T) //初始条件:二叉树存在,操作结果:将二叉树T清成空树
{
if(T==NULL)return;
else if((T->lchild==NULL)&&(T->rchild==NULL))
{
free(T);
T=NULL;
}
else
{
destroy(T->lchild);
destroy(T->rchild);
destroy(T);
}
}


void DestroyBitree(Bitree &T)
{
cout<<endl<<endl<<endl<<endl<<endl<<endl<<endl<<endl<<endl<<endl<<endl;
cout<<" 1.撤消学生记录。"<<endl;
cout<<endl<<endl<<endl<<endl<<endl<<endl<<endl<<endl<<endl<<endl;
char k;
if(T==NULL)
{
cout<<"并不存在学生记录!撤销操作无意义!"<<endl;
cout<<"按“任意字符键+回车键”返回主界面!"<<endl;
cin>>k;
return;
}
else
{
destroy(T);
cout<<"撤销成功!所有的学生信息都已被清除!"<<endl;
cout<<"按“任意字符键+回车键”返回主界面!"<<endl;
cin>>k;
}
}

/****************打印最后结果平衡树**************/
void PrintBitree(Bitree &T,int m)//按树状打印输出二叉树的元素,i 表示结点所在层次,初次调用时 m=0
{ int j;
if(T->rchild) PrintBitree(T->rchild,m+1);
for(j=1;j<=m;j++) printf(" "); //打印 i 个空格以表示出层次
cout<<T->studentname<<endl;//打印 T 元素,换行
for(j=1;j<=m;j++) printf(" "); //打印 i 个空格以表示出层次
cout<<T->studentnumber<<endl;//打印 T 元素,换行
if(T->lchild) PrintBitree(T->lchild,m+1);
}

void InsertChild(Bitree &T)
{
cout<<endl<<endl<<endl<<endl<<endl<<endl<<endl<<endl<<endl<<endl<<endl;
cout<<" 2.向学生记录中插入一个学生信息。"<<endl;
cout<<endl<<endl<<endl<<endl<<endl<<endl<<endl<<endl<<endl<<endl;
char k;
if(T==NULL)
{
cout<<"不存在学生记录,不能进行插入!"<<endl<<"您可以新建一个学生记录,记录要插入的学生信息!"<<endl;
cout<<"按“任意字符键+回车键”返回主界面!"<<endl;
cin>>k;
return;
}
Bitree a;
int i=0,m=1;
cout<<"这是在操作之前学生记录的存储状态!"<<endl;
cout<<"$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$"<<endl;
PrintBitree(T,m);
cout<<"$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$"<<endl<<endl<<endl;
while(1)
{
a=(Bitree)malloc(sizeof(node));
a->lchild=a->rchild=NULL;
while(1)
{
cout<<"请输入要插入的学生信息,如果要结束插入操作,请把“学生姓名”赋值为‘#’!"<<endl;
cout<<"注意:学生姓名中不能含有除字母以外的其它字符!"<<endl;
cout<<"学生姓名:";
cin>>a->studentname;
if(strcmp(a->studentname,"#")==0)break;
for(int z=0;a->studentname[z]!='\0';z++)
if((a->studentname[z]<65)||((a->studentname[z]>90)&&(a->studentname[z]<97))||(a->studentname[z]>122))
break;
if(a->studentname[z]!='\0')
{
cout<<"输入错误!请重新输入!"<<endl<<endl;
continue;
}
else
{
for(int t=0;a->studentname[t]!='\0';t++)
{
if((t==0)&&(a->studentname[t]>96)&&(a->studentname[t]<123))
a->studentname[t]=a->studentname[t]-32;
if((t!=0)&&(a->studentname[t]<91)&&(a->studentname[t]>64))
a->studentname[t]=a->studentname[t]+32;
}
break;
}
}
if(strcmp(a->studentname,"#")==0)
{
free(a);
cout<<endl;
break;
}
cout<<endl;
while(1)
{
cout<<"注意:学号必须是8位数字!"<<endl;
cout<<"学号:";
cin>>a->studentnumber;
cout<<endl;
for(int x=0;a->studentnumber[x]!='\0';x++)
if((a->studentnumber[x]<48)||(a->studentnumber[x]>57))break;
if((a->studentnumber[x]!='\0')||(x!=8))
cout<<"输入错误!请输入8位数字学号!"<<endl<<endl;
else break;
}
cout<<endl;
if(testnumber(T,a)==1)
{
cout<<"记录中已存在该学号的信息!请重新输入要插入的学生信息!"<<endl<<endl;
free(a);
continue;
}
while(1)
{
cout<<"性别(注意:只能输入male或female):";
cin>>a->sex;
if((strcmp(a->sex,"male")!=0)&&(strcmp(a->sex,"female")!=0))
cout<<"输入错误!请重新输入!"<<endl;
else break;
}
cout<<endl;
cout<<"专业:";
cin>>a->major;
cout<<endl;
cout<<"班级:";
cin>>a->classnumber;
cout<<endl;
InsertAVL(T,a,i);
cout<<"这是在操作之后学生记录的存储状态!"<<endl;
cout<<"$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$"<<endl;
m=1;
PrintBitree(T,m);
cout<<"$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$"<<endl<<endl<<endl;
}
}


void DeleteChild(Bitree &T)
{
cout<<endl<<endl<<endl<<endl<<endl<<endl<<endl<<endl<<endl<<endl<<endl;
cout<<" 3.从学生记录中删除一个学生信息。"<<endl;
cout<<endl<<endl<<endl<<endl<<endl<<endl<<endl<<endl<<endl<<endl;
char j;
if(T==NULL)
{
cout<<"不存在学生记录,不能进行删除!"<<endl;
cout<<"按“任意字符键+回车键”返回主界面!"<<endl;
cin>>j;
return;
}
Bitree a;
int i=0,m=1;
cout<<"这是在操作之前学生记录的存储状态!"<<endl;
cout<<"$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$"<<endl;
PrintBitree(T,m);
cout<<"$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$"<<endl<<endl<<endl;
while(1)
{
a=(Bitree)malloc(sizeof(node));
a->lchild=a->rchild=NULL;
while(1)
{
cout<<"请输入要删除的学生信息,如果要结束删除操作,请把“学生姓名”赋值为‘#’!"<<endl;
cout<<"注意:学生姓名中不能含有除字母以外的其它字符!"<<endl;
cout<<"学生姓名:";
cin>>a->studentname;
if(strcmp(a->studentname,"#")==0)break;
for(int z=0;a->studentname[z]!='\0';z++)
if((a->studentname[z]<65)||((a->studentname[z]>90)&&(a->studentname[z]<97))||(a->studentname[z]>122))
break;
if(a->studentname[z]!='\0')
{
cout<<"输入错误!请重新输入!"<<endl<<endl;
continue;
}
else
{
for(int t=0;a->studentname[t]!='\0';t++)
{
if((t==0)&&(a->studentname[t]>96)&&(a->studentname[t]<123))
a->studentname[t]=a->studentname[t]-32;
if((t!=0)&&(a->studentname[t]<91)&&(a->studentname[t]>64))
a->studentname[t]=a->studentname[t]+32;
}
break;
}
}
if(strcmp(a->studentname,"#")==0)
{
free(a);
cout<<endl;
break;
}
cout<<endl;
while(1)
{
cout<<"注意:学号必须是8位数字!"<<endl;
cout<<"学号:";
cin>>a->studentnumber;
cout<<endl;
for(int x=0;a->studentnumber[x]!='\0';x++)
if((a->studentnumber[x]<48)||(a->studentnumber[x]>57))break;
if((a->studentnumber[x]!='\0')||(x!=8))
cout<<"输入错误!请输入8位数字学号!"<<endl<<endl;
else break;
}
cout<<endl;
DeleteAVL(T,a,i);
free(a); //++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
cout<<"这是在操作之后学生记录的存储状态!"<<endl;
cout<<"$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$"<<endl;
m=1;
if(T)PrintBitree(T,m);
else cout<<"学生记录已被清空!"<<endl;
cout<<"$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$"<<endl<<endl<<endl;
}
}

void CreateBitree(Bitree &T)
{
cout<<endl<<endl<<endl<<endl<<endl<<endl<<endl<<endl<<endl<<endl<<endl;
cout<<" 0.新建学生记录。"<<endl;
cout<<endl<<endl<<endl<<endl<<endl<<endl<<endl<<endl<<endl<<endl;
char n;
int i=0,m=1;
Bitree a;
if(T)
{
cout<<"警告:学生记录已经存在!如果要重新建立一个学生记录,"<<endl<<"则会销毁原来的所有学生信息!"<<endl;
cout<<"这是当前学生记录的存储状态!"<<endl;
cout<<"$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$"<<endl;
PrintBitree(T,m);
cout<<"$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$"<<endl<<endl<<endl;
while(1)
{
cout<<"是否要继续进行该操作,如果是请按Y,否则请按N返回主界面(不区分大小写)!"<<endl;
cin>>n;
if((n!='Y')&&(n!='y')&&(n!='N')&&(n!='n'))
cout<<"输入错误!请重新输入!"<<endl;
else if((n=='Y')||(n=='y')){destroy(T);cout<<"原来的学生记录已被清空!"<<endl;break;}
else return;
}
}
while(1)
{


a=(Bitree)malloc(sizeof(node));
a->lchild=a->rchild=NULL;
while(1)
{
cout<<"请输入要记录的学生信息,如果要结束记录操作,请把“学生姓名”赋值为‘#’!"<<endl;
cout<<"注意:学生姓名中不能含有除字母以外的其它字符!"<<endl;
cout<<"学生姓名:";
cin>>a->studentname;
if(strcmp(a->studentname,"#")==0)break;
for(int z=0;a->studentname[z]!='\0';z++)
if((a->studentname[z]<65)||((a->studentname[z]>90)&&(a->studentname[z]<97))||(a->studentname[z]>122))
break;
if(a->studentname[z]!='\0')
{
cout<<"输入错误!请重新输入!"<<endl<<endl;
continue;
}
else
{
for(int t=0;a->studentname[t]!='\0';t++)
{
if((t==0)&&(a->studentname[t]>96)&&(a->studentname[t]<123))
a->studentname[t]=a->studentname[t]-32;
if((t!=0)&&(a->studentname[t]<91)&&(a->studentname[t]>64))
a->studentname[t]=a->studentname[t]+32;
}
break;
}
}
if(strcmp(a->studentname,"#")==0)
{
free(a);
cout<<endl;
break;
}
cout<<endl;
while(1)
{
cout<<"注意:学号必须是8位数字!"<<endl;
cout<<"学号:";
cin>>a->studentnumber;
cout<<endl;
for(int x=0;a->studentnumber[x]!='\0';x++)
if((a->studentnumber[x]<48)||(a->studentnumber[x]>57))break;
if((a->studentnumber[x]!='\0')||(x!=8))
cout<<"输入错误!请输入8位数字学号!"<<endl<<endl;
else break;
}
cout<<endl;
if(testnumber(T,a)==1)
{
cout<<"记录中已存在该学号的信息!请重新输入要插入的学生信息!"<<endl;
free(a);
continue;
}
while(1)
{
cout<<"性别(注意:只能输入male或female):";
cin>>a->sex;
if((strcmp(a->sex,"male")!=0)&&(strcmp(a->sex,"female")!=0))
cout<<"输入错误!请重新输入!"<<endl;
else break;
}
cout<<endl;
cout<<"专业:";
cin>>a->major;
cout<<endl;
cout<<"班级:";
cin>>a->classnumber;
cout<<endl;
InsertAVL(T,a,i);
cout<<"这是在操作之后,学生记录的存储状态!"<<endl;
cout<<"$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$"<<endl;
m=1;
PrintBitree(T,m);
cout<<"$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$"<<endl<<endl<<endl;

}
}


void InorderTraverse(Bitree &T) //中序遍历二叉树
{
if (T)
{ //若根结点不空
InorderTraverse(T->lchild);
cout<<"学生姓名:"<<T->studentname<<endl;
cout<<"学号:"<<T->studentnumber<<endl;
cout<<"性别:"<<T->sex<<endl;
cout<<"专业:"<<T->major<<endl;
cout<<"班级:"<<T->classnumber<<endl<<endl<<endl;
InorderTraverse(T->rchild);
}
}


吾不能变心以从俗兮 故将愁苦而终穷
2006-01-04 20:01
xtxei2468
Rank: 1
等 级:新手上路
帖 子:14
专家分:0
注 册:2006-1-4
收藏
得分:0 

void TraverseBitree(Bitree &T)
{
cout<<endl<<endl<<endl<<endl<<endl<<endl<<endl<<endl<<endl<<endl<<endl;
cout<<" 6.按照姓名+学号关键字,有序输出学生记录中所有的学生信息。"<<endl;
cout<<endl<<endl<<endl<<endl<<endl<<endl<<endl<<endl<<endl<<endl;
char k;
if(T==NULL)
{
cout<<"学生记录为空!没有任何学生信息!"<<endl;
cout<<"按“任意字符键+回车键”返回主界面!"<<endl;
cin>>k;
}
else
{
InorderTraverse(T);
cout<<"输出结束!按“任意字符键+回车键”返回主界面!"<<endl;
cin>>k;
}
}

void search(Bitree &T,Bitree &a)
{
if(T==NULL)
{
cout<<"查找失败!学生记录中没有要查找的学生信息!"<<endl;
return;
}
else if(cmp(a,T)>0)search(T->rchild,a);
else if(cmp(a,T)<0)search(T->lchild,a);
else
{
cout<<"学生姓名:"<<T->studentname<<endl;
cout<<"学号:"<<T->studentnumber<<endl;
cout<<"性别:"<<T->sex<<endl;
cout<<"专业:"<<T->major<<endl;
cout<<"班级:"<<T->classnumber<<endl<<endl<<endl;
return;
}
}


void SearchKeyNode(Bitree &T)
{
cout<<endl<<endl<<endl<<endl<<endl<<endl<<endl<<endl<<endl<<endl<<endl;
cout<<" 4.在学生记录中,按照姓名+学号关键字查找指定的学生信息。"<<endl;
cout<<endl<<endl<<endl<<endl<<endl<<endl<<endl<<endl<<endl<<endl;
char k;
Bitree a;
if(T==NULL)
{
cout<<"学生记录为空!没有任何学生信息!"<<endl;
cout<<"按“任意字符键+回车键”返回主界面!"<<endl;
cin>>k;
return;
}
while(1)
{
a=(Bitree)malloc(sizeof(node));
a->lchild=a->rchild=NULL;
while(1)
{
cout<<"请输入要查找的学生信息,如果要结束查找操作,请把“学生姓名”赋值为‘#’!"<<endl;
cout<<"注意:学生姓名中不能含有除字母以外的其它字符!"<<endl;
cout<<"学生姓名:";
cin>>a->studentname;
if(strcmp(a->studentname,"#")==0)break;
for(int z=0;a->studentname[z]!='\0';z++)
if((a->studentname[z]<65)||((a->studentname[z]>90)&&(a->studentname[z]<97))||(a->studentname[z]>122))
break;
if(a->studentname[z]!='\0')
{
cout<<"输入错误!请重新输入!"<<endl<<endl;
continue;
}
else
{
for(int t=0;a->studentname[t]!='\0';t++)
{
if((t==0)&&(a->studentname[t]>96)&&(a->studentname[t]<123))
a->studentname[t]=a->studentname[t]-32;
if((t!=0)&&(a->studentname[t]<91)&&(a->studentname[t]>64))
a->studentname[t]=a->studentname[t]+32;
}
break;
}
}
if(strcmp(a->studentname,"#")==0)
{
free(a);
cout<<endl;
break;
}
cout<<endl;
while(1)
{
cout<<"注意:学号必须是8位数字!"<<endl;
cout<<"学号:";
cin>>a->studentnumber;
cout<<endl;
for(int i=0;a->studentnumber[i]!='\0';i++)
if((a->studentnumber[i]<48)||(a->studentnumber[i]>57))break;
if((a->studentnumber[i]!='\0')||(i!=8))
cout<<"输入错误!请输入8位数字学号!"<<endl<<endl;
else break;
}
cout<<endl;
search(T,a);
free(a); //++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
}
}


void SearchName(Bitree &T,Bitree a)
{
if(T==NULL)
{
return;
}
if(strcmp(a->studentname,T->studentname)>0)
SearchName(T->rchild,a);
else if(strcmp(a->studentname,T->studentname)<0)
SearchName(T->lchild,a);
else
{
cout<<"学生姓名:"<<T->studentname<<endl;
cout<<"学号:"<<T->studentnumber<<endl;
cout<<"性别:"<<T->sex<<endl;
cout<<"专业:"<<T->major<<endl;
cout<<"班级:"<<T->classnumber<<endl<<endl<<endl;
SearchName(T->lchild,a);
SearchName(T->rchild,a);
}
}


void SearchNumber(Bitree &T,Bitree a)
{
if(T==NULL)
{
return;
}

if(strcmp(a->studentnumber,T->studentnumber)==0)
{
cout<<"学生姓名:"<<T->studentname<<endl;
cout<<"学号:"<<T->studentnumber<<endl;
cout<<"性别:"<<T->sex<<endl;
cout<<"专业:"<<T->major<<endl;
cout<<"班级:"<<T->classnumber<<endl<<endl<<endl;
return;
}
SearchNumber(T->lchild,a);
SearchNumber(T->rchild,a);
}

void SearchSex(Bitree &T,Bitree a)
{
if(T==NULL)
{
return;
}

if(strcmp(a->sex,T->sex)==0)
{
cout<<"学生姓名:"<<T->studentname<<endl;
cout<<"学号:"<<T->studentnumber<<endl;
cout<<"性别:"<<T->sex<<endl;
cout<<"专业:"<<T->major<<endl;
cout<<"班级:"<<T->classnumber<<endl<<endl<<endl;
}
SearchSex(T->lchild,a);
SearchSex(T->rchild,a);
}

void SearchMajor(Bitree &T,Bitree a)
{
if(T==NULL)
{
return;
}

if(strcmp(a->major,T->major)==0)
{
cout<<"学生姓名:"<<T->studentname<<endl;
cout<<"学号:"<<T->studentnumber<<endl;
cout<<"性别:"<<T->sex<<endl;
cout<<"专业:"<<T->major<<endl;
cout<<"班级:"<<T->classnumber<<endl<<endl<<endl;
}
SearchMajor(T->lchild,a);
SearchMajor(T->rchild,a);
}


void SearchClass(Bitree &T,Bitree a)
{
if(T==NULL)
{
return;
}

if(strcmp(a->classnumber,T->classnumber)==0)
{
cout<<"学生姓名:"<<T->studentname<<endl;
cout<<"学号:"<<T->studentnumber<<endl;
cout<<"性别:"<<T->sex<<endl;
cout<<"专业:"<<T->major<<endl;
cout<<"班级:"<<T->classnumber<<endl<<endl<<endl;
}
SearchClass(T->lchild,a);
SearchClass(T->rchild,a);
}


void SearchNode(Bitree &T)
{
cout<<endl<<endl<<endl<<endl<<endl<<endl<<endl<<endl<<endl<<endl<<endl;
cout<<" 5.在学生记录中,按照指定的条件搜索学生信息。"<<endl;
cout<<endl<<endl<<endl<<endl<<endl<<endl<<endl<<endl<<endl;
Bitree a;
char k[10];
char j;
if(T==NULL)
{
cout<<"学生记录为空!不能进行查找!"<<endl;
cout<<"按“任意字符键+回车键”返回主界面!"<<endl;
cin>>j;
return;
}
while(1)
{
cout<<endl;
cout<<" 请选择您要查找的关键字!"<<endl;
cout<<" **************************************************************"<<endl;
cout<<" 1.学生姓名"<<" 2.学号"<<" 3.性别"<<" 4.专业"<<" 5.班级"<<" 6.退出查找操作 "<<endl;
cout<<" **************************************************************"<<endl<<endl;
cout<<"如果您要按照某一关键字进行查找,请按下该关键字前所对应的数字键!"<<endl;
cout<<"如果要退出该操作,请按'6'!"<<endl;
cout<<"您的选择是:";
while(1)
{
cin>>k;
if(strlen(k)>1)
cout<<"输入错误!只能输入一个字符!请重新输入!"<<endl;
else break;
}
switch(k[0])
{
case '1':
while(1)
{
a=(Bitree)malloc(sizeof(node));
a->lchild=a->rchild=NULL;
while(1)
{
cout<<"请输入要查找的学生姓名,如果要结束按学生姓名查找,请把“学生姓名”赋值为‘#’!"<<endl;
cout<<"注意:学生姓名中不能含有除字母以外的其它字符!"<<endl;
cout<<"请输入学生姓名:";
cin>>a->studentname;
if(strcmp(a->studentname,"#")==0)break;
for(int z=0;a->studentname[z]!='\0';z++)
if((a->studentname[z]<65)||((a->studentname[z]>90)&&(a->studentname[z]<97))||(a->studentname[z]>122))
break;
if(a->studentname[z]!='\0')
{
cout<<"输入错误!请重新输入!"<<endl<<endl;
continue;
}
else
{
for(int t=0;a->studentname[t]!='\0';t++)
{
if((t==0)&&(a->studentname[t]>96)&&(a->studentname[t]<123))
a->studentname[t]=a->studentname[t]-32;
if((t!=0)&&(a->studentname[t]<91)&&(a->studentname[t]>64))
a->studentname[t]=a->studentname[t]+32;
}
break;
}
}
if(strcmp(a->studentname,"#")==0)
{
free(a);
break;
}
if(testname(T,a)==0)
{
cout<<"查找失败!记录中不存在要查找的学生姓名信息!"<<endl<<endl;
free(a);
continue;
}
SearchName(T,a);
free(a); //+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
}
break;
case '2':
while(1)
{
a=(Bitree)malloc(sizeof(node));
a->lchild=a->rchild=NULL;
while(1)
{
cout<<"请输入要查找的学号,如果要结束按学号查找,请把“学号”赋值为‘#’!"<<endl;
cout<<"注意:学号必须是8位数字!"<<endl;
cout<<"请输入学号:";
cin>>a->studentnumber;
cout<<endl;
if(strcmp(a->studentnumber,"#")==0)break;
for(int i=0;a->studentnumber[i]!='\0';i++)
if((a->studentnumber[i]<48)||(a->studentnumber[i]>57))break;
if((a->studentnumber[i]!='\0')||(i!=8))
cout<<"输入错误!请输入8位数字学号!"<<endl<<endl;
else break;
}
if(strcmp(a->studentnumber,"#")==0)
{
free(a);
break;
}
if(testnumber(T,a)==0)
{
cout<<"查找失败!记录中不存在要查找的学生学号信息!"<<endl<<endl;
free(a);
continue;
}
SearchNumber(T,a);
free(a);
}
break;
case '3':
while(1)
{
a=(Bitree)malloc(sizeof(node));
a->lchild=a->rchild=NULL;
while(1)
{
cout<<"请输入要查找的性别,如果要结束按性别查找,请把“性别”赋值为‘#’!"<<endl;
cout<<"请输入性别(注意:只能输入“male”或“female”或‘#’):";
cin>>a->sex;
if((strcmp(a->sex,"male")!=0)&&(strcmp(a->sex,"female")!=0)&&(strcmp(a->sex,"#")!=0))
cout<<"输入错误!请重新输入!"<<endl<<endl;
else break;
}
if(strcmp(a->sex,"#")==0)
{
free(a);
break;
}
if(testsex(T,a)==0)
{
cout<<"查找失败!记录中不存在要查找的学生性别信息!"<<endl<<endl;
free(a);
continue;
}
SearchSex(T,a);
free(a);
}
break;
case '4':
while(1)
{
a=(Bitree)malloc(sizeof(node));
a->lchild=a->rchild=NULL;
cout<<"请输入要查找的专业,如果要结束按专业查找,请把“专业”赋值为‘#’!"<<endl;
cout<<"请输入专业:";
cin>>a->major;
if(strcmp(a->major,"#")==0)
{
free(a);
break;
}
if(testmajor(T,a)==0)
{
cout<<"查找失败!记录中不存在要查找的学生专业信息!"<<endl<<endl;
free(a);
continue;
}
SearchMajor(T,a);
free(a);
}
break;
case '5':
while(1)
{
a=(Bitree)malloc(sizeof(node));
a->lchild=a->rchild=NULL;
cout<<"请输入要查找的班级,如果要结束按班级查找,请把“班级”赋值为‘#’!"<<endl;
cout<<"请输入班级号:";
cin>>a->classnumber;
if(strcmp(a->classnumber,"#")==0)
{
free(a);
break;
}
if(testclass(T,a)==0)
{
cout<<"查找失败!记录中不存在要查找的学生班级信息!"<<endl<<endl;
free(a);
continue;
}
SearchClass(T,a);
free(a);
}
break;
case '6':
return;
default:
cout<<"输入错误!请重新输入!";
}
}
}


void Print(Bitree &T)
{
cout<<endl<<endl<<endl<<endl<<endl<<endl<<endl<<endl<<endl<<endl<<endl;
cout<<" 7.打印当前学生记录的存储状态。"<<endl;
cout<<endl<<endl<<endl<<endl<<endl<<endl<<endl<<endl<<endl;
char k;
int m=1;
if(T==NULL)
{
cout<<"学生记录为空!没有任何学生信息!"<<endl;
cout<<"按“任意字符键+回车键”返回主界面!"<<endl;
cin>>k;
return;
}
cout<<"$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$"<<endl;
PrintBitree(T,m);
cout<<"$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$"<<endl<<endl<<endl;
cout<<"按“任意字符键+回车键”返回主界面!"<<endl;
cin>>k;
}

void Exit()
{
cout<<endl<<endl<<endl<<endl<<endl<<endl<<endl<<endl<<endl<<endl<<endl;
cout<<" 9.退出系统。"<<endl<<endl<<endl;
cout<<endl<<endl<<endl<<endl<<endl<<endl<<endl<<endl<<endl<<endl;
exit(0);
}


void main()
{
Bitree T;
char choose[10];
InitBitree(T);
while(1)
{
CreateFace();
while(1)
{
cin>>choose;
if(strlen(choose)>1)
cout<<"输入错误!只能输入一个字符!请重新输入!"<<endl;
else break;
}
cout<<endl;
switch(choose[0])
{
case '0':
CreateBitree(T);
break;
case '1':
DestroyBitree(T);
break;
case '2':
InsertChild(T);
break;
case '3':
DeleteChild(T);
break;
case '4':
SearchKeyNode(T);
break;
case '5':
SearchNode(T);
break;
case '6':
TraverseBitree(T);
break;
case '7':
Print(T);
break;
case '8':
ShowHelp();
break;
case '9':
Exit();
default:
cout<<"输入错误!请重新输入!";
}
}
}


吾不能变心以从俗兮 故将愁苦而终穷
2006-01-04 20:02
xtxei2468
Rank: 1
等 级:新手上路
帖 子:14
专家分:0
注 册:2006-1-4
收藏
得分:0 

不好意思
第一次发 弄成什么送金币了


吾不能变心以从俗兮 故将愁苦而终穷
2006-01-04 20:06
starrysky
Rank: 16Rank: 16Rank: 16Rank: 16
来 自:华中科技大学EI -T0405
等 级:版主
威 望:11
帖 子:602
专家分:1
注 册:2005-9-12
收藏
得分:0 

强啊 ,楼主

我要好好学习一下。


我的征途是星辰大海
2006-01-05 11:52
nqq622
Rank: 1
等 级:新手上路
帖 子:11
专家分:0
注 册:2006-1-4
收藏
得分:0 
我就要平衡二叉排序树的建立,增加,删除的C语言算法啊,能帮忙吗?
2006-01-05 18:25
high20033763
Rank: 1
等 级:新手上路
帖 子:85
专家分:0
注 册:2006-2-13
收藏
得分:0 

老兄,你的程序不是在winTC下运行的吧,那个编译环境不行的,我用vc++就有很多错误,
不过还是不错了

2006-02-13 15:30
jiang520
Rank: 1
等 级:新手上路
帖 子:207
专家分:0
注 册:2006-9-13
收藏
得分:0 
好强啊

,哪年哪月才写得出这样的代码哦

努力,努力吧,未来的天空,那一片湛蓝总会属于我的~
2006-11-14 17:16
快速回复:[原创]AVL树
数据加载中...
 
   



关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.042911 second(s), 9 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved