先序、中序、后序遍历二叉树
#include <iostream>
using namespace std;
typedef struct BiTNode{
char data;
struct BiTNode *lchild,*rchild;
}BiTNode,*BiTree;
void CreateBiTree (BiTree &T)
{
char ch;
cin >> ch;
if(ch=='#')
T=NULL;
else
{
T = new BiTNode;
T->data = ch;
CreateBiTree(T->lchild);
CreateBiTree(T->rchild);
}
}
void xianxu(BiTree T)
{
if(T)
{
cout << T->data;
xianxu(T->lchild);
xianxu(T->rchild);
}
}
void zhongxu(BiTree T)
{
if(T)
{
zhongxu(T->lchild);
cout << T->data;
zhongxu(T->rchild);
}
}
void hongxu(BiTree T)
{
if(T)
{
hongxu(T->lchild);
hongxu(T->rchild);
cout << T->data;
}
}
void main()
{
BiTree T;
CreateBiTree(T);
cout << "先序遍历为:" ;
xianxu(T);
cout << endl;
cout << "中序遍历为:" ;
zhongxu(T);
cout << endl;
cout << "后序遍历为:" ;
hongxu(T);
cout << endl;
}