主题:不能输出前序遍历的值
各位大侠帮忙看下小弟的前序建立二叉树并前序遍历二叉树的程序,程序运行结果不对,请大家帮忙改一下,谢谢啦,我用的是windowTC
#include<stdio.h>
#include<malloc.h>
#define OK 1
#define OVERFLOW -1
typedef struct BiTNode
{
char data;
struct BiTNode *lchild,*rchild;
}BiTNode,*BiTree;
int CreatBiTree(BiTree T)
{
char ch;
scanf("%c",&ch);
if(ch==' ')
T=NULL;
else
{
if(!(T=(BiTree *)malloc(sizeof(BiTNode))))
exit(OVERFLOW);
T->data=ch;
CreatBiTree(T->lchild);
CreatBiTree(T->rchild);
}
return OK;
}
int PreBiTree(BiTree T)
{
if(T!=NULL)
{
printf("%c",T->data);
getch();
PreBiTree(T->lchild);
getch();
PreBiTree(T->rchild);
getch();
}
return OK;
}
void main()
{
BiTree T;
printf("Enter the element: \n");
CreatBiTree(T);
PreBiTree(T);
getch();
}
#include<stdio.h>
#include<malloc.h>
#define OK 1
#define OVERFLOW -1
typedef struct BiTNode
{
char data;
struct BiTNode *lchild,*rchild;
}BiTNode,*BiTree;
int CreatBiTree(BiTree T)
{
char ch;
scanf("%c",&ch);
if(ch==' ')
T=NULL;
else
{
if(!(T=(BiTree *)malloc(sizeof(BiTNode))))
exit(OVERFLOW);
T->data=ch;
CreatBiTree(T->lchild);
CreatBiTree(T->rchild);
}
return OK;
}
int PreBiTree(BiTree T)
{
if(T!=NULL)
{
printf("%c",T->data);
getch();
PreBiTree(T->lchild);
getch();
PreBiTree(T->rchild);
getch();
}
return OK;
}
void main()
{
BiTree T;
printf("Enter the element: \n");
CreatBiTree(T);
PreBiTree(T);
getch();
}