HTRE 2022-04-13 14:33 采纳率: 93.5%
浏览 41
已结题

数据结构,使用先序,建立二叉树,错哪里了

#include<stdio.h>
#include<stdlib.h>
#include<string.h>
typedef struct tree {
struct tree*lchild,*rchild;
char date;
} tree,*ntree;
char ch[100];
int i=0;
int creat(ntree p) {
while(i<=strlen(ch)) {

    if(p->date=='#') return 0;
    else {
        p->date=ch[i];
        creat(p->lchild);
        creat(p->rchild);
    }
    i++;
    p=(ntree)malloc(sizeof(tree));
}

}
int main() {
gets(ch);
ntree p,head;
p=head=(ntree)malloc(sizeof(tree));
if(p) {
printf("初始化成功");
creat(p);
}
}

  • 写回答

2条回答 默认 最新

  • qzjhjxj 2022-04-14 00:28
    关注

    修改如下,供参考:

    #include<stdio.h>
    #include<stdlib.h>
    #include<string.h>
    typedef struct tree {
        struct tree*lchild,*rchild;
        char date;
    } tree,*ntree;
    char ch[100];
    int i=0;
    int creat(ntree& p) {
        //while(i<=strlen(ch)) {
        char c = ch[i++];
        if (c == '#' || c == '\0') p = NULL; //if(p->date=='#') return 0;
        else {
              p=(ntree)malloc(sizeof(tree));
              p->date=c;
              creat(p->lchild);
              creat(p->rchild);
        }
        //i++;
        //p=(ntree)malloc(sizeof(tree));
        //}
    }
    void firstprint(ntree p)
    {
        if(p){
            printf("%4c",p->date);
            firstprint(p->lchild);
            firstprint(p->rchild);
        }
    }
    int main() {
        gets(ch);
        ntree p,head;
        //p=head=(ntree)malloc(sizeof(tree));
        //if(p) {
        //      printf("初始化成功");
        creat(p);
        //}
        firstprint(p);
        
        return 0;
    }
    
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

问题事件

  • 系统已结题 4月22日
  • 已采纳回答 4月14日
  • 创建了问题 4月13日

悬赏问题

  • ¥15 下图接收小电路,谁知道原理
  • ¥15 装 pytorch 的时候出了好多问题,遇到这种情况怎么处理?
  • ¥20 IOS游览器某宝手机网页版自动立即购买JavaScript脚本
  • ¥15 手机接入宽带网线,如何释放宽带全部速度
  • ¥30 关于#r语言#的问题:如何对R语言中mfgarch包中构建的garch-midas模型进行样本内长期波动率预测和样本外长期波动率预测
  • ¥15 ETLCloud 处理json多层级问题
  • ¥15 matlab中使用gurobi时报错
  • ¥15 这个主板怎么能扩出一两个sata口
  • ¥15 不是,这到底错哪儿了😭
  • ¥15 2020长安杯与连接网探