Blue(l'mAlex的迷弟) 2023-11-19 14:21 采纳率: 92.3%
浏览 6
已结题

C语言 数据结构 二叉树

#include<stdio.h>
#include<string.h>
#include<stdlib.h>
#define max 50
typedef struct tree{
    struct tree *LChild;
    char data;
    struct tree *RChild;
}BitNode,*BiTree;
BiTree creatree(BiTree root,char*Data){
    static int flag=1;
    static int i = 0;
    if(Data[i]==' '){
        root = NULL;
        i++; 
        return 0;
    }
    if(flag!=1){root = (BitNode*)malloc(sizeof(BitNode));}
    root->data = Data[i];  
    //root->LChild = (BitNode*)malloc(sizeof(BitNode)); 
    //root->RChild = (BitNode*)malloc(sizeof(BitNode)); 
    i++;
    flag=0;
    creatree(root->LChild,Data);
    creatree(root->RChild,Data);
}

//先序遍历 
void PreOrder(BiTree root){
    if(root!=NULL){
        printf("%c",root->data);
        PreOrder(root->LChild);
        PreOrder(root->RChild); 
    }
}
//中序遍历
 void InOrder(BiTree root){
    if(root!=NULL){
        InOrder(root->LChild);
        printf("%c",root->data);
        InOrder(root->RChild); 
    }
}
//后序遍历
void PostOrder(BiTree root){
    if(root!=NULL){
        PostOrder(root->LChild);
        PostOrder(root->RChild);
        printf("%c",root->data); 
    }
} 
int main(){
    BiTree root;
    BiTree head;
    char *Data;
    Data=(char*)malloc(sizeof(char)*max);
    gets(Data);
    head =root = (BitNode*)malloc(sizeof(BitNode));
    creatree(root,Data);
    PreOrder(head);
    return 0;
} 

这代码有什么问题?

  • 写回答

2条回答 默认 最新

  • hbcqg712 2023-11-19 16:40
    关注

    你的代码有几个问题。首先,你可能没有正确地初始化你的树结构,这可能导致内存泄漏或者错误的内存访问。其次,你在使用 gets() 函数,这在实际的编程环境中已经被废弃,因为它可能导致缓冲区溢出。

    我会按照你的代码的逻辑进行修改,并给出详细的解释:

    #include<stdio.h>  
    #include<string.h>  
    #include<stdlib.h>  
      
    #define max 50  
      
    typedef struct tree{  
        struct tree *LChild;  
        char data;  
        struct tree *RChild;  
    }BitNode,*BiTree;  
      
    BiTree creatree(BiTree root,char* Data, int* i){  
        static int flag=1;  
        if(Data[*i]==' '){  
            root = NULL;  
            (*i)++;   
            return root;  
        }  
        if(flag!=1){  
            root = (BitNode*)malloc(sizeof(BitNode));  
            flag = 0; // Set flag to 0 after malloc, to ensure it won't be allocated again for this node.  
        }  
        root->data = Data[*i];    
        root->LChild = creatree(root->LChild,Data, i);   
        root->RChild = creatree(root->RChild,Data, i);   
        (*i)++;  
        return root;  
    }  
       
    void PreOrder(BiTree root){  
        if(root!=NULL){  
            printf("%c",root->data);  
            PreOrder(root->LChild);  
            PreOrder(root->RChild);   
        }  
    }  
      
    void InOrder(BiTree root){  
        if(root!=NULL){  
            InOrder(root->LChild);  
            printf("%c",root->data);  
            InOrder(root->RChild);   
        }  
    }  
      
    void PostOrder(BiTree root){  
        if(root!=NULL){  
            PostOrder(root->LChild);  
            PostOrder(root->RChild);  
            printf("%c",root->data);   
        }  
    }   
      
    int main(){  
        BiTree root;  
        BiTree head;  
        char *Data;  
        int i = 0; // Initialize index.   
        Data=(char*)malloc(sizeof(char)*max);  
        // Remove gets() and replace with a safe method to get input. Here I use fgets().   
        if (fgets(Data, max, stdin) == NULL) {   
            printf("Invalid input.\n");   
            return -1;   
        }       
        head = root = (BitNode*)malloc(sizeof(BitNode));  // Allocate memory for the root.   
        root = creatree(root,Data, &i); // Pass the address of the index.   
        PreOrder(head);  // Print the tree in pre-order.   
        return 0;   
    }
    
    
    
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

问题事件

  • 系统已结题 11月27日
  • 已采纳回答 11月19日
  • 修改了问题 11月19日
  • 创建了问题 11月19日

悬赏问题

  • ¥15 加装宝马安卓中控改变开机画面
  • ¥15 STK安装问题问问大家,这种情况应该怎么办
  • ¥15 更换了一个新的win10系统,再下载VS时碰到的问题,是C++组件的?
  • ¥15 关于罗技鼠标宏lua文件的问题
  • ¥15 halcon ocr mlp 识别问题
  • ¥15 已知曲线满足正余弦函数,根据其峰值,还原出整条曲线
  • ¥20 无法创建新的堆栈防护界面
  • ¥15 sessionStorage在vue中的用法
  • ¥15 wordpress更换域名后用户图片头像不显示
  • ¥15 如何在ubunto上安装CEF (Chromium Embedded Framework),并且基于qt实现打开一个web