头文件.docx
- 文档编号:10215947
- 上传时间:2023-02-09
- 格式:DOCX
- 页数:6
- 大小:14.90KB
头文件.docx
《头文件.docx》由会员分享,可在线阅读,更多相关《头文件.docx(6页珍藏版)》请在冰豆网上搜索。
头文件
//头文件
#ifndef_BINARYTREE_H_
#define_BINARYTREE_H_
#include"BinaryTreeNode.h"
#include
#include
usingnamespacestd;
template
classBinaryTree
{
BinaryTreeNode
protected:
BinaryTreeNode
voidDestroy(BinaryTreeNode
public:
BinaryTree(){m_root=NULL;}
BinaryTree(Tdata){m_root=newBinaryTreeNode
virtual~BinaryTree(void);
//判断是否为空树
boolIsEmpty()()const{returnm_root==NULL?
true:
false;}
//取得一个父节点的指针
BinaryTreeNode
//判断是否为左孩子
boolIsLeftChild(BinaryTreeNode
true:
false;}
//判断是否为右孩子
boolIsRightChild(BinaryTreeNode
true:
false;}
//取得整棵树的树根
BinaryTreeNode
BinaryTreeNode
{returnroot==NULL?
NULL:
root->GetLeftChild();}
BinaryTreeNode
{returnroot==NULL?
NULL:
root->GetRightChild();}
BinaryTreeNode
BinaryTreeNode
//返回一个结点的数据
TRetrieve(BinaryTreeNode
//设置一个结点的数据
voidAssign(BinaryTreeNode
voidInsertRightChild(BinaryTreeNode
voidInsertLeftChild(BinaryTreeNode
voidDeleteRightChild(BinaryTreeNode
voidDeleteLeftChild(BinaryTreeNode
virtualvoidPreOrderTraverse(BinaryTreeNode
virtualvoidInOrderTraverse(BinaryTreeNode
virtualvoidPostOrderTraverse(BinaryTreeNode
virtualvoidLevelOrderTraverse(BinaryTreeNode
};
template
BinaryTree
:
~BinaryTree(void)
{
Destroy(m_root);
m_root=NULL;
}
template
voidBinaryTree
:
Destroy(BinaryTreeNode
{
if(p!
=NULL)
{
Destroy(p->GetLeftChild());
Destroy(p->GetRightChild());
deletep;
}
}
template
voidBinaryTree
:
InsertLeftChild(BinaryTreeNode
{
BinaryTreeNode
q->SetLeftChild(p->GetLeftChild());
p->SetLeftChild(q);
}
template
voidBinaryTree
:
InsertRightChild(BinaryTreeNode
{
BinaryTreeNode
q->SetRightChild(p->GetRightChild());
p->SetRightChild(q);
}
//先序遍历的递归算法
//template
//voidBinaryTree
:
PreOrderTraverse(BinaryTreeNode
//{
//if(root!
=NULL)
//{
//cout<
//PreOrderTraverse(root->GetLeftChild());
//PreOrderTraverse(root->GetRightChild());
//}
//}
//先序遍历的栈结构算法
template
voidBinaryTree
:
PreOrderTraverse(BinaryTreeNode
{
stack
BinaryTreeNode
while(!
s.empty()||p!
=NULL)
{
while(p)
{
s.push(p);
cout<
p=p->GetLeftChild();
}
p=s.top();
s.pop();
p=p->GetRightChild();
}
}
//先序遍历的栈结构算法
template
voidBinaryTree
:
InOrderTraverse(BinaryTreeNode
{
stack
BinaryTreeNode
while(!
s.empty()||p!
=NULL)
{
while(p)
{
s.push(p);
p=p->GetLeftChild();
}
p=s.top();
s.pop();
cout<
p=p->GetRightChild();
}
}
////后序遍历的递归算法
//template
//voidBinaryTree
:
PostOrderTraverse(BinaryTreeNode
//{
//if(root!
=NULL)
//{
//PreOrderTraverse(root->GetLeftChild());
//PreOrderTraverse(root->GetRightChild());
//}
//cout<
//}
//按层遍历整棵树
template
voidBinaryTree
:
LevelOrderTraverse(BinaryTreeNode
{
queue
if(root!
=NULL)
{
q.push(root);
}
while(!
q.empty())
{
root=q.front(),q.pop();
cout<
if(root->GetLeftChild())
q.push(root->GetLeftChild());
if(root->GetRightChild())
q.push(root->GetRightChild());
}
}
//后序遍历的栈结构算法
template
voidBinaryTree
:
PostOrderTraverse(BinaryTreeNode
{
stack
BinaryTreeNode
while(!
s.empty()||p!
=NULL)
{
while(p)
{
s.push(p);
p=p->GetLeftChild();
}
p=s.top();
//cout<
s.pop();
p=p->GetRightChild();
}
}
#endif
//主程序
#include
#include"BinaryTreeNode.h"
#include"BinaryTree.h"
usingnamespacestd;
voidmain()
{
BinaryTree
myBinTree.InsertLeftChild(myBinTree.GetRoot(),'B');
myBinTree.InsertRightChild(myBinTree.GetRoot(),'C');
myBinTree.InsertLeftChild(myBinTree.GetRoot()->GetLeftChild(),'D');
myBinTree.InsertRightChild(myBinTree.GetRoot()->GetLeftChild(),'E');
myBinTree.InsertLeftChild(myBinTree.GetRoot()->GetRightChild(),'F');
myBinTree.InsertRightChild(myBinTree.GetRoot()->GetRightChild(),'G');
myBinTree.PreOrderTraverse(myBinTree.GetRoot());
cout< myBinTree.InOrderTraverse(myBinTree.GetRoot()); cout< myBinTree.PostOrderTraverse(myBinTree.GetRoot()); cout< myBinTree.LevelOrderTraverse(myBinTree.GetRoot()); cout< system("pause"); }
- 配套讲稿:
如PPT文件的首页显示word图标,表示该PPT已包含配套word讲稿。双击word图标可打开word文档。
- 特殊限制:
部分文档作品中含有的国旗、国徽等图片,仅作为作品整体效果示例展示,禁止商用。设计者仅对作品中独创性部分享有著作权。
- 关 键 词:
- 文件
