简体   繁体   中英

Print binary tree asc/desc limiting number of nodes in C

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

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);
    }
}

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;
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM