1 / 68

アルゴリズムとデータ構造 補足資料 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.

lamya
Download Presentation

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

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. アルゴリズムとデータ構造補足資料13-2「2分探索木への節点の追加」アルゴリズムとデータ構造補足資料13-2「2分探索木への節点の追加」 横浜国立大学 理工学部 数物・電子情報系学科 富井尚志

  2. 探索木のオペレータ • 探索木を探索する • 探索木に節点を追加(挿入)する • 探索木から節点を削除する

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

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

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

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

  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 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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

More Related