二叉树 C语言实现

二叉树中储存的数据范围仅限于26个英文字母
程序提示用户输入二叉树的先序和中序序列
接受输入后,调用相应函数完成二叉树的创建
成功创建后:程序自动输出该二叉树的后续遍历序列
~~~~~~
试了很多都不行,
那位高手能帮一下?
以下是我自己写的:
没有错误,可是在输入时就有问题

#include <stdio.h>
#include <stdlib.h>

typedef struct BTree{
char data;
struct BTree *lchild;
struct BTree *rchild;
}BinTree;
BinTree *pre_order(BinTree *p)
{
char ch;
if(p!=NULL)
{
scanf("%c",&ch);
if(ch==' ')
return NULL;
else
{
p=(BinTree *)malloc(sizeof(BinTree));
p->data=ch;
pre_order(p->lchild);
pre_order(p->rchild);
return p;
}
}
}
BinTree *in_order(BinTree *p)
{
char ch;
if(p)
{
in_order(p->lchild);
scanf("%c",&ch);
if(ch==' ')
return NULL;
in_order(p->rchild);
}
}
post_order(BinTree *p)
{
if(p!=NULL)
{
post_order(p->lchild);
post_order(p->rchild);
printf("%c ",p->data);
}
}
void main()
{
BinTree tree;
printf("Please Enter the pre_order:\n");
tree=*pre_order(&tree);
printf("\n");
printf("Please Enter the in_order:\n");
in_order(&tree);
printf("\n");
post_order(&tree);
printf("\n");
}

编译通过
先序创建并输出
#include <stdio.h>
#include <stdlib.h>

typedef struct BTree{
char data;
struct BTree *lchild;
struct BTree *rchild;
}BinTree;
BinTree *pre_order()
{
BinTree *p;
char ch;
scanf("%c",&ch);
if(ch==' ')
return NULL;
p=(BinTree *)malloc(sizeof(BinTree));
p->data=ch;
p->lchild=pre_order();
p->rchild=pre_order();
return p;
}
BinTree *in_order()
{
BinTree *p;
char ch;
p->lchild=in_order();
scanf("%c",&ch);
if(ch==' ')
return NULL;
p->rchild=in_order();
}
void post_order(BinTree *p)
{
if(p!=NULL)
{
post_order(p->lchild);
post_order(p->rchild);
printf("%c ",p->data);
}
}
void main()
{
BinTree *tree;
printf("Please Enter the pre_order:\n");
tree=pre_order();
printf("\n");
//printf("Please Enter the in_order:\n");
//tree=in_order();
//printf("\n");
post_order(tree);
printf("\n");
}
先序和中序不能同时使用,要么就光先序,要么就再用另一个数做中序
温馨提示:答案为网友推荐,仅供参考
第1个回答  2007-05-10
BinTree *in_order(BinTree *p)
{
char ch;
if(p)
{
in_order(p->lchild);
scanf("%c",&ch);
if(ch==' ')
return NULL;
in_order(p->rchild);
}
}
好象不能创建吧???