680 likes | 819 Views
アルゴリズムとデータ構造 補足資料 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.
E N D
アルゴリズムとデータ構造補足資料13-2「2分探索木への節点の追加」アルゴリズムとデータ構造補足資料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 NULL
探索木(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 7 NULL
探索木(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 7 NULL
探索木(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 7 NULL 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 t 7 NULL
探索木(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 7 NULL 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 t 7
探索木(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 7 NULL 7 1 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 t 7
探索木(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 7 NULL 7 1 NULL NULL 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 t 7
探索木(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 7 NULL 7 1 NULL NULL 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 t 7
探索木(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 7 NULL 7 1 NULL NULL
探索木(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 7 7 1 NULL NULL
探索木(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 2 7 1 NULL NULL
探索木(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 2 7 1 NULL NULL
探索木(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 2 7 1 NULL NULL 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 t 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 2が入るのは、 7の左部分木 root 2 7 1 NULL NULL 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 t 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 2が入るのは、 7の左部分木 root 2 7 1 NULL NULL search(2, root) : 呼出2 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 t 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 t 2 NULL
探索木(search tree) int a[] = { 7, 2, 9, 1, 6, 9, 8, …} main() root = NULL; while( ( y=get_data() )!= EOD ) root = search( y, root ) … y 2が入るのは、 7の左部分木 root 2 7 1 NULL NULL search(2, root) : 呼出2 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 t 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 t 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 2が入るのは、 7の左部分木 root 2 7 1 NULL NULL search(2, root) : 呼出2 2 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 t 1 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 t 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 2が入るのは、 7の左部分木 root 2 7 1 NULL NULL search(2, root) : 呼出2 2 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 t 1 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 t 2 NULL NULL
探索木(search tree) int a[] = { 7, 2, 9, 1, 6, 9, 8, …} main() root = NULL; while( ( y=get_data() )!= EOD ) root = search( y, root ) … y 2が入るのは、 7の左部分木 root 2 7 1 NULL NULL search(2, root) : 呼出2 2 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 t 1 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 t 2 NULL NULL
探索木(search tree) int a[] = { 7, 2, 9, 1, 6, 9, 8, …} main() root = NULL; while( ( y=get_data() )!= EOD ) root = search( y, root ) … y 2が入るのは、 7の左部分木 root 2 7 1 NULL NULL search(2, root) : 呼出2 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 t 1 2 NULL NULL
探索木(search tree) int a[] = { 7, 2, 9, 1, 6, 9, 8, …} main() root = NULL; while( ( y=get_data() )!= EOD ) root = search( y, root ) … y 2が入るのは、 7の左部分木 root 2 7 1 NULL search(2, root) : 呼出2 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 t 1 2 NULL NULL
探索木(search tree) int a[] = { 7, 2, 9, 1, 6, 9, 8, …} main() root = NULL; while( ( y=get_data() )!= EOD ) root = search( y, root ) … y 2が入るのは、 7の左部分木 root 2 7 1 NULL search(2, root) : 呼出2 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 t 1 2 NULL NULL
探索木(search tree) int a[] = { 7, 2, 9, 1, 6, 9, 8, …} main() root = NULL; while( ( y=get_data() )!= EOD ) root = search( y, root ) … y 2が入るのは、 7の左部分木 root 2 7 1 NULL 2 1 NULL NULL
探索木(search tree) int a[] = { 7, 2, 9, 1, 6, 9, 8, …} main() root = NULL; while( ( y=get_data() )!= EOD ) root = search( y, root ) … y 2が入るのは、 7の左部分木 root 2 7 1 NULL 2 1 NULL NULL
探索木(search tree) int a[] = { 7, 2, 9, 1, 6, 9, 8, …} main() root = NULL; while( ( y=get_data() )!= EOD ) root = search( y, root ) … y 2が入るのは、 7の左部分木 root 2 7 1 NULL 2 1 NULL NULL
探索木(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 9 7 1 NULL 2 1 NULL NULL
探索木(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 9 7 1 NULL 2 1 NULL NULL
探索木(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 9 7 1 NULL search(9, root) : 呼出4 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 t 1 9 NULL NULL
探索木(search tree) int a[] = { 7, 2, 9, 1, 6, 9, 8, …} main() root = NULL; while( ( y=get_data() )!= EOD ) root = search( y, root ) … y 9が入るのは、 7の右部分木 root 9 7 1 NULL search(9, root) : 呼出4 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 t 1 9 NULL NULL
探索木(search tree) int a[] = { 7, 2, 9, 1, 6, 9, 8, …} main() root = NULL; while( ( y=get_data() )!= EOD ) root = search( y, root ) … y 9が入るのは、 7の右部分木 root 9 7 1 NULL search(9, root) : 呼出4 2 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 t 1 9 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 t 9 NULL NULL NULL
探索木(search tree) int a[] = { 7, 2, 9, 1, 6, 9, 8, …} main() root = NULL; while( ( y=get_data() )!= EOD ) root = search( y, root ) … y 9が入るのは、 7の右部分木 root 9 7 1 NULL search(9, root) : 呼出4 2 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 t 1 9 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 t 9 NULL NULL
探索木(search tree) int a[] = { 7, 2, 9, 1, 6, 9, 8, …} main() root = NULL; while( ( y=get_data() )!= EOD ) root = search( y, root ) … y 9が入るのは、 7の右部分木 root 9 7 1 NULL search(9, root) : 呼出4 2 9 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 t 1 1 9 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 t 9 NULL NULL
探索木(search tree) int a[] = { 7, 2, 9, 1, 6, 9, 8, …} main() root = NULL; while( ( y=get_data() )!= EOD ) root = search( y, root ) … y 9が入るのは、 7の右部分木 root 9 7 1 NULL search(9, root) : 呼出4 2 9 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 t 1 1 9 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 t 9 NULL NULL NULL NULL
探索木(search tree) int a[] = { 7, 2, 9, 1, 6, 9, 8, …} main() root = NULL; while( ( y=get_data() )!= EOD ) root = search( y, root ) … y 9が入るのは、 7の右部分木 root 9 7 1 NULL search(9, root) : 呼出4 2 9 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 t 1 1 9 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 t 9 NULL NULL NULL NULL
探索木(search tree) int a[] = { 7, 2, 9, 1, 6, 9, 8, …} main() root = NULL; while( ( y=get_data() )!= EOD ) root = search( y, root ) … y 9が入るのは、 7の右部分木 root 9 7 1 NULL search(9, root) : 呼出4 2 9 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 t 1 1 9 NULL NULL NULL NULL
探索木(search tree) int a[] = { 7, 2, 9, 1, 6, 9, 8, …} main() root = NULL; while( ( y=get_data() )!= EOD ) root = search( y, root ) … y 9が入るのは、 7の右部分木 root 9 7 1 search(9, root) : 呼出4 2 9 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 t 1 1 9 NULL NULL NULL NULL
探索木(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 9 7 1 search(9, root) : 呼出4 2 9 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 t 1 1 9 NULL NULL NULL NULL
探索木(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 9 7 1 2 9 1 1 NULL NULL NULL NULL
探索木(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 1 7 1 2 9 1 1 NULL NULL NULL NULL
探索木(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 1 7 1 search(1, root) : 呼出6 2 9 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 t 1 1 1 NULL NULL NULL NULL
探索木(search tree) int a[] = { 7, 2, 9, 1, 6, 9, 8, …} main() root = NULL; while( ( y=get_data() )!= EOD ) root = search( y, root ) … y 1が入るのは、 7の左部分木 root 1 7 1 search(1, root) : 呼出6 2 9 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 t 1 1 1 NULL NULL NULL NULL
探索木(search tree) int a[] = { 7, 2, 9, 1, 6, 9, 8, …} search(1, t->left) : 呼出7 main() 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 t 1 root = NULL; while( ( y=get_data() )!= EOD ) root = search( y, root ) … y 1が入るのは、 7の左部分木 root 1 7 1 search(1, root) : 呼出6 2 9 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 t 1 1 1 NULL NULL NULL NULL
探索木(search tree) int a[] = { 7, 2, 9, 1, 6, 9, 8, …} search(1, t->left) : 呼出7 main() 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 t 1 root = NULL; while( ( y=get_data() )!= EOD ) root = search( y, root ) … y 1が入るのは、 7の左部分木 root 1 7 1 search(1, root) : 呼出6 2 9 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 t 1 1が入るのは、 2の左部分木 1 1 NULL NULL NULL NULL
探索木(search tree) int a[] = { 7, 2, 9, 1, 6, 9, 8, …} search(1, t->left) : 呼出7 main() 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 t 1 root = NULL; while( ( y=get_data() )!= EOD ) root = search( y, root ) … y 1が入るのは、 7の左部分木 root 1 7 1 search(1, root) : 呼出6 2 9 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 t 1 1が入るのは、 2の左部分木 1 1 search(1, NULL) : 呼出8 NULL NULL NULL NULL 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 t 1 NULL
探索木(search tree) int a[] = { 7, 2, 9, 1, 6, 9, 8, …} search(1, t->left) : 呼出7 main() 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 t 1 root = NULL; while( ( y=get_data() )!= EOD ) root = search( y, root ) … y 1が入るのは、 7の左部分木 root 1 7 1 search(1, root) : 呼出6 2 9 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 t 1 1が入るのは、 2の左部分木 1 1 search(1, NULL) : 呼出8 NULL NULL NULL NULL 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 t 1
探索木(search tree) int a[] = { 7, 2, 9, 1, 6, 9, 8, …} search(1, t->left) : 呼出7 main() 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 t 1 root = NULL; while( ( y=get_data() )!= EOD ) root = search( y, root ) … y 1が入るのは、 7の左部分木 root 1 7 1 search(1, root) : 呼出6 2 9 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 t 1 1が入るのは、 2の左部分木 1 1 search(1, NULL) : 呼出8 NULL NULL NULL NULL 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 t 1 1 1
探索木(search tree) int a[] = { 7, 2, 9, 1, 6, 9, 8, …} search(1, t->left) : 呼出7 main() 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 t 1 root = NULL; while( ( y=get_data() )!= EOD ) root = search( y, root ) … y 1が入るのは、 7の左部分木 root 1 7 1 search(1, root) : 呼出6 2 9 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 t 1 1が入るのは、 2の左部分木 1 1 search(1, NULL) : 呼出8 NULL NULL NULL NULL 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 t 1 1 1 NULL NULL
探索木(search tree) int a[] = { 7, 2, 9, 1, 6, 9, 8, …} search(1, t->left) : 呼出7 main() 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 t 1 root = NULL; while( ( y=get_data() )!= EOD ) root = search( y, root ) … y 1が入るのは、 7の左部分木 root 1 7 1 search(1, root) : 呼出6 2 9 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 t 1 1が入るのは、 2の左部分木 1 1 search(1, NULL) : 呼出8 NULL NULL NULL NULL 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 t 1 1 1 NULL NULL