- 1、原创力文档(book118)网站文档一经付费(服务费),不意味着购买了该文档的版权,仅供个人/单位学习、研究之用,不得用于商业用途,未经授权,严禁复制、发行、汇编、翻译或者网络传播等,侵权必究。。
- 2、本站所有内容均由合作方或网友上传,本站不对文档的完整性、权威性及其观点立场正确性做任何保证或承诺!文档内容仅供研究参考,付费前请自行鉴别。如您付费,意味着您自己接受本站规则且自行承担风险,本站不退款、不进行额外附加服务;查看《如何避免下载的几个坑》。如果您已付费下载过本站文档,您可以点击 这里二次下载。
- 3、如文档侵犯商业秘密、侵犯著作权、侵犯人身权等,请点击“版权申诉”(推荐),也可以打举报电话:400-050-0827(电话支持时间:9:00-18:30)。
- 4、该文档为VIP文档,如果想要下载,成为VIP会员后,下载免费。
- 5、成为VIP后,下载本文档将扣除1次下载权益。下载后,不支持退款、换文档。如有疑问请联系我们。
- 6、成为VIP后,您将拥有八大权益,权益包括:VIP文档下载权益、阅读免打扰、文档格式转换、高级专利检索、专属身份标志、高级客服、多端互通、版权登记。
- 7、VIP文档为合作方或网友上传,每下载1次, 网站将根据用户上传文档的质量评分、类型等,对文档贡献者给予高额补贴、流量扶持。如果你也想贡献VIP文档。上传文档
查看更多
二叉树实现
实验三:二叉树的实现
班级:2010级数学班 学号:201008101127 姓名:裴志威
【实验目的】
掌握二结构;
掌握二叉树的二叉链表存储结构;
验证二叉树的二叉链表存储及遍历操作。
【实验内容】
建立一棵含有n个结点的二叉树,采用二叉树链表存储;
输出前序、中序和后序遍历该二叉树的遍历结果。
【实验内容与步骤】
#includeiostream.h
template class T
struct BinTreeNode //二叉树结点类定义
{ T data; //数据域
BinTreeNodeT *leftChild, *rightChild; //左子女、右子女链域
BinTreeNode () { leftChild = NULL; rightChild = NULL; }
BinTreeNode (T x, BinTreeNodeT *left = NULL, BinTreeNodeT *right = NULL)
{ data = x; leftChild = left; rightChild = right; }
};
template class T
class BinaryTree //二叉树类定义
{
public:
BinaryTree () { root=NULL; } //构造函数
BinaryTree (BinTreeNodeT *root); //构造函数
~BinaryTree (); //析构函数
int CounteBinTree (BinTreeNodeT *root,int count); //计算根节点
int Size (BinTreeNodeT *root); //返回结点数
int Height (BinTreeNodeT *root); //返回树高度
void Print_PreOrder(BinTreeNodeT *root);
void Print_InOrder(BinTreeNodeT *root);
void Print_PostOrder(BinTreeNodeT *root);
bool IsEmpty()
{ return root=NULL;}
void Creat(BinTreeNodeT *root);//从文件读入建树
void Destroy (BinTreeNodeT *root); //删除根节点
protected:
BinTreeNodeT *root; //二叉树的根指针
};
template class T
BinaryTreeT::BinaryTree (BinTreeNodeT *root)
{ Creat(root); }
template class T
void BinaryTreeT::Creat(BinTreeNodeT *root)
{ char ch; cinch;
if(ch==#) root=NULL;
else
{root = new BinTreeNodeT; //建立根结点
root-data=ch;
Creat(root-leftChild); //递归建立左子树
Creat(root-rightChild);//递归建立右子树
}
}
template class T
BinaryTreeT::~BinaryTree ()
{ Destroy (root); }
template class T
void BinaryTreeT::Destroy (BinTreeNodeT *root)
{ if(root!=NULL)
{Destroy(root-leftChild); Destroy(root-rightChild); delete root; }
// root=NULL;
}
template class T
void BinaryTreeT::Print_PreOrder(BinTreeNodeT *root)
{ if(root==NULL) return ;
else {coutroot-data ;
Print_PreOrder(root-leftChild); Print_PreOrder(root-rightChild);
}}
template class T
void BinaryTreeT::Print_InOrder(BinTreeNodeT *root)
{ if(root==NULL) return ;
else {Print_PreOrder(root-leftChild);
coutro
原创力文档


文档评论(0)