Presentation failing in binary tree

2
  

I can not display my tree in either order or preorder as in the example below the cursor just goes out and expects another statement

#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);

}
    
asked by anonymous 23.08.2017 / 23:07

1 answer

2

There are a few minor issues with your code:

#include <conio.h>

Do not use this. This library is super outdated, obsolete and not a standard library. Also, it's not being used at all, 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 solve this is simply by changing the error message. A better way is to separate the message display logic from the query logic. This is possible if the consultaRecursiva function returns 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 , not just whether it's there or not.

In function main , your error 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 seems to be working with C, not with C ++ ( new is something of C ++), so let's use malloc instead:

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

This system("pause"); within your preOrdem function will make the system choke on large trees, making you have to press a key for each node, which is very annoying. Remove it.

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

#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 what is dynamically allocated in memory. I leave this to you.

    
23.08.2017 / 23:41