アルゴリズム(2)
本問を選択(Select this problem){ する(Yes),しない(No) } 受験番号(No.)
次のように定義したラベルを持った二分木について,下の問題に答えよ。ただし,leftとrightには子へのポ インタが入る。また,子がない場合には,NULL(= 0)が入れられているものとする。なお,解答には裏面も使用し て良い。(Answer the questions about the data structure of binary trees with labels defined below. A pointer to a child is assigned to the field left or right, and NULL (= 0) is assigned for indicating that no child exists. The reverse side can be used for the answers if needed.)
typedef struct btree btree;
struct btree { int label;
btree *left;
btree *right;
};
問1:二分木のlabelの値の合計を返す関数を再帰呼び出しを使って定義せよ。(Q1: Define a function taking a binary tree and returning the total sum of the values of label. Use recursion in the definition.)
問2: 問1の関数を再帰呼び出しを使わず定義せよ。ただし,次のスタックを使え。(Q2: Define the same function as Q1 without recursion and with using the stack defined as follows:)
int stackp;
btree* stack[1000];
void initialize_stack(void) { stackp = 0; } void push(btree *x) { stack[stackp++] = x; } btree *pop(void) { return stack[--stackp]; } int empty_stack(void) { return stackp == 0; }
問3:二分木のlabelの値を,深さ1(根)のノード,深さ2のノード,深さ3ノード,. . . の順で出力する関数 を定義せよ。(Q3: Define the function taking a binary tree and printing out the values of labelin order of the node in depth 1 (the root node), nodes in depth 2, nodes in depth 3, . . ..)