#include <iostream>
using namespace std;
const int MaxSize = 100;
template <typename T> //二叉树的结点结构
struct BiNode
{
T data;
BiNode<T> *lchild,*rchild;
BiNode<T> *btr;
int flag;
};
template <typename T>
class BiTree
{
private:
BiNode<T> *root;
BiNode<T> *Create();
void Release(BiNode<T> *root); //析构函数调用
public:
BiTree(); //构造函数,初始化一棵二叉树,其前序序列由键盘输入
~BiTree(); //析构函数,释放二叉链表中各结点的存储空间
BiNode<T> *GetRoot(); //获得指向根结点的指针
void PreOrder(BiNode<T> *root); //前序遍历二叉树
void InOrder(BiNode<T> *root); //中序遍历二叉树
void PostOrder(BiNode<T> *root); //后序遍历二叉树
void LevelOrder(BiNode<T> *root); //层序遍历二叉树
};
/*
*前置条件:无
*输 入:无
*功 能:调用Create()函数
*输 出:无
*后置条件:无
*/
template <typename T>
BiTree<T>::BiTree()
{
this->root = Create();
}
/*
*前置条件:空二叉树
*输 入:数据ch;
*功 能:初始化一棵二叉树,构造函数调用
*输 出:无
*后置条件:产生一棵二叉树
*/
template <typename T>
BiNode<T> *BiTree<T>::Create()
{
BiNode<T> *root;
T ch;
cin>>ch;
if(ch == '#')
root = NULL;
else{
root = new BiNode<T>; //生成一个结点
root->data = ch;
root->lchild = Create(); //递归建立左子树
root->rchild = Create(); //递归建立右子树
}
return root;
}
/*
*前置条件:二叉树已存在
*输 入:无
*功 能:获取指向二叉树根结点的指针
*输 出:指向二叉树根结点的指针
*后置条件:二叉树不变
*/
template <typename T>
BiNode<T> *BiTree<T>::GetRoot()
{
return root;
}
/*
*前置条件:二叉树已存在
*输 入:无
*功 能:释放二叉链表中各结点的存储空间
*输 出:无
*后置条件:二叉树不存在
*/
template<class T>
BiTree<T>::~BiTree(void)
{
Release(root);
}
/*
*前置条件:二叉树已存在
*输 入:无
*功 能:前序非递归遍历二叉树
*输 出:二叉树中结点的一个线性排列
*后置条件:二叉树不变
*/
template <typename T>
void BiTree<T>::PreOrder(BiNode<T> *root)
{
int top = -1;
BiNode<T> *Stack[MaxSize];
while(root != NULL || top != -1)
{
while(root != NULL)
{
cout<<root->data<<" ";
Stack[++top] = root;
root = root->lchild;
}
if(top != -1)
{
root = Stack[top--];
root = root->rchild;
}
}
}
/*
*前置条件:二叉树已存在
*输 入:无
*功 能:中序非递归遍历二叉树
*输 出:二叉树中结点的一个线性排列
*后置条件:二叉树不变
*/
template <typename T>
void BiTree<T>::InOrder(BiNode<T> *root)
{
int top = -1;
BiNode<T> *Stack[MaxSize];
while(root != NULL || top != -1)
{
while(root != NULL)
{
Stack[++top] = root;
root = root->lchild;
}
if(top != -1)
{
root = Stack[top--];
cout<<root->data<<" ";
root = root->rchild;
}
}
}
/*
*前置条件:二叉树已存在
*输 入:无
*功 能:后序非递归遍历二叉树
*输 出:二叉树中结点的一个线性排列
*后置条件:二叉树不变
*/
template <typename T>
void BiTree<T>::PostOrder(BiNode<T> *root)
{
int top = -1;
BiNode<T> Stack[MaxSize];
while(root != NULL || top != -1)
{
while(root != NULL)
{
top++;
Stack[top].btr = root;
Stack[top].flag = 0;
root = root->lchild;
}
while(top != -1 && Stack[top].flag == 1)
{
root = Stack[top--].btr;
cout<<root->data<<" ";
}
if(top != -1)
{
Stack[top].flag = 1;
root =Stack[top].btr;
root = root->rchild;
}
else
break;
}
}
/*
*前置条件:二叉树已存在
*输 入:无
*功 能:层序遍历二叉树
*输 出:二叉树中结点的一个线性排列
*后置条件:二叉树不变
*/
template <typename T>
void BiTree<T>::LevelOrder(BiNode<T> *root)
{
int front,rear;
front = rear = 0; //采用顺序队列,并假定不会发生上溢
BiNode<T> *Queue[MaxSize];
if(root == NULL)
return ;
Queue[++rear] = root;
while(front != rear)
{
BiNode<T> *btr;
btr = Queue[++front];
cout<<btr->data<<" ";
if(btr->lchild != NULL)
Queue[++rear] = btr->lchild;
if(btr->rchild != NULL)
Queue[++rear] = btr->rchild;
}
}
/*
*前置条件:二叉树已经存在
*输 入:无
*功 能:释放二叉树的存储空间,析构函数调用
*输 出:无
*后置条件:二叉树不存在
*/
template<class T>
void BiTree<T>::Release(BiNode<T>* root)
{
if (root != NULL){
Release(root->lchild); //释放左子树
Release(root->rchild); //释放右子树
delete root;
}
}
int main()
{
BiTree<char> bitree; //创建一棵树
BiNode<char> *binode;
binode = bitree.GetRoot(); //获取指向根结点的指针
cout<<"------前序非递归遍历------"<<endl;
bitree.PreOrder(binode);
cout<<endl;
cout<<"------中序非递归遍历------"<<endl;
bitree.InOrder(binode);
cout<<endl;
cout<<"------后序非递归遍历------"<<endl;
bitree.PostOrder(binode);
cout<<endl;
cout<<"------层序遍历------"<<endl;
bitree.LevelOrder(binode);
cout<<endl;
return 0;
}