Next Sec.:  バックトラッキング
Upper Sec.:  動的データ構造
 Prev. Sec.:  線形リスト
 
 
 例:組織の系統図,数式の表現,探索木,LISPのS式,etc.
 
数式のリスト表現
a*(b-c)+d/e

 
2分木の各節点(node)xに対して
yをSl(x)の元、zをSr(x)の元と仮定した時
f(y) < f(x) < f(z)
 
ランダムな入力に対する2分探索木の生成
入力例 49 22 35 67 82 10 58 45 ...
メインプログラム
#include <stdio.h>
#include <stdlib.h>
struct node {
        int    data;
        struct node     *left;
        struct node     *right;
};
struct node *searchtree(struct node *p, int x);
void main(void)
{
        struct node *root = NULL;
        int x;
        scanf("%d", &x);
        while (x > 0) {
                root = searchtree(root, x);
                scanf("%d", &x);
        }
}
 
 木探索(登録)プロシージャ
struct node *searchtree(struct node *p, int x)
{
        if (p == NULL) {
                p = (struct node *)malloc(sizeof(struct node));
                p->data = x;
                p->left = NULL;
                p->right = NULL;
        } else if (x < p->data)
                p->left = searchtree(p->left, x);
        else if (x > p->data)
                p->right = searchtree(p->right, x);
        return(p);
}