Path in levels in the binary tree

Asked

Viewed 334 times

1

I’m trying to create a function that traverses a binary tree in levels through a chained queue, but when I compile it shows the error message "Segmentation failure (recorded core image)".

//Estrutura da árvore
typedef struct no{
    int chave; 
    struct no* esq;
    struct no* dir;
}NO;

typedef struct arvore{
    NO* pont;
}ARVORE;

//Estrutura da fila
typedef struct elemento{
    NO* reg;
    struct elemento* prox;
}ELEMENTO;

typedef struct fila{
    ELEMENTO* head;
}FILA;

void criaFila(FILA* fila){
    fila->head = NULL;
} 

void insereFila(FILA* fila, NO* p){
    ELEMENTO* novo = (ELEMENTO *) malloc(sizeof(ELEMENTO));
    novo->reg = p;
    novo->prox = NULL;
    if(fila->head==NULL){
        fila->head = novo;
    }else{
        ELEMENTO* pointer = fila->head;
        ELEMENTO* ant = NULL;
        while(pointer!=NULL){
            ant = pointer;
            pointer = pointer->prox;
        }
        ant->prox = novo;
        free(pointer);
    }
}

NO* removeFila(FILA* fila){
    if(fila->head!=NULL){
        ELEMENTO* pointer = fila->head;
        fila->head = pointer->prox;
        return pointer->reg;
    }
}

int tamanhoFila(FILA* fila){
    ELEMENTO* pointer = fila->head;
    int tam;
    while(pointer!=NULL){
        tam++;
        pointer = pointer->prox;
    }
    return tam;
}
void percurso(NO* raiz){
    FILA fila;   
    NO* prox;
    criaFila(&fila);
    insereFila(&fila, raiz);
    while(tamanhoFila(&fila)>0){
        prox = removeFila(&fila);
        printf("%d ", prox->chave);
        if(prox->esq!=NULL){
            insereFila(&fila, prox->esq);
        }
        if(prox->dir!=NULL){
            insereFila(&fila, prox->dir);
        }
    }
}
  • "segmentation failure" occurs in program execution, not in compilation!

1 answer

1


You are initializing the variable tam with no value, you have to assign a value to it before calling it in the while cycle.

//Estrutura da árvore
typedef struct no{
    int chave; 
    struct no* esq;
    struct no* dir;
}NO;

typedef struct arvore{
    NO* pont;
}ARVORE;

//Estrutura da fila
typedef struct elemento{
    NO* reg;
    struct elemento* prox;
}ELEMENTO;

typedef struct fila{
    ELEMENTO* head;
}FILA;

void criaFila(FILA* fila){
    fila->head = NULL;
} 

void insereFila(FILA* fila, NO* p){
    ELEMENTO* novo = (ELEMENTO *) malloc(sizeof(ELEMENTO));
    novo->reg = p;
    novo->prox = NULL;
    if(fila->head==NULL){
        fila->head = novo;
    }else{
        ELEMENTO* pointer = fila->head;
        ELEMENTO* ant = NULL;
        while(pointer!=NULL){
            ant = pointer;
            pointer = pointer->prox;
        }
        ant->prox = novo;
        free(pointer);
    }
}

NO* removeFila(FILA* fila){
    if(fila->head!=NULL){
        ELEMENTO* pointer = fila->head;
        fila->head = pointer->prox;
        return pointer->reg;
    }
}

int tamanhoFila(FILA* fila){
    ELEMENTO* pointer = fila->head;
    int tam;
    tam=0;
    while(pointer!=NULL){
        tam++;
        pointer = pointer->prox;
    }
    return tam;
}
void percurso(NO* raiz){
    FILA fila;   
    NO* prox;
    criaFila(&fila);
    insereFila(&fila, raiz);
    while(tamanhoFila(&fila)>0){
        prox = removeFila(&fila);
        printf("%d ", prox->chave);
        if(prox->esq!=NULL){
            insereFila(&fila, prox->esq);
        }
        if(prox->dir!=NULL){
            insereFila(&fila, prox->dir);
        }
    }
}
  • True, it was inattentive! assigns zero to variable and worked. Thanks for the help.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.