qq_41235711 2018-10-20 00:02 采纳率: 66.7%
浏览 1301
已采纳

用数组建立二叉树运行出错,不知道哪里错了

感觉逻辑没问题,就是不知道哪里错了,各位帮帮忙,看了好几天了~~~

 #include <iostream>
using namespace std;
#include <stdlib.h>
#define NULLVALUE 0

typedef int Elemtype;

typedef struct Tree{
    Elemtype data;
    struct Tree *LTree;
    struct Tree *RTree;
};

void Create(Tree *tree,Elemtype *data,int &start,int len){
    if(start>=len||data[start]==NULLVALUE){
        return ;
    }
    else{
        tree=(Tree *)malloc(sizeof(Tree));
        tree->data=data[start];
        ++start;
        Create(tree->LTree,data,start,len);
        ++start;
        Create(tree->RTree,data,start,len);
    }
}

void Print(Tree *tree){
    if(tree==NULL)
        return ;
    cout<<tree->data<<"  ";
    Print(tree->LTree);
    Print(tree->RTree);
}

int main(){
    Elemtype data[]={1,2,4,0,0,5,0,0,3,6,0,0,7};
    int len=sizeof(data)/sizeof(data[0]);
    Tree *tree;
    int start=0;
    Create(tree,data,start,len);
    Print(tree);
    return 0;
} 
  • 写回答

1条回答 默认 最新

  • threenewbee 2018-10-20 01:56
    关注

    给你修改好了,如果问题解决,麻烦点下我回答右边的采纳,并且同时采纳下 https://ask.csdn.net/questions/703113

    // Q703225.cpp : Defines the entry point for the console application.
    //
    
    #include "stdafx.h"
    
    
    #include <iostream>
    using namespace std;
    #include <stdlib.h>
    #define NULLVALUE 0
    
    typedef int Elemtype;
    
    typedef struct Tree{
        Elemtype data;
        struct Tree *LTree;
        struct Tree *RTree;
    };
    
    void Create(Tree **tree,Elemtype *data,int &start,int len){
        if(start>=len||data[start]==NULLVALUE){
            *tree = NULL;
            return ;
        }
        else{
            *tree=(Tree *)malloc(sizeof(Tree));
            (*tree)->data=data[start];
            ++start;
            Create(&(*tree)->LTree,data,start,len);
            ++start;
            Create(&(*tree)->RTree,data,start,len);
        }
    }
    
    void Print(Tree *tree){
        if(tree==NULL)
            return ;
        cout<<tree->data<<"  ";
        Print(tree->LTree);
        Print(tree->RTree);
    }
    
    int main(){
        Elemtype data[]={1,2,4,0,0,5,0,0,3,6,0,0,7};
        int len=sizeof(data)/sizeof(data[0]);
        Tree *tree=NULL;
        int start=0;
        Create(&tree,data,start,len);
        Print(tree);
        return 0;
    } 
    

    图片说明

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥30 Matlab打开默认名称带有/的光谱数据
  • ¥50 easyExcel模板 动态单元格合并列
  • ¥15 res.rows如何取值使用
  • ¥15 在odoo17开发环境中,怎么实现库存管理系统,或独立模块设计与AGV小车对接?开发方面应如何设计和开发?请详细解释MES或WMS在与AGV小车对接时需完成的设计和开发
  • ¥15 CSP算法实现EEG特征提取,哪一步错了?
  • ¥15 游戏盾如何溯源服务器真实ip?需要30个字。后面的字是凑数的
  • ¥15 vue3前端取消收藏的不会引用collectId
  • ¥15 delphi7 HMAC_SHA256方式加密
  • ¥15 关于#qt#的问题:我想实现qcustomplot完成坐标轴
  • ¥15 下列c语言代码为何输出了多余的空格