#include <stdio.h>
#include <malloc.h>
typedef struct BTnode
{
int data;
struct BTnode *pLchild;
struct BTnode *pRchild;
}BTNode;
BTNode* createBTree()//静态造链式树
{
BTNode* pA =(BTNode*) malloc(sizeof(BTNode));
BTNode* pB =(BTNode*) malloc(sizeof(BTNode));
BTNode* pC =(BTNode*) malloc(sizeof(BTNode));
BTNode* pD =(BTNode*) malloc(sizeof(BTNode));
BTNode* pE =(BTNode*) malloc(sizeof(BTNode));
pA->data = 'A';
pB->data = 'B';
pC->data = 'C';
pD->data = 'D';
pE->data = 'E';
pA->pLchild = pB;
pA->pRchild = pC;
pB->pLchild = NULL;
pB->pRchild = NULL;
pC->pLchild = pD;
pC->pRchild = NULL;
pD->pLchild = NULL;
pD->pRchild = pE;
pE->pLchild = NULL;
pE->pRchild = NULL;
return pA;
}
void First_print(BTNode* pT) //先序遍历
{
//先访问根节点 先序访问左子树 先序访问右子树
if(pT)
{
printf("%c
",pT->data);
if(pT->pLchild)
{
First_print(pT->pLchild);
}
if(pT->pRchild)
{
First_print(pT->pRchild);
}
}
}
void Middl_print(BTNode* pT) //mid序遍历
{
//先访问根节点 先序访问左子树 先序访问右子树
if(pT)
{
if(pT->pLchild)
{
Middl_print(pT->pLchild);
}
printf("%c
",pT->data);
if(pT->pRchild)
{
Middl_print(pT->pRchild);
}
}
}
void end_print(BTNode* pT) //先序遍历
{
//先访问根节点 先序访问左子树 先序访问右子树
if(pT)
{
if(pT->pLchild)
{
end_print(pT->pLchild);
}
if(pT->pRchild)
{
end_print(pT->pRchild);
}
printf("%c
",pT->data);
}
}
int main ()
{
BTNode* pT = createBTree();
printf("first traverse print:
");
First_print(pT);
printf("middle traverse print:
");
Middl_print(pT);
printf("end traverse print:
");
end_print(pT);
return 0;
}