Presentation failing in binary tree

Asked

Viewed 54 times

2

I can’t present my tree either in order or preOrden as in the example below the cursor just vanishes and waits for another instruction

#include <stdio.h>
#include <conio.h>
#include <stdlib.h>

typedef struct tipoNo{
    int valor;
    struct tipoNo *esq; 
    struct tipoNo *dir;

}TNo;

TNo* inserir (TNo *raiz, int valorParametro){ 

    if (raiz == NULL){
        raiz = new TNo; // criação de um novo no 
        raiz->valor = valorParametro;
        raiz->dir = NULL;
        raiz->esq = NULL;
    } else{
        if (valorParametro < raiz->valor){
            raiz->esq = inserir(raiz->esq ,valorParametro); 
        } else{
            raiz->dir = inserir(raiz->dir ,valorParametro);
        }
    }
    return raiz;
}

int consultaRecursiva (TNo *raiz ,int itemConsulta){
    if (raiz == NULL){
        printf("Sua Arvore esta vazia !! \n");
        system("pause");
        return 0;
    } else {
        if (raiz->valor == itemConsulta){
            printf("Achou !!!!");
            system("pause");
            return 1 ;
        } else{
            if (raiz->valor > itemConsulta){
                return consultaRecursiva(raiz->esq ,itemConsulta);

            } else{
                return consultaRecursiva(raiz->dir ,itemConsulta);

            }
        }
    }
}

void preOrdem (TNo *raiz_aux){
    if (raiz_aux != NULL){
        printf("%d", raiz_aux->valor);
        preOrdem(raiz_aux->esq);
        preOrdem(raiz_aux->dir);
        system("pause");
    }
}



int main (){
    TNo *raiz; // ponteiro do tipo NO
    raiz = NULL; // a raiz DEVE estar nula
    int op;

    do {
        system("cls");
        printf ("INFORME UMA OPCAO ");
        printf ("\nPara inserir digite 1 ");
        printf ("\nPara consultar digite 2 ");
        printf ("\nPara preOrdem digite 3 \n");
        printf ("Para sair digite 0 : ");
        scanf ("%d",&op);

        if (op == 1){
            int novoValor;
            printf("Entre com um novo valor : ");
            scanf ("%d",&novoValor);
            inserir (raiz ,novoValor);
        }
        if (op == 2){
            int itemPesquisa;
            printf("Entre com uma elemento para a busca : ");
            scanf ("%d",&itemPesquisa);
            consultaRecursiva(raiz ,itemPesquisa);

        }
        if (op == 3){

            preOrdem(raiz);
        }

    }while(op != 0);

}

1 answer

2


There are a few little problems in your code:

#include <conio.h>

Don’t use that. This library is super old-fashioned, obsolete and not a standard library. Besides, it’s not being used for anything, it’s just in your silly code.

Its function consultaRecursiva confuses the empty tree with the tree that does not contain the searched element. The simplest way to resolve this is simply by changing the error message. A better way is to separate message display logic from query logic. This is possible if function consultaRecursiva return the node where the element was found instead of 0 or 1. After all, in the real world, anyone searching for an element in a binary tree will probably want to know where this element is, and not just whether it is there or not.

In function main, your mistake is here:

inserir (raiz ,novoValor);

What you wanted was this:

raiz = inserir(raiz, novoValor);

Another detail is this line:

raiz = new TNo; // criação de um novo no 

As you here seem to be working with C, and not with C++ (the new is something from C++), so let’s use malloc instead:

raiz = (Tno *) malloc(sizeof Tno);

That one system("pause"); within its function preOrdem will make the system get well choked on large trees, making you have to press a key for each node, which is very annoying. Remove this.

Also, you can give a simplified in some points of the code (especially when using else if). See below how it looks:

#include <stdio.h>
#include <stdlib.h>

typedef struct tipoNo {
    int valor;
    struct tipoNo *esq; 
    struct tipoNo *dir;
} TNo;

TNo* inserir(TNo *no, int valorParametro) { 
    if (no == NULL) {
        no = (Tno *) malloc(sizeof Tno);
        no->valor = valorParametro;
        no->dir = NULL;
        no->esq = NULL;
    } else if (valorParametro < no->valor) {
        no->esq = inserir(no->esq, valorParametro); 
    } else {
        no->dir = inserir(no->dir, valorParametro);
    }
    return no;
}

TNo *consultaRecursiva(TNo *no, int itemConsulta) {
    if (no == NULL) return NULL;
    if (no->valor == itemConsulta) return no;
    TNo *lado = no->valor > itemConsulta ? no->esq : no->dir;
    return consultaRecursiva(lado, itemConsulta);
}

void preOrdem(TNo *no) {
    if (no != NULL) {
        printf("%d ", no->valor);
        preOrdem(no->esq);
        preOrdem(no->dir);
    }
}

int main() {
    TNo *raiz = NULL;
    int op;

    do {
        system("cls");
        printf("INFORME UMA OPCAO ");
        printf("\nPara inserir digite 1");
        printf("\nPara consultar digite 2");
        printf("\nPara preOrdem digite 3\n");
        printf("Para sair digite 0: ");
        scanf("%d", &op);

        if (op == 1) {
            int novoValor;
            printf("Entre com um novo valor: ");
            scanf("%d", &novoValor);
            raiz = inserir(raiz, novoValor);
        } else if (op == 2) {
            int itemPesquisa;
            printf("Entre com uma elemento para a busca : ");
            scanf ("%d", &itemPesquisa);
            TNo *achou = consultaRecursiva(raiz, itemPesquisa);
            if (raiz == NULL) {
                printf("Sua arvore esta vazia.\n");
            } else if (achou == NULL) {
                printf("O elemento %d nao esta na arvore.\n", itemPesquisa);
            } else {
                printf("O elemento %d esta na arvore.\n", itemPesquisa);
            }
            system("pause");
        } else if (op == 3) {
            preOrdem(raiz);
            system("pause");
        }
    } while(op != 0);
}

It is also always important to have a function to clean up that which is dynamically allocated in memory. I leave this one to you.

  • Thanks for the support I managed to solve all the problems . It is a college exercise so it helped a lot .

Browser other questions tagged

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