数据结构 树的应用.doc
上传人:yy****24 上传时间:2024-09-10 格式:DOC 页数:5 大小:52KB 金币:16 举报 版权申诉
预览加载中,请您耐心等待几秒...

数据结构 树的应用.doc

数据结构树的应用.doc

预览

在线预览结束,喜欢就下载吧,查找使用更方便

16 金币

下载此文档

如果您无法下载资料,请参考说明:

1、部分资料下载需要金币,请确保您的账户上有足够的金币

2、已购买过的文档,再次下载不重复扣费

3、资料包下载后请先用软件解压,在使用对应软件打开

--《数据结构》实验报告实验三、树结构的应用一、实验目的熟练掌握二叉树的存储结构以及进行遍历的各种算法,并根据实际问题的要求灵活运用。二、实验内容本次实验仍以班级学生信息作为管理对象,以学生信息中的学生成绩为关键字,生成一棵二叉排序树,查找特定结点。三、完成情况typedefintKeyType;typedefstructnode{KeyTypescore;charnum[8];charname[8];chargender[3];structnode*lchild,*rchild;}BSTNode;typedefBSTNode*BSTree;voidInsertBST(BSTree*q,BSTNode*student){BSTNode*f,*p=*q;while(p){f=p;p=((student->score)<=(p->score))?p->lchild:p->rchild;}p=(BSTNode*)malloc(sizeof(BSTNode));p->score=student->score;strcpy(p->num,student->num);strcpy(p->name,student->name);strcpy(p->gender,student->gender);p->lchild=p->rchild=NULL;if(*q==NULL)*q=p;elseif(student->score<=f->score)f->lchild=p;elsef->rchild=p;}BSTreeCreateBST(void){BSTreeT=NULL;BSTNode*student;inta=1;student=(BSTNode*)malloc(sizeof(BSTNode));printf("请输入学生信息:\n");while(a){printf("学号\t姓名\t性别\t成绩\n");scanf("%s%s%s%d",student->num,student->name,student->gender,&student->score);InsertBST(&T,student);printf("继续?1=是0=否\n");scanf("%d",&a);}returnT;}voidInOrder(BSTreeroot){if(root!=NULL){InOrder(root->lchild);printf("%s\t%s\t%s\t%d\n",root->num,root->name,root->gender,root->score);InOrder(root->rchild);}}BSTNode*SearchBST(BSTreeT,KeyTypestuScore){if(T==NULL||stuScore==T->score)returnT;if(stuScore<=T->score)returnSearchBST(T->lchild,stuScore);if(stuScore>T->score)returnSearchBST(T->rchild,stuScore);}voidmain(){BSTreeroot;BSTNode*student;KeyTypestuScore;printf("\n生成二叉排序树:\n");root=CreateBST();printf("\n中序遍历二叉排序树,输出结果\n");printf("\n学号(8)姓名(8)性别(3)成绩\n");printf("-------------------------------------------\n");InOrder(root);printf("\n利用二叉排序树进行查找:\n");printf("\n请输入要查询的学生成绩:\n");scanf("%d",&stuScore);student=SearchBST(root,stuScore);if(!student)printf("没有查找到成绩为%d的学生");else{printf("成绩为%d的学生信息:");printf("%s,%s,%s,%d\n",student->num,student->name,student->gender,student->score);}getchar();}四、实验结果五、问题与解决BSTreeCreateBST函数中的循环部分起初总是执行出错,设置的循环判断语句没有起到作用,用的是字符型的,最后换成整形1、0对应是否判