Traversal of binary tree
二分木のなぞり

ミニレポート第 7 回課題

教科書 p.96 List6.1 を実際に動作させるプログラムを作成せよ.

教科書List 6.1 にある節の定義, 関数 preorder, inorder, postorder は配布されたサンプルコードの list06_1.c にあるのでそれをコピーして (c ディレクトリから cp ../algo/list06_1.c report07.c)プログラムの作成を始めればよい. ただし,以下の変更を行う必要がある.

プログラムの最初に stdio.h のインクルードを追加

プログラムの最初の #include <stdlib.h> の行の上に以下の行を追加せよ.

#include <stdio.h>

図 r7.1 stdio.h のインクルード

main ルーチンの作成

preorder, inorder, postorder 関数を使って実際になぞる木を作成 する必要がある.教科書 p.93 Fig 6.5 (a) の木を作成する部分と 3 つの関数を使ってなぞる部分を main 内に書く. 以下の main ルーチンをプログラムの最後に挿入せよ.

int main(void)
{
    struct node *rootp, *ap, *bp, *cp, *dp;

    if ((ap = malloc(sizeof(struct node))) == NULL) /* 節 a 作成 */
        exit(1);
    if ((bp = malloc(sizeof(struct node))) == NULL) /* 節 b 作成 */
        exit(1);
    if ((cp = malloc(sizeof(struct node))) == NULL) /* 節 c 作成 */
        exit(1);
    if ((dp = malloc(sizeof(struct node))) == NULL) /* 節 d 作成 */
        exit(1);
    ap->label = 'a';                          /* 節 a の値を設定 */
    bp->label = 'b';                          /* 節 b の値を設定 */
    cp->label = 'c';                          /* 節 c の値を設定 */
    dp->label = 'd';                          /* 節 d の値を設定 */
    ap->left = bp;                      /* a の左の子に b を登録 */
    ap->right = dp;                     /* a の右の子に d を登録 */
    bp->left = cp;                      /* b の左の子に c を登録 */
    bp->right = NULL;                   /* b の右は NULL         */
    cp->left = NULL;                    /* c の左は NULL         */
    cp->right = NULL;                   /* c の右は NULL         */
    dp->left = NULL;                    /* d の左は NULL         */
    dp->right = NULL;                   /* d の右は NULL         */
    rootp = ap;        /* 根を指すためのポインタ rootp に a を登録 */

    printf("preorder\n");
    preorder(rootp);
    printf("inorder\n");
    inorder(rootp);
    printf("postorder\n");
    postorder(rootp);
}

図 r7.2 main ルーチン

プログラム全体についての注意

注意事項

受理した学生のリストを 1 クラス提出者 2 クラス提出者 3 クラス提出者 に掲載する. 締切までに提出したにもかかわらず締切後一週間を過ぎても掲載されていない 場合は山本 <hiroshi@tokai.ac.jp>まで連絡せよ. 未提出の学生はすみやかに再提出せよ. 再提出後一週間経っても掲載されない場合も 山本まで連絡せよ.


Updated in November 4, 2019, index.html, Yamamoto Hiroshi Web