数据结构 二叉树的遍历 运行结果不对

代码可运行,但结果不对,没有运行完,这是怎么回事捏?谢谢帮助!

#include<stdio.h>
#include<stdlib.h>
typedef char TElemType ;
#define ERROR 0;
typedef struct BiTNode{
	TElemType data;
	struct BiTNode *lchild,*rchild;//左右孩子指针 
}BiTNode,*BiTree;
//先序遍历创建二叉树 
BiTree creatbitree(BiTree T){
	TElemType ch;
	printf("请按照先序遍历输入二叉树:");
	scanf("%c",ch);
	if(ch=='&') T=NULL;//@代表空格字符 
	else{
		T=(BiTNode*)malloc(sizeof(BiTNode));
		T->data=ch;//生成根结点 
		T->lchild=creatbitree(T->lchild);//生成左子树 
		T->rchild=creatbitree(T->rchild);//生成右子树 
	}
	printf("二叉树创建成功!"); 
	return T; 	}
//先序遍历
void preorder(BiTree T) {
	printf("先序遍历二叉树:");
	if(T){
		printf("%c ",T->data);
		preorder(T->lchild);
		preorder(T->rchild);}
	else printf("空树!"); }
//中序递归遍历 
void inorder(BiTree T){
	printf("中序递归遍历二叉树:") ;
	if(T){
	inorder(T->lchild)	;
	printf("%c ",T->data);
	inorder(T->rchild);}
	else printf("空树!") ; }
//后序遍历 
void postorder(BiTree T){
	printf("后序遍历二叉树:"); 
	if(T){
		postorder(T->lchild);
		postorder(T->rchild);
		printf("%c ",T->data);}
	else printf("空树!"); }
int main(){
	BiTree S,T;
	S=NULL; 
	T=creatbitree(S);
	preorder(T) ;
	inorder(T);
	postorder(T);
	return 0;
}

这是我的运行结果:

二叉树如图:

#include<stdio.h>
#include<stdlib.h>
typedef char TElemType ;
#define ERROR 0;
typedef struct BiTNode{
    TElemType data;
    struct BiTNode *lchild,*rchild;//左右孩子指针
}BiTNode,*BiTree;
//先序遍历创建二叉树
void creatbitree(BiTree &T){        //////////////////////////C++引用就是给变量起别名,T前面加一个&
    TElemType ch;

    scanf("%c",&ch);                /////////////////////输入错误  &ch
    if(ch=='@') T=NULL;//@代表空格字符        /////////////////ch=='@'
    else{
        T=(BiTNode*)malloc(sizeof(BiTNode));
        T->data=ch;//生成根结点
        creatbitree(T->lchild);//生成左子树      ///////////对应改变
        creatbitree(T->rchild);//生成右子树      ///////////对应改变
    }

}                                           ///////////////////去掉return
//先序遍历
void preorder(BiTree T) {

    if(T){
        printf("%c ",T->data);
        preorder(T->lchild);
        preorder(T->rchild);}
    else printf("@ "); }
//中序递归遍历
void inorder(BiTree T){

    if(T){
        inorder(T->lchild)	;
        printf("%c ",T->data);
        inorder(T->rchild);}
    else printf("@ ") ; }
//后序遍历
void postorder(BiTree T){

    if(T){
        postorder(T->lchild);
        postorder(T->rchild);
        printf("%c ",T->data);}
    else printf("@ "); }
int main(){
    BiTree T;
    printf("请按照先序遍历输入二叉树:\n");
    creatbitree(T); //////////////////////借助C++引用,不要S
    printf("二叉树创建成功!\n");

    printf("先序遍历二叉树:\n");
    preorder(T) ;

    printf("\n中序递归遍历二叉树:\n") ;
    inorder(T);

    printf("\n后序遍历二叉树:\n");
    postorder(T);
    return 0;
}
//ABC@@DE@G@@F@@@

借助C++的引用。顺便修饰了一下,把文字说明移动了位置,不然写在函数内部每一次建立节点都要输出文字。

 

#include<stdio.h>
#include<stdlib.h>
typedef char TElemType ;
#define ERROR 0;
typedef struct BiTNode{
    TElemType data;
    struct BiTNode *lchild,*rchild;//左右孩子指针
}BiTNode,*BiTree;
//先序遍历创建二叉树
BiTree creatbitree(){       /////////////////////////////////不要参数
    BiTree T;               /////////////////////////////////直接定义一个T然后返回
    TElemType ch;

    scanf("%c",&ch);        //////////////////////////////// &ch
    if(ch=='@') T=NULL;//@代表空格字符        ////////////////ch=='@'
    else{
        T=(BiTNode*)malloc(sizeof(BiTNode));
        T->data=ch;//生成根结点
        T->lchild=creatbitree();//生成左子树     /////////////不要参数
        T->rchild=creatbitree();//生成右子树     /////////////不要参数
    }

    return T; 	}
//先序遍历
void preorder(BiTree T) {

    if(T){
        printf("%c ",T->data);
        preorder(T->lchild);
        preorder(T->rchild);}
    else printf("@ "); }
//中序递归遍历
void inorder(BiTree T){

    if(T){
        inorder(T->lchild)	;
        printf("%c ",T->data);
        inorder(T->rchild);}
    else printf("@ ") ; }
//后序遍历
void postorder(BiTree T){

    if(T){
        postorder(T->lchild);
        postorder(T->rchild);
        printf("%c ",T->data);}
    else printf("@ "); }
int main(){
    BiTree T;
    printf("请按照先序遍历输入二叉树:\n");
    T=creatbitree();        //////////////////////不要参数S
    printf("二叉树创建成功!\n");

    printf("先序遍历二叉树:\n");
    preorder(T) ;

    printf("\n中序递归遍历二叉树:\n") ;
    inorder(T);

    printf("\n后序遍历二叉树:\n");
    postorder(T);
    return 0;
}
//ABC@@DE@G@@F@@@

另一个办法,没有借助C++的引用。也可以做。

也许对你有帮助:https://blog.csdn.net/it_xiangqiang/category_10768339.html