当我尝试将一个数字添加到我的二叉树中时,会出现著名的Segmentation Fault。
我猜错误是函数中的指针inserir_no
。也许我应该使用指针辅助。
#include <stdio.h>
#include <stdlib.h>
/* create a node */
struct no {
int info;
struct no *esq;
struct no *dir;
};
/* function prototypes */
void inserir_no(struct no *arv, int x);
void inserir_no(struct no *arv, int x)
{
if(arv == NULL) {
printf("foi");
arv = (struct no *) calloc(1, sizeof(struct no));
arv->info = x;
arv->esq = NULL;
arv->dir = NULL;
}
else if(x < arv->info) {
inserir_no(arv->esq, x);
}
else {
inserir_no(arv->dir, x);
}
}
int main(void)
{
struct no *a;
int valor;
a = NULL;
/* fazer um menu depois */
scanf("%d", &valor);
inserir_no(a, valor);
printf("\nDADOS:\n%d", a->info);
return 0;
}