I'm working on a program with trees, it's the first one I do and when I compile it I throw segmantation fault and I do not know what is wrong.
struct Nodo {
int Dato;
struct nodo *izq, *der;
};
typedef struct Nodo nodoArbol;
typedef nodoArbol *pArbol;
pArbol arbol;
void agregar(pArbol *raiz, int dato){
printf("%d", dato);
if((*raiz)==NULL){
(*raiz) = malloc(sizeof(struct Nodo));
(*raiz)->Dato=dato;
printf("%d", (*raiz)->Dato);
(*raiz)->izq=NULL;
(*raiz)->der=NULL; }
else if((*raiz)->Dato<=dato){
agregar(&((*raiz)->der), dato); }
else if((*raiz)->Dato>dato){
agregar(&((*raiz)->izq), dato);}
}
/*
void modificar(pArbol raiz, int dato1, int dato2){
if(raiz->Dato==dato1){
raiz->Dato=dato2;
return;}
else if(raiz->Dato<dato1){
modificar(raiz->der, dato1, dato2);
return;}
else if(raiz->Dato>dato1){
modificar(raiz->izq, dato1, dato2);}
}*/
void eliminar(pArbol *raiz, int datoe){
if((*raiz)->Dato==datoe){
free(*raiz);
}
else if((*raiz)->Dato<datoe){
eliminar((*raiz)->der,datoe);
}
else if((*raiz)->Dato>datoe){
eliminar((*raiz)->izq,datoe);
}
}
void preorden(pArbol nodo){
if (nodo!=NULL){
printf("\n%d", nodo->Dato);
preorden(nodo->izq);
preorden(nodo->der);
}
}
int main()
{
char o=' ';
int d;
struct nodo *arbol;
while(o!='t'){
printf("agregar a, mostrar m, terminar t\n");
scanf("%c", &o);
printf("%c", o);
switch(o){
case 'a':
printf("Ingrese dato a agregar\n");
scanf("%i", &d);
printf("%d", d);
agregar(arbol, d);
break;
case 'm':
preorden(arbol);
break;
}
}
return 0;
}