アルゴリズム (2)
本問を選択 (Select this problem) { する (Yes),しない (No) } No.
次のstructで定義された2分木について下記の問に答えよ.Answer the questions below on binary trees represented by the followingstruct:
struct node { int label;
struct node *left;
struct node *right;
};
(1) 2分木を受け取り,すべてのノードのラベルを出力する関数を3つ 書け.それぞれの関数は,異なる順 番でラベルを出力するものとする.2分木にはループがないと仮定してもよい。Write three different functions that take a binary tree and that prints out the labels of all the nodes. Each function must print out the labels in a different order. You may assume that there exists no loop in the binary tree.
(2) 下記の関数 generate() で生成された2分木に上の3つの関数を適用した際の出力を書け.Write the output obtained by applying the above functions to the binary tree generated by the following function generate().
struct node* gen(int m, int n) { int c;
struct node *x;
if (m > n) return NULL;
x = (struct node*)malloc(sizeof(struct node));
c = (m + n) / 2;
x->left = gen(m, c - 1);
x->label = c;
x->right = gen(c + 1, n);
return x;
}
struct node* generate(void) { return gen(1, 10);
}