#include <iostream>
#include "malloc.h"
using namespace std;
typedef struct BTNode
{
char data;
struct BTNode *Lchild,*Rchild;
}BTNode;
struct BTNode *lpRoot,*temp;
void visit(char a)
{
printf("%c
",a);
}
//先序遍历的递归算法
void PreorderTraverse(BTNode *T)
{
if (T!=NULL)
{
cout<<T->data;
PreorderTraverse(T->Lchild);
PreorderTraverse(T->Rchild);
}
}
//中序遍历的递归算法
void InorderTraverse(BTNode *T)
{
if (T!=NULL)
{
InorderTraverse(T->Lchild);
visit(T->data);
InorderTraverse(T->Rchild);
}
}
//后序遍历的递归算法
void PostorderTraverse(BTNode *T)
{
if (T!=NULL)
{
PostorderTraverse(T->Lchild);
PostorderTraverse(T->Rchild);
visit(T->data);
}
}
BTNode *Preorder_Create_BTree(BTNode *T)
{
char ch;
cin>>ch;
if (ch=='0')
{
T=NULL;
return(T);
}
else
{
T=(BTNode*)malloc(sizeof(BTNode));
T->data=ch;
Preorder_Create_BTree(T->Lchild);
Preorder_Create_BTree(T->Rchild);
return(T);
}
}
void Release(BTNode *T)
{
if (T!=NULL)
{
Release(T->Lchild);
Release(T->Rchild);
delete T;
T=NULL;
}
}
void main()
{
temp=Preorder_Create_BTree(lpRoot);
PreorderTraverse(temp);
getchar();
}