二分木から出力されるノードの数を制限するのに問題があります。私は現在のコードを持っています:
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);
}
最初のアイデアはstatic int x = 0;
、and のインクリメントの中に何かを入れることでしたcentralEsquerda()
が、2 番目の再帰的な call( centralEsquerda(a->dir, lim)
) が原因で、正しく動作しません。テストされたコードの下:
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);
}
}
BTree は、すべての BTree と同じように、左が下、右が大きいという順序になっています。昇順で印刷するには関数 を使用しcentralEsquerda()
、降順で印刷するにはcentralDireita()
、再帰呼び出しを逆にするだけで、正しいノードを最初に呼び出します( a->dir
)。
したがって、上記のコードでは、1、2、3、4、5、6、7、8、9、10、11、12、13、14、15、16、17、18、19、20、および使用したいのですが、1、2、3、4、5 と表示されますcentralEsquerda(node, 5)
。
何か案は?ps。queue/list を使いたくない
[アップデート]
以下のコードで解決しましたが、満足できませんでした...
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);
}
}