typedef struct BiTNode //创建树的节点并声明根节点指针
{
char data;
struct BiTNode *lchild,*rchild;
}BiTNode,*BiTree;
void CreateBiTree(BiTree &T) //先序序列建立二叉树
{
char ch;
scanf("%c",&ch);
if(ch=='^') T=NULL;
else
{
T=new BiTNode;
T->data=ch;
CreateBiTree(T->lchild);
CreateBiTree(T->rchild);
}
}
void PreOrderTraverse(BiTree &T) //先序遍历
{
if(T)
{
cout<<T->data;
PreOrderTraverse(T->lchild);
PreOrderTraverse(T->rchild);
}
}
void InOrderTraverse(BiTree &T) //中序遍历
{
if(T)
{
InOrderTraverse(T->lchild);
cout<<T->data;
InOrderTraverse(T->rchild);
}
}
void PostOrderTraverse(BiTree &T) //后序遍历
{
if(T)
{
PostOrderTraverse(T->lchild);
PostOrderTraverse(T->rchild);
cout<<T->data;
}
}
int main()
{
BiTree T;
CreateBiTree(T);
PreOrderTraverse(T);
cout<<endl;
InOrderTraverse(T);
cout<<endl;
PostOrderTraverse(T);
cout<<endl;
return 0;
}