二叉树实验源代码_第1页
二叉树实验源代码_第2页
二叉树实验源代码_第3页
二叉树实验源代码_第4页
二叉树实验源代码_第5页
全文预览已结束

下载本文档

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

文档简介

第五章 树实验目的:1. 掌握二叉树的链式存储结构的定义及实现方法。2. 掌握二叉树的先序、中序和后序遍历方法。3. 掌握二叉树的结点个数和树的深度的计算方法。实验内容:1. 建立一棵含有n个结点的二叉树,采用二叉链表存储。2. 分别用前序、中序和后序遍历该二叉树,输出访问到的结点。3. 计算该二叉树的结点个数和二叉树的深度,输出计算结果。/参考代码#include#include#includetemplate struct BinTreeNode /二叉树结点类定义 T data; /数据域 BinTreeNode *leftChild, *rightChild; /左子女、右子女链域 BinTreeNode ():leftChild(NULL),rightChild(NULL)/构造函数 BinTreeNode (T x, BinTreeNode *l = NULL, BinTreeNode *r = NULL) data = x; leftChild = l; rightChild = r; ; template class BinaryTree /二叉树类定义public: BinaryTree (T value) : RefValue(value), root(NULL) /构造函数 CreateBinTree (cin,root); BinaryTree (BinaryTree& s); /复制构造函数 BinaryTree () destroy(root); /析构函数 bool IsEmpty () return root = NULL;/判二叉树空否 int Height () return Height(root); /求树高度 int Size () return Size(root); /求结点数 BinTreeNode *getRoot () const return root; /取根 void preOrder (void (*visit) (BinTreeNode *p) /前序遍历 preOrder (root, visit); void inOrder (void (*visit) (BinTreeNode *p) /中序遍历 inOrder (root, visit); void postOrder (void (*visit) (BinTreeNode *p) /后序遍历 postOrder (root, visit); protected: BinTreeNode *root; /二叉树的根指针 T RefValue; /数据输入停止标志 void CreateBinTree (istream& in,BinTreeNode *& subTree); /从文件读入建树 void destroy (BinTreeNode * subTree); void preOrder (BinTreeNode* subTree, void (*visit) (BinTreeNode *p); /前序遍历 void inOrder (BinTreeNode* subTree, void (*visit) (BinTreeNode *p); /中序遍历 void postOrder (BinTreeNode* subTree, void (*visit) (BinTreeNode *p); /后序遍历 int Size (BinTreeNode *subTree) const;/返回结点数 int Height ( BinTreeNode * subTree);/返回树高度 /其他函数略; template void BinaryTree:destroy (BinTreeNode * subTree) /删除根为subTree的子树 if (subTree != NULL) destroy (subTree-leftChild); /删除左子树 destroy (subTree-rightChild); /删除右子树 delete subTree; /删除根结点 template void BinaryTree:CreateBinTree (istream& in, BinTreeNode *& subTree) T item; if ( !in.eof () ) /未读完, 读入并建树 in item; /读入根结点的值 if (item != RefValue) subTree = new BinTreeNode(item); /建立根结点 if (subTree = NULL) cerr 存储分配错!leftChild); /递归建立左子树CreateBinTree (in, subTree-rightChild);/递归建立右子树 else subTree = NULL; /封闭指向空子树的指针 template /前序void BinaryTree:preOrder (BinTreeNode * subTree, void (*visit) (BinTreeNode *p) if (subTree!= NULL) visit (subTree);/访问根结点 preOrder (subTree-leftChild, visit); /遍历左子树 preOrder (subTree-rightChild, visit); /遍历右子树 template /中序void BinaryTree:inOrder (BinTreeNode * subTree, void (*visit) (BinTreeNode *p) if (subTree != NULL) inOrder (subTree-leftChild, visit); /遍历左子树 visit (subTree);/访问根结点 inOrder (subTree-rightChild, visit); /遍历右子树 template /后序void BinaryTree:postOrder (BinTreeNode * subTree, void (*visit) (BinTreeNode *p) if (subTree != NULL ) postOrder (subTree-leftChild, visit);/遍历左子树 postOrder (subTree-rightChild, visit); /遍历右子树 visit (subTree); /访问根结点 template int BinaryTree:Size (BinTreeNode * subTree) const if (subTree = NULL) return 0; /空树 else return 1+Size (subTree-leftChild)+ Size (subTree-rightChild);template int BinaryTree:Height ( BinTreeNode * subTree) if (subTree = NULL) return 0;/空树高度为0 else int i = Height (subTree-leftChild); int j = Height (subTree-rightChild); return (i j) ? j+1 : i+1; void print(BinTreeNode *p) coutdata;void main() BinaryTree T(#);cout前序遍历结果为:;T.preOrder(print);coutendl中序遍历结果为

温馨提示

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

评论

0/150

提交评论