鱼C论坛

 找回密码
 立即注册
查看: 1724|回复: 3

[已解决]c语言二叉树建立和遍历出错

[复制链接]
发表于 2021-5-14 19:27:18 | 显示全部楼层 |阅读模式

马上注册,结交更多好友,享用更多功能^_^

您需要 登录 才可以下载或查看,没有账号?立即注册

x
建立好像没问题,遍历有错看不出来,求助
#include <stdio.h>
#include <stdlib.h>
typedef struct tree{
    int data;
    struct tree *left;
    struct tree *right;
}BiTree;
void pushTree(BiTree *);
void ptree2(BiTree *);
int main(void)
{
    BiTree *p;
    pushTree(p);
    printf("%d",p->data);
    return 0;
}
void pushTree(BiTree *root)
{
    int i;
    printf("shuru\n");
    scanf("%d",&i);
    if(i==9999)
        root=NULL;
    else{
        root=(struct tree*)malloc(sizeof(struct tree));
        root->data=i;
        printf("%d左指数\n",root->data);
        pushTree(root->left);
        printf("%d右指数\n",root->data);
        pushTree(root->right);
    }
}
void ptree2(BiTree *root)
{
    if(root)
    {
        printf("%d  ",root->data);
        ptree2(root->left);
        ptree2(root->right);
    }
}


最佳答案
2021-5-15 16:26:11
本帖最后由 Stubborn 于 2021-5-15 18:54 编辑

我学C没有学多久,不过看你的这个函数
void pushTree(BiTree *root)
root=(struct tree*)malloc(sizeof(struct tree));

你申请下来的root确定不会覆盖传进来的root指针吗?
你确定你申请下来的root节点,"挂“在之前节点的左或者右侧了吗?
#include <stdio.h>
#include <stdlib.h>
#define TElemType int

typedef struct BitNode{
    TElemType data;
    struct BitNode *left, *right;
}BitNode, *BitTree;

void CreateBiTree(BitTree *);
void DisplayBiTree(BitTree *);

int main(void)
{

    BitTree tree;
    CreateBiTree(&tree);
    DisplayBiTree(&tree);
    return 0;
}

void CreateBiTree(BitTree *root)
{
    int value = 999;
    printf("input data value>>[   ] \b\b");
    scanf("%d", &value);

    if (value == 999)
    {
        *root = NULL;
    } else
    {
        *root = (BitTree) malloc(sizeof(BitNode));
        (*root)->data = value;
        CreateBiTree(&(*root)->left);
        CreateBiTree(&(*root)->right);
    }

}

void DisplayBiTree(BitTree *root)
{
    if (root)
    {
        printf(" value is > %d", (*root)->data);
        DisplayBiTree(&(*root)->left);
        DisplayBiTree(&(*root)->right);
    }
}
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复

使用道具 举报

 楼主| 发表于 2021-5-14 19:29:31 | 显示全部楼层
有人可以告诉我怎么发悬赏贴吗
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2021-5-15 16:26:11 | 显示全部楼层    本楼为最佳答案   
本帖最后由 Stubborn 于 2021-5-15 18:54 编辑

我学C没有学多久,不过看你的这个函数
void pushTree(BiTree *root)
root=(struct tree*)malloc(sizeof(struct tree));

你申请下来的root确定不会覆盖传进来的root指针吗?
你确定你申请下来的root节点,"挂“在之前节点的左或者右侧了吗?
#include <stdio.h>
#include <stdlib.h>
#define TElemType int

typedef struct BitNode{
    TElemType data;
    struct BitNode *left, *right;
}BitNode, *BitTree;

void CreateBiTree(BitTree *);
void DisplayBiTree(BitTree *);

int main(void)
{

    BitTree tree;
    CreateBiTree(&tree);
    DisplayBiTree(&tree);
    return 0;
}

void CreateBiTree(BitTree *root)
{
    int value = 999;
    printf("input data value>>[   ] \b\b");
    scanf("%d", &value);

    if (value == 999)
    {
        *root = NULL;
    } else
    {
        *root = (BitTree) malloc(sizeof(BitNode));
        (*root)->data = value;
        CreateBiTree(&(*root)->left);
        CreateBiTree(&(*root)->right);
    }

}

void DisplayBiTree(BitTree *root)
{
    if (root)
    {
        printf(" value is > %d", (*root)->data);
        DisplayBiTree(&(*root)->left);
        DisplayBiTree(&(*root)->right);
    }
}
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

 楼主| 发表于 2021-5-19 21:20:32 | 显示全部楼层
Stubborn 发表于 2021-5-15 16:26
我学C没有学多久,不过看你的这个函数
void pushTree(BiTree *root)
root=(struct tree*)malloc(sizeof(s ...

这个我自己改对了,创建二叉树函数传进去的应该是指向指针的指针,这样才能修改数字也能修改地址
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

小黑屋|手机版|Archiver|鱼C工作室 ( 粤ICP备18085999号-1 | 粤公网安备 44051102000585号)

GMT+8, 2024-9-21 11:12

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

快速回复 返回顶部 返回列表