我自己写的二叉树递归遍历在输出时有错误!请大家看看!
读入一个字符串,作为节点!#define MAX 30
#include <stdio.h>
#include <malloc.h>
typedef struct btnode
{
char data;
struct btnode *lchild,*rchild;
}bttree;
bttree *cre_tree(char *str,int i,int m)
{
bttree *p;
if(i> =m)
return NULL;
p=(bttree *)malloc(sizeof(bttree));
p-> data=str[i];
p-> lchild=cre_tree(str,i+1,m);
p-> rchild=cre_tree(str,i+2,m);
return p;
}
void preorder(bttree *t)
{
if(t!=NULL)
{
printf("%c",t-> data);
if(t-> lchild)
{
printf("-> ");
preorder(t-> lchild);
}
if(t-> rchild)
{
printf("-> ");
preorder(t-> rchild);
}
}
}
void inorder(bttree *t)
{
if(t!=NULL)
{
inorder(t-> lchild);
printf("%c",t-> data);
printf("-> ");
inorder(t-> rchild);
}
}
void postorder(bttree *t)
{
if(t!=NULL)
{
postorder(t-> lchild);
inorder(t-> rchild);
printf("%c",t-> data);
printf("-> ");
}
}
void main()
{
int i,n;
char str[MAX];
bttree *root;
printf("please input a bbtree node num:\n");
scanf("%d",&n);
getchar();
printf("please input a string which length is %d:",n);
for(i=0;i <n;i++)
str[i]=getchar();
printf("\n\n");
root=cre_tree(str,0,n);
printf("the tree is already created\n");
printf("\n");
printf("the result after preorder processing:");
preorder(root);
printf("\n");
printf("the result after inorder processing:");
inorder(root);
printf("\n");
printf("the result after postorder processing:");
inorder(root);
printf("\n\n");
}