平衡二叉树学生信息管理系统程序(共13页).doc
精选优质文档-倾情为你奉上#include<stdio.h>#include<string.h>typedef struct A char NO10; char name10; char birt10; char clas10; char sex2;Elemtype;typedef struct BElemtype data;int bf;struct B *lchild;struct B *rchild;node,*pnode;void left1(pnode &ptree,int &taller)pnode p1,p2;if(ptree->bf=0)ptree->bf=1;taller=1;else if (ptree->bf=-1)ptree->bf=0;taller=0;elsep1=ptree->lchild;if (p1->bf=1)ptree->lchild=p1->rchild;p1->rchild=ptree;ptree->bf=p1->bf=0;ptree=p1;else if (p1->bf=-1)p2=p1->rchild;p1->rchild=p2->lchild;p2->lchild=p1;ptree->lchild=p2->rchild;p2->rchild=ptree;if (p2->bf=0)ptree->bf=p1->bf=0;else if (p2->bf=1)p1->bf=0;ptree->bf=-1;elsep1->bf=1;ptree->bf=0;ptree=p2;ptree->bf=0;taller=0;void right1(pnode &ptree,int &taller)pnode p1,p2;if(ptree->bf=0)ptree->bf=-1;taller=1;else if (ptree->bf=1)ptree->bf=0;taller=0;elsep1=ptree->rchild;if (p1->bf=-1)ptree->rchild=p1->lchild;p1->lchild=ptree;ptree->bf=p1->bf=0;ptree=p1;else if (p1->bf=1)p2=p1->lchild;p1->lchild=p2->rchild;p2->rchild=p1;ptree->rchild=p2->lchild;p2->lchild=ptree;if (p2->bf=0)ptree->bf=p1->bf=0;else if (p2->bf=-1)p1->bf=0;ptree->bf=1;elsep1->bf=-1;ptree->bf=0;ptree=p2;ptree->bf=0;taller=0;int insert(pnode &ptree,Elemtype e,int &taller)if (ptree=NULL)ptree=new node;ptree->data=e;ptree->lchild=ptree->rchild=NULL;ptree->bf=0;taller=1;elseif (strcmp(e.NO,ptree->data.NO)=0)taller=0;return 0;if(strcmp(e.NO,ptree->data.NO)<0)if(insert(ptree->lchild,e,taller)=0)return 0;if(taller=1)left1(ptree,taller);elseif(insert(ptree->rchild,e,taller)=0)return 0;if(taller=1)right1(ptree,taller);return 1;void left2(pnode &ptree,int &taller)pnode p1,p2;if (ptree->bf=1)ptree->bf=0;taller=1;else if (ptree->bf=0)ptree->bf=-1;taller=0;elsep1=ptree->rchild;if(p1->bf=0)ptree->rchild=p1->lchild;p1->lchild=ptree;p1->bf=1;ptree->bf=-1;ptree=p1;taller=0;else if (p1->bf=-1)ptree->rchild=p1->lchild;p1->lchild=ptree;ptree->bf=p1->bf=0;ptree=p1;taller=1;elsep2=p1->lchild;p1->lchild=p2->rchild;p2->rchild=p1;ptree->rchild=p2->lchild;p2->lchild=ptree;if (p2->bf=0)ptree->bf=0;p1->bf=0;else if (p2->bf=-1)ptree->bf=1;p1->bf=0;elseptree->bf=0;p1->bf=-1;p2->bf=0;ptree=p2;taller=1;void right2(pnode &ptree,int &taller)pnode p1,p2;if (ptree->bf=-1)ptree->bf=0;taller=1;else if (ptree->bf=0)ptree->bf=1;taller=0;elsep1=ptree->lchild;if(p1->bf=0)ptree->lchild=p1->rchild;p1->rchild=ptree;p1->bf=-1;ptree->bf=1;ptree=p1;taller=0;else if (p1->bf=1)ptree->lchild=p1->rchild;p1->rchild=ptree;ptree->bf=p1->bf=0;ptree=p1;taller=1;elsep2=p1->rchild;p1->rchild=p2->lchild;p2->lchild=p1;ptree->lchild=p2->rchild;p2->rchild=ptree;if (p2->bf=0)ptree->bf=0;p1->bf=0;else if (p2->bf=1)ptree->bf=-1;p1->bf=0;elseptree->bf=0;p1->bf=1;p2->bf=0;ptree=p2;taller=1;void delete2(pnode q,pnode &r,int &taller)if (r->rchild=NULL)q->data=r->data;q=r;r=r->lchild;delete q;taller=1;elsedelete2(q,r->rchild,taller);if(taller=1)right2(r,taller);int delete1(pnode &ptree,char x10,int &taller)int k;pnode q;if(ptree=NULL)return 0;else if (strcmp(x,ptree->data.NO)<0)k=delete1(ptree->lchild,x,taller);if(taller=1)left2(ptree,taller);return k;else if (strcmp(x,ptree->data.NO)>0)k=delete1(ptree->rchild,x,taller);if(taller=1)right2(ptree,taller);return k;elseq=ptree;if (ptree->rchild=NULL)ptree=ptree->lchild;delete q;taller=1;else if (ptree->lchild=NULL)ptree=ptree->rchild;delete q;taller=1;elsedelete2(q,q->lchild,taller);if(taller=1)left2(q,taller);ptree=q;return 1;void create(pnode &ptree,int n)int j;Elemtype e;printf("输入学生信息:姓名,学号,生日,班级,性别n");for(int i=0;i<n;i+)scanf("%s%s%s%s%s",e.name,e.NO,e.birt,e.clas,e.sex);insert(ptree,e,j);void display(pnode ptree)if (ptree)display(ptree->lchild);printf("%-10s %-10s %-10s %-10s %-10sn",ptree->data.name,ptree->data.NO,ptree->data.birt,ptree->data.clas,ptree->data.sex);display(ptree->rchild);void find(pnode ptree,char *ch)if(ptree)if (strcmp(ptree->data.name,ch)=0)printf("姓名 学号 生日 班级 性别n");printf("%-10s %-10s %-10s %-10s %-10snn",ptree->data.name,ptree->data.NO,ptree->data.birt,ptree->data.clas,ptree->data.sex);return ;elsefind(ptree->lchild,ch);find(ptree->rchild,ch);void save(pnode ptree,FILE *p)if (ptree)save(ptree->lchild,p);p=fopen("student.txt","a");fprintf(p,"%st%st%st%st%sn",ptree->data.name,ptree->data.NO,ptree->data.birt,ptree->data.clas,ptree->data.sex);fclose(p);save(ptree->rchild,p);void read(pnode &ptree,FILE *p)int j;char c;Elemtype e;if(p=fopen("student.txt","r")=NULL)return ;while(c=fgetc(p)!=EOF)fscanf(p,"%s%s%s%s%s",e.name,e.NO,e.birt,e.clas,e.sex);insert(ptree,e,j);fclose(p);void Print(pnode ptree,int indent)if (ptree = NULL) return;for(int i = 0; i < indent; i+)printf("|- ");printf("%-10s %-10s %-10s %-10s %-10sn",ptree->data.name,ptree->data.NO,ptree->data.birt,ptree->data.clas,ptree->data.sex);Print(ptree->lchild, indent+1);Print(ptree->rchild, indent+1);void change(pnode ptree,Elemtype e)if (ptree=NULL)return ;elseif (strcmp(ptree->data.NO,e.NO)=0)ptree->data=e;return;elsechange(ptree->lchild,e);change(ptree->rchild,e);void male(pnode ptree)char m2="m"if (ptree)male(ptree->lchild);if(strcmp(ptree->data.sex,m)=0)printf("%-10s %-10s %-10s %-10s %-10sn",ptree->data.name,ptree->data.NO,ptree->data.birt,ptree->data.clas,ptree->data.sex);male(ptree->rchild);void fmale(pnode ptree)char m2="f"if (ptree)fmale(ptree->lchild);if(strcmp(ptree->data.sex,m)=0)printf("%-10s %-10s %-10s %-10s %-10sn",ptree->data.name,ptree->data.NO,ptree->data.birt,ptree->data.clas,ptree->data.sex);fmale(ptree->rchild);void main()int z; FILE *p=NULL;pnode ptree=NULL;printf("请选择:n1-载入学生信息n2-重新创建信息n"); scanf("%d",&z);if(z=1)read(ptree,p); Elemtype e,a;int t=1;char ch10,x10;int j,k,n;while (t)printf("* 平衡二叉树实现学生基本信息管理*n");printf("*nn");printf("1-创建n");printf("2-插入n");printf("3-删除n");printf("4-修改n");printf("5-查找n");printf("6-显示n");printf("7-排序n"); printf("8-分组n");printf("9-保存n");printf("0-退出n");printf("输入操作选项(0-9)n");scanf("%d",&k);switch (k) case 1:printf("输入要创建的学生数n");scanf("%d",&n);create(ptree,n);break; case 2:printf("输入学生信息: 姓名,学号,生日,班级,性别n");scanf("%s%s%s%s%s",e.name,e.NO,e.birt,e.clas,e.sex);insert(ptree,e,j);break;case 3:printf("输入要删除的学生的学号n");scanf("%s",x);delete1(ptree,x,j);break;case 4:printf("输入修改后的信息: 姓名,学号,生日,班级,性别n");scanf("%s%s%s%s%s",a.name,a.NO,a.birt,a.clas,a.sex);change(ptree,a); break;case 5:printf("输入要查找的学生姓名n");scanf("%s",ch);find(ptree,ch);printf("n");break;case 6: printf("凹入显示:nn");Print(ptree,0);printf("n");break;case 7:printf("按学号排序结果如下:nn"); printf("姓名 学号 生日 班级 性别n");display(ptree);printf("n");break;case 8:printf("男生基本信息如下:nn");male(ptree);printf("n");printf("女生基本信息如下:nn");fmale(ptree);printf("n");break;case 9:p=fopen("student.txt","wt");fprintf(p,"n");fclose(p);save(ptree,p);printf("学生信息已存入文件nn");break;case 0:printf("谢谢使用,再见n");t=0;break;专心-专注-专业