数据结构实验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、实验设备(环境)及要求硬件:支持 intel pentium 及其以上 cpu ,内存 128mb 以上、硬盘 1gb 以上容量的微机。软件:配有 windows98/2000/xp 操作系统,安装 visual c+ 。5、实验时间:10学时6、该文档的文件名不要修改,存入 命名的文件夹中7、该表中的数据只需填空,已有内容不要修改实验结果(运行结果界面及源程序,运行结果界面放在前面):#include #include #include #include #define student etypestruct studentcharnumber8;charname8;charsex3;int

3、 age;charplace20;struct binarytreenodeetype data;binarytreenode *lchild;binarytreenode *rchild;struct qtypebinarytreenode *ptr;struct queueqtype *element;int front;int rear;int maxsize;struct node_ptrbinarytreenode*ptr; ;void creatqueue(queue &q,int maxqueuesize)/创建队列q.maxsize=maxqueuesize;q.element

4、=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) return false;result=q.element(q.fro

5、nt+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;bool dequeue(queue &q,qtype &result)/元素

6、出队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;return ptr;void makebinarytree(binarytreenode *root, bi

7、narytreenode *left,binarytreenode *right)/构造二叉树之间的关系root-lchild=left;root-rchild=right;void outputbinarytreenode(binarytreenode *p)/输出节点cout data.number data.sex data.age data.placeendl;coutendl;void levelorder_ltor_utod(binarytreenode *bt)/从左至右,从上至下按层次遍历一棵二叉树queue q;qtype temp;binarytreen

8、ode *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_rtol_utod(binarytreenode *bt)/从右至左,从上至下按层次遍历一棵二叉树queue q;qtype temp;bin

9、arytreenode *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(q,temp);void levelorder_ltor_dtou(binarytreenode *bt)/从左至右,从下至上按层次遍历一棵二叉树queue q;qtype

10、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,temp);if(p-lchild)temp.ptr=p-lchild;enqueue(q,temp);for(int i=q.rear;ifrontkeep;i-)outputbinarytreeno

11、de(q.elementi.ptr);void levelorder_rtol_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 =lchild)temp.ptr=p-lchild;enqueue(q,tem

12、p);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) return 0;int highl=binaryheight(bt-lchild);int highr=binaryheight(bt-rchild);if(highlhighr)return +highl;elsereturn +highr;void bi

13、narydelete(binarytreenode *bt)/二叉树删除算法if(bt)binarydelete(bt-lchild);binarydelete(bt-rchild);delete bt;int binarynodesum(binarytreenode *bt)/计算二叉树中的节点数if(!bt) return 0; queue q;qtype temp;binarytreenode *p;int maxqueuesize=50;int index=0;creatqueue(q,maxqueuesize);p=bt;temp.ptr=p;enqueue(q,temp);whil

14、e(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,temp);return index;void digitaltostring(char str,int n)chartemp;chark=1;inti=0;while (n & i80)k=n%10+48;n=n/10;stri=k;i+;stri=0;int len=strlen(str);for (i=0;ile

15、n/2;i+)temp=stri;stri=strlen-i-1;strlen-i-1=temp;char*getouputinformationstring(int widthofdata,char *outputinformation,char *outputstring)/将一个元素的字符串outputinformation转换为宽度为widthofdata的等长字符串outputstring /例如,姓名是由四个字符组成,而widthofdata为8,则在姓名字符串的两边分别连接两个字符,形成8个长度的字符串intleft_space,right_space;inti;charleft

16、_space_string16=;charright_space_string16=;intadd_space;intlen_outputinformation=strlen(outputinformation);/计算outputinformation的字符个数add_space=widthofdata - len_outputinformation;/计算需要补充的字符个数left_space=add_space/2;/计算outputinformation左边需要补充的字符个数right_space=add_space-left_space;/计算outputinformation右边需

17、要补充的字符个数for(i=1;i=left_space;i+)/形成outputinformation左边需要补充的空格字符串strcat(left_space_string, );for(i=1;);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);

18、break;case 5:digitaltostring(outputinformation,elementk.ptr-data.age);break;default:break;returnoutputinformation;/输出二叉树void outputbinarytree(binarytreenode *bt)node_ptrtemp,*element;binarytreenode *p;intmaxsize; intbinarytreehigh;inti,j,k;binarytreehigh=binaryheight(bt);maxsize=(int) pow(2,binarytr

19、eehigh);element = new node_ptr maxsize+1;/定义一个用于存放二叉树结点指针的数组for (i=1;i=maxsize;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-

20、rchild;element2*i+1=temp;intwidthofdata=5;intintervalofdata=3;/coutwidthofdata=widthofdataendl;/coutintervalofdata=intervalofdataendl;/coutbinarytreehigh=binarytreehighendl;intposition_of_central617;/存放每一元素输出位置中心(距离屏幕左边界的字符数)introw,col;/二维数组的行列下标变量for(i=0;i=binarytreehigh;i+)/存放每一元素输出位置中心(距离屏幕左边界的字符

21、数),初值为0for(j=0;j=pow(2,binarytreehigh-1);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);

22、/由k编号的结点换算出该结点是底层中从左至右第j个结点右上方/即编号为k的结点中心点垂直线左边最底层上有j个结点row=(int)(log10(i)/log10(2)+1);/计算中心点值存放在position_of_centralrowcol中的rowcol=(int)(i-(pow(2,(row-1)-1);/计算中心点值存放在position_of_centralrowcol中的colif(row=binarytreehigh) /计算底层i结点距离左边界的字符数,左边无间隙position_of_centralrowcol= (j-1)*widthofdata + (j-1)*inte

23、rvalofdata + (widthofdata/2 +1);else/计算非底层i结点距离左边界的字符数,position_of_centralrowcol=j*widthofdata + (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(int item=1;item=item_max;ite

24、m+)/输出每个数据元素多个item成员的值inthalf_len_pre_value=0;/同一行前一个输出的元素值长度的一半,同一行第一个输出的元素值前没有值输出,初值为0inthalf_len_now_value=widthofdata/2;/同一行当前输出的元素值长度的一半,其值始终为widthofdata的一半kk=kkk;/kk是满二叉树中每一层结点的编号变化,从某层的最左边第一个结点编号kkk开始for(j=1;j=pow(2,i-1);j+)/输出满二叉树中同一层次上的每个数据元素的同一个成员的值charoutputinformation20=;char*outputinfor

25、mation;if (elementkk.ptr)/获取每个数据元素的一个成员的值outputinformation,如姓名、年龄等outputinformation=getouputinformation(item,kk,outputinformation,element);if (position_of_centralij)/输出数据中点值存在时,position_of_centralij非0charoutputstring80=;char*outputstring;/下面语句将每个数据元素的一个成员的值outputinformation,如姓名、年龄等,转换为等长widthofdata的

26、字符串outputstringoutputstring=getouputinformationstring(widthofdata,outputinformation,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

27、;half_len_pre_value=widthofdata/2;/调整同一行前一个输出的元素值长度为widthofdata的一半kk+;/if (position_of_centralij)/for(j=1;j=pow(2,i-1);j+)coutendl;/for(int item=1;item=5;item+)charline3=;charoutputline80;charmidspace80;intnodenumber;if(i=1)/对深度为binarytreehigh的满二叉树从上至下,从左至右的编号,计算第i层上起始结点的编号nodenumbernodenumber=1;els

28、enodenumber=(int)(pow(2,i)-1)-(pow(2,i-1)-1);/第i(i!=1)层上的第1个结点编号nodenumberfor(j=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, );/计算左右两个孩子之间一半的

29、连线outline,由若干个line构成/注意line是汉字字符,一个占两个字符位,m是左右两个孩子之间的line数,所以m要除4strcpy(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_centr

30、ali+1j-1)/2;for(k=1;k=m;k+)strcat(midspace, );if (element2*nodenumber.ptr) & (element2*nodenumber+1.ptr)/左右子树都存在时,左右两个孩子上的连接线coutprespaceoutputlineoutputline;if (element2*nodenumber.ptr) & (!element2*nodenumber+1.ptr)/左子树存在,右子树不存在时,左右两个孩子上的连接线coutprespaceoutputlinemidspace;if (!element2*nodenumber.p

31、tr) & (element2*nodenumber+1.ptr)/左子树不存在,右子树存在时左右两个孩子上的连接线coutprespacemidspaceoutputline;if (!element2*nodenumber.ptr) & (!element2*nodenumber+1.ptr)/左子树不存在,右子树不存在时,没有连接线,是空格串coutprespacemidspace midspace ;nodenumber=nodenumber+1;/结点编号加1coutendl;/for(i=1;i=binarytreehigh;i+)delete element;/释放工作空间int

32、 main()binarytreenode*bt,*p11;etype x;int choice;char number8= ,001,002,003,004,005,006,007,008,009,010;char name8= ,百里,东郭,太史,闻人,公孙,赫连,钟离,鲜鱼,轩辕,南门;char sex3= ,女,男,女,男,女,女,男,男,女,男;char place20= ,重庆,长安,东莞,安庆,承德,四平,安稳,香港,金门,龙门;for(int i=1;i=10;i+)strcpy(x.number , numberi);strcpy( , namei);strcpy

33、(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)coutendl;cout*二叉树的简单运

温馨提示

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

评论

0/150

提交评论