jqtree 2021-05-05 10:45 采纳率: 80%
浏览 55
已采纳

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

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

#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;
}

这是我的运行结果:

二叉树如图:

  • 写回答

3条回答 默认 最新

  • 正在学C++ 2021-05-05 11:13
    关注
    #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++的引用。顺便修饰了一下,把文字说明移动了位置,不然写在函数内部每一次建立节点都要输出文字。

     

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?

悬赏问题

  • ¥15 链接问题 C++LNK2001 无法解析的外部符号
  • ¥50 安装pyaudiokits失败
  • ¥15 计组这些题应该咋做呀
  • ¥60 更换迈创SOL6M4AE卡的时候,驱动要重新装才能使用,怎么解决?
  • ¥15 让node服务器有自动加载文件的功能
  • ¥15 jmeter脚本回放有的是对的有的是错的
  • ¥15 r语言蛋白组学相关问题
  • ¥15 Python时间序列如何拟合疏系数模型
  • ¥15 求学软件的前人们指明方向🥺
  • ¥50 如何增强飞上天的树莓派的热点信号强度,以使得笔记本可以在地面实现远程桌面连接