主题:我的这个二叉树表达式的程序运行上有问题,请各位高手帮忙指点。
我编的是二叉树表达式,要求分别用先序,中序,后序表示出来。程序运行后结果并不是按先序,中序,后序表示出来。而是出来一些莫名其妙的结果,我看不懂,希望高手给小女指点一下。小女不胜感激!谢谢:)
#include<stdio.h>
#include<stdlib.h>
typedef struct BiTNode
{
char data;
struct BiTNode *lchild,*rchild;
}BiTNode,*BiTree;
BiTNode* CreateBiTreeBit( )
{
char ch;
scanf("%c", &ch);
if(ch != '#') //子树结束条件
{
BiTNode* T = (BiTNode *)malloc(sizeof(BiTNode));
if(T != NULL)
{
T->data = ch;
T->lchild = CreateBiTreeBit();
T->rchild = CreateBiTreeBit();
}
return T;
}
return NULL;
}
void paseTree(BiTNode *t)
{
if(t != NULL)
{
printf("%c \n", t->data);
paseTree(t->lchild);
paseTree(t->rchild);
}
}
void inorderTree(BiTNode *t)
{
if(t != NULL)
{
inorderTree(t->lchild);
printf("%c \n", t->data);
inorderTree(t->rchild);
}
}
void postTree(BiTNode *t)
{
if(t != NULL)
{
postTree(t->lchild);
postTree(t->rchild);
printf("%c \n", t->data);
}
}
void main()
{
BiTNode *t;
t = CreateBiTreeBit();
paseTree(t);
inorderTree(t);
postTree(t);
}
#include<stdio.h>
#include<stdlib.h>
typedef struct BiTNode
{
char data;
struct BiTNode *lchild,*rchild;
}BiTNode,*BiTree;
BiTNode* CreateBiTreeBit( )
{
char ch;
scanf("%c", &ch);
if(ch != '#') //子树结束条件
{
BiTNode* T = (BiTNode *)malloc(sizeof(BiTNode));
if(T != NULL)
{
T->data = ch;
T->lchild = CreateBiTreeBit();
T->rchild = CreateBiTreeBit();
}
return T;
}
return NULL;
}
void paseTree(BiTNode *t)
{
if(t != NULL)
{
printf("%c \n", t->data);
paseTree(t->lchild);
paseTree(t->rchild);
}
}
void inorderTree(BiTNode *t)
{
if(t != NULL)
{
inorderTree(t->lchild);
printf("%c \n", t->data);
inorderTree(t->rchild);
}
}
void postTree(BiTNode *t)
{
if(t != NULL)
{
postTree(t->lchild);
postTree(t->rchild);
printf("%c \n", t->data);
}
}
void main()
{
BiTNode *t;
t = CreateBiTreeBit();
paseTree(t);
inorderTree(t);
postTree(t);
}