二叉树先序遍历,帮忙看下这个程序执行结果为什么不对呀,输出了一堆?和P
#include <stdio.h>#include <malloc.h>
typedef struct BiTree
{
char data;
struct BiTree * pLchild;
struct BiTree * pRchild;
}BITREE,* PBT;
PBT createBiTree(void)
{
PBT pBTa = (PBT)malloc(sizeof(BITREE));
PBT pBTb = (PBT)malloc(sizeof(BITREE));
PBT pBTc = (PBT)malloc(sizeof(BITREE));
PBT pBTd = (PBT)malloc(sizeof(BITREE));
PBT pBTe = (PBT)malloc(sizeof(BITREE));
PBT pBTf = (PBT)malloc(sizeof(BITREE));
PBT pBTg = (PBT)malloc(sizeof(BITREE));
pBTa->data = 'A';
pBTb->data = 'B';
pBTc->data = 'C';
pBTd->data = 'D';
pBTe->data = 'E';
pBTf->data = 'F';
pBTg->data = 'G';
pBTa->pLchild = pBTc;
pBTa->pRchild = pBTb;
pBTc->pLchild = NULL;
pBTc->pRchild = pBTd;
pBTd->pLchild = pBTd->pRchild = NULL;
pBTb->pLchild = pBTe;
pBTb->pRchild = pBTf;
pBTe->pLchild = NULL;
pBTe->pRchild = pBTg;
pBTf->pLchild = pBTf->pRchild = NULL;
return pBTa;
}
void preTraverse(PBT pBT)
{
if(pBT != NULL)
{
printf("%c\n",pBT->data);
if (pBT->pLchild != NULL)
{
preTraverse(pBT->pLchild);
}
if (pBT->pRchild != NULL)
{
preTraverse(pBT->pRchild);
}
}
return;
}
int main(void)
{
PBT pBT = createBiTree();
preTraverse(pBT);
return 0;
}