アルゴリズムとデータ構造 補足資料 13-2 「 2 分探索木への節点の追加」...

68
アアアアアアアアアアアア アアアア 13-2 2 アアアアアアアアアアアアアアアアア アアアア アア アアアアアアア アアアア

Post on 19-Dec-2015

214 views

Category:

Documents


0 download

TRANSCRIPT

アルゴリズムとデータ構造補足資料 13-2

「 2 分探索木への節点の追加」

横浜国立大学理工学部

数物・電子情報系学科富井尚志

探索木のオペレータ• 探索木を探索する

• 探索木に節点を追加(挿入)する

• 探索木から節点を削除する

探索木 (search tree)

main()

root = NULL;while( ( y=get_data() )!= EOD ) root = search( y, root )

y root NULL

int a[] = { 7, 2, 9, 1, 6, 9, 8, …}

探索木 (search tree)

main()

root = NULL;while( ( y=get_data() )!= EOD ) root = search( y, root )

y 7 root NULL

int a[] = { 7, 2, 9, 1, 6, 9, 8, …}

探索木 (search tree)

main()

root = NULL;while( ( y=get_data() )!= EOD ) root = search( y, root )

y 7 root NULL

int a[] = { 7, 2, 9, 1, 6, 9, 8, …}

探索木 (search tree)

main()

root = NULL;while( ( y=get_data() )!= EOD ) root = search( y, root )

y 7 root NULL

int a[] = { 7, 2, 9, 1, 6, 9, 8, …}

search(7, NULL) : 呼出 1

if ( t == NULL ) { t = (struct tree *)malloc(sizeof(struct tree)); t->key = x; t->count = 1; t->left = NULL; t->right = NULL;}else if ( x < t->key ) t->left = search( x, t->left );else if ( x > t->key ) t->right = search( x, t->right );else (t->count)++;

return (t);

x 7 t NULL

探索木 (search tree)

main()

root = NULL;while( ( y=get_data() )!= EOD ) root = search( y, root )

y 7 root NULL

int a[] = { 7, 2, 9, 1, 6, 9, 8, …}

search(7, NULL) : 呼出 1

if ( t == NULL ) { t = (struct tree *)malloc(sizeof(struct tree)); t->key = x; t->count = 1; t->left = NULL; t->right = NULL;}else if ( x < t->key ) t->left = search( x, t->left );else if ( x > t->key ) t->right = search( x, t->right );else (t->count)++;

return (t);

x 7 t

探索木 (search tree)

main()

root = NULL;while( ( y=get_data() )!= EOD ) root = search( y, root )

y 7 root NULL

int a[] = { 7, 2, 9, 1, 6, 9, 8, …}

search(7, NULL) : 呼出 1

if ( t == NULL ) { t = (struct tree *)malloc(sizeof(struct tree)); t->key = x; t->count = 1; t->left = NULL; t->right = NULL;}else if ( x < t->key ) t->left = search( x, t->left );else if ( x > t->key ) t->right = search( x, t->right );else (t->count)++;

return (t);

x 7 t

71

探索木 (search tree)

main()

root = NULL;while( ( y=get_data() )!= EOD ) root = search( y, root )

y 7 root NULL

int a[] = { 7, 2, 9, 1, 6, 9, 8, …}

search(7, NULL) : 呼出 1

if ( t == NULL ) { t = (struct tree *)malloc(sizeof(struct tree)); t->key = x; t->count = 1; t->left = NULL; t->right = NULL;}else if ( x < t->key ) t->left = search( x, t->left );else if ( x > t->key ) t->right = search( x, t->right );else (t->count)++;

return (t);

x 7 t

7

NULL

NULL

1

探索木 (search tree)

main()

root = NULL;while( ( y=get_data() )!= EOD ) root = search( y, root )

y 7 root NULL

int a[] = { 7, 2, 9, 1, 6, 9, 8, …}

search(7, NULL) : 呼出 1

if ( t == NULL ) { t = (struct tree *)malloc(sizeof(struct tree)); t->key = x; t->count = 1; t->left = NULL; t->right = NULL;}else if ( x < t->key ) t->left = search( x, t->left );else if ( x > t->key ) t->right = search( x, t->right );else (t->count)++;

return (t);

x 7 t

7

NULL

NULL

1

探索木 (search tree)

main()

root = NULL;while( ( y=get_data() )!= EOD ) root = search( y, root )

y 7 root NULL

int a[] = { 7, 2, 9, 1, 6, 9, 8, …}

7

NULL

NULL

1

探索木 (search tree)

main()

root = NULL;while( ( y=get_data() )!= EOD ) root = search( y, root )

y 7 root

int a[] = { 7, 2, 9, 1, 6, 9, 8, …}

7

NULL

NULL

1

探索木 (search tree)

main()

root = NULL;while( ( y=get_data() )!= EOD ) root = search( y, root )

y 2 root

int a[] = { 7, 2, 9, 1, 6, 9, 8, …}

7

NULL

NULL

1

探索木 (search tree)

main()

root = NULL;while( ( y=get_data() )!= EOD ) root = search( y, root )

y 2 root

int a[] = { 7, 2, 9, 1, 6, 9, 8, …}

7

NULL

NULL

1

探索木 (search tree)

main()

root = NULL;while( ( y=get_data() )!= EOD ) root = search( y, root )

y 2 root

int a[] = { 7, 2, 9, 1, 6, 9, 8, …}

7

NULL

NULL

1

search(2, root) : 呼出 2

if ( t == NULL ) { t = (struct tree *)malloc(sizeof(struct tree)); t->key = x; t->count = 1; t->left = NULL; t->right = NULL;}else if ( x < t->key ) t->left = search( x, t->left );else if ( x > t->key ) t->right = search( x, t->right );else (t->count)++;

return (t);

x 2 t

探索木 (search tree)

main()

root = NULL;while( ( y=get_data() )!= EOD ) root = search( y, root )

y 2 root

int a[] = { 7, 2, 9, 1, 6, 9, 8, …}

7

NULL

NULL

1

search(2, root) : 呼出 2

if ( t == NULL ) { t = (struct tree *)malloc(sizeof(struct tree)); t->key = x; t->count = 1; t->left = NULL; t->right = NULL;}else if ( x < t->key ) t->left = search( x, t->left );else if ( x > t->key ) t->right = search( x, t->right );else (t->count)++;

return (t);

x 2 t

2 が入るのは、7 の左部分木

探索木 (search tree)

main()

root = NULL;while( ( y=get_data() )!= EOD ) root = search( y, root )

y 2 root

int a[] = { 7, 2, 9, 1, 6, 9, 8, …}

7

NULL

NULL

1

search(2, root) : 呼出 2

if ( t == NULL ) { t = (struct tree *)malloc(sizeof(struct tree)); t->key = x; t->count = 1; t->left = NULL; t->right = NULL;}else if ( x < t->key ) t->left = search( x, t->left );else if ( x > t->key ) t->right = search( x, t->right );else (t->count)++;

return (t);

x 2 t

2 が入るのは、7 の左部分木

search(2, NULL) : 呼出 3

if ( t == NULL ) { t = (struct tree *)malloc(sizeof(struct tree)); t->key = x; t->count = 1; t->left = NULL; t->right = NULL;}else if ( x < t->key ) t->left = search( x, t->left );else if ( x > t->key ) t->right = search( x, t->right );else (t->count)++;

return (t);

x 2 t NULL

探索木 (search tree)

main()

root = NULL;while( ( y=get_data() )!= EOD ) root = search( y, root )

y 2 root

int a[] = { 7, 2, 9, 1, 6, 9, 8, …}

7

NULL

NULL

1

search(2, root) : 呼出 2

if ( t == NULL ) { t = (struct tree *)malloc(sizeof(struct tree)); t->key = x; t->count = 1; t->left = NULL; t->right = NULL;}else if ( x < t->key ) t->left = search( x, t->left );else if ( x > t->key ) t->right = search( x, t->right );else (t->count)++;

return (t);

x 2 t

2 が入るのは、7 の左部分木

search(2, NULL) : 呼出 3

if ( t == NULL ) { t = (struct tree *)malloc(sizeof(struct tree)); t->key = x; t->count = 1; t->left = NULL; t->right = NULL;}else if ( x < t->key ) t->left = search( x, t->left );else if ( x > t->key ) t->right = search( x, t->right );else (t->count)++;

return (t);

x 2 t

探索木 (search tree)

main()

root = NULL;while( ( y=get_data() )!= EOD ) root = search( y, root )

y 2 root

int a[] = { 7, 2, 9, 1, 6, 9, 8, …}

7

NULL

NULL

1

search(2, root) : 呼出 2

if ( t == NULL ) { t = (struct tree *)malloc(sizeof(struct tree)); t->key = x; t->count = 1; t->left = NULL; t->right = NULL;}else if ( x < t->key ) t->left = search( x, t->left );else if ( x > t->key ) t->right = search( x, t->right );else (t->count)++;

return (t);

x 2 t

2 が入るのは、7 の左部分木

search(2, NULL) : 呼出 2

if ( t == NULL ) { t = (struct tree *)malloc(sizeof(struct tree)); t->key = x; t->count = 1; t->left = NULL; t->right = NULL;}else if ( x < t->key ) t->left = search( x, t->left );else if ( x > t->key ) t->right = search( x, t->right );else (t->count)++;

return (t);

x 2 t

21

探索木 (search tree)

main()

root = NULL;while( ( y=get_data() )!= EOD ) root = search( y, root )

y 2 root

int a[] = { 7, 2, 9, 1, 6, 9, 8, …}

7

NULL

NULL

1

search(2, root) : 呼出 2

if ( t == NULL ) { t = (struct tree *)malloc(sizeof(struct tree)); t->key = x; t->count = 1; t->left = NULL; t->right = NULL;}else if ( x < t->key ) t->left = search( x, t->left );else if ( x > t->key ) t->right = search( x, t->right );else (t->count)++;

return (t);

x 2 t

2 が入るのは、7 の左部分木

search(2, NULL) : 呼出 2

if ( t == NULL ) { t = (struct tree *)malloc(sizeof(struct tree)); t->key = x; t->count = 1; t->left = NULL; t->right = NULL;}else if ( x < t->key ) t->left = search( x, t->left );else if ( x > t->key ) t->right = search( x, t->right );else (t->count)++;

return (t);

x 2 t

2

NULL

NULL

1

探索木 (search tree)

main()

root = NULL;while( ( y=get_data() )!= EOD ) root = search( y, root )

y 2 root

int a[] = { 7, 2, 9, 1, 6, 9, 8, …}

7

NULL

NULL

1

search(2, root) : 呼出 2

if ( t == NULL ) { t = (struct tree *)malloc(sizeof(struct tree)); t->key = x; t->count = 1; t->left = NULL; t->right = NULL;}else if ( x < t->key ) t->left = search( x, t->left );else if ( x > t->key ) t->right = search( x, t->right );else (t->count)++;

return (t);

x 2 t

2 が入るのは、7 の左部分木

search(2, NULL) : 呼出 3

if ( t == NULL ) { t = (struct tree *)malloc(sizeof(struct tree)); t->key = x; t->count = 1; t->left = NULL; t->right = NULL;}else if ( x < t->key ) t->left = search( x, t->left );else if ( x > t->key ) t->right = search( x, t->right );else (t->count)++;

return (t);

x 2 t

2

NULL

NULL

1

探索木 (search tree)

main()

root = NULL;while( ( y=get_data() )!= EOD ) root = search( y, root )

y 2 root

int a[] = { 7, 2, 9, 1, 6, 9, 8, …}

7

NULL

NULL

1

search(2, root) : 呼出 2

if ( t == NULL ) { t = (struct tree *)malloc(sizeof(struct tree)); t->key = x; t->count = 1; t->left = NULL; t->right = NULL;}else if ( x < t->key ) t->left = search( x, t->left );else if ( x > t->key ) t->right = search( x, t->right );else (t->count)++;

return (t);

x 2 t

2 が入るのは、7 の左部分木

2

NULL

NULL

1

探索木 (search tree)

main()

root = NULL;while( ( y=get_data() )!= EOD ) root = search( y, root )

y 2 root

int a[] = { 7, 2, 9, 1, 6, 9, 8, …}

7

NULL

1

search(2, root) : 呼出 2

if ( t == NULL ) { t = (struct tree *)malloc(sizeof(struct tree)); t->key = x; t->count = 1; t->left = NULL; t->right = NULL;}else if ( x < t->key ) t->left = search( x, t->left );else if ( x > t->key ) t->right = search( x, t->right );else (t->count)++;

return (t);

x 2 t

2 が入るのは、7 の左部分木

2

NULL

NULL

1

探索木 (search tree)

main()

root = NULL;while( ( y=get_data() )!= EOD ) root = search( y, root )

y 2 root

int a[] = { 7, 2, 9, 1, 6, 9, 8, …}

7

NULL

1

search(2, root) : 呼出 2

if ( t == NULL ) { t = (struct tree *)malloc(sizeof(struct tree)); t->key = x; t->count = 1; t->left = NULL; t->right = NULL;}else if ( x < t->key ) t->left = search( x, t->left );else if ( x > t->key ) t->right = search( x, t->right );else (t->count)++;

return (t);

x 2 t

2 が入るのは、7 の左部分木

2

NULL

NULL

1

探索木 (search tree)

main()

root = NULL;while( ( y=get_data() )!= EOD ) root = search( y, root )

y 2 root

int a[] = { 7, 2, 9, 1, 6, 9, 8, …}

7

NULL

1

2 が入るのは、7 の左部分木

2

NULL

NULL

1

探索木 (search tree)

main()

root = NULL;while( ( y=get_data() )!= EOD ) root = search( y, root )

y 2 root

int a[] = { 7, 2, 9, 1, 6, 9, 8, …}

7

NULL

1

2 が入るのは、7 の左部分木

2

NULL

NULL

1

探索木 (search tree)

main()

root = NULL;while( ( y=get_data() )!= EOD ) root = search( y, root )

y 2 root

int a[] = { 7, 2, 9, 1, 6, 9, 8, …}

7

NULL

1

2 が入るのは、7 の左部分木

2

NULL

NULL

1

探索木 (search tree)

main()

root = NULL;while( ( y=get_data() )!= EOD ) root = search( y, root )

y 9 root

int a[] = { 7, 2, 9, 1, 6, 9, 8, …}

7

NULL

1

2

NULL

NULL

1

探索木 (search tree)

main()

root = NULL;while( ( y=get_data() )!= EOD ) root = search( y, root )

y 9 root

int a[] = { 7, 2, 9, 1, 6, 9, 8, …}

7

NULL

1

2

NULL

NULL

1

探索木 (search tree)

main()

root = NULL;while( ( y=get_data() )!= EOD ) root = search( y, root )

y 9 root

int a[] = { 7, 2, 9, 1, 6, 9, 8, …}

7

NULL

1

2

NULL

NULL

1

search(9, root) : 呼出 4

if ( t == NULL ) { t = (struct tree *)malloc(sizeof(struct tree)); t->key = x; t->count = 1; t->left = NULL; t->right = NULL;}else if ( x < t->key ) t->left = search( x, t->left );else if ( x > t->key ) t->right = search( x, t->right );else (t->count)++;

return (t);

x 9 t

探索木 (search tree)

main()

root = NULL;while( ( y=get_data() )!= EOD ) root = search( y, root )

y 9 root

int a[] = { 7, 2, 9, 1, 6, 9, 8, …}

7

NULL

1

2

NULL

NULL

1

search(9, root) : 呼出 4

if ( t == NULL ) { t = (struct tree *)malloc(sizeof(struct tree)); t->key = x; t->count = 1; t->left = NULL; t->right = NULL;}else if ( x < t->key ) t->left = search( x, t->left );else if ( x > t->key ) t->right = search( x, t->right );else (t->count)++;

return (t);

x 9 t

9 が入るのは、7 の右部分木

探索木 (search tree)

main()

root = NULL;while( ( y=get_data() )!= EOD ) root = search( y, root )

y 9 root

int a[] = { 7, 2, 9, 1, 6, 9, 8, …}

7

NULL

1

2

NULL

NULL

1

search(9, root) : 呼出 4

if ( t == NULL ) { t = (struct tree *)malloc(sizeof(struct tree)); t->key = x; t->count = 1; t->left = NULL; t->right = NULL;}else if ( x < t->key ) t->left = search( x, t->left );else if ( x > t->key ) t->right = search( x, t->right );else (t->count)++;

return (t);

x 9 t

9 が入るのは、7 の右部分木

search(9, NULL) : 呼出 5

if ( t == NULL ) { t = (struct tree *)malloc(sizeof(struct tree)); t->key = x; t->count = 1; t->left = NULL; t->right = NULL;}else if ( x < t->key ) t->left = search( x, t->left );else if ( x > t->key ) t->right = search( x, t->right );else (t->count)++;

return (t);

x 9 t NULL

探索木 (search tree)

main()

root = NULL;while( ( y=get_data() )!= EOD ) root = search( y, root )

y 9 root

int a[] = { 7, 2, 9, 1, 6, 9, 8, …}

7

NULL

1

2

NULL

NULL

1

search(9, root) : 呼出 4

if ( t == NULL ) { t = (struct tree *)malloc(sizeof(struct tree)); t->key = x; t->count = 1; t->left = NULL; t->right = NULL;}else if ( x < t->key ) t->left = search( x, t->left );else if ( x > t->key ) t->right = search( x, t->right );else (t->count)++;

return (t);

x 9 t

9 が入るのは、7 の右部分木

search(9, NULL) : 呼出 5

if ( t == NULL ) { t = (struct tree *)malloc(sizeof(struct tree)); t->key = x; t->count = 1; t->left = NULL; t->right = NULL;}else if ( x < t->key ) t->left = search( x, t->left );else if ( x > t->key ) t->right = search( x, t->right );else (t->count)++;

return (t);

x 9 t

探索木 (search tree)

main()

root = NULL;while( ( y=get_data() )!= EOD ) root = search( y, root )

y 9 root

int a[] = { 7, 2, 9, 1, 6, 9, 8, …}

7

NULL

1

2

NULL

NULL

1

search(9, root) : 呼出 4

if ( t == NULL ) { t = (struct tree *)malloc(sizeof(struct tree)); t->key = x; t->count = 1; t->left = NULL; t->right = NULL;}else if ( x < t->key ) t->left = search( x, t->left );else if ( x > t->key ) t->right = search( x, t->right );else (t->count)++;

return (t);

x 9 t

9 が入るのは、7 の右部分木

search(9, NULL) : 呼出 5

if ( t == NULL ) { t = (struct tree *)malloc(sizeof(struct tree)); t->key = x; t->count = 1; t->left = NULL; t->right = NULL;}else if ( x < t->key ) t->left = search( x, t->left );else if ( x > t->key ) t->right = search( x, t->right );else (t->count)++;

return (t);

x 9 t

91

探索木 (search tree)

main()

root = NULL;while( ( y=get_data() )!= EOD ) root = search( y, root )

y 9 root

int a[] = { 7, 2, 9, 1, 6, 9, 8, …}

7

NULL

1

2

NULL

NULL

1

search(9, root) : 呼出 4

if ( t == NULL ) { t = (struct tree *)malloc(sizeof(struct tree)); t->key = x; t->count = 1; t->left = NULL; t->right = NULL;}else if ( x < t->key ) t->left = search( x, t->left );else if ( x > t->key ) t->right = search( x, t->right );else (t->count)++;

return (t);

x 9 t

9 が入るのは、7 の右部分木

search(9, NULL) : 呼出 5

if ( t == NULL ) { t = (struct tree *)malloc(sizeof(struct tree)); t->key = x; t->count = 1; t->left = NULL; t->right = NULL;}else if ( x < t->key ) t->left = search( x, t->left );else if ( x > t->key ) t->right = search( x, t->right );else (t->count)++;

return (t);

x 9 t

9

NULL

NULL

1

探索木 (search tree)

main()

root = NULL;while( ( y=get_data() )!= EOD ) root = search( y, root )

y 9 root

int a[] = { 7, 2, 9, 1, 6, 9, 8, …}

7

NULL

1

2

NULL

NULL

1

search(9, root) : 呼出 4

if ( t == NULL ) { t = (struct tree *)malloc(sizeof(struct tree)); t->key = x; t->count = 1; t->left = NULL; t->right = NULL;}else if ( x < t->key ) t->left = search( x, t->left );else if ( x > t->key ) t->right = search( x, t->right );else (t->count)++;

return (t);

x 9 t

9 が入るのは、7 の右部分木

search(9, NULL) : 呼出 5

if ( t == NULL ) { t = (struct tree *)malloc(sizeof(struct tree)); t->key = x; t->count = 1; t->left = NULL; t->right = NULL;}else if ( x < t->key ) t->left = search( x, t->left );else if ( x > t->key ) t->right = search( x, t->right );else (t->count)++;

return (t);

x 9 t

9

NULL

NULL

1

探索木 (search tree)

main()

root = NULL;while( ( y=get_data() )!= EOD ) root = search( y, root )

y 9 root

int a[] = { 7, 2, 9, 1, 6, 9, 8, …}

7

NULL

1

2

NULL

NULL

1

search(9, root) : 呼出 4

if ( t == NULL ) { t = (struct tree *)malloc(sizeof(struct tree)); t->key = x; t->count = 1; t->left = NULL; t->right = NULL;}else if ( x < t->key ) t->left = search( x, t->left );else if ( x > t->key ) t->right = search( x, t->right );else (t->count)++;

return (t);

x 9 t

9 が入るのは、7 の右部分木

9

NULL

NULL

1

探索木 (search tree)

main()

root = NULL;while( ( y=get_data() )!= EOD ) root = search( y, root )

y 9 root

int a[] = { 7, 2, 9, 1, 6, 9, 8, …}

71

2

NULL

NULL

1

search(9, root) : 呼出 4

if ( t == NULL ) { t = (struct tree *)malloc(sizeof(struct tree)); t->key = x; t->count = 1; t->left = NULL; t->right = NULL;}else if ( x < t->key ) t->left = search( x, t->left );else if ( x > t->key ) t->right = search( x, t->right );else (t->count)++;

return (t);

x 9 t

9 が入るのは、7 の右部分木

9

NULL

NULL

1

探索木 (search tree)

main()

root = NULL;while( ( y=get_data() )!= EOD ) root = search( y, root )

y 9 root

int a[] = { 7, 2, 9, 1, 6, 9, 8, …}

71

2

NULL

NULL

1

search(9, root) : 呼出 4

if ( t == NULL ) { t = (struct tree *)malloc(sizeof(struct tree)); t->key = x; t->count = 1; t->left = NULL; t->right = NULL;}else if ( x < t->key ) t->left = search( x, t->left );else if ( x > t->key ) t->right = search( x, t->right );else (t->count)++;

return (t);

x 9 t

9

NULL

NULL

1

探索木 (search tree)

main()

root = NULL;while( ( y=get_data() )!= EOD ) root = search( y, root )

y 9 root

int a[] = { 7, 2, 9, 1, 6, 9, 8, …}

71

2

NULL

NULL

19

NULL

NULL

1

探索木 (search tree)

main()

root = NULL;while( ( y=get_data() )!= EOD ) root = search( y, root )

y 1 root

int a[] = { 7, 2, 9, 1, 6, 9, 8, …}

71

2

NULL

NULL

19

NULL

NULL

1

探索木 (search tree)

main()

root = NULL;while( ( y=get_data() )!= EOD ) root = search( y, root )

y 1 root

int a[] = { 7, 2, 9, 1, 6, 9, 8, …}

71

2

NULL

NULL

19

NULL

NULL

1

search(1, root) : 呼出 6

if ( t == NULL ) { t = (struct tree *)malloc(sizeof(struct tree)); t->key = x; t->count = 1; t->left = NULL; t->right = NULL;}else if ( x < t->key ) t->left = search( x, t->left );else if ( x > t->key ) t->right = search( x, t->right );else (t->count)++;

return (t);

x 1 t

探索木 (search tree)

main()

root = NULL;while( ( y=get_data() )!= EOD ) root = search( y, root )

y 1 root

int a[] = { 7, 2, 9, 1, 6, 9, 8, …}

71

2

NULL

NULL

19

NULL

NULL

1

search(1, root) : 呼出 6

if ( t == NULL ) { t = (struct tree *)malloc(sizeof(struct tree)); t->key = x; t->count = 1; t->left = NULL; t->right = NULL;}else if ( x < t->key ) t->left = search( x, t->left );else if ( x > t->key ) t->right = search( x, t->right );else (t->count)++;

return (t);

x 1 t

1 が入るのは、7 の左部分木

探索木 (search tree)

main()

root = NULL;while( ( y=get_data() )!= EOD ) root = search( y, root )

y 1 root

int a[] = { 7, 2, 9, 1, 6, 9, 8, …}

71

2

NULL

NULL

19

NULL

NULL

1

search(1, root) : 呼出 6

if ( t == NULL ) { t = (struct tree *)malloc(sizeof(struct tree)); t->key = x; t->count = 1; t->left = NULL; t->right = NULL;}else if ( x < t->key ) t->left = search( x, t->left );else if ( x > t->key ) t->right = search( x, t->right );else (t->count)++;

return (t);

x 1 t

1 が入るのは、7 の左部分木

search(1, t->left) : 呼出 7

if ( t == NULL ) { t = (struct tree *)malloc(sizeof(struct tree)); t->key = x; t->count = 1; t->left = NULL; t->right = NULL;}else if ( x < t->key ) t->left = search( x, t->left );else if ( x > t->key ) t->right = search( x, t->right );else (t->count)++;

return (t);

x 1 t

探索木 (search tree)

main()

root = NULL;while( ( y=get_data() )!= EOD ) root = search( y, root )

y 1 root

int a[] = { 7, 2, 9, 1, 6, 9, 8, …}

71

2

NULL

NULL

19

NULL

NULL

1

search(1, root) : 呼出 6

if ( t == NULL ) { t = (struct tree *)malloc(sizeof(struct tree)); t->key = x; t->count = 1; t->left = NULL; t->right = NULL;}else if ( x < t->key ) t->left = search( x, t->left );else if ( x > t->key ) t->right = search( x, t->right );else (t->count)++;

return (t);

x 1 t

1 が入るのは、7 の左部分木

search(1, t->left) : 呼出 7

if ( t == NULL ) { t = (struct tree *)malloc(sizeof(struct tree)); t->key = x; t->count = 1; t->left = NULL; t->right = NULL;}else if ( x < t->key ) t->left = search( x, t->left );else if ( x > t->key ) t->right = search( x, t->right );else (t->count)++;

return (t);

x 1 t

1 が入るのは、2 の左部分木

探索木 (search tree)

main()

root = NULL;while( ( y=get_data() )!= EOD ) root = search( y, root )

y 1 root

int a[] = { 7, 2, 9, 1, 6, 9, 8, …}

71

2

NULL

NULL

19

NULL

NULL

1

search(1, root) : 呼出 6

if ( t == NULL ) { t = (struct tree *)malloc(sizeof(struct tree)); t->key = x; t->count = 1; t->left = NULL; t->right = NULL;}else if ( x < t->key ) t->left = search( x, t->left );else if ( x > t->key ) t->right = search( x, t->right );else (t->count)++;

return (t);

x 1 t

1 が入るのは、7 の左部分木

search(1, t->left) : 呼出 7

if ( t == NULL ) { t = (struct tree *)malloc(sizeof(struct tree)); t->key = x; t->count = 1; t->left = NULL; t->right = NULL;}else if ( x < t->key ) t->left = search( x, t->left );else if ( x > t->key ) t->right = search( x, t->right );else (t->count)++;

return (t);

x 1 t

1 が入るのは、2 の左部分木search(1, NULL) : 呼出 8

if ( t == NULL ) { t = (struct tree *)malloc(sizeof(struct tree)); t->key = x; t->count = 1; t->left = NULL; t->right = NULL;}else if ( x < t->key ) t->left = search( x, t->left );else if ( x > t->key ) t->right = search( x, t->right );else (t->count)++;

return (t);

x 1 t NULL

探索木 (search tree)

main()

root = NULL;while( ( y=get_data() )!= EOD ) root = search( y, root )

y 1 root

int a[] = { 7, 2, 9, 1, 6, 9, 8, …}

71

2

NULL

NULL

19

NULL

NULL

1

search(1, root) : 呼出 6

if ( t == NULL ) { t = (struct tree *)malloc(sizeof(struct tree)); t->key = x; t->count = 1; t->left = NULL; t->right = NULL;}else if ( x < t->key ) t->left = search( x, t->left );else if ( x > t->key ) t->right = search( x, t->right );else (t->count)++;

return (t);

x 1 t

1 が入るのは、7 の左部分木

search(1, t->left) : 呼出 7

if ( t == NULL ) { t = (struct tree *)malloc(sizeof(struct tree)); t->key = x; t->count = 1; t->left = NULL; t->right = NULL;}else if ( x < t->key ) t->left = search( x, t->left );else if ( x > t->key ) t->right = search( x, t->right );else (t->count)++;

return (t);

x 1 t

1 が入るのは、2 の左部分木search(1, NULL) : 呼出 8

if ( t == NULL ) { t = (struct tree *)malloc(sizeof(struct tree)); t->key = x; t->count = 1; t->left = NULL; t->right = NULL;}else if ( x < t->key ) t->left = search( x, t->left );else if ( x > t->key ) t->right = search( x, t->right );else (t->count)++;

return (t);

x 1 t

探索木 (search tree)

main()

root = NULL;while( ( y=get_data() )!= EOD ) root = search( y, root )

y 1 root

int a[] = { 7, 2, 9, 1, 6, 9, 8, …}

71

2

NULL

NULL

19

NULL

NULL

1

search(1, root) : 呼出 6

if ( t == NULL ) { t = (struct tree *)malloc(sizeof(struct tree)); t->key = x; t->count = 1; t->left = NULL; t->right = NULL;}else if ( x < t->key ) t->left = search( x, t->left );else if ( x > t->key ) t->right = search( x, t->right );else (t->count)++;

return (t);

x 1 t

1 が入るのは、7 の左部分木

search(1, t->left) : 呼出 7

if ( t == NULL ) { t = (struct tree *)malloc(sizeof(struct tree)); t->key = x; t->count = 1; t->left = NULL; t->right = NULL;}else if ( x < t->key ) t->left = search( x, t->left );else if ( x > t->key ) t->right = search( x, t->right );else (t->count)++;

return (t);

x 1 t

1 が入るのは、2 の左部分木

11

search(1, NULL) : 呼出 8

if ( t == NULL ) { t = (struct tree *)malloc(sizeof(struct tree)); t->key = x; t->count = 1; t->left = NULL; t->right = NULL;}else if ( x < t->key ) t->left = search( x, t->left );else if ( x > t->key ) t->right = search( x, t->right );else (t->count)++;

return (t);

x 1 t

探索木 (search tree)

main()

root = NULL;while( ( y=get_data() )!= EOD ) root = search( y, root )

y 1 root

int a[] = { 7, 2, 9, 1, 6, 9, 8, …}

71

2

NULL

NULL

19

NULL

NULL

1

search(1, root) : 呼出 6

if ( t == NULL ) { t = (struct tree *)malloc(sizeof(struct tree)); t->key = x; t->count = 1; t->left = NULL; t->right = NULL;}else if ( x < t->key ) t->left = search( x, t->left );else if ( x > t->key ) t->right = search( x, t->right );else (t->count)++;

return (t);

x 1 t

1 が入るのは、7 の左部分木

search(1, t->left) : 呼出 7

if ( t == NULL ) { t = (struct tree *)malloc(sizeof(struct tree)); t->key = x; t->count = 1; t->left = NULL; t->right = NULL;}else if ( x < t->key ) t->left = search( x, t->left );else if ( x > t->key ) t->right = search( x, t->right );else (t->count)++;

return (t);

x 1 t

1 が入るのは、2 の左部分木

1

NULL

NULL

1

search(1, NULL) : 呼出 8

if ( t == NULL ) { t = (struct tree *)malloc(sizeof(struct tree)); t->key = x; t->count = 1; t->left = NULL; t->right = NULL;}else if ( x < t->key ) t->left = search( x, t->left );else if ( x > t->key ) t->right = search( x, t->right );else (t->count)++;

return (t);

x 1 t

探索木 (search tree)

main()

root = NULL;while( ( y=get_data() )!= EOD ) root = search( y, root )

y 1 root

int a[] = { 7, 2, 9, 1, 6, 9, 8, …}

71

2

NULL

NULL

19

NULL

NULL

1

search(1, root) : 呼出 6

if ( t == NULL ) { t = (struct tree *)malloc(sizeof(struct tree)); t->key = x; t->count = 1; t->left = NULL; t->right = NULL;}else if ( x < t->key ) t->left = search( x, t->left );else if ( x > t->key ) t->right = search( x, t->right );else (t->count)++;

return (t);

x 1 t

1 が入るのは、7 の左部分木

search(1, t->left) : 呼出 7

if ( t == NULL ) { t = (struct tree *)malloc(sizeof(struct tree)); t->key = x; t->count = 1; t->left = NULL; t->right = NULL;}else if ( x < t->key ) t->left = search( x, t->left );else if ( x > t->key ) t->right = search( x, t->right );else (t->count)++;

return (t);

x 1 t

1 が入るのは、2 の左部分木

1

NULL

NULL

1

search(1, NULL) : 呼出 8

if ( t == NULL ) { t = (struct tree *)malloc(sizeof(struct tree)); t->key = x; t->count = 1; t->left = NULL; t->right = NULL;}else if ( x < t->key ) t->left = search( x, t->left );else if ( x > t->key ) t->right = search( x, t->right );else (t->count)++;

return (t);

x 1 t

探索木 (search tree)

main()

root = NULL;while( ( y=get_data() )!= EOD ) root = search( y, root )

y 1 root

int a[] = { 7, 2, 9, 1, 6, 9, 8, …}

71

2

NULL

NULL

19

NULL

NULL

1

search(1, root) : 呼出 6

if ( t == NULL ) { t = (struct tree *)malloc(sizeof(struct tree)); t->key = x; t->count = 1; t->left = NULL; t->right = NULL;}else if ( x < t->key ) t->left = search( x, t->left );else if ( x > t->key ) t->right = search( x, t->right );else (t->count)++;

return (t);

x 1 t

1 が入るのは、7 の左部分木

search(1, t->left) : 呼出 7

if ( t == NULL ) { t = (struct tree *)malloc(sizeof(struct tree)); t->key = x; t->count = 1; t->left = NULL; t->right = NULL;}else if ( x < t->key ) t->left = search( x, t->left );else if ( x > t->key ) t->right = search( x, t->right );else (t->count)++;

return (t);

x 1 t

1 が入るのは、2 の左部分木

1

NULL

NULL

1

探索木 (search tree)

main()

root = NULL;while( ( y=get_data() )!= EOD ) root = search( y, root )

y 1 root

int a[] = { 7, 2, 9, 1, 6, 9, 8, …}

71

2

NULL

19

NULL

NULL

1

search(1, root) : 呼出 6

if ( t == NULL ) { t = (struct tree *)malloc(sizeof(struct tree)); t->key = x; t->count = 1; t->left = NULL; t->right = NULL;}else if ( x < t->key ) t->left = search( x, t->left );else if ( x > t->key ) t->right = search( x, t->right );else (t->count)++;

return (t);

x 1 t

1 が入るのは、7 の左部分木

search(1, t->left) : 呼出 7

if ( t == NULL ) { t = (struct tree *)malloc(sizeof(struct tree)); t->key = x; t->count = 1; t->left = NULL; t->right = NULL;}else if ( x < t->key ) t->left = search( x, t->left );else if ( x > t->key ) t->right = search( x, t->right );else (t->count)++;

return (t);

x 1 t

1 が入るのは、2 の左部分木

1

NULL

NULL

1

探索木 (search tree)

main()

root = NULL;while( ( y=get_data() )!= EOD ) root = search( y, root )

y 1 root

int a[] = { 7, 2, 9, 1, 6, 9, 8, …}

71

2

NULL

19

NULL

NULL

1

search(1, root) : 呼出 6

if ( t == NULL ) { t = (struct tree *)malloc(sizeof(struct tree)); t->key = x; t->count = 1; t->left = NULL; t->right = NULL;}else if ( x < t->key ) t->left = search( x, t->left );else if ( x > t->key ) t->right = search( x, t->right );else (t->count)++;

return (t);

x 1 t

1 が入るのは、7 の左部分木

search(1, t->left) : 呼出 7

if ( t == NULL ) { t = (struct tree *)malloc(sizeof(struct tree)); t->key = x; t->count = 1; t->left = NULL; t->right = NULL;}else if ( x < t->key ) t->left = search( x, t->left );else if ( x > t->key ) t->right = search( x, t->right );else (t->count)++;

return (t);

x 1 t

1

NULL

NULL

1

探索木 (search tree)

main()

root = NULL;while( ( y=get_data() )!= EOD ) root = search( y, root )

y 1 root

int a[] = { 7, 2, 9, 1, 6, 9, 8, …}

71

2

NULL

19

NULL

NULL

1

search(1, root) : 呼出 6

if ( t == NULL ) { t = (struct tree *)malloc(sizeof(struct tree)); t->key = x; t->count = 1; t->left = NULL; t->right = NULL;}else if ( x < t->key ) t->left = search( x, t->left );else if ( x > t->key ) t->right = search( x, t->right );else (t->count)++;

return (t);

x 1 t

1 が入るのは、7 の左部分木

1

NULL

NULL

1

探索木 (search tree)

main()

root = NULL;while( ( y=get_data() )!= EOD ) root = search( y, root )

y 1 root

int a[] = { 7, 2, 9, 1, 6, 9, 8, …}

71

2

NULL

19

NULL

NULL

1

search(1, root) : 呼出 6

if ( t == NULL ) { t = (struct tree *)malloc(sizeof(struct tree)); t->key = x; t->count = 1; t->left = NULL; t->right = NULL;}else if ( x < t->key ) t->left = search( x, t->left );else if ( x > t->key ) t->right = search( x, t->right );else (t->count)++;

return (t);

x 1 t

1

NULL

NULL

1

探索木 (search tree)

main()

root = NULL;while( ( y=get_data() )!= EOD ) root = search( y, root )

y 1 root

int a[] = { 7, 2, 9, 1, 6, 9, 8, …}

71

2

NULL

19

NULL

NULL

1

1

NULL

NULL

1

探索木 (search tree)

main()

root = NULL;while( ( y=get_data() )!= EOD ) root = search( y, root )

y 6 root

int a[] = { 7, 2, 9, 1, 6, 9, 8, …}

71

2

NULL

19

NULL

NULL

1

1

NULL

NULL

1

探索木 (search tree)

main()

root = NULL;while( ( y=get_data() )!= EOD ) root = search( y, root )

y 6 root

int a[] = { 7, 2, 9, 1, 6, 9, 8, …}

71

2

NULL

19

NULL

NULL

1

6 が入るのは、7 の左部分木

1

NULL

NULL

1

探索木 (search tree)

main()

root = NULL;while( ( y=get_data() )!= EOD ) root = search( y, root )

y 6 root

int a[] = { 7, 2, 9, 1, 6, 9, 8, …}

71

2

NULL

19

NULL

NULL

1

6 が入るのは、2 の右部分木

1

NULL

NULL

1

6 が入るのは、7 の左部分木

探索木 (search tree)

main()

root = NULL;while( ( y=get_data() )!= EOD ) root = search( y, root )

y 6 root

int a[] = { 7, 2, 9, 1, 6, 9, 8, …}

71

21

9

NULL

NULL

1

6 が入るのは、2 の右部分木

1

NULL

NULL

1

6 が入るのは、7 の左部分木

6

NULL

NULL

1

探索木 (search tree)

main()

root = NULL;while( ( y=get_data() )!= EOD ) root = search( y, root )

y 9 root

int a[] = { 7, 2, 9, 1, 6, 9, 8, …}

71

21

9

NULL

NULL

1

1

NULL

NULL

16

NULL

NULL

1

探索木 (search tree)

main()

root = NULL;while( ( y=get_data() )!= EOD ) root = search( y, root )

y 9 root

int a[] = { 7, 2, 9, 1, 6, 9, 8, …}

71

21

9

NULL

NULL

1

1

NULL

NULL

16

NULL

NULL

1

9 が入るのは、7 の右部分木

探索木 (search tree)

main()

root = NULL;while( ( y=get_data() )!= EOD ) root = search( y, root )

y 9 root

int a[] = { 7, 2, 9, 1, 6, 9, 8, …}

71

21

9

NULL

NULL

2

1

NULL

NULL

16

NULL

NULL

1

9 が入るのは、7 の右部分木

9 があったのでcount++

探索木 (search tree)

main()

root = NULL;while( ( y=get_data() )!= EOD ) root = search( y, root )

y 8 root

int a[] = { 7, 2, 9, 1, 6, 9, 8, …}

71

21

9

NULL

NULL

2

1

NULL

NULL

16

NULL

NULL

1

探索木 (search tree)

main()

root = NULL;while( ( y=get_data() )!= EOD ) root = search( y, root )

y 8 root

int a[] = { 7, 2, 9, 1, 6, 9, 8, …}

71

21

9

NULL

NULL

2

1

NULL

NULL

16

NULL

NULL

1

8 が入るのは、7 の右部分木

探索木 (search tree)

main()

root = NULL;while( ( y=get_data() )!= EOD ) root = search( y, root )

y 8 root

int a[] = { 7, 2, 9, 1, 6, 9, 8, …}

71

21

9

NULL

NULL

2

1

NULL

NULL

16

NULL

NULL

1

8 が入るのは、7 の右部分木

8 が入るのは、9 の左部分木

探索木 (search tree)

main()

root = NULL;while( ( y=get_data() )!= EOD ) root = search( y, root )

y 8 root

int a[] = { 7, 2, 9, 1, 6, 9, 8, …}

71

21

9

NULL

2

1

NULL

NULL

16

NULL

NULL

1

8 が入るのは、7 の右部分木

8 が入るのは、9 の左部分木

8

NULL

NULL

1

探索木 (search tree)

main()

root = NULL;while( ( y=get_data() )!= EOD ) root = search( y, root )

y 8 root

int a[] = { 7, 2, 9, 1, 6, 9, 8, …}

71

21

9

NULL

2

1

NULL

NULL

16

NULL

NULL

18

NULL

NULL

1