Download presentation
Presentation is loading. Please wait.
1
アルゴリズムとデータ構造 補足資料 13-2 「 2 分探索木への節点の追加」 横浜国立大学 理工学部 数物・電子情報系学科 富井尚志
2
探索木のオペレータ 探索木を探索する 探索木に節点を追加(挿入)する 探索木から節点を削除する
3
探索木 (search tree) main() root = NULL; while( ( y=get_data() )!= EOD ) root = search( y, root ) … yroot NUL L int a[] = { 7, 2, 9, 1, 6, 9, 8, …}
4
探索木 (search tree) main() root = NULL; while( ( y=get_data() )!= EOD ) root = search( y, root ) … y 7 root NUL L int a[] = { 7, 2, 9, 1, 6, 9, 8, …}
5
探索木 (search tree) main() root = NULL; while( ( y=get_data() )!= EOD ) root = search( y, root ) … y 7 root NUL L int a[] = { 7, 2, 9, 1, 6, 9, 8, …}
6
探索木 (search tree) main() root = NULL; while( ( y=get_data() )!= EOD ) root = search( y, root ) … y 7 root NUL L 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 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 NUL L
7
探索木 (search tree) main() root = NULL; while( ( y=get_data() )!= EOD ) root = search( y, root ) … y 7 root NUL L 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 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
8
探索木 (search tree) main() root = NULL; while( ( y=get_data() )!= EOD ) root = search( y, root ) … y 7 root NUL L 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 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 1
9
探索木 (search tree) main() root = NULL; while( ( y=get_data() )!= EOD ) root = search( y, root ) … y 7 root NUL L 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 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 NULLNULL NULLNULL 1
10
探索木 (search tree) main() root = NULL; while( ( y=get_data() )!= EOD ) root = search( y, root ) … y 7 root NUL L 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 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 NULLNULL NULLNULL 1
11
探索木 (search tree) main() root = NULL; while( ( y=get_data() )!= EOD ) root = search( y, root ) … y 7 root NUL L int a[] = { 7, 2, 9, 1, 6, 9, 8, …} 7 NULLNULL NULLNULL 1
12
探索木 (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 NULLNULL NULLNULL 1
13
探索木 (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 NULLNULL NULLNULL 1
14
探索木 (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 NULLNULL NULLNULL 1
15
探索木 (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 NULLNULL NULLNULL 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 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
16
探索木 (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 NULLNULL NULLNULL 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 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 の左部分木
17
探索木 (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 NULLNULL NULLNULL 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 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 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 NUL L
18
探索木 (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 NULLNULL NULLNULL 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 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 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
19
探索木 (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 NULLNULL NULLNULL 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 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 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 1
20
探索木 (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 NULLNULL NULLNULL 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 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 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 NULLNULL NULLNULL 1
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 NULLNULL NULLNULL 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 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 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 NULLNULL NULLNULL 1
22
探索木 (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 NULLNULL NULLNULL 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 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 NULLNULL NULLNULL 1
23
探索木 (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 NULLNULL 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 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 NULLNULL NULLNULL 1
24
探索木 (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 NULLNULL 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 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 NULLNULL NULLNULL 1
25
探索木 (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 NULLNULL 1 2 が入るのは、 7 の左部分木 2 NULLNULL NULLNULL 1
26
探索木 (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 NULLNULL 1 2 が入るのは、 7 の左部分木 2 NULLNULL NULLNULL 1
27
探索木 (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 NULLNULL 1 2 が入るのは、 7 の左部分木 2 NULLNULL NULLNULL 1
28
探索木 (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 NULLNULL 1 2 NULLNULL NULLNULL 1
29
探索木 (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 NULLNULL 1 2 NULLNULL NULLNULL 1
30
探索木 (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 NULLNULL 1 2 NULLNULL NULLNULL 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 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
31
探索木 (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 NULLNULL 1 2 NULLNULL NULLNULL 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 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 の右部分木
32
探索木 (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 NULLNULL 1 2 NULLNULL NULLNULL 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 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 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 NUL L
33
探索木 (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 NULLNULL 1 2 NULLNULL NULLNULL 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 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 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
34
探索木 (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 NULLNULL 1 2 NULLNULL NULLNULL 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 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 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 1
35
探索木 (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 NULLNULL 1 2 NULLNULL NULLNULL 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 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 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 NULLNULL NULLNULL 1
36
探索木 (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 NULLNULL 1 2 NULLNULL NULLNULL 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 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 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 NULLNULL NULLNULL 1
37
探索木 (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 NULLNULL 1 2 NULLNULL NULLNULL 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 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 NULLNULL NULLNULL 1
38
探索木 (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 1 2 NULLNULL NULLNULL 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 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 NULLNULL NULLNULL 1
39
探索木 (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 1 2 NULLNULL NULLNULL 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 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 NULLNULL NULLNULL 1
40
探索木 (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 1 2 NULLNULL NULLNULL 1 9 NULLNULL NULLNULL 1
41
探索木 (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, …} 7 1 2 NULLNULL NULLNULL 1 9 NULLNULL NULLNULL 1
42
探索木 (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, …} 7 1 2 NULLNULL NULLNULL 1 9 NULLNULL NULLNULL 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 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
43
探索木 (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, …} 7 1 2 NULLNULL NULLNULL 1 9 NULLNULL NULLNULL 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 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 の左部分木
44
探索木 (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, …} 7 1 2 NULLNULL NULLNULL 1 9 NULLNULL NULLNULL 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 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 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
45
探索木 (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, …} 7 1 2 NULLNULL NULLNULL 1 9 NULLNULL NULLNULL 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 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 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 の左部分木
46
探索木 (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, …} 7 1 2 NULLNULL NULLNULL 1 9 NULLNULL NULLNULL 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 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 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 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 NUL L
47
探索木 (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, …} 7 1 2 NULLNULL NULLNULL 1 9 NULLNULL NULLNULL 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 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 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 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
48
探索木 (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, …} 7 1 2 NULLNULL NULLNULL 1 9 NULLNULL NULLNULL 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 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 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 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 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
49
探索木 (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, …} 7 1 2 NULLNULL NULLNULL 1 9 NULLNULL NULLNULL 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 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 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 NULLNULL NULLNULL 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 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
50
探索木 (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, …} 7 1 2 NULLNULL NULLNULL 1 9 NULLNULL NULLNULL 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 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 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 NULLNULL NULLNULL 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 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
51
探索木 (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, …} 7 1 2 NULLNULL NULLNULL 1 9 NULLNULL NULLNULL 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 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 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 NULLNULL NULLNULL 1
52
探索木 (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, …} 7 1 2 NULLNULL 1 9 NULLNULL NULLNULL 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 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 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 NULLNULL NULLNULL 1
53
探索木 (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, …} 7 1 2 NULLNULL 1 9 NULLNULL NULLNULL 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 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 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 NULLNULL NULLNULL 1
54
探索木 (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, …} 7 1 2 NULLNULL 1 9 NULLNULL NULLNULL 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 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 NULLNULL NULLNULL 1
55
探索木 (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, …} 7 1 2 NULLNULL 1 9 NULLNULL NULLNULL 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 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 NULLNULL NULLNULL 1
56
探索木 (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, …} 7 1 2 NULLNULL 1 9 NULLNULL NULLNULL 1 1 NULLNULL NULLNULL 1
57
探索木 (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, …} 7 1 2 NULLNULL 1 9 NULLNULL NULLNULL 1 1 NULLNULL NULLNULL 1
58
探索木 (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, …} 7 1 2 NULLNULL 1 9 NULLNULL NULLNULL 1 6 が入るのは、 7 の左部分木 1 NULLNULL NULLNULL 1
59
探索木 (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, …} 7 1 2 NULLNULL 1 9 NULLNULL NULLNULL 1 6 が入るのは、 2 の右部分木 1 NULLNULL NULLNULL 1 6 が入るのは、 7 の左部分木
60
探索木 (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, …} 7 1 2 1 9 NULLNULL NULLNULL 1 6 が入るのは、 2 の右部分木 1 NULLNULL NULLNULL 1 6 が入るのは、 7 の左部分木 6 NULLNULL NULLNULL 1
61
探索木 (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 1 2 1 9 NULLNULL NULLNULL 1 1 NULLNULL NULLNULL 1 6 NULLNULL NULLNULL 1
62
探索木 (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 1 2 1 9 NULLNULL NULLNULL 1 1 NULLNULL NULLNULL 1 6 NULLNULL NULLNULL 1 9 が入るのは、 7 の右部分木
63
探索木 (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 1 2 1 9 NULLNULL NULLNULL 2 1 NULLNULL NULLNULL 1 6 NULLNULL NULLNULL 1 9 が入るのは、 7 の右部分木 9 があったので count++
64
探索木 (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, …} 7 1 2 1 9 NULLNULL NULLNULL 2 1 NULLNULL NULLNULL 1 6 NULLNULL NULLNULL 1
65
探索木 (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, …} 7 1 2 1 9 NULLNULL NULLNULL 2 1 NULLNULL NULLNULL 1 6 NULLNULL NULLNULL 1 8 が入るのは、 7 の右部分木
66
探索木 (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, …} 7 1 2 1 9 NULLNULL NULLNULL 2 1 NULLNULL NULLNULL 1 6 NULLNULL NULLNULL 1 8 が入るのは、 7 の右部分木 8 が入るのは、 9 の左部分木
67
探索木 (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, …} 7 1 2 1 9 NULLNULL 2 1 NULLNULL NULLNULL 1 6 NULLNULL NULLNULL 1 8 が入るのは、 7 の右部分木 8 が入るのは、 9 の左部分木 8 NULLNULL NULLNULL 1
68
探索木 (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, …} 7 1 2 1 9 NULLNULL 2 1 NULLNULL NULLNULL 1 6 NULLNULL NULLNULL 1 8 NULLNULL NULLNULL 1
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.