#include<iostream>
using namespace std;
typedef char datatype;
typedef struct node{
datatype data;
struct node*lchild,*rchild;//左右节点
} bintnode;
typedef bintnode *bintree;
bintree root;//指向二叉树根节点的指针
//创建二叉树
bintree createbintree()
{
char ch;
bintree t;
if((ch = getchar()) == '#')
t = NULL;
else
{
t = new bintnode;
t->data = ch;
t->lchild=createbintree();
t->rchild=createbintree();
}
return t;
}
//先序递归遍历
void preorder(bintree t)
{
if(t)
{
printf("%c",t->data);
preorder(t->lchild);
preorder(t->rchild);
}
}
//中序递归遍历
void inorder(bintree t)
{
if(t)
{
inorder(t->lchild);
printf("%c",t->data);
inorder(t->rchild);
}
}
//后序递归遍历
void postorder(bintree t)
{
if(t)
{
postorder(t->lchild);
postorder(t->rchild);
printf("%c",t->data);
}
}
//采用前序来遍历删除
void del(bintree t)
{
if(t)
{
delete t;
del(t->lchild);
del(t->rchild);
}
}
int main()
{
bintree t = createbintree();
preorder(t);
cout << endl;
inorder(t);
cout << endl;
postorder(t);
cout << endl;
del(t); //删除动态内存
return 0;
}
测试实例:abd#e##fg###c##