Tree strictly binary

5
  

A binary search tree is strictly binary if all nodes in the tree have 2 children or none. Implement a function that checks whether a binary search tree is strictly binary.

// Estrutura de dados
typedef struct {
    int chave;
}tipo_elemento;

typedef struct nodo{
    struct nodo *dir, *esq;
    tipo_elemento e;
}tipo_nodo;

typedef tipo_nodo *apontador;

// Implementação
int estritamente_bin(apontador a){
    if(!a->dir && !a->esq)
        return 1;

    if(a->dir && a->esq)
        return estritamente_bin(a->esq) && estritamente_bin(a->dir);

    return 0;
}

Any suggestions and / or criticisms to improve the above implementation?

    
asked by anonymous 15.01.2018 / 16:22

1 answer

4

Your code is fine, I have only a few suggestions.

The first is about this:

typedef tipo_nodo *apontador;

That's why I think it does not contribute much to the readability of the code. I think it's important that pointers be clearly treated as such, especially when using the -> operator, and so I suggest using tipo_nodo * in the estritamente_bin function.

The function estritamente_bin can be reduced to this:

int estritamente_bin(tipo_nodo *a) {
    return (!a->dir && !a->esq)
            || (a->dir && a->esq && estritamente_bin(a->esq) && estritamente_bin(a->dir));
}
    
15.01.2018 / 16:33