Binaria tree with 3 pointers has special treatment?

Asked

Viewed 91 times

1

I am setting up a hospital program and the order of the patients will be by a binary tree, taking into account a degree of urgency existing in the patient struct, as shown below. My question is: Do you have any special treatment for 3 pointers? Because when I run the program it seems that it is not saved in the tree structure when I try to show the tree in order it is always empty.

Structs of patient and tree:

typedef struct Paciente{
    char nome[50];
    char cpf[50];
    char sexo[20];
    char sintomas[500];
    int telefone;
    int idade;
    int codi;
    int urg;
    struct Paciente *prox;
}paciente;

typedef struct Arvore{
    struct Paciente *No;
    struct Arvore *Esq;
    struct Arvore *Dir;
}arvore;

Add and Show tree functions:

void adicionar(arvore **Arv, paciente *pac){
    arvore *temp = NULL;
    if (*Arv == NULL){
        temp = (arvore*)malloc(sizeof(arvore));
        temp->Esq = NULL;
        temp->Dir = NULL;
        temp->No = pac;
        *Arv = temp;
    }
    else {
        if (pac->urg < ((*Arv)->No->urg)){
            adicionar(&((*Arv)->Esq), pac);
        }
        else{
            adicionar(&((*Arv)->Dir), pac);
        }
    }
}

// Em Ordem (Do menor pro maior)
void Mostrar(arvore *Arv){
    if(Arv != NULL){
        Mostrar(Arv->Esq);
        printf("Nome: %s\nCPF: %s\nSexo: %s\nIdade: %d\nTelefone: %d\nSintomas: %s\nUrgencia: %d\nCódigo: %d\n", Arv->No->nome, Arv->No->cpf, Arv->No->sexo, Arv->No->idade, Arv->No->telefone, Arv->No->sintomas, Arv->No->urg, Arv->No->codi);
        Mostrar(Arv->Dir);
    }
}

On Main I create it like this:

arvore *Arv = NULL;
  • Why does a patient have a patient inside him? This doesn’t conflict with the need to use a tree?

  • This is for a chained list of patients, but it’s for a completely different part of the code. Logically I didn’t find any error but it still doesn’t show the tree, so I thought maybe 3 pointers had a different treatment.

  • No, the number of pointers does not interfere in anything with abstraction. As is the call of adicionar? The only problem I found would involve forgetting to pass the root address and pass the root itself. Although I think you made a weird recursion on the function adicionar (I would iterate, but then I would be programming), I still haven’t found anything inside it.

  • add (&Arv, aux); Onde aux is a patient struct with the information already filled out. I used this recursion because it’s the only way I know how to do it, I looked at the wiki of a tree and adapted it. I do not understand why not save the tree, I find no mistake for such.

  • 1

    That one aux is a patient? Or a patient pointer whose content was dynamically allocated?

  • 1

    I have to agree with @Jeffersonquesado, that your recursion is a little weird at least, and probably more complicated than usual with things like &((*Arv)->Esq). Still I don’t see exactly what the problem is. In this little test that I did seemed to be working properly.

  • This aux is a patient, I put the function of adding directly to the function that registers the patient, so it does not even pass the chained list, it is direct. Since you commented that you know how to do the same recursion in a simpler way, could you demonstrate? This was the best I could do alone. I will try to look for the bug in other places then. Thanks for the help!

  • This is the complete file of the program, see if maybe you can find the error that doesn’t let it save in the tree: link

Show 3 more comments
No answers

Browser other questions tagged

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