郝斌《数据结构》课程源代码
树的实现
用者自取,自由分享
#include <stdio.h>
struct BTNode
{
char data;
struct BTNode * pLchild;
struct BTNode * pRchild;
};
struct BTNode * CreatBTree(void);
void PreTraverseBTree(struct BTNode *);
void InTraverseBTree(struct BTNode *);
void PostTraverseBTree(struct BTNode *);
int main(void)
{
struct BTNode * pT = CreatBTree();
PreTraverseBTree(pT);
InTraverseBTree(pT);
PostTraverseBTree(pT);
return 0;
}
struct BTNode * CreatBTree(void)
{
struct BTNode * pA = (struct BTNode *)malloc(sizeof(struct BTNode));
struct BTNode * pB = (struct BTNode *)malloc(sizeof(struct BTNode));
struct BTNode * pC = (struct BTNode *)malloc(sizeof(struct BTNode));
struct BTNode * pD = (struct BTNode *)malloc(sizeof(struct BTNode));
struct BTNode * pE = (struct BTNode *)malloc(sizeof(struct BTNode));
pA->data = 'A';
pB->data = 'B';
pC->data = 'C';
pD->data = 'D';
pE->data = 'E';
pA->pLchild = pB;
pA->pRchild = pC;
pB->pLchild = pB->pRchild = NULL;
pC->pLchild = pD;
pC->pRchild = NULL;
pD->pLchild = NULL;
pD->pRchild = pE;
pE->pLchild = pE->pRchild = NULL;
return pA;
}
void PreTraverseBTree(struct BTNode * pT)
{
if(NULL != pT)
{
printf("%c\n", pT->data);
if(NULL != pT->pLchild)
{
PreTraverseBTree(pT->pLchild);
}
if(NULL != pT->pRchild)
{
PreTraverseBTree(pT->pRchild);
}
}
}
void InTraverseBTree(struct BTNode * pT)
{
if(NULL != pT)
{
if(NULL != pT->pLchild)
{
InTraverseBTree(pT->pLchild);
}
printf("%c\n", pT->data);
if(NULL != pT->pRchild)
{
InTraverseBTree(pT->pRchild);
}
}
}
void PostTraverseBTree(struct BTNode * pT)
{
if(NULL != pT)
{
if(NULL != pT->pLchild)
{
PostTraverseBTree(pT->pLchild);
}
if(NULL != pT->pRchild)
{
PostTraverseBTree(pT->pRchild);
}
printf("%c\n", pT->data);
}
}