ImageVerifierCode 换一换
格式:DOCX , 页数:30 ,大小:158.37KB ,
资源ID:10397911      下载积分:3 金币
快捷下载
登录下载
邮箱/手机:
温馨提示:
快捷下载时,用户名和密码都是您填写的邮箱或者手机号,方便查询和重复下载(系统自动生成)。 如填写123,账号就是123,密码也是123。
特别说明:
请自助下载,系统不会自动发送文件的哦; 如果您已付费,想二次下载,请登录后访问:我的下载记录
支付方式: 支付宝    微信支付   
验证码:   换一换

加入VIP,免费下载
 

温馨提示:由于个人手机设置不同,如果发现不能下载,请复制以下地址【https://www.bdocx.com/down/10397911.html】到电脑端继续下载(重复下载不扣费)。

已注册用户请登录:
账号:
密码:
验证码:   换一换
  忘记密码?
三方登录: 微信登录   QQ登录  

下载须知

1: 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。
2: 试题试卷类文档,如果标题没有明确说明有答案则都视为没有答案,请知晓。
3: 文件的所有权益归上传用户所有。
4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
5. 本站仅提供交流平台,并不能对任何下载内容负责。
6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。

版权提示 | 免责声明

本文(数据结构实验3 二叉树层次遍历.docx)为本站会员(b****8)主动上传,冰豆网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对上载内容本身不做任何修改或编辑。 若此文所含内容侵犯了您的版权或隐私,请立即通知冰豆网(发送邮件至service@bdocx.com或直接QQ联系客服),我们立即给予删除!

数据结构实验3 二叉树层次遍历.docx

1、数据结构实验3 二叉树层次遍历数据结构实验3实验报告实验项目3:二叉树层次遍历学号姓名课程号实验地点指导教师时间评语: 按时完成实验;实验内容和过程记录完整;回答问题完整、正确;实验报告的撰写认真、格式符合要求;无抄袭的行为。成绩教师签字二叉树从左至右,从上至下层次遍历1、预习要求:二叉树结构定义和层次遍历。2、实验目的: (1)了解二叉树结构层次遍历概念;(2)理解二叉树二种不同层次遍历过程;(3)掌握二叉树层次遍历算法程序。3、实验内容及要求:(1)建立包含10个结点的二叉树(树结构和数据元素的值由自己设定);(2)完成二叉树从左至右,从上至下层次遍历程序;(3)给出程序和遍历程序的结果。

2、4、实验设备(环境)及要求硬件:支持 Intel Pentium 及其以上 CPU ,内存 128MB 以上、硬盘 1GB 以上容量的微机。软件:配有 Windows98/2000/XP 操作系统,安装 Visual C+ 。5、实验时间:10学时6、该文档的文件名不要修改,存入 命名的文件夹中7、该表中的数据只需填空,已有内容不要修改实验结果(运行结果界面及源程序,运行结果界面放在前面):#include #include #include #include #define STUDENT ETypestruct STUDENT char number8; char name8; char

3、sex3; int age; char place20;struct BinaryTreeNode EType data; BinaryTreeNode *LChild; BinaryTreeNode *RChild;struct QType BinaryTreeNode *ptr;struct Queue QType *element; int front; int rear; int maxsize;struct Node_Ptr BinaryTreeNode *ptr; ;void CreatQueue(Queue &Q,int MaxQueueSize)/创建队列 Q.maxsize=

4、MaxQueueSize; Q.element=new QTypeQ.maxsize+1; Q.front=0; Q.rear=0;bool IsEmpty(Queue &Q)/判断队列是否为空 if(Q.front=Q.rear) return true; return false;bool IsFull(Queue &Q)/判断队列是否为满 if(Q.front=(Q.rear+1)%(Q.maxsize+1) return true; return false;bool GetFront(Queue &Q,QType &result)/取出队头元素 if(IsEmpty(Q) retur

5、n false; result=Q.element(Q.front+1)%(Q.maxsize+1); return true;bool GetRear(Queue &Q,QType &result)/取出队尾元素 if(IsEmpty(Q) return false; result=Q.elementQ.rear; return true;bool EnQueue(Queue &Q,QType &x)/元素进队 if(IsFull(Q) return false; Q.rear=(Q.rear+1)%(Q.maxsize+1); Q.elementQ.rear=x; return true;

6、bool DeQueue(Queue &Q,QType &result)/元素出队 if(IsEmpty(Q) return false; Q.front=(Q.front+1)%(Q.maxsize+1); result=Q.elementQ.front; return true;BinaryTreeNode *MakeNode(EType &x)/构造节点 BinaryTreeNode *ptr; ptr=new BinaryTreeNode; if(!ptr) return NULL; ptr-data=x; ptr-LChild=NULL; ptr-RChild=NULL; retur

7、n ptr;void MakeBinaryTree(BinaryTreeNode *root, BinaryTreeNode *left,BinaryTreeNode *right)/构造二叉树之间的关系 root-LChild=left; root-RChild=right;void OutputBinaryTreeNode(BinaryTreeNode *p)/输出节点 cout data.number data.name data.sex data.age data.placeendl; coutendl;void LevelOrder_LtoR_UtoD(BinaryTreeNode

8、*BT)/从左至右,从上至下按层次遍历一棵二叉树 Queue Q; QType temp; BinaryTreeNode *p; int maxqueuesize=50; CreatQueue(Q,maxqueuesize); p=BT; temp.ptr=p; EnQueue(Q,temp); coutendl; cout 学号 姓名 性别 年龄 住址 endl; cout =LChild) temp.ptr=p-LChild; EnQueue(Q,temp); if(p-RChild) temp.ptr=p-RChild; EnQueue(Q,temp); void LevelOrder_

9、RtoL_UtoD(BinaryTreeNode *BT)/从右至左,从上至下按层次遍历一棵二叉树 Queue Q; QType temp; BinaryTreeNode *p; int maxqueuesize=50; CreatQueue(Q,maxqueuesize); p=BT; temp.ptr=p; EnQueue(Q,temp); coutendl; cout 学号 姓名 性别 年龄 住址 endl; cout =RChild) temp.ptr=p-RChild; EnQueue(Q,temp); if(p-LChild) temp.ptr=p-LChild; EnQueue(

10、Q,temp); void LevelOrder_LtoR_DtoU(BinaryTreeNode *BT)/从左至右,从下至上按层次遍历一棵二叉树 Queue Q; QType temp; BinaryTreeNode *p; int maxqueuesize=50; CreatQueue(Q,maxqueuesize); int frontkeep=Q.front; p=BT; temp.ptr=p; EnQueue(Q,temp); coutendl; cout 学号 姓名 性别 年龄 住址 endl; cout =RChild) temp.ptr=p-RChild; EnQueue(Q

11、,temp); if(p-LChild) temp.ptr=p-LChild; EnQueue(Q,temp); for(int i=Q.rear;ifrontkeep;i-) OutputBinaryTreeNode(Q.elementi.ptr);void LevelOrder_RtoL_DtoU(BinaryTreeNode *BT)/从右至左,从下至上按层次遍历一棵二叉树 Queue Q; QType temp; BinaryTreeNode *p; int maxqueuesize=50; CreatQueue(Q,maxqueuesize); int frontkeep=Q.fro

12、nt; p=BT; temp.ptr=p; EnQueue(Q,temp); coutendl; cout 学号 姓名 性别 年龄 住址 endl; cout =LChild) temp.ptr=p-LChild; EnQueue(Q,temp); if(p-RChild) temp.ptr=p-RChild; EnQueue(Q,temp); for(int i=Q.rear;ifrontkeep;i-) OutputBinaryTreeNode(Q.elementi.ptr);int BinaryHeight(BinaryTreeNode *BT)/返回二叉树的高度 if(!BT) ret

13、urn 0; int HighL=BinaryHeight(BT-LChild); int HighR=BinaryHeight(BT-RChild); if(HighLHighR) return +HighL; else return +HighR;void BinaryDelete(BinaryTreeNode *BT)/二叉树删除算法 if(BT) BinaryDelete(BT-LChild); BinaryDelete(BT-RChild); delete BT; int BinaryNodeSum(BinaryTreeNode *BT)/计算二叉树中的节点数 if(!BT) ret

14、urn 0; Queue Q; QType temp; BinaryTreeNode *p; int maxqueuesize=50; int index=0; CreatQueue(Q,maxqueuesize); p=BT; temp.ptr=p; EnQueue(Q,temp); while(p) if(!DeQueue(Q,temp) break; p=temp.ptr; /出队成功 index+; if(p-LChild) temp.ptr=p-LChild; EnQueue(Q,temp); if(p-RChild) temp.ptr=p-RChild; EnQueue(Q,tem

15、p); return index;void DigitalToString(char str,int n) char temp; char k=1; int i=0; while (n & i80) k=n%10+48; n=n/10; stri=k; i+; stri=0; int len=strlen(str); for (i=0;ilen/2;i+) temp=stri; stri=strlen-i-1; strlen-i-1=temp; char *GetOuputInformationString(int WidthOfData, char *OutputInformation, c

16、har *outputstring)/将一个元素的字符串OutputInformation转换为宽度为WidthOfData的等长字符串outputstring /例如,姓名是由四个字符组成,而WidthOfData为8,则在姓名字符串的两边分别连接两个字符,形成8个长度的字符串 int left_space,right_space; int i; char left_space_string16=; char right_space_string16=; int add_space; int len_OutputInformation=strlen(OutputInformation); /

17、计算OutputInformation的字符个数 add_space=WidthOfData - len_OutputInformation; /计算需要补充的字符个数 left_space=add_space/2; /计算OutputInformation左边需要补充的字符个数 right_space=add_space-left_space; /计算OutputInformation右边需要补充的字符个数 for(i=1;i=left_space;i+) /形成OutputInformation左边需要补充的空格字符串 strcat(left_space_string, ); for(i=

18、1;idata.name); break; case 2: strcat(outputInformation,elementk.ptr-data.number); break; case 3: strcat(outputInformation,elementk.ptr-data.place); break; case 4: strcat(outputInformation,elementk.ptr-data.sex); break; case 5: DigitalToString(outputInformation,elementk.ptr-data.age); break; default:

19、 break; return outputInformation;/输出二叉树void OutputBinaryTree(BinaryTreeNode *BT) Node_Ptr temp,*element; BinaryTreeNode *p; int MaxSize; int BinaryTreeHigh; int i,j,k; BinaryTreeHigh=BinaryHeight(BT); MaxSize=(int) pow(2,BinaryTreeHigh); element = new Node_Ptr MaxSize+1; /定义一个用于存放二叉树结点指针的数组 for (i=1

20、;i=MaxSize;i+) /对指针数组初始化,初值设为NULL elementi.ptr=NULL; p = BT; temp.ptr = p; if (p) element1=temp; for (i=1;iLChild ) /&!p-Lflag/线索树特有的处理与一般二叉树不同之处 temp.ptr = p-LChild; element2*i=temp; if (p-RChild ) /&!p-Rflag/线索树特有的处理与一般二叉树不同之处 temp.ptr = p-RChild; element2*i+1=temp; int WidthOfData=5; int Interval

21、OfData=3;/ coutWidthOfData=WidthOfDataendl;/ coutIntervalOfData=IntervalOfDataendl;/ coutBinaryTreeHigh=BinaryTreeHighendl; int position_of_central617; /存放每一元素输出位置中心(距离屏幕左边界的字符数) int row,col; /二维数组的行列下标变量 for(i=0;i=BinaryTreeHigh;i+) /存放每一元素输出位置中心(距离屏幕左边界的字符数),初值为0 for(j=0;j=pow(2,BinaryTreeHigh-1);

22、j+) position_of_centralij=0; for(i=1;i=pow(2,BinaryTreeHigh)-1;i+) /对深度为BinaryTreeHigh的满二叉树的所有结点计算每个结点输出的中心点位置 k=i*2; /k指向i的左子树根结点 while (k=pow(2,BinaryTreeHigh)-1) /k不断推进到i编号的结点左子树中最右子孙结点 k=2*k+1; k=k/2; /k的值就是i编号的结点左子树中最右子孙结点的编号 j=(int)(k-(pow(2,(BinaryTreeHigh-1)-1); /由k编号的结点换算出该结点是底层中从左至右第j个结点右上

23、方 /即编号为k的结点中心点垂直线左边最底层上有j个结点 row=(int)(log10(i)/log10(2)+1); /计算中心点值存放在position_of_centralrowcol中的row col=(int)(i-(pow(2,(row-1)-1); /计算中心点值存放在position_of_centralrowcol中的col if(row=BinaryTreeHigh) /计算底层i结点距离左边界的字符数,左边无间隙 position_of_centralrowcol= (j-1)*WidthOfData + (j-1)*IntervalOfData + (WidthOfData/2 +1); else /计算非底层i结点距离左边界的字符数, position_of_centralrowcol=j*WidthOfData + (j-1)*IntervalOfData + (IntervalOfData/2 +1); char prespace100; int m; int kk; int kkk; int item_max=5; coutendl; for(i=1;i=BinaryTreeHigh;i+) kkk=(i

copyright@ 2008-2022 冰豆网网站版权所有

经营许可证编号:鄂ICP备2022015515号-1