头文件.docx

上传人:b****7 文档编号:10215947 上传时间:2023-02-09 格式:DOCX 页数:6 大小:14.90KB
下载 相关 举报
头文件.docx_第1页
第1页 / 共6页
头文件.docx_第2页
第2页 / 共6页
头文件.docx_第3页
第3页 / 共6页
头文件.docx_第4页
第4页 / 共6页
头文件.docx_第5页
第5页 / 共6页
点击查看更多>>
下载资源
资源描述

头文件.docx

《头文件.docx》由会员分享,可在线阅读,更多相关《头文件.docx(6页珍藏版)》请在冰豆网上搜索。

头文件.docx

头文件

//头文件

#ifndef_BINARYTREE_H_

#define_BINARYTREE_H_

#include"BinaryTreeNode.h"

#include

#include

usingnamespacestd;

template

classBinaryTree

{

BinaryTreeNode*m_root;

protected:

BinaryTreeNode*Parent(BinaryTreeNode*root,BinaryTreeNode*p);

voidDestroy(BinaryTreeNode*p);

public:

BinaryTree(){m_root=NULL;}

BinaryTree(Tdata){m_root=newBinaryTreeNode(data);}

virtual~BinaryTree(void);

//判断是否为空树

boolIsEmpty()()const{returnm_root==NULL?

true:

false;}

//取得一个父节点的指针

BinaryTreeNode*GetParent(BinaryTreeNode*p){returnParent(m_root,p);}

//判断是否为左孩子

boolIsLeftChild(BinaryTreeNode*p){returnp==GetParent(p)->GetLeftChild()?

true:

false;}

//判断是否为右孩子

boolIsRightChild(BinaryTreeNode*p){returnp==GetParent(p)->GetRightChild()?

true:

false;}

//取得整棵树的树根

BinaryTreeNode*GetRoot(){returnm_root;}

BinaryTreeNode*LeftChild(BinaryTreeNode*root)const

{returnroot==NULL?

NULL:

root->GetLeftChild();}

BinaryTreeNode*RightChild(BinaryTreeNode*root)const

{returnroot==NULL?

NULL:

root->GetRightChild();}

BinaryTreeNode*LeftSibling(BinaryTreeNode*leftChild);

BinaryTreeNode*RightSibling(BinaryTreeNode*rightChild);

//返回一个结点的数据

TRetrieve(BinaryTreeNode*p)const{returnp->GetData()};

//设置一个结点的数据

voidAssign(BinaryTreeNode*p,constT&d)const{p->SetData(d);}

voidInsertRightChild(BinaryTreeNode*p,constT&d)const;

voidInsertLeftChild(BinaryTreeNode*p,constT&d)const;

voidDeleteRightChild(BinaryTreeNode*p){Destroy(p->GetRightChild());}

voidDeleteLeftChild(BinaryTreeNode*p){Destroy(p->GetLeftChild());};

virtualvoidPreOrderTraverse(BinaryTreeNode*root)const;//先序遍历整棵树

virtualvoidInOrderTraverse(BinaryTreeNode*root)const;//中序遍历整棵树

virtualvoidPostOrderTraverse(BinaryTreeNode*root)const;//后序遍历整棵树

virtualvoidLevelOrderTraverse(BinaryTreeNode*root)const;//按层遍历整棵树

};

template

BinaryTree:

:

~BinaryTree(void)

{

Destroy(m_root);

m_root=NULL;

}

template

voidBinaryTree:

:

Destroy(BinaryTreeNode*p)

{

if(p!

=NULL)

{

Destroy(p->GetLeftChild());

Destroy(p->GetRightChild());

deletep;

}

}

template

voidBinaryTree:

:

InsertLeftChild(BinaryTreeNode*p,constT&d)const

{

BinaryTreeNode*q=newBinaryTreeNode(d);

q->SetLeftChild(p->GetLeftChild());

p->SetLeftChild(q);

}

template

voidBinaryTree:

:

InsertRightChild(BinaryTreeNode*p,constT&d)const

{

BinaryTreeNode*q=newBinaryTreeNode(d);

q->SetRightChild(p->GetRightChild());

p->SetRightChild(q);

}

//先序遍历的递归算法

//template

//voidBinaryTree:

:

PreOrderTraverse(BinaryTreeNode*root)const

//{

//if(root!

=NULL)

//{

//cout<GetData();

//PreOrderTraverse(root->GetLeftChild());

//PreOrderTraverse(root->GetRightChild());

//}

//}

//先序遍历的栈结构算法

template

voidBinaryTree:

:

PreOrderTraverse(BinaryTreeNode*root)const

{

stack*>s;

BinaryTreeNode*p=root;

while(!

s.empty()||p!

=NULL)

{

while(p)

{

s.push(p);

cout<GetData();

p=p->GetLeftChild();

}

p=s.top();

s.pop();

p=p->GetRightChild();

}

}

//先序遍历的栈结构算法

template

voidBinaryTree:

:

InOrderTraverse(BinaryTreeNode*root)const

{

stack*>s;

BinaryTreeNode*p=root;

while(!

s.empty()||p!

=NULL)

{

while(p)

{

s.push(p);

p=p->GetLeftChild();

}

p=s.top();

s.pop();

cout<GetData();

p=p->GetRightChild();

}

}

////后序遍历的递归算法

//template

//voidBinaryTree:

:

PostOrderTraverse(BinaryTreeNode*root)const

//{

//if(root!

=NULL)

//{

//PreOrderTraverse(root->GetLeftChild());

//PreOrderTraverse(root->GetRightChild());

//}

//cout<GetData();

//}

//按层遍历整棵树

template

voidBinaryTree:

:

LevelOrderTraverse(BinaryTreeNode*root)const

{

queue*>q;

if(root!

=NULL)

{

q.push(root);

}

while(!

q.empty())

{

root=q.front(),q.pop();

cout<GetData();

if(root->GetLeftChild())

q.push(root->GetLeftChild());

if(root->GetRightChild())

q.push(root->GetRightChild());

}

}

//后序遍历的栈结构算法

template

voidBinaryTree:

:

PostOrderTraverse(BinaryTreeNode*root)const

{

stack*>s;

BinaryTreeNode*p=root;

while(!

s.empty()||p!

=NULL)

{

while(p)

{

s.push(p);

p=p->GetLeftChild();

}

p=s.top();

//cout<GetData();

s.pop();

p=p->GetRightChild();

}

}

#endif

//主程序

#include

#include"BinaryTreeNode.h"

#include"BinaryTree.h"

usingnamespacestd;

voidmain()

{

BinaryTreemyBinTree('A');

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");

}

展开阅读全文
相关资源
猜你喜欢
相关搜索

当前位置:首页 > 高等教育 > 文学

copyright@ 2008-2022 冰豆网网站版权所有

经营许可证编号:鄂ICP备2022015515号-1