2

I'm having some trouble to limit the number of nodes printed from a binary tree. I have the current code:

abp.h

struct TNodoA {
    int info;
    struct TNodoA *esq;
    struct TNodoA *dir;
};

typedef struct TNodoA pNodoA;

void centralEsquerda(pNodoA *a, int lim);
pNodoA* InsereArvore(pNodoA *a, int ch);

abp.c

void centralEsquerda(pNodoA *a, int lim) {
    if (a != NULL) {
        centralEsquerda(a->esq, lim);
        printf("%d, ", a->info);
        centralEsquerda(a->dir, lim);
    }
}

pNodoA* InsereArvore(pNodoA *a, int ch) {
    if (a == NULL) {
        a = (pNodoA*) malloc(sizeof (pNodoA));
        a->info = ch;
        a->esq = NULL;
        a->dir = NULL;
        return a;
    } else
        if (ch < a->info)
        a->esq = InsereArvore(a->esq, ch);
    else if (ch > a->info)
        a->dir = InsereArvore(a->dir, ch);
    return a;
}

main.c

int teste() {
    int valores[20] = {10, 5, 15, 3, 8, 13, 18, 2, 4, 7, 9, 12, 14, 17, 19, 1, 6, 11, 16, 20};
    pNodoA *arv = NULL;

    for (int i = 0; i < 20; i++) {
        arv = InsereArvore(arv, valores[i]);
    }

    centralEsquerda(arv, 4);
}

The first idea was to put some static int x = 0; inside the centralEsquerda() and increment, but due to the second recursive call(centralEsquerda(a->dir, lim)) it doesn't work properly. Below the code tested:

void centralEsquerda(pNodoA *a, int lim) {
    static int x = 0;
    if (a != NULL && x < lim) {
        x++;
        centralEsquerda(a->esq, lim);
        printf("%d, ", a->info);
        centralEsquerda(a->dir, lim);
    }
}

The BTree is already ordered like every BTrees, lower at left, greater at right. To print in asc order I used the function centralEsquerda(), and to print in desc order I used centralDireita() that just invert the recursive call, it calls the right node first(a->dir).

So, with the codes above, will be printed 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20 and I wish to use centralEsquerda(node, 5) and it should print 1, 2, 3, 4, 5.

Any ideas? ps. can't want to use queue/list

[UPDATE]

Solved with the code below, but didn't pleased me...

void centralEsquerda(pNodoA *a, int lim) {
    static int count = 0;
    if (a != NULL) {
        centralEsquerda(a->esq, lim);
        if (count >= lim)
            return;
        else
            count++;
        printf("%d, ", a->info);
        centralEsquerda(a->dir, lim);
    }
}
Vadim Kotov
  • 8,084
  • 8
  • 48
  • 62
henrique
  • 1,072
  • 10
  • 17

1 Answers1

1

I hate that my first answer is untested code. Please consider it as pseudocode. heh heh. I think your solution (though functional) didn't please you because it was not as elegant as your original recursive tree-traversing code. We lovers of recursion tend to obsess over such things. Here is a snippet (untested) that I find more recursively pleasing:

int centralEsquerda(pNodoA *a, int lim) {
    if (a != NULL && lim > 0) {
        lim = centralEsquerda(a->esq, lim);
        if (lim-- > 0) printf("%d, ", a->info);
        lim = centralEsquerda(a->dir, lim);
    }
    return lim;
}
Jim_H
  • 26
  • 4