Binary tree returning empty in search

Asked

Viewed 165 times

3

In my menu it is not doing the correct output in the print. In addition to the search return only empty tree.

#include<iostream>
#include<string>
#include<stdlib.h>
#include<stdio.h>
using namespace std;

typedef int TipoChave;

typedef struct TipoRegistro{
    TipoChave Chave;
    string Nome;
    float Preco;
    int QuantEstoque;
}TipoRegistro;

typedef struct TipoNo* Apontador;

typedef struct TipoNo{
    TipoRegistro Reg;
    Apontador Esq, Dir;
}TipoNo;


void ImprimeEmOrdem(TipoNo *pRaiz)
{
    if(pRaiz != NULL)
    {
        ImprimeEmOrdem(pRaiz->Esq);
        cout<<"IMPRIME  "<<pRaiz->Reg.Chave;
        ImprimeEmOrdem(pRaiz->Dir);
    }
}

void Pesquisa(TipoRegistro x, Apontador *p)
{
    if((*p) == NULL)
    {
        cout<<"ERRO 1: Regristo nao esta presente\n"<<endl;
    }

    if(x.Chave < (*p)->Reg.Chave)
    {
        Pesquisa(x, &((*p)->Esq));
    }
    if(x.Chave > (*p)->Reg.Chave)
    {
        Pesquisa(x, &((*p)->Dir));
    }
    else
    {
        x = (*p)->Reg;
    }
}

void Insere(TipoRegistro x, Apontador *p)
{
    cout << "teste";
    if(*p == NULL)
    {
        cout << "primeiro if";
        *p = (Apontador)malloc(sizeof(TipoNo));
        (*p)->Reg = x;
        (*p)->Esq = NULL;
        (*p)->Dir = NULL;
        cout << "Registro inserido com sucesso";
    }

    if(x.Chave < (*p)->Reg.Chave)
    {
        cout << "segundo if";
        Insere(x,&(*p)->Esq);
        return;
    }

    if(x.Chave > (*p)->Reg.Chave)
    {
        cout << "terceiro if";
        Insere(x, &(*p)->Dir);
        return;
    }

    //else cout<<"ERRO 2 : Regristo ja existe na arvore\n";
}

void Inicializa(Apontador p) //Apontador 'e um ponteiro
{
    p = NULL;
}

int main()
{
    int op, c = 1;
    Apontador No, Topo;
    TipoRegistro x;

    Topo = NULL;

    //No = Topo;

    do      //while(c != 0)
    {
        cout<<"Escolha Uma das opcoes abaixo"<<endl;
        cout<<"\t1. Inserir Produtos:  \n\t2. Buscar Produtos \n\t3. SAIR"<<endl;
        cin>>op;


        switch (op)
        {

            case 1 :
                cout<<"DIGITE O CODIGO : ";
                cin>>x.Chave;
                cout<<"DIGITE O NOME DO PRODUTO : ";
                cin>>x.Nome;
                cout<<"DIGITE O PRECO DO PRODUTO : ";
                cin>>x.Preco;
                cout<<"DIGITE A QUANTIDADE DISPONIVEL NO ESTOQUE : ";
                cin>>x.QuantEstoque;

                cout << x.Chave << endl;
                cout << (Topo == NULL) << endl;

                Insere(x, &Topo);

                cout << Topo->Reg.Chave;


                Pesquisa(x, &Topo);

                break;

            case 2 :
                cout<<"DIGITE O CODIGO OU NOME A SER BUSCADO : ";
                cin>>x.Chave;
                Pesquisa(x, &Topo);
                break;

            case 3 :
                cout<<"VOCE ESTA SAINDO\n";
                c = 0 ;
                break;

            default :
                cout<<"OPCAO INVALIDA";
                break;
        }

    }while(op !=3 );

    ImprimeEmOrdem(Topo);
}

1 answer

4


Initialization

First, the function of Inicializa(No) assigns the value null for the variable passed by parameter. However, this is an input-only argument, and therefore the value is not reflected outside the function scope.

Thus, the variable of No before the function has memory junk, and after the function continues possessing memory junk. To fix this, one must transform the parameter p of the function in an input and output parameter. This can be done in two ways (the first can be used in C or C++, but the second recommended to be used only in C++):

C/C++:

void Inicializa(Apontador *p) {
    *p = NULL;
}

C++:

void Inicializa(Apontador &p) {
    p = NULL;
}

So the call Inicializa(No), will correctly initialize the variable value No.

The second boot problem in your code is on the line if ( c = 1 ) Topo = No;. The meaning of this line for the compiler is:

  1. Assign 1 to the variable c;
  2. Check whether c is true;
  3. Assign No to Topo.

By step 1, the step 2 will be evaluated as true always, and therefore in every iteration of your looping, will be made attribution of No to Topo. Therefore:

if ( c = 1 ) Topo = No;

Do the same as simply:

Topo = No;

The variable c is not used anywhere else in your code. I believe this is not your goal. If it is, as you can notice, it can be removed without semantic and logical problems.

Insertion

I consider that at least the first boot error has been corrected.

After reading the value to be inserted in your tree, you call the command Insere passing the read value x.Chave and the variable Topo. She suffers from the same problem as function Inicializa:

The variable Topo is passed in the parameter p just as input argument, and so all changes made to it within the function Insere are not reflected outside its scope.

The correction can be done in the same way as the function Inicializa, and will remain as exercise the correction (she is a more boring poquinho, since the variable p is used in recursive calls; but the logic is the same, and it’s a great exercise on pointers).

Apparently, these are the corrections that answer your question.

Observing

In the explanation of the parameter transformation p in function Inicializa in an input and output parameter, there is a little discussion about which of the two methods should be used, since both through a user and through a reference, the parameter can be passed as input and output.

In any case, I will not look into this because this is a question of good practice, and one that is largely open to opinion. So I put the two methods and I didn’t suggest any of them.

  • Vinicius, after you told me about these errors I made the changes, but the inserts keep leaving the terminal, in that decide to compile in the terminal using G++, and the file generated from the segmentation failure saying that can not record in memory. I don’t know why this is happening. If you can help me...

  • Sure, I can! Update your code here in the post, so I know how it turned out.

  • On which line is the error occurring?

  • On my computer it is inserting correctly however when it has to return in the menu, it of the Gmentation fault. Then give up this code and made another much more complete and efficient. Thanks Vinicius. I will update this.

Browser other questions tagged

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