Binary tree of search

Asked

Viewed 176 times

0

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


struct arvore
{
    int key;
    struct arvore * right, *left;


};

typedef struct arvore Arvore;   

Arvore * alocar(int key)
{
    Arvore * p = (Arvore *)malloc(sizeof(Arvore));
    p->key = key;
    p->right = NULL;
    p->left = NULL;
    return p;

}

int procurafolha(Arvore * p)
{
    int a = 0;
    if(p->right == NULL && p->left == NULL)
    {
        return p->key;
    }
    else
    { 
        if(p->left != NULL)
        {   
            a = a + procurafolha(p->left);
        }
        if(p->right != NULL)
        {
            a = a + procurafolha(p->right);
        }
    }   

    return a;

}

int procurasemfolha(Arvore * p)
{
    int a = 0;

    if(p->left != NULL)
    {
        a = a + procurasemfolha(p->left);
    }
    if(p->right != NULL)
    {
        a = a + procurasemfolha(p->right);
    }
    if(p->right != NULL || p->left != NULL)
    {
        a = a + p->key;

    }

    return a;

}




Arvore * insere(Arvore * p, int key)
{

    if(p == NULL)
    {
        p = alocar(key);
        return p;

    }
    else if(key < p->key)
    {
        p->left = insere(p->left, key);
    }   
    else if(key > p->key)
    {
        p->right = insere(p->right, key);
    }



}

void libera(Arvore * p)
{
    if(p != NULL)
    {
        libera(p->left);
        libera(p->right);
        free(p);
        p = NULL;
    }   


}


int main()
{

    Arvore *p = NULL;
    char a;
    int n, i = 0;
    int qtd, qtd01;

    while(1)
    {
        scanf("%d", &n);
        p = insere(p, n);
        scanf("%c", &a);
        if(a == '\n')
        {
            break;
        }

    }

    qtd = procurafolha(p);
    qtd01 = procurasemfolha(p);

    printf("%d %d\n", qtd, qtd01);

    libera(p);





return 0;   
}

Explanation of the problem: You should print two integer values separated by space, the first with the sum of the elements that are sheets, the second with the sum of the internal elements (they are not sheets).

Test 1:

entree

1 2 3 4 5 6 7 8 9 10

exit

10 45

Test 2:

entree

5 3 7 2 4 6 8

exit

20 15

Test 3:

entree

10 2 3 0 5 1 15 20 4 6 7 8 11 9 12 16 19 17 18 21

exit

65 139

1 answer

0

Its function insere has the missing return, which does not allow you to correctly build the tree. The value to return has to be p:

Arvore * insere(Arvore * p, int key){
    ...

    return p; //falta este retorno
}

And this will already make your job answer the problem.

See how they get the right result in Ideone

However you can considerably simplify the logic of the functions to find the sums. Follow my suggestion for both:

int soma_folhas(Arvore *p){
    if (p == NULL) return 0;

    int valorNo = (p->left == NULL && p->right == NULL) ? p->key : 0;
    return valorNo + soma_folhas(p->left) + soma_folhas(p->right);
}

int soma_nao_folhas(Arvore *p){
    if (p == NULL) return 0;

    int valorNo = (p->left == NULL && p->right == NULL) ? 0 : p->key;
    return valorNo + soma_nao_folhas(p->left) + soma_nao_folhas(p->right);
}

Example of this version also in Ideone

I stress that only the valorNo changed between both functions, because a sheet is the inverse of no sheet. Therefore only the value of true and false of the ternary operator was exchanged.

Notice that I also changed the name of the functions so that they were more intuitive and perceptible relative to what they actually do.

Browser other questions tagged

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