厦门理工c语言数据结构实验_第1页
厦门理工c语言数据结构实验_第2页
厦门理工c语言数据结构实验_第3页
厦门理工c语言数据结构实验_第4页
厦门理工c语言数据结构实验_第5页
已阅读5页,还剩3页未读 继续免费阅读

下载本文档

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

文档简介

精品文档数据结构实验报告实验序号:6 实验项目名称:树和二叉树的操作学号姓名专业、班实验地点指导教师实验时间一、实验目的及要求1、进一步掌握指针变量、动态变量的含义。2、掌握二叉树的结构特征,以及各种存储结构的特点及适用范围。3、掌握用指针类型描述、访问和处理二叉树的运算。4、掌握用二叉树前序、中序、后序、层次遍历的方法。二、实验设备(环境)及要求微型计算机;windows 操作系统;Microsoft Visual Studio 6.0集成开发环境。三、实验内容与步骤1根据P129的方法,将a*b-(c+d*e/f)+g)转化为表达式二叉树(绘图),并写出表达式二叉树的前序、中序和后序遍历顺序。2. 链式表表示和实现二叉树如下:#include #include #define max 50typedef struct liuyuint data;struct liuyu *lchild,*rchild;test;liuyu *root,*p,*qmax;int sum=0;int m=sizeof(test); void insert_data(int x) /*生成二叉排序树*/ liuyu *p,*q,*s;s=(test*)malloc(m);s-data=x;s-lchild=NULL;s-rchild=NULL;if(!root)root=s; p=root; while(p) /*如何接入二叉排序树的适当位置*/q=p;if(p-data=x)printf(data already exist! n);return;else if(xdata)p=p-lchild; else p=p-rchild;if(xdata)q-lchild=s;else q-rchild=s;void main() /*先生成二叉排序树*/int i,x;i=1; root=NULL; /*千万别忘了赋初值给root!*/doprintf(please input data%d:,i);i+;scanf(%d,&x); /*从键盘采集数据,以-9999表示输入结束*/if(x=-9999) printf(nNow output data value:n); else insert_data(x); /*调用插入数据元素的函数*/ while(x!=-9999); 改写以上程序,实现功能如下(任选两题):1.编写函数实现前序、中序和后序遍历。运行结果截图:2.编写函数实现计算叶节点个数。运行结果截图:四、分析与讨论对上机实践结果进行分析,上机的心得体会。五、教师评语签名:日期:成绩附源程序清单:1. #include #include typedef int TElemType;typedef struct BiTNodeTElemType data;struct BiTNode *lchild,*rchild;BiNode, *Bitree;DLR( Bitree root ) if (root !=NULL) /非空二叉树 printf(%d,root-data); /访问D DLR(root-lchild); /递归遍历左子树 DLR(root-rchild); /递归遍历右子树 return(0); LDR(Bitree root) if(root !=NULL) LDR(root-lchild); printf(%d,root-data); LDR(root-rchild); return(0);LRD (Bitree root) if(root !=NULL) LRD(root-lchild); LRD(root-rchild); printf(%d,root-data); return(0);Bitree root;/定义根结点 void insert_data(int x) /*生成/树*/ Bitree p,q,s;s=(Bitree)malloc(sizeof(BiNode); /创建结点s-data=x; /结点赋值s-lchild=NULL;s-rchild=NULL;if(!root)root=s; elsep=root; while(p) /*如何接入二叉排序树的适当位置*/q=p;if(p-data=x) /相同结点不能重复插入printf(data already exist! n);return;else if(xdata)p=p-lchild; else p=p-rchild;if(xdata)q-lchild=s;else q-rchild=s;void main() /*先生成二叉排序树*/int i=1,x; /i记录结点个数,x存放结点值root=NULL; /*千万别忘了赋初值给root!*/printf(请输入数据,-9999表示输入结束n);doprintf(please input data %d:,i);i+;scanf(%d,&x); /*从键盘采集数据,以-9999表示输入结束*/if(x=-9999) printf(nNow output data value:n); else insert_data(x); /*调用插入数据元素的函数*/while(x!=-9999); printf(nDLR); DLR(root); printf(nLDR); LDR(root); printf(nLRD); LRD(root);2.#include #include typedef int TElemType;typedef struct BiTNodeTElemType data;struct BiTNode *lchild,*rchild;BiNode, *Bitree;Bitree root;/定义根结点 int CountLeaf (Bitree root) /返回指针T所指二叉树中所有叶子结点个数int m,n; if (!root ) return 0; if (!root-lchild & !root-rchild) return 1; else m = CountLeaf( root-lchild); n = CountLeaf( root-rchild); return (m+n); /else / CountLeafvoid insert_data(int x) /*生成/树*/ Bitree p,q,s;s=(Bitree)malloc(sizeof(BiNode); /创建结点s-data=x; /结点赋值s-lchild=NULL;s-rchild=NULL;if(!root)root=s; elsep=root; while(p) /*如何接入二叉排序树的适当位置*/q=p;if(p-data=x) /相同结点不能重复插入printf(data already exist! n);return;else if(xdata)p=p-lchild; else p=p-rchild;if(xdata)q-lchild=s;else q-rchild=s;void main() /*先生成二叉排序树*/int i=1,x; /i记录结点个数,x存放结点值int sum;root=NULL; /*千万别忘了赋初值给root!*/printf(请输入数据,-9999表示输入结束n);doprintf(please input data %d:,i);i+;scanf(%d,&x); /*从键盘采集数据,以-

温馨提示

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

评论

0/150

提交评论