<問題> 木の分岐数を b, 最大の深さをmとするとき、深さdにあるノードを知りたい。特に、木の左端と右端のノード名を知りたい。
<実行例>
algo2>node 2 6 3 branching factor= 2, max depth= 6 depth= 3 d:0 0 d:1 1 2 d:2 3 4 5 6 d:3 7 8 9 10 11 12 13 14
algo2>node1 3 4 3 branching factor= 3, max depth= 4 depth= 3 d:0 0 - 0 d:1 1 - 3 d:2 4 - 12 d:3 13 - 39
解答例 #include <stdio.h> #include <stdlib.h> #include <math.h> int main (int argc, char *argv[]){ int b, m, d; /* Branching factor, Max depth */ int node, i, j; if (argc != 4) { printf("Usage: command [branching factor] [max depth] [depth]\n"); exit (0) ; } b = atoi(argv[1]); m = atoi(argv[2]); d = atoi(argv[3]); printf("branching factor=%2d, max depth=%2d depth=%2d\n", b, m, d); node = 0; for (i = 0; i <= d; i++) { printf("d:%d ",i); printf("%5d - ",node); node = node + pow(b, i) - 1; printf("%5d\n",node); node++; } return 0; }