Download presentation
Presentation is loading. Please wait.
1
アルゴリズムとデータ構造 補足資料 13-3 「 2 分探索木からの節点の削除」 横浜国立大学 理工学部 数物・電子情報系学科 富井尚志
2
探索木のオペレータ 探索木を探索する 探索木に節点を追加(挿入)する 探索木から節点を削除する 1. 端点(葉: leaf )の削除 2. 一つの子孫しか持たない節点の削除 3. 二つの子孫を持つ接点の削除
3
探索木のオペレータ 探索木を探索する 探索木に節点を追加(挿入)する 探索木から節点を削除する 1. 端点(葉: leaf )の削除 2. 一つの子孫しか持たない節点の削除 3. 二つの子孫を持つ接点の削除
4
端点(葉: leaf )の削除 main() … root = delete(8, root);... root 1 NULLNULL NULLNULL 4 3 NULLNULL NULLNULL 5 NULLNULL NULLNULL 6 NULLNULL 2 8 NULLNULL NULLNULL 10 NULLNULL NULLNULL 9 7 ※ メンバ count は省略 delete(8, root)
5
端点(葉: leaf )の削除 main() … root = delete(8, root);... root 1 NULLNULL NULLNULL 4 3 NULLNULL NULLNULL 5 NULLNULL NULLNULL 6 NULLNULL 2 8 NULLNULL NULLNULL 10 NULLNULL NULLNULL 9 7 ※ メンバ count は省略 if ( t == NULL ) printf(“ 見つかりませんでした \n”); else if ( x key ) t->left = delete( x, t->left ); else if ( x > t->key ) t->right = delete( x, t->right ); else{ if( t->right == NULL ){ q = t; t = t->left; free(q); } else if( t->left == NULL ){ q = t; t = t->right; free(q); } else { t->left = del( t, t->left ); } return (t); x 8 t delete(8, root) q
6
端点(葉: leaf )の削除 main() … root = delete(8, root);... root 1 NULLNULL NULLNULL 4 3 NULLNULL NULLNULL 5 NULLNULL NULLNULL 6 NULLNULL 2 8 NULLNULL NULLNULL 10 NULLNULL NULLNULL 9 7 ※ メンバ count は省略 if ( t == NULL ) printf(“ 見つかりませんでした \n”); else if ( x key ) t->left = delete( x, t->left ); else if ( x > t->key ) t->right = delete( x, t->right ); else{ if( t->right == NULL ){ q = t; t = t->left; free(q); } else if( t->left == NULL ){ q = t; t = t->right; free(q); } else { t->left = del( t, t->left ); } return (t); x 8 t delete(8, root) q
7
端点(葉: leaf )の削除 main() … root = delete(8, root);... root 1 NULLNULL NULLNULL 4 3 NULLNULL NULLNULL 5 NULLNULL NULLNULL 6 NULLNULL 2 8 NULLNULL NULLNULL 10 NULLNULL NULLNULL 9 7 ※ メンバ count は省略 if ( t == NULL ) printf(“ 見つかりませんでした \n”); else if ( x key ) t->left = delete( x, t->left ); else if ( x > t->key ) t->right = delete( x, t->right ); else{ if( t->right == NULL ){ q = t; t = t->left; free(q); } else if( t->left == NULL ){ q = t; t = t->right; free(q); } else { t->left = del( t, t->left ); } return (t); x 8 t delete(8, root) q if ( t == NULL ) printf(“ 見つかりませんでした \n”); else if ( x key ) t->left = delete( x, t->left ); else if ( x > t->key ) t->right = delete( x, t->right ); else{ if( t->right == NULL ){ q = t; t = t->left; free(q); } else if( t->left == NULL ){ q = t; t = t->right; free(q); } else { t->left = del( t, t->left ); } return (t); x 8 tq delete(8, t->right)
8
端点(葉: leaf )の削除 main() … root = delete(8, root);... root 1 NULLNULL NULLNULL 4 3 NULLNULL NULLNULL 5 NULLNULL NULLNULL 6 NULLNULL 2 8 NULLNULL NULLNULL 10 NULLNULL NULLNULL 9 7 ※ メンバ count は省略 if ( t == NULL ) printf(“ 見つかりませんでした \n”); else if ( x key ) t->left = delete( x, t->left ); else if ( x > t->key ) t->right = delete( x, t->right ); else{ if( t->right == NULL ){ q = t; t = t->left; free(q); } else if( t->left == NULL ){ q = t; t = t->right; free(q); } else { t->left = del( t, t->left ); } return (t); x 8 t delete(8, root) q if ( t == NULL ) printf(“ 見つかりませんでした \n”); else if ( x key ) t->left = delete( x, t->left ); else if ( x > t->key ) t->right = delete( x, t->right ); else{ if( t->right == NULL ){ q = t; t = t->left; free(q); } else if( t->left == NULL ){ q = t; t = t->right; free(q); } else { t->left = del( t, t->left ); } return (t); x 8 tq delete(8, t->right)
9
端点(葉: leaf )の削除 main() … root = delete(8, root);... root 1 NULLNULL NULLNULL 4 3 NULLNULL NULLNULL 5 NULLNULL NULLNULL 6 NULLNULL 2 8 NULLNULL NULLNULL 10 NULLNULL NULLNULL 9 7 ※ メンバ count は省略 if ( t == NULL ) printf(“ 見つかりませんでした \n”); else if ( x key ) t->left = delete( x, t->left ); else if ( x > t->key ) t->right = delete( x, t->right ); else{ if( t->right == NULL ){ q = t; t = t->left; free(q); } else if( t->left == NULL ){ q = t; t = t->right; free(q); } else { t->left = del( t, t->left ); } return (t); x 8 t delete(8, root) q if ( t == NULL ) printf(“ 見つかりませんでした \n”); else if ( x key ) t->left = delete( x, t->left ); else if ( x > t->key ) t->right = delete( x, t->right ); else{ if( t->right == NULL ){ q = t; t = t->left; free(q); } else if( t->left == NULL ){ q = t; t = t->right; free(q); } else { t->left = del( t, t->left ); } return (t); x 8 tq delete(8, t->right) if ( t == NULL ) printf(“ 見つかりませんでした \n”); else if ( x key ) t->left = delete( x, t->left ); else if ( x > t->key ) t->right = delete( x, t->right ); else{ if( t->right == NULL ){ q = t; t = t->left; free(q); } else if( t->left == NULL ){ q = t; t = t->right; free(q); } else { t->left = del( t, t->left ); } return (t); x 8 tq delete(8, t->right)
10
端点(葉: leaf )の削除 main() … root = delete(8, root);... root 1 NULLNULL NULLNULL 4 3 NULLNULL NULLNULL 5 NULLNULL NULLNULL 6 NULLNULL 2 8 NULLNULL NULLNULL 10 NULLNULL NULLNULL 9 7 ※ メンバ count は省略 if ( t == NULL ) printf(“ 見つかりませんでした \n”); else if ( x key ) t->left = delete( x, t->left ); else if ( x > t->key ) t->right = delete( x, t->right ); else{ if( t->right == NULL ){ q = t; t = t->left; free(q); } else if( t->left == NULL ){ q = t; t = t->right; free(q); } else { t->left = del( t, t->left ); } return (t); x 8 t delete(8, root) q if ( t == NULL ) printf(“ 見つかりませんでした \n”); else if ( x key ) t->left = delete( x, t->left ); else if ( x > t->key ) t->right = delete( x, t->right ); else{ if( t->right == NULL ){ q = t; t = t->left; free(q); } else if( t->left == NULL ){ q = t; t = t->right; free(q); } else { t->left = del( t, t->left ); } return (t); x 8 tq delete(8, t->right) if ( t == NULL ) printf(“ 見つかりませんでした \n”); else if ( x key ) t->left = delete( x, t->left ); else if ( x > t->key ) t->right = delete( x, t->right ); else{ if( t->right == NULL ){ q = t; t = t->left; free(q); } else if( t->left == NULL ){ q = t; t = t->right; free(q); } else { t->left = del( t, t->left ); } return (t); x 8 tq delete(8, t->right)
11
端点(葉: leaf )の削除 main() … root = delete(8, root);... root 1 NULLNULL NULLNULL 4 3 NULLNULL NULLNULL 5 NULLNULL NULLNULL 6 NULLNULL 2 8 NULLNULL NULLNULL 10 NULLNULL NULLNULL 9 7 ※ メンバ count は省略 if ( t == NULL ) printf(“ 見つかりませんでした \n”); else if ( x key ) t->left = delete( x, t->left ); else if ( x > t->key ) t->right = delete( x, t->right ); else{ if( t->right == NULL ){ q = t; t = t->left; free(q); } else if( t->left == NULL ){ q = t; t = t->right; free(q); } else { t->left = del( t, t->left ); } return (t); x 8 t delete(8, root) q if ( t == NULL ) printf(“ 見つかりませんでした \n”); else if ( x key ) t->left = delete( x, t->left ); else if ( x > t->key ) t->right = delete( x, t->right ); else{ if( t->right == NULL ){ q = t; t = t->left; free(q); } else if( t->left == NULL ){ q = t; t = t->right; free(q); } else { t->left = del( t, t->left ); } return (t); x 8 tq delete(8, t->right) if ( t == NULL ) printf(“ 見つかりませんでした \n”); else if ( x key ) t->left = delete( x, t->left ); else if ( x > t->key ) t->right = delete( x, t->right ); else{ if( t->right == NULL ){ q = t; t = t->left; free(q); } else if( t->left == NULL ){ q = t; t = t->right; free(q); } else { t->left = del( t, t->left ); } return (t); x 8 tq delete(8, t->right)
12
端点(葉: leaf )の削除 main() … root = delete(8, root);... root 1 NULLNULL NULLNULL 4 3 NULLNULL NULLNULL 5 NULLNULL NULLNULL 6 NULLNULL 2 8 NULLNULL NULLNULL 10 NULLNULL NULLNULL 9 7 ※ メンバ count は省略 if ( t == NULL ) printf(“ 見つかりませんでした \n”); else if ( x key ) t->left = delete( x, t->left ); else if ( x > t->key ) t->right = delete( x, t->right ); else{ if( t->right == NULL ){ q = t; t = t->left; free(q); } else if( t->left == NULL ){ q = t; t = t->right; free(q); } else { t->left = del( t, t->left ); } return (t); x 8 t delete(8, root) q if ( t == NULL ) printf(“ 見つかりませんでした \n”); else if ( x key ) t->left = delete( x, t->left ); else if ( x > t->key ) t->right = delete( x, t->right ); else{ if( t->right == NULL ){ q = t; t = t->left; free(q); } else if( t->left == NULL ){ q = t; t = t->right; free(q); } else { t->left = del( t, t->left ); } return (t); x 8 tq delete(8, t->right) if ( t == NULL ) printf(“ 見つかりませんでした \n”); else if ( x key ) t->left = delete( x, t->left ); else if ( x > t->key ) t->right = delete( x, t->right ); else{ if( t->right == NULL ){ q = t; t = t->left; free(q); } else if( t->left == NULL ){ q = t; t = t->right; free(q); } else { t->left = del( t, t->left ); } return (t); x 8 t NUL L q delete(8, t->right)
13
端点(葉: leaf )の削除 main() … root = delete(8, root);... root 1 NULLNULL NULLNULL 4 3 NULLNULL NULLNULL 5 NULLNULL NULLNULL 6 NULLNULL 2 8 NULLNULL NULLNULL 10 NULLNULL NULLNULL 9 7 ※ メンバ count は省略 if ( t == NULL ) printf(“ 見つかりませんでした \n”); else if ( x key ) t->left = delete( x, t->left ); else if ( x > t->key ) t->right = delete( x, t->right ); else{ if( t->right == NULL ){ q = t; t = t->left; free(q); } else if( t->left == NULL ){ q = t; t = t->right; free(q); } else { t->left = del( t, t->left ); } return (t); x 8 t delete(8, root) q if ( t == NULL ) printf(“ 見つかりませんでした \n”); else if ( x key ) t->left = delete( x, t->left ); else if ( x > t->key ) t->right = delete( x, t->right ); else{ if( t->right == NULL ){ q = t; t = t->left; free(q); } else if( t->left == NULL ){ q = t; t = t->right; free(q); } else { t->left = del( t, t->left ); } return (t); x 8 tq delete(8, t->right) if ( t == NULL ) printf(“ 見つかりませんでした \n”); else if ( x key ) t->left = delete( x, t->left ); else if ( x > t->key ) t->right = delete( x, t->right ); else{ if( t->right == NULL ){ q = t; t = t->left; free(q); } else if( t->left == NULL ){ q = t; t = t->right; free(q); } else { t->left = del( t, t->left ); } return (t); x 8 t NUL L q delete(8, t->right)
14
端点(葉: leaf )の削除 main() … root = delete(8, root);... root 1 NULLNULL NULLNULL 4 3 NULLNULL NULLNULL 5 NULLNULL NULLNULL 6 NULLNULL 2 8 NULLNULL NULLNULL 10 NULLNULL NULLNULL 9 7 ※ メンバ count は省略 if ( t == NULL ) printf(“ 見つかりませんでした \n”); else if ( x key ) t->left = delete( x, t->left ); else if ( x > t->key ) t->right = delete( x, t->right ); else{ if( t->right == NULL ){ q = t; t = t->left; free(q); } else if( t->left == NULL ){ q = t; t = t->right; free(q); } else { t->left = del( t, t->left ); } return (t); x 8 t delete(8, root) q if ( t == NULL ) printf(“ 見つかりませんでした \n”); else if ( x key ) t->left = delete( x, t->left ); else if ( x > t->key ) t->right = delete( x, t->right ); else{ if( t->right == NULL ){ q = t; t = t->left; free(q); } else if( t->left == NULL ){ q = t; t = t->right; free(q); } else { t->left = del( t, t->left ); } return (t); x 8 tq delete(8, t->right) if ( t == NULL ) printf(“ 見つかりませんでした \n”); else if ( x key ) t->left = delete( x, t->left ); else if ( x > t->key ) t->right = delete( x, t->right ); else{ if( t->right == NULL ){ q = t; t = t->left; free(q); } else if( t->left == NULL ){ q = t; t = t->right; free(q); } else { t->left = del( t, t->left ); } return (t); x 8 t NUL L q delete(8, t->right)
15
端点(葉: leaf )の削除 main() … root = delete(8, root);... root 1 NULLNULL NULLNULL 4 3 NULLNULL NULLNULL 5 NULLNULL NULLNULL 6 NULLNULL 2 8 NULLNULL NULLNULL 10 NULLNULL NULLNULL 9 7 ※ メンバ count は省略 if ( t == NULL ) printf(“ 見つかりませんでした \n”); else if ( x key ) t->left = delete( x, t->left ); else if ( x > t->key ) t->right = delete( x, t->right ); else{ if( t->right == NULL ){ q = t; t = t->left; free(q); } else if( t->left == NULL ){ q = t; t = t->right; free(q); } else { t->left = del( t, t->left ); } return (t); x 8 t delete(8, root) q if ( t == NULL ) printf(“ 見つかりませんでした \n”); else if ( x key ) t->left = delete( x, t->left ); else if ( x > t->key ) t->right = delete( x, t->right ); else{ if( t->right == NULL ){ q = t; t = t->left; free(q); } else if( t->left == NULL ){ q = t; t = t->right; free(q); } else { t->left = del( t, t->left ); } return (t); x 8 tq delete(8, t->right) if ( t == NULL ) printf(“ 見つかりませんでした \n”); else if ( x key ) t->left = delete( x, t->left ); else if ( x > t->key ) t->right = delete( x, t->right ); else{ if( t->right == NULL ){ q = t; t = t->left; free(q); } else if( t->left == NULL ){ q = t; t = t->right; free(q); } else { t->left = del( t, t->left ); } return (t); x 8 t NUL L q delete(8, t->right) NULL
16
端点(葉: leaf )の削除 main() … root = delete(8, root);... root 1 NULLNULL NULLNULL 4 3 NULLNULL NULLNULL 5 NULLNULL NULLNULL 6 NULLNULL 2 10 NULLNULL NULLNULL 9 NULLNULL 7 ※ メンバ count は省略 if ( t == NULL ) printf(“ 見つかりませんでした \n”); else if ( x key ) t->left = delete( x, t->left ); else if ( x > t->key ) t->right = delete( x, t->right ); else{ if( t->right == NULL ){ q = t; t = t->left; free(q); } else if( t->left == NULL ){ q = t; t = t->right; free(q); } else { t->left = del( t, t->left ); } return (t); x 8 t delete(8, root) q if ( t == NULL ) printf(“ 見つかりませんでした \n”); else if ( x key ) t->left = delete( x, t->left ); else if ( x > t->key ) t->right = delete( x, t->right ); else{ if( t->right == NULL ){ q = t; t = t->left; free(q); } else if( t->left == NULL ){ q = t; t = t->right; free(q); } else { t->left = del( t, t->left ); } return (t); x 8 tq delete(8, t->right) NULL
17
端点(葉: leaf )の削除 main() … root = delete(8, root);... root 1 NULLNULL NULLNULL 4 3 NULLNULL NULLNULL 5 NULLNULL NULLNULL 6 NULLNULL 2 10 NULLNULL NULLNULL 9 NULLNULL 7 ※ メンバ count は省略 if ( t == NULL ) printf(“ 見つかりませんでした \n”); else if ( x key ) t->left = delete( x, t->left ); else if ( x > t->key ) t->right = delete( x, t->right ); else{ if( t->right == NULL ){ q = t; t = t->left; free(q); } else if( t->left == NULL ){ q = t; t = t->right; free(q); } else { t->left = del( t, t->left ); } return (t); x 8 t delete(8, root) q if ( t == NULL ) printf(“ 見つかりませんでした \n”); else if ( x key ) t->left = delete( x, t->left ); else if ( x > t->key ) t->right = delete( x, t->right ); else{ if( t->right == NULL ){ q = t; t = t->left; free(q); } else if( t->left == NULL ){ q = t; t = t->right; free(q); } else { t->left = del( t, t->left ); } return (t); x 8 tq delete(8, t->right)
18
端点(葉: leaf )の削除 main() … root = delete(8, root);... root 1 NULLNULL NULLNULL 4 3 NULLNULL NULLNULL 5 NULLNULL NULLNULL 6 NULLNULL 2 10 NULLNULL NULLNULL 9 NULLNULL 7 ※ メンバ count は省略 if ( t == NULL ) printf(“ 見つかりませんでした \n”); else if ( x key ) t->left = delete( x, t->left ); else if ( x > t->key ) t->right = delete( x, t->right ); else{ if( t->right == NULL ){ q = t; t = t->left; free(q); } else if( t->left == NULL ){ q = t; t = t->right; free(q); } else { t->left = del( t, t->left ); } return (t); x 8 t delete(8, root) q
19
端点(葉: leaf )の削除 main() … root = delete(8, root);... root 1 NULLNULL NULLNULL 4 3 NULLNULL NULLNULL 5 NULLNULL NULLNULL 6 NULLNULL 2 10 NULLNULL NULLNULL 9 NULLNULL 7 ※ メンバ count は省略 if ( t == NULL ) printf(“ 見つかりませんでした \n”); else if ( x key ) t->left = delete( x, t->left ); else if ( x > t->key ) t->right = delete( x, t->right ); else{ if( t->right == NULL ){ q = t; t = t->left; free(q); } else if( t->left == NULL ){ q = t; t = t->right; free(q); } else { t->left = del( t, t->left ); } return (t); x 8 t delete(8, root) q
20
端点(葉: leaf )の削除 main() … root = delete(8, root);... root 1 NULLNULL NULLNULL 4 3 NULLNULL NULLNULL 5 NULLNULL NULLNULL 6 NULLNULL 2 10 NULLNULL NULLNULL 9 NULLNULL 7 ※ メンバ count は省略
21
探索木のオペレータ 探索木を探索する 探索木に節点を追加(挿入)する 探索木から節点を削除する 1. 端点(葉: leaf )の削除 2. 一つの子孫しか持たない節点の削除 3. 二つの子孫を持つ接点の削除
22
一つの子孫しか持たない節点の削除 main() … root = delete(6, root);... root 1 NULLNULL NULLNULL 4 3 NULLNULL NULLNULL 5 NULLNULL NULLNULL 6 NULLNULL 2 8 NULLNULL NULLNULL 10 NULLNULL NULLNULL 9 7 ※ メンバ count は省略 delete(6, root)
23
一つの子孫しか持たない節点の削除 main() … root = delete(6, root);... root 1 NULLNULL NULLNULL 4 3 NULLNULL NULLNULL 5 NULLNULL NULLNULL 2 8 NULLNULL NULLNULL 10 NULLNULL NULLNULL 9 7 ※ メンバ count は省略 ゴールのイメージ 6 NULLNULL
24
一つの子孫しか持たない節点の削除 main() … root = delete(6, root);... root 1 NULLNULL NULLNULL 4 3 NULLNULL NULLNULL 5 NULLNULL NULLNULL 6 NULLNULL 2 8 NULLNULL NULLNULL 10 NULLNULL NULLNULL 9 7 ※ メンバ count は省略 delete(6, root)
25
一つの子孫しか持たない節点の削除 main() … root = delete(6, root);... root 1 NULLNULL NULLNULL 4 3 NULLNULL NULLNULL 5 NULLNULL NULLNULL 6 NULLNULL 2 8 NULLNULL NULLNULL 10 NULLNULL NULLNULL 9 7 ※ メンバ count は省略 if ( t == NULL ) printf(“ 見つかりませんでした \n”); else if ( x key ) t->left = delete( x, t->left ); else if ( x > t->key ) t->right = delete( x, t->right ); else{ if( t->right == NULL ){ q = t; t = t->left; free(q); } else if( t->left == NULL ){ q = t; t = t->right; free(q); } else { t->left = del( t, t->left ); } return (t); x 6 tq delete(6, root)
26
一つの子孫しか持たない節点の削除 main() … root = delete(6, root);... root 1 NULLNULL NULLNULL 4 3 NULLNULL NULLNULL 5 NULLNULL NULLNULL 6 NULLNULL 2 8 NULLNULL NULLNULL 10 NULLNULL NULLNULL 9 7 ※ メンバ count は省略 if ( t == NULL ) printf(“ 見つかりませんでした \n”); else if ( x key ) t->left = delete( x, t->left ); else if ( x > t->key ) t->right = delete( x, t->right ); else{ if( t->right == NULL ){ q = t; t = t->left; free(q); } else if( t->left == NULL ){ q = t; t = t->right; free(q); } else { t->left = del( t, t->left ); } return (t); x 6 tq delete(6, root)
27
一つの子孫しか持たない節点の削除 main() … root = delete(6, root);... root 1 NULLNULL NULLNULL 4 3 NULLNULL NULLNULL 5 NULLNULL NULLNULL 6 NULLNULL 2 8 NULLNULL NULLNULL 10 NULLNULL NULLNULL 9 7 ※ メンバ count は省略 if ( t == NULL ) printf(“ 見つかりませんでした \n”); else if ( x key ) t->left = delete( x, t->left ); else if ( x > t->key ) t->right = delete( x, t->right ); else{ if( t->right == NULL ){ q = t; t = t->left; free(q); } else if( t->left == NULL ){ q = t; t = t->right; free(q); } else { t->left = del( t, t->left ); } return (t); x 6 tq delete(6, root) if ( t == NULL ) printf(“ 見つかりませんでした \n”); else if ( x key ) t->left = delete( x, t->left ); else if ( x > t->key ) t->right = delete( x, t->right ); else{ if( t->right == NULL ){ q = t; t = t->left; free(q); } else if( t->left == NULL ){ q = t; t = t->right; free(q); } else { t->left = del( t, t->left ); } return (t); x 6 tq delete(6, t->left)
28
一つの子孫しか持たない節点の削除 main() … root = delete(6, root);... root 1 NULLNULL NULLNULL 4 3 NULLNULL NULLNULL 5 NULLNULL NULLNULL 6 NULLNULL 2 8 NULLNULL NULLNULL 10 NULLNULL NULLNULL 9 7 ※ メンバ count は省略 if ( t == NULL ) printf(“ 見つかりませんでした \n”); else if ( x key ) t->left = delete( x, t->left ); else if ( x > t->key ) t->right = delete( x, t->right ); else{ if( t->right == NULL ){ q = t; t = t->left; free(q); } else if( t->left == NULL ){ q = t; t = t->right; free(q); } else { t->left = del( t, t->left ); } return (t); x 6 tq delete(6, root) if ( t == NULL ) printf(“ 見つかりませんでした \n”); else if ( x key ) t->left = delete( x, t->left ); else if ( x > t->key ) t->right = delete( x, t->right ); else{ if( t->right == NULL ){ q = t; t = t->left; free(q); } else if( t->left == NULL ){ q = t; t = t->right; free(q); } else { t->left = del( t, t->left ); } return (t); x 6 tq delete(6, t->left)
29
一つの子孫しか持たない節点の削除 main() … root = delete(6, root);... root 1 NULLNULL NULLNULL 4 3 NULLNULL NULLNULL 5 NULLNULL NULLNULL 6 NULLNULL 2 8 NULLNULL NULLNULL 10 NULLNULL NULLNULL 9 7 ※ メンバ count は省略 if ( t == NULL ) printf(“ 見つかりませんでした \n”); else if ( x key ) t->left = delete( x, t->left ); else if ( x > t->key ) t->right = delete( x, t->right ); else{ if( t->right == NULL ){ q = t; t = t->left; free(q); } else if( t->left == NULL ){ q = t; t = t->right; free(q); } else { t->left = del( t, t->left ); } return (t); x 6 tq delete(6, root) if ( t == NULL ) printf(“ 見つかりませんでした \n”); else if ( x key ) t->left = delete( x, t->left ); else if ( x > t->key ) t->right = delete( x, t->right ); else{ if( t->right == NULL ){ q = t; t = t->left; free(q); } else if( t->left == NULL ){ q = t; t = t->right; free(q); } else { t->left = del( t, t->left ); } return (t); x 6 tq delete(6, t->left) if ( t == NULL ) printf(“ 見つかりませんでした \n”); else if ( x key ) t->left = delete( x, t->left ); else if ( x > t->key ) t->right = delete( x, t->right ); else{ if( t->right == NULL ){ q = t; t = t->left; free(q); } else if( t->left == NULL ){ q = t; t = t->right; free(q); } else { t->left = del( t, t->left ); } return (t); x 6 tq delete(6, t->left)
30
一つの子孫しか持たない節点の削除 main() … root = delete(6, root);... root 1 NULLNULL NULLNULL 4 3 NULLNULL NULLNULL 5 NULLNULL NULLNULL 6 NULLNULL 2 8 NULLNULL NULLNULL 10 NULLNULL NULLNULL 9 7 ※ メンバ count は省略 if ( t == NULL ) printf(“ 見つかりませんでした \n”); else if ( x key ) t->left = delete( x, t->left ); else if ( x > t->key ) t->right = delete( x, t->right ); else{ if( t->right == NULL ){ q = t; t = t->left; free(q); } else if( t->left == NULL ){ q = t; t = t->right; free(q); } else { t->left = del( t, t->left ); } return (t); x 6 tq delete(6, root) if ( t == NULL ) printf(“ 見つかりませんでした \n”); else if ( x key ) t->left = delete( x, t->left ); else if ( x > t->key ) t->right = delete( x, t->right ); else{ if( t->right == NULL ){ q = t; t = t->left; free(q); } else if( t->left == NULL ){ q = t; t = t->right; free(q); } else { t->left = del( t, t->left ); } return (t); x 6 tq delete(6, t->left) if ( t == NULL ) printf(“ 見つかりませんでした \n”); else if ( x key ) t->left = delete( x, t->left ); else if ( x > t->key ) t->right = delete( x, t->right ); else{ if( t->right == NULL ){ q = t; t = t->left; free(q); } else if( t->left == NULL ){ q = t; t = t->right; free(q); } else { t->left = del( t, t->left ); } return (t); x 6 tq delete(6, t->left)
31
一つの子孫しか持たない節点の削除 main() … root = delete(6, root);... root 1 NULLNULL NULLNULL 4 3 NULLNULL NULLNULL 5 NULLNULL NULLNULL 6 NULLNULL 2 8 NULLNULL NULLNULL 10 NULLNULL NULLNULL 9 7 ※ メンバ count は省略 if ( t == NULL ) printf(“ 見つかりませんでした \n”); else if ( x key ) t->left = delete( x, t->left ); else if ( x > t->key ) t->right = delete( x, t->right ); else{ if( t->right == NULL ){ q = t; t = t->left; free(q); } else if( t->left == NULL ){ q = t; t = t->right; free(q); } else { t->left = del( t, t->left ); } return (t); x 6 tq delete(6, root) if ( t == NULL ) printf(“ 見つかりませんでした \n”); else if ( x key ) t->left = delete( x, t->left ); else if ( x > t->key ) t->right = delete( x, t->right ); else{ if( t->right == NULL ){ q = t; t = t->left; free(q); } else if( t->left == NULL ){ q = t; t = t->right; free(q); } else { t->left = del( t, t->left ); } return (t); x 6 tq delete(6, t->left) if ( t == NULL ) printf(“ 見つかりませんでした \n”); else if ( x key ) t->left = delete( x, t->left ); else if ( x > t->key ) t->right = delete( x, t->right ); else{ if( t->right == NULL ){ q = t; t = t->left; free(q); } else if( t->left == NULL ){ q = t; t = t->right; free(q); } else { t->left = del( t, t->left ); } return (t); x 6 tq delete(6, t->left)
32
一つの子孫しか持たない節点の削除 main() … root = delete(6, root);... root 1 NULLNULL NULLNULL 4 3 NULLNULL NULLNULL 5 NULLNULL NULLNULL 6 NULLNULL 2 8 NULLNULL NULLNULL 10 NULLNULL NULLNULL 9 7 ※ メンバ count は省略 if ( t == NULL ) printf(“ 見つかりませんでした \n”); else if ( x key ) t->left = delete( x, t->left ); else if ( x > t->key ) t->right = delete( x, t->right ); else{ if( t->right == NULL ){ q = t; t = t->left; free(q); } else if( t->left == NULL ){ q = t; t = t->right; free(q); } else { t->left = del( t, t->left ); } return (t); x 6 tq delete(6, root) if ( t == NULL ) printf(“ 見つかりませんでした \n”); else if ( x key ) t->left = delete( x, t->left ); else if ( x > t->key ) t->right = delete( x, t->right ); else{ if( t->right == NULL ){ q = t; t = t->left; free(q); } else if( t->left == NULL ){ q = t; t = t->right; free(q); } else { t->left = del( t, t->left ); } return (t); x 6 tq delete(6, t->left) if ( t == NULL ) printf(“ 見つかりませんでした \n”); else if ( x key ) t->left = delete( x, t->left ); else if ( x > t->key ) t->right = delete( x, t->right ); else{ if( t->right == NULL ){ q = t; t = t->left; free(q); } else if( t->left == NULL ){ q = t; t = t->right; free(q); } else { t->left = del( t, t->left ); } return (t); x 6 tq delete(6, t->left)
33
一つの子孫しか持たない節点の削除 main() … root = delete(6, root);... root 1 NULLNULL NULLNULL 4 3 NULLNULL NULLNULL 5 NULLNULL NULLNULL 6 NULLNULL 2 8 NULLNULL NULLNULL 10 NULLNULL NULLNULL 9 7 ※ メンバ count は省略 if ( t == NULL ) printf(“ 見つかりませんでした \n”); else if ( x key ) t->left = delete( x, t->left ); else if ( x > t->key ) t->right = delete( x, t->right ); else{ if( t->right == NULL ){ q = t; t = t->left; free(q); } else if( t->left == NULL ){ q = t; t = t->right; free(q); } else { t->left = del( t, t->left ); } return (t); x 6 tq delete(6, root) if ( t == NULL ) printf(“ 見つかりませんでした \n”); else if ( x key ) t->left = delete( x, t->left ); else if ( x > t->key ) t->right = delete( x, t->right ); else{ if( t->right == NULL ){ q = t; t = t->left; free(q); } else if( t->left == NULL ){ q = t; t = t->right; free(q); } else { t->left = del( t, t->left ); } return (t); x 6 tq delete(6, t->left) if ( t == NULL ) printf(“ 見つかりませんでした \n”); else if ( x key ) t->left = delete( x, t->left ); else if ( x > t->key ) t->right = delete( x, t->right ); else{ if( t->right == NULL ){ q = t; t = t->left; free(q); } else if( t->left == NULL ){ q = t; t = t->right; free(q); } else { t->left = del( t, t->left ); } return (t); x 6 tq delete(6, t->left)
34
一つの子孫しか持たない節点の削除 main() … root = delete(6, root);... root 1 NULLNULL NULLNULL 4 3 NULLNULL NULLNULL 5 NULLNULL NULLNULL 6 NULLNULL 2 8 NULLNULL NULLNULL 10 NULLNULL NULLNULL 9 7 ※ メンバ count は省略 if ( t == NULL ) printf(“ 見つかりませんでした \n”); else if ( x key ) t->left = delete( x, t->left ); else if ( x > t->key ) t->right = delete( x, t->right ); else{ if( t->right == NULL ){ q = t; t = t->left; free(q); } else if( t->left == NULL ){ q = t; t = t->right; free(q); } else { t->left = del( t, t->left ); } return (t); x 6 tq delete(6, root) if ( t == NULL ) printf(“ 見つかりませんでした \n”); else if ( x key ) t->left = delete( x, t->left ); else if ( x > t->key ) t->right = delete( x, t->right ); else{ if( t->right == NULL ){ q = t; t = t->left; free(q); } else if( t->left == NULL ){ q = t; t = t->right; free(q); } else { t->left = del( t, t->left ); } return (t); x 6 tq delete(6, t->left) if ( t == NULL ) printf(“ 見つかりませんでした \n”); else if ( x key ) t->left = delete( x, t->left ); else if ( x > t->key ) t->right = delete( x, t->right ); else{ if( t->right == NULL ){ q = t; t = t->left; free(q); } else if( t->left == NULL ){ q = t; t = t->right; free(q); } else { t->left = del( t, t->left ); } return (t); x 6 tq delete(6, t->left)
35
一つの子孫しか持たない節点の削除 main() … root = delete(6, root);... root 1 NULLNULL NULLNULL 4 3 NULLNULL NULLNULL 5 NULLNULL NULLNULL 6 NULLNULL 2 8 NULLNULL NULLNULL 10 NULLNULL NULLNULL 9 7 ※ メンバ count は省略 if ( t == NULL ) printf(“ 見つかりませんでした \n”); else if ( x key ) t->left = delete( x, t->left ); else if ( x > t->key ) t->right = delete( x, t->right ); else{ if( t->right == NULL ){ q = t; t = t->left; free(q); } else if( t->left == NULL ){ q = t; t = t->right; free(q); } else { t->left = del( t, t->left ); } return (t); x 6 tq delete(6, root) if ( t == NULL ) printf(“ 見つかりませんでした \n”); else if ( x key ) t->left = delete( x, t->left ); else if ( x > t->key ) t->right = delete( x, t->right ); else{ if( t->right == NULL ){ q = t; t = t->left; free(q); } else if( t->left == NULL ){ q = t; t = t->right; free(q); } else { t->left = del( t, t->left ); } return (t); x 6 tq delete(6, t->left)
36
一つの子孫しか持たない節点の削除 main() … root = delete(6, root);... root 1 NULLNULL NULLNULL 4 3 NULLNULL NULLNULL 5 NULLNULL NULLNULL 2 8 NULLNULL NULLNULL 10 NULLNULL NULLNULL 9 7 ※ メンバ count は省略 if ( t == NULL ) printf(“ 見つかりませんでした \n”); else if ( x key ) t->left = delete( x, t->left ); else if ( x > t->key ) t->right = delete( x, t->right ); else{ if( t->right == NULL ){ q = t; t = t->left; free(q); } else if( t->left == NULL ){ q = t; t = t->right; free(q); } else { t->left = del( t, t->left ); } return (t); x 6 tq delete(6, root) if ( t == NULL ) printf(“ 見つかりませんでした \n”); else if ( x key ) t->left = delete( x, t->left ); else if ( x > t->key ) t->right = delete( x, t->right ); else{ if( t->right == NULL ){ q = t; t = t->left; free(q); } else if( t->left == NULL ){ q = t; t = t->right; free(q); } else { t->left = del( t, t->left ); } return (t); x 6 tq delete(6, t->left)
37
一つの子孫しか持たない節点の削除 main() … root = delete(6, root);... root 1 NULLNULL NULLNULL 4 3 NULLNULL NULLNULL 5 NULLNULL NULLNULL 2 8 NULLNULL NULLNULL 10 NULLNULL NULLNULL 9 7 ※ メンバ count は省略 if ( t == NULL ) printf(“ 見つかりませんでした \n”); else if ( x key ) t->left = delete( x, t->left ); else if ( x > t->key ) t->right = delete( x, t->right ); else{ if( t->right == NULL ){ q = t; t = t->left; free(q); } else if( t->left == NULL ){ q = t; t = t->right; free(q); } else { t->left = del( t, t->left ); } return (t); x 6 tq delete(6, root) if ( t == NULL ) printf(“ 見つかりませんでした \n”); else if ( x key ) t->left = delete( x, t->left ); else if ( x > t->key ) t->right = delete( x, t->right ); else{ if( t->right == NULL ){ q = t; t = t->left; free(q); } else if( t->left == NULL ){ q = t; t = t->right; free(q); } else { t->left = del( t, t->left ); } return (t); x 6 tq delete(6, t->left)
38
一つの子孫しか持たない節点の削除 main() … root = delete(6, root);... root 1 NULLNULL NULLNULL 4 3 NULLNULL NULLNULL 5 NULLNULL NULLNULL 2 8 NULLNULL NULLNULL 10 NULLNULL NULLNULL 9 7 ※ メンバ count は省略 if ( t == NULL ) printf(“ 見つかりませんでした \n”); else if ( x key ) t->left = delete( x, t->left ); else if ( x > t->key ) t->right = delete( x, t->right ); else{ if( t->right == NULL ){ q = t; t = t->left; free(q); } else if( t->left == NULL ){ q = t; t = t->right; free(q); } else { t->left = del( t, t->left ); } return (t); x 6 tq delete(6, root)
39
一つの子孫しか持たない節点の削除 main() … root = delete(6, root);... root 1 NULLNULL NULLNULL 4 3 NULLNULL NULLNULL 5 NULLNULL NULLNULL 2 8 NULLNULL NULLNULL 10 NULLNULL NULLNULL 9 7 ※ メンバ count は省略 if ( t == NULL ) printf(“ 見つかりませんでした \n”); else if ( x key ) t->left = delete( x, t->left ); else if ( x > t->key ) t->right = delete( x, t->right ); else{ if( t->right == NULL ){ q = t; t = t->left; free(q); } else if( t->left == NULL ){ q = t; t = t->right; free(q); } else { t->left = del( t, t->left ); } return (t); x 6 tq delete(6, root)
40
一つの子孫しか持たない節点の削除 main() … root = delete(6, root);... root 1 NULLNULL NULLNULL 4 3 NULLNULL NULLNULL 5 NULLNULL NULLNULL 2 8 NULLNULL NULLNULL 10 NULLNULL NULLNULL 9 7 ※ メンバ count は省略
41
探索木のオペレータ 探索木を探索する 探索木に節点を追加(挿入)する 探索木から節点を削除する 1. 端点(葉: leaf )の削除 2. 一つの子孫しか持たない節点の削除 3. 二つの子孫を持つ接点の削除
42
二つの子孫を持つ接点の削除 main() … root = delete(7, root);... root 1 NULLNULL NULLNULL 4 3 NULLNULL NULLNULL 5 NULLNULL NULLNULL 6 NULLNULL 2 8 NULLNULL NULLNULL 10 NULLNULL NULLNULL 9 7 ※ メンバ count は省略 delete(7, root)
43
二つの子孫を持つ接点の削除 main() … root = delete(7, root);... root 1 NULLNULL NULLNULL 4 3 NULLNULL NULLNULL 5 NULLNULL NULLNULL 2 8 NULLNULL NULLNULL 10 NULLNULL NULLNULL 9 6 ※ メンバ count は省略 6 NULLNULL 値をコ ピー 左部分木の 最右要素で 置き換える ゴールのイメージ
44
二つの子孫を持つ接点の削除 main() … root = delete(7, root);... root 1 NULLNULL NULLNULL 4 3 NULLNULL NULLNULL 5 NULLNULL NULLNULL 6 NULLNULL 2 8 NULLNULL NULLNULL 10 NULLNULL NULLNULL 9 7 ※ メンバ count は省略 delete(7, root)
45
二つの子孫を持つ接点の削除 main() … root = delete(7, root);... root 1 NULLNULL NULLNULL 4 3 NULLNULL NULLNULL 5 NULLNULL NULLNULL 6 NULLNULL 2 8 NULLNULL NULLNULL 10 NULLNULL NULLNULL 9 7 ※ メンバ count は省略 if ( t == NULL ) printf(“ 見つかりませんでした \n”); else if ( x key ) t->left = delete( x, t->left ); else if ( x > t->key ) t->right = delete( x, t->right ); else{ if( t->right == NULL ){ q = t; t = t->left; free(q); } else if( t->left == NULL ){ q = t; t = t->right; free(q); } else { t->left = del( t, t->left ); } return (t); x 7 tq delete(7, root)
46
二つの子孫を持つ接点の削除 main() … root = delete(7, root);... root 1 NULLNULL NULLNULL 4 3 NULLNULL NULLNULL 5 NULLNULL NULLNULL 6 NULLNULL 2 8 NULLNULL NULLNULL 10 NULLNULL NULLNULL 9 7 ※ メンバ count は省略 if ( t == NULL ) printf(“ 見つかりませんでした \n”); else if ( x key ) t->left = delete( x, t->left ); else if ( x > t->key ) t->right = delete( x, t->right ); else{ if( t->right == NULL ){ q = t; t = t->left; free(q); } else if( t->left == NULL ){ q = t; t = t->right; free(q); } else { t->left = del( t, t->left ); } return (t); x 7 tq delete(7, root)
47
二つの子孫を持つ接点の削除 main() … root = delete(7, root);... root 1 NULLNULL NULLNULL 4 3 NULLNULL NULLNULL 5 NULLNULL NULLNULL 6 NULLNULL 2 8 NULLNULL NULLNULL 10 NULLNULL NULLNULL 9 7 ※ メンバ count は省略 if ( t == NULL ) printf(“ 見つかりませんでした \n”); else if ( x key ) t->left = delete( x, t->left ); else if ( x > t->key ) t->right = delete( x, t->right ); else{ if( t->right == NULL ){ q = t; t = t->left; free(q); } else if( t->left == NULL ){ q = t; t = t->right; free(q); } else { t->left = del( t, t->left ); } return (t); x 7 tq delete(7, root) if ( t->right != NULL ) t->right = del( dstt, t->right ); else{ dstt->key = t->key; dstt->count = t->count; q = t; t = t->left; free(q); } return (t); dstt t del(t, t->left) q
48
二つの子孫を持つ接点の削除 main() … root = delete(7, root);... root 1 NULLNULL NULLNULL 4 3 NULLNULL NULLNULL 5 NULLNULL NULLNULL 6 NULLNULL 2 8 NULLNULL NULLNULL 10 NULLNULL NULLNULL 9 7 ※ メンバ count は省略 if ( t == NULL ) printf(“ 見つかりませんでした \n”); else if ( x key ) t->left = delete( x, t->left ); else if ( x > t->key ) t->right = delete( x, t->right ); else{ if( t->right == NULL ){ q = t; t = t->left; free(q); } else if( t->left == NULL ){ q = t; t = t->right; free(q); } else { t->left = del( t, t->left ); } return (t); x 7 tq delete(7, root) if ( t->right != NULL ) t->right = del( dstt, t->right ); else{ dstt->key = t->key; dstt->count = t->count; q = t; t = t->left; free(q); } return (t); dstt t del(t, t->left) q
49
二つの子孫を持つ接点の削除 main() … root = delete(7, root);... root 1 NULLNULL NULLNULL 4 3 NULLNULL NULLNULL 5 NULLNULL NULLNULL 6 NULLNULL 2 8 NULLNULL NULLNULL 10 NULLNULL NULLNULL 9 7 ※ メンバ count は省略 if ( t == NULL ) printf(“ 見つかりませんでした \n”); else if ( x key ) t->left = delete( x, t->left ); else if ( x > t->key ) t->right = delete( x, t->right ); else{ if( t->right == NULL ){ q = t; t = t->left; free(q); } else if( t->left == NULL ){ q = t; t = t->right; free(q); } else { t->left = del( t, t->left ); } return (t); x 7 tq delete(7, root) if ( t->right != NULL ) t->right = del( dstt, t->right ); else{ dstt->key = t->key; dstt->count = t->count; q = t; t = t->left; free(q); } return (t); dstt t del(t, t->left) q if ( t->right != NULL ) t->right = del( dstt, t->right ); else{ dstt->key = t->key; dstt->count = t->count; q = t; t = t->left; free(q); } return (t); dstt t del(t, t->left) q
50
二つの子孫を持つ接点の削除 main() … root = delete(7, root);... root 1 NULLNULL NULLNULL 4 3 NULLNULL NULLNULL 5 NULLNULL NULLNULL 6 NULLNULL 2 8 NULLNULL NULLNULL 10 NULLNULL NULLNULL 9 7 ※ メンバ count は省略 if ( t == NULL ) printf(“ 見つかりませんでした \n”); else if ( x key ) t->left = delete( x, t->left ); else if ( x > t->key ) t->right = delete( x, t->right ); else{ if( t->right == NULL ){ q = t; t = t->left; free(q); } else if( t->left == NULL ){ q = t; t = t->right; free(q); } else { t->left = del( t, t->left ); } return (t); x 7 tq delete(7, root) if ( t->right != NULL ) t->right = del( dstt, t->right ); else{ dstt->key = t->key; dstt->count = t->count; q = t; t = t->left; free(q); } return (t); dstt t del(t, t->left) q if ( t->right != NULL ) t->right = del( dstt, t->right ); else{ dstt->key = t->key; dstt->count = t->count; q = t; t = t->left; free(q); } return (t); dstt t del(t, t->left) q 値をコ ピー
51
二つの子孫を持つ接点の削除 main() … root = delete(7, root);... root 1 NULLNULL NULLNULL 4 3 NULLNULL NULLNULL 5 NULLNULL NULLNULL 6 NULLNULL 2 8 NULLNULL NULLNULL 10 NULLNULL NULLNULL 9 6 ※ メンバ count は省略 if ( t == NULL ) printf(“ 見つかりませんでした \n”); else if ( x key ) t->left = delete( x, t->left ); else if ( x > t->key ) t->right = delete( x, t->right ); else{ if( t->right == NULL ){ q = t; t = t->left; free(q); } else if( t->left == NULL ){ q = t; t = t->right; free(q); } else { t->left = del( t, t->left ); } return (t); x 7 tq delete(7, root) if ( t->right != NULL ) t->right = del( dstt, t->right ); else{ dstt->key = t->key; dstt->count = t->count; q = t; t = t->left; free(q); } return (t); dstt t del(t, t->left) q if ( t->right != NULL ) t->right = del( dstt, t->right ); else{ dstt->key = t->key; dstt->count = t->count; q = t; t = t->left; free(q); } return (t); dstt t del(t, t->left) q 値をコ ピー
52
二つの子孫を持つ接点の削除 main() … root = delete(7, root);... root 1 NULLNULL NULLNULL 4 3 NULLNULL NULLNULL 5 NULLNULL NULLNULL 6 NULLNULL 2 8 NULLNULL NULLNULL 10 NULLNULL NULLNULL 9 6 ※ メンバ count は省略 if ( t == NULL ) printf(“ 見つかりませんでした \n”); else if ( x key ) t->left = delete( x, t->left ); else if ( x > t->key ) t->right = delete( x, t->right ); else{ if( t->right == NULL ){ q = t; t = t->left; free(q); } else if( t->left == NULL ){ q = t; t = t->right; free(q); } else { t->left = del( t, t->left ); } return (t); x 7 tq delete(7, root) if ( t->right != NULL ) t->right = del( dstt, t->right ); else{ dstt->key = t->key; dstt->count = t->count; q = t; t = t->left; free(q); } return (t); dstt t del(t, t->left) q if ( t->right != NULL ) t->right = del( dstt, t->right ); else{ dstt->key = t->key; dstt->count = t->count; q = t; t = t->left; free(q); } return (t); dstt t del(t, t->left) q
53
二つの子孫を持つ接点の削除 main() … root = delete(7, root);... root 1 NULLNULL NULLNULL 4 3 NULLNULL NULLNULL 5 NULLNULL NULLNULL 6 NULLNULL 2 8 NULLNULL NULLNULL 10 NULLNULL NULLNULL 9 6 ※ メンバ count は省略 if ( t == NULL ) printf(“ 見つかりませんでした \n”); else if ( x key ) t->left = delete( x, t->left ); else if ( x > t->key ) t->right = delete( x, t->right ); else{ if( t->right == NULL ){ q = t; t = t->left; free(q); } else if( t->left == NULL ){ q = t; t = t->right; free(q); } else { t->left = del( t, t->left ); } return (t); x 7 tq delete(7, root) if ( t->right != NULL ) t->right = del( dstt, t->right ); else{ dstt->key = t->key; dstt->count = t->count; q = t; t = t->left; free(q); } return (t); dstt t del(t, t->left) q if ( t->right != NULL ) t->right = del( dstt, t->right ); else{ dstt->key = t->key; dstt->count = t->count; q = t; t = t->left; free(q); } return (t); dstt t del(t, t->left) q
54
二つの子孫を持つ接点の削除 main() … root = delete(7, root);... root 1 NULLNULL NULLNULL 4 3 NULLNULL NULLNULL 5 NULLNULL NULLNULL 6 NULLNULL 2 8 NULLNULL NULLNULL 10 NULLNULL NULLNULL 9 6 ※ メンバ count は省略 if ( t == NULL ) printf(“ 見つかりませんでした \n”); else if ( x key ) t->left = delete( x, t->left ); else if ( x > t->key ) t->right = delete( x, t->right ); else{ if( t->right == NULL ){ q = t; t = t->left; free(q); } else if( t->left == NULL ){ q = t; t = t->right; free(q); } else { t->left = del( t, t->left ); } return (t); x 7 tq delete(7, root) if ( t->right != NULL ) t->right = del( dstt, t->right ); else{ dstt->key = t->key; dstt->count = t->count; q = t; t = t->left; free(q); } return (t); dstt t del(t, t->left) q if ( t->right != NULL ) t->right = del( dstt, t->right ); else{ dstt->key = t->key; dstt->count = t->count; q = t; t = t->left; free(q); } return (t); dstt t del(t, t->left) q
55
二つの子孫を持つ接点の削除 main() … root = delete(7, root);... root 1 NULLNULL NULLNULL 4 3 NULLNULL NULLNULL 5 NULLNULL NULLNULL 6 NULLNULL 2 8 NULLNULL NULLNULL 10 NULLNULL NULLNULL 9 6 ※ メンバ count は省略 if ( t == NULL ) printf(“ 見つかりませんでした \n”); else if ( x key ) t->left = delete( x, t->left ); else if ( x > t->key ) t->right = delete( x, t->right ); else{ if( t->right == NULL ){ q = t; t = t->left; free(q); } else if( t->left == NULL ){ q = t; t = t->right; free(q); } else { t->left = del( t, t->left ); } return (t); x 7 tq delete(7, root) if ( t->right != NULL ) t->right = del( dstt, t->right ); else{ dstt->key = t->key; dstt->count = t->count; q = t; t = t->left; free(q); } return (t); dstt t del(t, t->left) q if ( t->right != NULL ) t->right = del( dstt, t->right ); else{ dstt->key = t->key; dstt->count = t->count; q = t; t = t->left; free(q); } return (t); dstt t del(t, t->left) q
56
二つの子孫を持つ接点の削除 main() … root = delete(7, root);... root 1 NULLNULL NULLNULL 4 3 NULLNULL NULLNULL 5 NULLNULL NULLNULL 6 NULLNULL 2 8 NULLNULL NULLNULL 10 NULLNULL NULLNULL 9 6 ※ メンバ count は省略 if ( t == NULL ) printf(“ 見つかりませんでした \n”); else if ( x key ) t->left = delete( x, t->left ); else if ( x > t->key ) t->right = delete( x, t->right ); else{ if( t->right == NULL ){ q = t; t = t->left; free(q); } else if( t->left == NULL ){ q = t; t = t->right; free(q); } else { t->left = del( t, t->left ); } return (t); x 7 tq delete(7, root) if ( t->right != NULL ) t->right = del( dstt, t->right ); else{ dstt->key = t->key; dstt->count = t->count; q = t; t = t->left; free(q); } return (t); dstt t del(t, t->left) q
57
二つの子孫を持つ接点の削除 main() … root = delete(7, root);... root 1 NULLNULL NULLNULL 4 3 NULLNULL NULLNULL 5 NULLNULL NULLNULL 2 8 NULLNULL NULLNULL 10 NULLNULL NULLNULL 9 6 ※ メンバ count は省略 if ( t == NULL ) printf(“ 見つかりませんでした \n”); else if ( x key ) t->left = delete( x, t->left ); else if ( x > t->key ) t->right = delete( x, t->right ); else{ if( t->right == NULL ){ q = t; t = t->left; free(q); } else if( t->left == NULL ){ q = t; t = t->right; free(q); } else { t->left = del( t, t->left ); } return (t); x 7 tq delete(7, root) if ( t->right != NULL ) t->right = del( dstt, t->right ); else{ dstt->key = t->key; dstt->count = t->count; q = t; t = t->left; free(q); } return (t); dstt t del(t, t->left) q
58
二つの子孫を持つ接点の削除 main() … root = delete(7, root);... root 1 NULLNULL NULLNULL 4 3 NULLNULL NULLNULL 5 NULLNULL NULLNULL 2 8 NULLNULL NULLNULL 10 NULLNULL NULLNULL 9 6 ※ メンバ count は省略 if ( t == NULL ) printf(“ 見つかりませんでした \n”); else if ( x key ) t->left = delete( x, t->left ); else if ( x > t->key ) t->right = delete( x, t->right ); else{ if( t->right == NULL ){ q = t; t = t->left; free(q); } else if( t->left == NULL ){ q = t; t = t->right; free(q); } else { t->left = del( t, t->left ); } return (t); x 7 tq delete(7, root) if ( t->right != NULL ) t->right = del( dstt, t->right ); else{ dstt->key = t->key; dstt->count = t->count; q = t; t = t->left; free(q); } return (t); dstt t del(t, t->left) q
59
二つの子孫を持つ接点の削除 main() … root = delete(7, root);... root 1 NULLNULL NULLNULL 4 3 NULLNULL NULLNULL 5 NULLNULL NULLNULL 2 8 NULLNULL NULLNULL 10 NULLNULL NULLNULL 9 6 ※ メンバ count は省略 if ( t == NULL ) printf(“ 見つかりませんでした \n”); else if ( x key ) t->left = delete( x, t->left ); else if ( x > t->key ) t->right = delete( x, t->right ); else{ if( t->right == NULL ){ q = t; t = t->left; free(q); } else if( t->left == NULL ){ q = t; t = t->right; free(q); } else { t->left = del( t, t->left ); } return (t); x 7 tq delete(7, root)
60
二つの子孫を持つ接点の削除 main() … root = delete(7, root);... root 1 NULLNULL NULLNULL 4 3 NULLNULL NULLNULL 5 NULLNULL NULLNULL 2 8 NULLNULL NULLNULL 10 NULLNULL NULLNULL 9 6 ※ メンバ count は省略 if ( t == NULL ) printf(“ 見つかりませんでした \n”); else if ( x key ) t->left = delete( x, t->left ); else if ( x > t->key ) t->right = delete( x, t->right ); else{ if( t->right == NULL ){ q = t; t = t->left; free(q); } else if( t->left == NULL ){ q = t; t = t->right; free(q); } else { t->left = del( t, t->left ); } return (t); x 7 tq delete(7, root)
61
二つの子孫を持つ接点の削除 main() … root = delete(7, root);... root 1 NULLNULL NULLNULL 4 3 NULLNULL NULLNULL 5 NULLNULL NULLNULL 2 8 NULLNULL NULLNULL 10 NULLNULL NULLNULL 9 6 ※ メンバ count は省略 if ( t == NULL ) printf(“ 見つかりませんでした \n”); else if ( x key ) t->left = delete( x, t->left ); else if ( x > t->key ) t->right = delete( x, t->right ); else{ if( t->right == NULL ){ q = t; t = t->left; free(q); } else if( t->left == NULL ){ q = t; t = t->right; free(q); } else { t->left = del( t, t->left ); } return (t); x 7 tq delete(7, root)
62
二つの子孫を持つ接点の削除 main() … root = delete(7, root);... root 1 NULLNULL NULLNULL 4 3 NULLNULL NULLNULL 5 NULLNULL NULLNULL 2 8 NULLNULL NULLNULL 10 NULLNULL NULLNULL 9 6 ※ メンバ count は省略
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.