数据结构实验报告.docx
- 文档编号:1604961
- 上传时间:2022-10-23
- 格式:DOCX
- 页数:16
- 大小:107.60KB
数据结构实验报告.docx
《数据结构实验报告.docx》由会员分享,可在线阅读,更多相关《数据结构实验报告.docx(16页珍藏版)》请在冰豆网上搜索。
数据结构实验报告
数据结构实验报告
一.题目要求
1)编程实现二叉排序树,包括生成、插入,删除;
2)对二叉排序树进行先根、中根、与后根非递归遍历;
3)每次对树得修改操作与遍历操作得显示结果都需要在屏幕上用树得形状表示出来。
4)分别用二叉排序树与数组去存储一个班(50人以上)得成员信息(至少包括学号、姓名、成绩3项),对比查找效率,并说明在什么情况下二叉排序树效率高,为什么?
二.解决方案
对于前三个题目要求,我们用一个程序实现代码如下
#include〈windows、h>
#include 〈stdio、h〉
#include #include"Stack、h” //栈得头文件,没有用上 typedef int ElemType; //数据类型 typedef int Status; //返回值类型 //定义二叉树结构 typedefstructBiTNode{ ElemType data; //数据域 struct BiTNode*lChild,*rChild;//左右子树域 }BiTNode,*BiTree; intInsertBST(BiTree&T,intkey){//插入二叉树函数 ﻩif(T==NULL) { ﻩT=(BiTree)malloc(sizeof(BiTNode)); T—〉data=key; ﻩT->lChild=T-〉rChild=NULL; ﻩreturn1; } else if(key<T—〉data){ InsertBST(T-〉lChild,key); } ﻩelseif(key>T—>data){ ﻩﻩInsertBST(T—〉rChild,key); } ﻩelse ﻩreturn0; } BiTreeCreateBST(int a[],intn){//创建二叉树函数 BiTreebst=NULL; inti=0; ﻩwhile(i<n){ ﻩInsertBST(bst,a[i]); ﻩi++; ﻩ} returnbst; } intDelete(BiTree&T) { BiTreeq,s; if(! (T)—〉rChild){//右子树为空重接它得左子树 ﻩq=T; ﻩT=(T)-〉lChild; ﻩfree(q); ﻩ}else{ if(! (T)—〉lChild){//若左子树空则重新接它得右子树 ﻩﻩq=T; ﻩT=(T)-〉rChild; ﻩ}else{ ﻩq=T; ﻩs=(T)—>lChild; ﻩﻩwhile(s—〉rChild){ ﻩﻩﻩﻩq=s;s=s-〉rChild; ﻩﻩ} ﻩ(T)—>data=s-〉data;//s指向被删除结点得前驱 ﻩif(q! =T) ﻩﻩq—>rChild=s->lChild; ﻩelse ﻩﻩq-〉lChild=s->lChild; ﻩﻩﻩfree(s); } } ﻩreturn 1; } //删除函数,在T中删除key元素 int DeleteBST(BiTree&T,intkey){ if(! T)return0; else{ ﻩif(key==(T)—>data)returnDelete(T); ﻩﻩelse{ ﻩﻩif(key<(T)->data) ﻩreturnDeleteBST(T—〉lChild,key); ﻩﻩﻩelse ﻩﻩreturnDeleteBST(T-〉rChild,key); ﻩ} ﻩ} } intPosttreeDepth(BiTreeT){//求深度 ﻩinthr,hl,max; ﻩif(! T==NULL){ ﻩhl=PosttreeDepth(T->lChild); hr=PosttreeDepth(T—>rChild); ﻩmax=hl>hr? hl: hr; ﻩreturnmax+1; ﻩ} ﻩelse return0; } voidprinttree(BiTreeT,intnlayer){//打印二叉树 if(T==NULL)return; ﻩprinttree(T->rChild,nlayer+1); for(int i=0;i<nlayer;i++){ ﻩprintf(” "); ﻩ} printf("%d\n",T—〉data); printtree(T->lChild,nlayer+1); } voidPreOrderNoRec(BiTreeroot)//先序非递归遍历 { ﻩBiTree p=root; BiTreestack[50]; intnum=0; while(NULL! =p||num>0) { ﻩwhile(NULL! =p) { ﻩﻩprintf(”%d”,p-〉data); ﻩﻩﻩstack[num++]=p; ﻩﻩﻩp=p—〉lChild; ﻩ} ﻩﻩnum——; p=stack[num]; ﻩﻩp=p->rChild; } printf(”\n"); } void InOrderNoRec(BiTreeroot)//中序非递归遍历 { BiTreep=root; intnum=0; BiTreestack[50]; while(NULL! =p||num〉0) ﻩ{ ﻩwhile(NULL! =p) ﻩ{ ﻩstack[num++]=p; ﻩﻩp=p->lChild; ﻩ} num-—; ﻩﻩp=stack[num]; ﻩprintf("%d",p—>data); p=p—>rChild; ﻩ} printf(”\n"); } void PostOrderNoRec(BiTreeroot)//后序非递归遍历 { BiTreep=root; BiTreestack[50]; intnum=0; BiTree have_visited=NULL; while(NULL! =p||num>0) ﻩ{ ﻩwhile(NULL! =p) { ﻩﻩstack[num++]=p; ﻩﻩﻩp=p—〉lChild; ﻩ} ﻩp=stack[num—1]; ﻩif(NULL==p—>rChild||have_visited==p-〉rChild) { printf("%d ”,p-〉data); ﻩﻩnum-—; ﻩhave_visited=p; ﻩp=NULL; ﻩ} ﻩelse { ﻩﻩp=p—>rChild; } } printf(”\n"); } int main(){//主函数 printf(" -—-————————---—--—-——二叉排序树得实现-——--—--—--——-—-——-"); printf(”\n"); int layer; ﻩinti; intnum; printf(”输入节点个数: "); ﻩscanf(”%d”,&num); ﻩprintf("依次输入这些整数(要不相等)"); ﻩint *arr=(int*)malloc(num*sizeof(int)); for(i=0;i〈num;i++){ scanf("%d",arr+i); ﻩ} BiTree bst=CreateBST(arr,num); ﻩprintf("\n"); printf(”二叉树创建成功! ”); printf(”\n”); layer=PosttreeDepth(bst); printf("树状图为: \n”); printtree(bst,layer); ﻩint j; intT; ﻩintK; for(;;){ loop: ﻩprintf("\n”); printf(" ***********************按提示输入操作符************************: ”); printf(”\n”); printf(" 1: 插入节点2: 删除节点3: 打印二叉树4: 非递归遍历二叉树5: 退出"); ﻩscanf(”%d",&j); ﻩswitch(j){ ﻩﻩcase1: ﻩﻩprintf("输入要插入得节点: ”); scanf("%d”,&T); ﻩInsertBST(bst,T); ﻩﻩprintf(”插入成功! ”); printf(”树状图为: \n"); ﻩprinttree(bst,layer); ﻩﻩbreak; case2: ﻩﻩprintf("输入要删除得节点”); ﻩscanf("%d”,&K); DeleteBST(bst,K); ﻩﻩprintf(”删除成功! "); printf(”树状图为: \n"); printtree(bst,layer); ﻩbreak; case 3: ﻩﻩlayer=PosttreeDepth(bst); ﻩﻩprinttree(bst,layer); ﻩﻩbreak; ﻩcase 4: ﻩﻩﻩprintf(”非递归遍历二叉树"); ﻩprintf("先序遍历: \n”); PreOrderNoRec(bst); ﻩprintf(”中序遍历: \n"); ﻩﻩInOrderNoRec(bst); ﻩprintf(”后序遍历: \n”); ﻩﻩPostOrderNoRec(bst); ﻩﻩﻩprintf(”树状图为: \n"); ﻩprinttree(bst,layer); ﻩbreak; ﻩcase5: ﻩprintf("程序执行完毕! "); ﻩreturn0; } gotoloop; ﻩ} ﻩreturn0; } 对于第四小问,要储存学生得三个信息,需要把上面程序修改一下,二叉树结构变为 typedef intElemType; //数据类型 typedef stringSlemType; typedef int Status; //返回值类型 //定义二叉树结构 typedef structBiTNode{ SlemTypeﻩname; ElemTypescore; ElemTypeno; //数据域 structBiTNode*lChild,*rChild;//左右子树域 }BiTNode,*BiTree; 参数不就是key,而就是另外三个 intInsertBST(BiTree&T,int no,intscore,stringname){//插入二叉树函数 ﻩif(T==NULL) { ﻩT =(BiTree)malloc(sizeof(BiTNode)); T—>no=no;T—〉name=name;T-〉score=score; ﻩT->lChil
- 配套讲稿:
如PPT文件的首页显示word图标,表示该PPT已包含配套word讲稿。双击word图标可打开word文档。
- 特殊限制:
部分文档作品中含有的国旗、国徽等图片,仅作为作品整体效果示例展示,禁止商用。设计者仅对作品中独创性部分享有著作权。
- 关 键 词:
- 数据结构 实验 报告