#include<iostream>
using namespace std;
template<typename t>
struct binode
{
t data;
binode<t>*lchild,*rchild;
};
template<typename t>
class bitree
{
private:
binode<t>*creat(binode<t>*bt)
{
char ch;
cin>>ch;
if(ch=='#')
bt=NULL;
else{
bt=new binode<t>;
bt->data=ch;
bt->lchild=creat(bt->lchild);
bt->rchild=creat(bt->rchild);
}
return bt;
}
void release(binode<t>*bt)
{
if(bt==NULL)
return;
else
{
release(bt->lchild);
release(bt->rchild);
delete bt;
}
}
void preorder(binode<t>*bt)
{
if(bt==NULL)
return;
else
{
cout<<bt->data;
preorder(bt->lchild);
preorder(bt->rchild);
}
}
void inorder(binode<t>*bt)
{
if(bt==NULL)
return;
else{
inorder(bt->lchild);
cout<<bt->data;
inorder(bt->rchild);
}
}
void postorder(binode<t>*bt)
{
if(bt==NULL)
return;
else{
inorder(bt->lchild);
inorder(bt->rchild);
cout<<bt->data;
}
}
binode<t>*root;
public:
bitree()
{
root=creat(root);
}
~bitree()
{
release(root);
}
void preorder()
{
preorder(root);
}
void inorder()
{
inorder(root);
}
void postorder()
{
postorder(root);
}
};
int main()
{
bitree<char> f;
cout<<"该二叉树的前序遍历的序列是:"<<endl;
f.preorder();
cout<<"\n该二叉树的中序遍历序列是:"<<endl;
f.inorder();
cout<<"\n该二叉树的后序遍历序列是:"<<endl;
f.postorder();
return 0;
}
运行结果:
123##4##5#6##
该二叉树的前序遍历的序列是:
123456
该二叉树的中序遍历序列是:
324156
该二叉树的后序遍历序列是:
324561
Process returned 0 (0x0) execution time : 19.111 s
Press any key to continue.