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

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

Upload: lamya

Post on 09-Jan-2016

45 views

Category:

Documents


1 download

DESCRIPTION

アルゴリズムとデータ構造 補足資料 13-2 「 2 分探索木への節点の追加」. 横浜国立大学 理工 学部 数物・電子情報系学科 富井尚志. 探索木のオペレータ. 探索木 を探索する 探索木に節点を追加(挿入)する 探索木から節点を削除する. 探索木 (search tree). int a[] = { 7, 2, 9, 1, 6, 9, 8, …}. main(). root = NULL; while( ( y= get_data () )!= EOD ) root = search( y, root ) …. y. root. - PowerPoint PPT Presentation

TRANSCRIPT

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

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

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

横浜国立大学理工学部

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

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

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

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

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

Page 3: アルゴリズムとデータ構造 補足資料 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, …}

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

探索木 (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, …}

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

探索木 (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, …}

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

探索木 (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

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

探索木 (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

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

探索木 (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

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

探索木 (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

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

探索木 (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

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

探索木 (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

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

探索木 (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

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

探索木 (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

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

探索木 (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

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

探索木 (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

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

探索木 (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 の左部分木

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

探索木 (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

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

探索木 (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

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

探索木 (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

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

探索木 (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

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

探索木 (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

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

探索木 (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

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

探索木 (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

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

探索木 (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

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

探索木 (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

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

探索木 (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

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

探索木 (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

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

探索木 (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

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

探索木 (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

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

探索木 (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

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

探索木 (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 の右部分木

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

探索木 (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

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

探索木 (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

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

探索木 (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

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

探索木 (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

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

探索木 (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

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

探索木 (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

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

探索木 (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

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

探索木 (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

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

探索木 (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

Page 41: アルゴリズムとデータ構造 補足資料 13-2 「 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

Page 42: アルゴリズムとデータ構造 補足資料 13-2 「 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

Page 43: アルゴリズムとデータ構造 補足資料 13-2 「 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 の左部分木

Page 44: アルゴリズムとデータ構造 補足資料 13-2 「 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

Page 45: アルゴリズムとデータ構造 補足資料 13-2 「 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 の左部分木

Page 46: アルゴリズムとデータ構造 補足資料 13-2 「 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

Page 47: アルゴリズムとデータ構造 補足資料 13-2 「 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

Page 48: アルゴリズムとデータ構造 補足資料 13-2 「 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 の左部分木

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

Page 49: アルゴリズムとデータ構造 補足資料 13-2 「 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 の左部分木

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

Page 50: アルゴリズムとデータ構造 補足資料 13-2 「 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 の左部分木

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

Page 51: アルゴリズムとデータ構造 補足資料 13-2 「 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 の左部分木

1

NULL

NULL

1

Page 52: アルゴリズムとデータ構造 補足資料 13-2 「 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

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

Page 53: アルゴリズムとデータ構造 補足資料 13-2 「 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

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

Page 54: アルゴリズムとデータ構造 補足資料 13-2 「 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

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

Page 55: アルゴリズムとデータ構造 補足資料 13-2 「 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

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

Page 56: アルゴリズムとデータ構造 補足資料 13-2 「 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

19

NULL

NULL

1

1

NULL

NULL

1

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

探索木 (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

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

探索木 (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

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

探索木 (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 の左部分木

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

探索木 (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

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

探索木 (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

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

探索木 (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 の右部分木

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

探索木 (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++

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

探索木 (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

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

探索木 (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 の右部分木

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

探索木 (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 の左部分木

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

探索木 (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

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

探索木 (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