0

Tengo este código, cuando lo ejecuto me da segmetation fail(algo así). No me marca ningun error de compilacion, pero no se me ejecuta correctamente. Se que no es la forma normal de las preguntar pero no encuentro ningún error. Tengo que hacer todos los análisis dentro de la misma función. Lo que intento hacer es lo siguiente: Buscar el menor elemento del árbol con un hijo. Cantidad de nodos hojas pares divisores de la raíz. Cantidad de nodos que tengan solo un hijo derecho.

#include <stdio.h>
#include <stdlib.h>
typedef struct arbol{
    int dato;
    struct arbol *izq;
    struct arbol *der;
    }treenode;
    
void crear (treenode **hoja){
    int numero;
    printf ("ingrese numero\n");
    scanf ("%d",&numero);
    while (numero!=-1){
        insertar(&(*hoja),numero);
        printf ("ingrese numero\n");
        scanf ("%d",&numero);
    }
}
void insertar (treenode **hoja,int elem){
    int numero=elem;
    if (elem==-1)
    return;
    if (*hoja==NULL){//creo la hoja vacia
    (hoja)=(treenode)malloc(sizeof(treenode));
    (*hoja)->dato=elem;
    (*hoja)->der=NULL;
    (*hoja)->izq=NULL;
    }else{
        if (numero>(*hoja)->dato){
            insertar (&(*hoja)->der,elem);
        }else{
            insertar (&(*hoja)->izq,elem);
        }
    }
return;
}
void mostrar (treenode *hoja){
if (hoja!=NULL){
    mostrar(hoja->izq); 
    printf("%d ",hoja->dato);
    mostrar(hoja->der);
    }
    return;
}
void analizar(treenode *hoja,int *min,int numero,int *cant,int *cant2){
    if(hoja!=NULL){
        if(hoja->izq==NULL && hoja->der!=NULL || hoja->izq!=NULL && hoja->der==NULL){ 
            *min=hoja->dato; 
        }
        if(hoja->dato%2==0){
            if(numero%hoja->dato==0){
                *cant++;
            }
        }
        if(hoja->izq==NULL && hoja->der !=NULL){
            *cant2++;
        }
    }
    analizar(hoja->der,min,numero,cant,cant2);
    analizar(hoja->izq,min,numero,cant,cant2);
}

int main (){
    printf ("arboles\n");
    treenode* arbol=NULL;
    crear (&arbol);
    printf ("**\n");
    printf ("mostrar arbol\n");
    printf ("***\n");
    mostrar (arbol);
    int raiz=(arbol)->dato;
    int min, cant=0 , cant2=0; 
    analizar(arbol, &min , raiz, &cant, &cant2);
    return 0;
}

1 respuesta 1

1

(hoja)=malloc() seguido de (*hoja)-> es altamente sospechoso. Intenta con

    (*hoja)=(treenode*)malloc(sizeof(treenode));

Tu Respuesta

By clicking “Publica tu respuesta”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

¿No es la respuesta que buscas? Examina otras preguntas con la etiqueta o formula tu propia pregunta.