



全文预览已结束
下载本文档
版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领
文档简介
实验三:二叉树的实现班级:2010级数学班 学号:201008101127 姓名:裴志威 【实验目的】(1) 掌握二结构;(2) 掌握二叉树的二叉链表存储结构;(3) 验证二叉树的二叉链表存储及遍历操作。【实验内容】(1) 建立一棵含有n个结点的二叉树,采用二叉树链表存储;(2) 输出前序、中序和后序遍历该二叉树的遍历结果。【实验内容与步骤】#includetemplate struct BinTreeNode /二叉树结点类定义T data; /数据域 BinTreeNode *leftChild, *rightChild; /左子女、右子女链域 BinTreeNode () leftChild = NULL;rightChild = NULL; BinTreeNode (T x, BinTreeNode *left = NULL, BinTreeNode *right = NULL) data = x;leftChild = left; rightChild = right; ; template class BinaryTree /二叉树类定义 public: BinaryTree () root=NULL; /构造函数 BinaryTree (BinTreeNode *&root); /构造函数 BinaryTree (); /析构函数 int CounteBinTree (BinTreeNode *root,int &count); /计算根节点int Size (BinTreeNode *root); /返回结点数int Height (BinTreeNode *root); /返回树高度void Print_PreOrder(BinTreeNode *&root);void Print_InOrder(BinTreeNode *&root);void Print_PostOrder(BinTreeNode *&root);bool IsEmpty() return root=NULL; void Creat(BinTreeNode *&root);/从文件读入建树void Destroy (BinTreeNode *root); /删除根节点protected: BinTreeNode *root; /二叉树的根指针 ;template BinaryTree:BinaryTree (BinTreeNode *&root)Creat(root); template void BinaryTree:Creat(BinTreeNode *&root)char ch; cinch;if(ch=#) root=NULL;elseroot = new BinTreeNode; /建立根结点 root-data=ch;Creat(root-leftChild); /递归建立左子树 Creat(root-rightChild);/递归建立右子树 template BinaryTree:BinaryTree ()Destroy (root); template void BinaryTree:Destroy (BinTreeNode *root)if(root!=NULL)Destroy(root-leftChild); Destroy(root-rightChild); delete root;/root=NULL;template void BinaryTree:Print_PreOrder(BinTreeNode *&root)if(root=NULL) return ;elsecoutdataleftChild); Print_PreOrder(root-rightChild);template void BinaryTree:Print_InOrder(BinTreeNode *&root)if(root=NULL) return ;elsePrint_PreOrder(root-leftChild);coutdatarightChild);template void BinaryTree:Print_PostOrder(BinTreeNode *&root)if(root=NULL) return ;else Print_PreOrder(root-leftChild); Print_PreOrder(root-rightChild);coutdata ;template int BinaryTree:CounteBinTree (BinTreeNode *root,int &count)if(root)if(root-leftChild=NULL&root-rightChild=NULL)count+;CounteBinTree(root-leftChild,count);CounteBinTree(root-rightChild,count);return count; template int BinaryTree:Size (BinTreeNode *root)if (root = NULL) return 0; /空树 elsereturn 1+Size (root-leftChild) + Size (root-rightChild); template int BinaryTree:Height (BinTreeNode *root) int i,j;if(root=NULL) return 0;else i=Height(root-leftChild);j=Height(root-rightChild);return 1+(ij)?j:i); void main() int count=0;/,i,jcout请输入节点元素,以#结束!endl;BinTreeNode*subtree;subtree=new BinTreeNode;BinaryTreehyb(subtree);cout树的前序访问遍序为:endl;hyb.Print_PreOrder(subtree);coutendl;cout树的中序访问遍序为:endl;hyb.Print_InOrder(subtree);coutendl;cout树的后序访问遍序为:endl;hyb.Print_PostOrder(subtree);coutendl;cout树叶结点个数为:hyb.CounteBinTree(subtree,count);coutendl;cout树高度和深度为:hyb.Height(subtre
温馨提示
- 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
- 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
- 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
- 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
- 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
- 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
- 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。
最新文档
- 小学心理剧课程开发与实施中的心理健康教育课程评价体系构建论文
- 家长教育理念与学生学业表现的关系论文
- 现代商业化对高中生中秋节文化传承的影响与对策研究论文
- 节能环保科管理制度
- 英语兴趣组管理制度
- 茶馆茶水间管理制度
- 大班语言育儿理论经验幼儿教育教育专区
- 记账实操-水泥厂账务处理
- 山东省烟台市2025年中考地理真题(含答案)
- 江苏省宿迁市泗阳县2024-2025学年七年级下学期期末练习生物试卷(含答案)
- 2025年全国统一高考数学试卷(全国一卷)含答案
- 2024年江西省中考生物·地理合卷试卷真题(含答案)
- 车间安全环保培训知识
- 绵阳市2023年学业考试暨高中阶段招生考试(地理、生物试题)
- 少儿美术绘画课件- 艺米中班 4岁-5岁 《荔枝》
- 托管班带生源转让合同
- 第三方融资服务(FA)协议
- 画册设计制作报价单
- 借助数学实验 促进思维发展
- 人民币教具正反面完美打印版
- 真空系统设计培训课件
评论
0/150
提交评论