数据结构实验3二叉树层次遍历_第1页
数据结构实验3二叉树层次遍历_第2页
数据结构实验3二叉树层次遍历_第3页
数据结构实验3二叉树层次遍历_第4页
数据结构实验3二叉树层次遍历_第5页
已阅读5页,还剩16页未读 继续免费阅读

下载本文档

版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领

文档简介

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

2、备(环境)及要求硬件:支持IntelPentium及其以上CPU,内存128MB以上、硬盘1GB以上容量的微机。软件:配有Windows98/2000/XP操作系统,安装VisualC+。5、实验时间:10学时6、该文档的文件名不要更正,存入命名的文件夹中7、该表中的数据只需填空,已有内容不要更正实验结果(运行结果界面及源程序,运行结果界面放在前面):数据构造实验报告二一二年数据构造实验报告二一二年数据构造实验报告二一二年#include#include#include#include#defineSTUDENTETypestructSTUDENTcharcharcharintcharnumb

3、er8;name8;sex3;age;place20;structBinaryTreeNodeETypedata;BinaryTreeNode*LChild;BinaryTreeNode*RChild;structQTypeBinaryTreeNode*ptr;structQueueQType*element;intfront;intrear;intmaxsize;structNode_PtrBinaryTreeNode*ptr;voidCreatQueue(Queue&Q,intMaxQueueSize)/创办行列Q.maxsize=MaxQueueSize;数据构造实验报告二一二年Q.el

4、ement=newQTypeQ.maxsize+1;Q.front=0;Q.rear=0;boolIsEmpty(Queue&Q)/判断行列可否为空if(Q.front=Q.rear)returntrue;returnfalse;boolIsFull(Queue&Q)/判断行列可否为满if(Q.front=(Q.rear+1)%(Q.maxsize+1)returntrue;returnfalse;boolGetFront(Queue&Q,QType&result)/取出队头元素if(IsEmpty(Q)returnfalse;result=Q.element(Q.front+1)%(Q.ma

5、xsize+1);returntrue;boolGetRear(Queue&Q,QType&result)/取出队尾元素if(IsEmpty(Q)returnfalse;result=Q.elementQ.rear;returntrue;boolEnQueue(Queue&Q,QType&x)/元素进队if(IsFull(Q)returnfalse;Q.rear=(Q.rear+1)%(Q.maxsize+1);Q.elementQ.rear=x;returntrue;boolDeQueue(Queue&Q,QType&result)/元素出队if(IsEmpty(Q)returnfalse;

6、Q.front=(Q.front+1)%(Q.maxsize+1);result=Q.elementQ.front;数据构造实验报告二一二年returntrue;BinaryTreeNode*MakeNode(EType&x)/构造节点BinaryTreeNode*ptr;ptr=newBinaryTreeNode;if(!ptr)returnNULL;ptr-data=x;ptr-LChild=NULL;ptr-RChild=NULL;returnptr;voidMakeBinaryTree(BinaryTreeNode*root,BinaryTreeNode*left,BinaryTree

7、Node*right)/构造二叉树之间的关系root-LChild=left;root-RChild=right;voidOutputBinaryTreeNode(BinaryTreeNode*p)/输出节点data.sexdata.agedata.placeendl;coutendl;voidLevelOrder_LtoR_UtoD(BinaryTreeNode*BT)/从左至右,从上至下按层次遍历一棵二叉树QueueQ;QTypetemp;BinaryTreeNode*p;intmaxqueuesize=50;CreatQueue(Q,max

8、queuesize);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);voidLevelOrder_RtoL_UtoD(BinaryTreeNode*BT)/从右至左,从上至下按层次遍历一棵二叉树QueueQTypeBinaryTreeNodeQ;temp;*p;intmaxqueuesize=50;CreatQueue(Q,maxqueue

9、size);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(Q,temp);voidLevelOrder_LtoR_DtoU(BinaryTreeNode*BT)/从左至右,从下至上按层次遍历一棵二叉树QueueQ;QTypetemp;BinaryTreeNode*p;intmaxqueuesize=50;CreatQueue(Q,m

10、axqueuesize);intfrontkeep=Q.front;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(Q,temp);for(inti=Q.rear;ifrontkeep;i-)OutputBinaryTreeNode(Q.elementi.ptr);voidLevelOrder_RtoL_DtoU(BinaryTreeNode*BT)/从右至

11、左,从下至上按层次遍历一棵二叉树QueueQ;QTypetemp;BinaryTreeNode*p;intmaxqueuesize=50;数据构造实验报告二一二年CreatQueue(Q,maxqueuesize);intfrontkeep=Q.front;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(inti=Q.rear;ifr

12、ontkeep;i-)OutputBinaryTreeNode(Q.elementi.ptr);intBinaryHeight(BinaryTreeNode*BT)/返回二叉树的高度if(!BT)return0;intHighL=BinaryHeight(BT-LChild);intHighR=BinaryHeight(BT-RChild);if(HighLHighR)return+HighL;elsereturn+HighR;voidBinaryDelete(BinaryTreeNode*BT)/二叉树删除算法if(BT)BinaryDelete(BT-LChild);数据构造实验报告二一二

13、年BinaryDelete(BT-RChild);deleteBT;intBinaryNodeSum(BinaryTreeNode*BT)/计算二叉树中的节点数if(!BT)return0;QueueQ;QTypetemp;BinaryTreeNode*p;intmaxqueuesize=50;intindex=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-LCh

14、ild;EnQueue(Q,temp);if(p-RChild)temp.ptr=p-RChild;EnQueue(Q,temp);returnindex;voidDigitalToString(charstr,intn)chartemp;chark=1;inti=0;while(n&i80)k=n%10+48;数据构造实验报告二一二年n=n/10;stri=k;i+;stri=0;intlen=strlen(str);for(i=0;ilen/2;i+)temp=stri;stri=strlen-i-1;strlen-i-1=temp;char*GetOuputInformationStri

15、ng(intWidthOfData,char*OutputInformation,char*outputstring)/将一个元素的字符串OutputInformation变换为宽度为WidthOfData的等长字符串outputstring/比方,姓名是由四个字符组成,而WidthOfData为8,则在姓名字符串的两边分别连接两个字符,形成8个长度的字符串intleft_space,right_space;inti;charleft_space_string16=;charright_space_string16=;intadd_space;intlen_OutputInformation=

16、strlen(OutputInformation);/计算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_s

17、tring,);数据构造实验报告二一二年for(i=1;);break;case2:strcat(outputInformation,elementk.ptr-data.number);break;case3:strcat(outputInformation,elementk.ptr-data.place);break;case4:strcat(outputInformation,elementk.ptr-data.sex);break;case5:DigitalToString(outputInformation,elementk.ptr-data.age);break;

18、default:break;数据构造实验报告二一二年returnoutputInformation;/输出二叉树voidOutputBinaryTree(BinaryTreeNode*BT)Node_Ptrtemp,*element;BinaryTreeNode*p;intMaxSize;intBinaryTreeHigh;inti,j,k;BinaryTreeHigh=BinaryHeight(BT);MaxSize=(int)pow(2,BinaryTreeHigh);element=newNode_PtrMaxSize+1;/定义一个用于存放二叉树结点指针的数组for(i=1;i=Max

19、Size;i+)/对指针数组初始化,初值设为NULLelementi.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;数据构造实验报告二一二年intWidthOfData=5;intIntervalOfData=3;coutWidthOfDat

20、a=WidthOfDataendl;coutIntervalOfData=IntervalOfDataendl;coutBinaryTreeHigh=BinaryTreeHighendl;intposition_of_central617;/存放每一元素输出地址中心(距离屏幕左界线的字符数)introw,col;for(i=0;i=BinaryTreeHigh;i+)/二维数组的行列下标变量/存放每一元素输出地址中心(距离屏幕左界线的字符数),初值为0for(j=0;j=pow(2,BinaryTreeHigh-1);j+)position_of_centralij=0;for(i=1;i=p

21、ow(2,BinaryTreeHigh)-1;i+)/对深度为BinaryTreeHigh的满二叉树的所有结点计算每个结点输出的中心点地址k=i*2;while(k=pow(2,BinaryTreeHigh)-1)/k/k指向i的左子树根结点不断推进到i编号的结点左子树中最右后辈结点k=2*k+1;k=k/2;/k的值就是i编号的结点左子树中最右后辈结点的编号j=(int)(k-(pow(2,(BinaryTreeHigh-1)-1);/由k编号的结点换算出该结点是基层中从左至右第j个结点右上方/即编号为k的结点中心点垂直线左边最基层上有j个结点row=(int)(log10(i)/log10

22、(2)+1);/计算中心点值存放在position_of_centralrowcol中的rowcol=(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*WidthO

23、fData+(j-1)*IntervalOfData+(IntervalOfData/2+1);数据构造实验报告二一二年charprespace100;intm;intkk;intkkk;intitem_max=5;coutendl;for(i=1;i=BinaryTreeHigh;i+)kkk=(int)pow(2,i-1);/kkk是满二叉树中每一层第1个结点的编号for(intitem=1;item=item_max;item+)/输出每个数据元素多个item成员的值inthalf_len_pre_value=0;/同一行前一个输出的元素值长度的一半,同一行第一个输出的元素值前没有值输出

24、,初值为0inthalf_len_now_value=WidthOfData/2;/同一行当前输出的元素值长度的一半,其值向来为WidthOfData的一半kk=kkk;/kk是满二叉树中每一层结点的编号变化,从某层的最左边第一个结点编号kkk开始for(j=1;j=pow(2,i-1);j+)/输出满二叉树中同一层次上的每个数据元素的同一个成员的值charoutputInformation20=;char*OutputInformation;if(elementkk.ptr)/获取每个数据元素的一个成员的值OutputInformation,如姓名、年龄等OutputInformation=

25、GetOuputInformation(item,kk,outputInformation,element);if(position_of_centralij)/输出数据中点值存在时,position_of_centralij非0charoutputstring80=;char*OutputString;名、年龄等,变换为等长/下面语句将每个数据元素的一个成员的值WidthOfData的字符串OutputStringOutputInformation,如姓数据构造实验报告二一二年OutputString=GetOuputInformationString(WidthOfData,OutputI

26、nformation,outputstring);/生成两个孩子从前的空格串prespace/组成:=-strcpy(prespace,);m=(position_of_centralij-position_of_centralij-1-1-half_len_pre_value-half_len_now_value);for(k=1;k=m;k+)strcat(prespace,);coutprespace;coutOutputString;half_len_pre_value=WidthOfData/2;/调整同一行前一个输出的元素值长度为WidthOfData的一半kk+;/if(posi

27、tion_of_centralij)/for(j=1;j=pow(2,i-1);j+)coutendl;/for(intitem=1;item=5;item+)charline3=;charOutputLine80;charmidspace80;intnodenumber;if(i=1)/对深度为BinaryTreeHigh号,计算第i层上初步结点的编号nodenumber的满二叉树从上至下,从左至右的编nodenumber=1;else点编号nodenumber=(int)(pow(2,i)-1)-(pow(2,i-1)-1);nodenumber/第i(i!=1)层上的第1个结for(j=

28、1;j=pow(2,i-1);j+)/输出同一层次上的线条if(i=BinaryTreeHigh)break;/最基层下面没有线条,所以break/生成两个数据元素从前的前导空格串prespacestrcpy(prespace,);m=(position_of_centrali+1j-position_of_centrali+1j-1-1);数据构造实验报告二一二年for(k=1;k=m;k+)strcat(prespace,);/计算左右两个孩子之间一半的连线OutLine,由若干个line组成/注意line是汉字字符,一个占两个字符位,m是左右两个孩子之间的line数,所以m要除4strc

29、py(OutputLine,);m=(position_of_centrali+1j+1-position_of_centrali+1j-1)/4;for(k=1;k=m;k+)strcat(OutputLine,line);/计算左右两个孩子之间一半的空格字符的个数m,,所以要除2。由m形成左右两个孩子之间的空格串midspacestrcpy(midspace,);m=(position_of_centrali+1j+1-position_of_centrali+1j-1)/2;for(k=1;k=m;k+)strcat(midspace,);if(element2*nodenumber.p

30、tr)&(element2*nodenumber+1.ptr)/左右子树都存在时,左右两个孩子上的连接线coutprespaceOutputLineOutputLine;if(element2*nodenumber.ptr)&(!element2*nodenumber+1.ptr)/左子树存在,右子树不存在时,左右两个孩子上的连接线coutprespaceOutputLinemidspace;if(!element2*nodenumber.ptr)&(element2*nodenumber+1.ptr)/左子树不存在,右子树存在时左右两个孩子上的连接线coutprespacemidspaceO

31、utputLine;if(!element2*nodenumber.ptr)&(!element2*nodenumber+1.ptr)/左子树不存在,右子树不存在时,没有连接线,是空格串coutprespacemidspacemidspace;nodenumber=nodenumber+1;/结点编号加1coutendl;/for(i=1;i=BinaryTreeHigh;i+)数据构造实验报告二一二年deleteelement;/释下班作空间intmain()BinaryTreeNode*BT,*P11;ETypex;intchoice;charnumber8=,001,002,003,00

32、4,005,006,007,008,009,010;charname8=,百里,东郭,太史,闻人,公孙,赫连,钟离,鲜鱼,轩辕,南门;charsex3=,女,男,女,男,女,女,男,男,女,男;charplace20=,重庆,长安,东莞,安庆,承德,四平,安稳,香港,金门,龙门;for(inti=1;i=10;i+)strcpy(x.number,numberi);strcpy(,namei);strcpy(x.sex,sexi);x.age=20+i;strcpy(x.place,placei);Pi=MakeNode(x);MakeBinaryTree(P1,P2,P3);MakeBinaryTree(P2,P4,P5);MakeBinaryTree(P3,NULL,P6);MakeBinaryTree(P4,P7,NULL);MakeBinaryTree(P5,NULL,P8);MakeBinaryTree(P6,P9,P10);MakeBinaryTree(P7,NULL,NULL);MakeBinaryTree(P8,NULL,NULL);MakeBinaryTree(P9,NULL,NULL);MakeBinaryTree(P10,NULL,NULL);BT=P1;while(true)couten

温馨提示

  • 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
  • 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
  • 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
  • 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
  • 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
  • 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
  • 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。

评论

0/150

提交评论