アルゴリズムとデータ構造 補足資料 13-3 「 2 分探索木からの節点の削除」

62
アアアアアアアアアアアア アアアア 13-3 2 アアアアアアアアアアアアアアアアアア アアアア アア アアアアアアア アアアア

Upload: moira

Post on 18-Jan-2016

57 views

Category:

Documents


0 download

DESCRIPTION

アルゴリズムとデータ構造 補足資料 13-3 「 2 分探索木からの節点の削除」. 横浜国立大学 理工 学部 数物・電子情報系学科 富井尚志. 探索木のオペレータ. 探索木 を探索する 探索木に節点を追加(挿入)する 探索木から節点を削除する 端点(葉: leaf )の削除 一つの子孫しか持たない節点の削除 二つの子孫を持つ接点の削除. 探索木のオペレータ. 探索木 を探索する 探索木に節点を追加(挿入)する 探索木から節点を削除する 端点(葉: leaf )の削除 一つの子孫しか持たない節点の削除 二つの子孫を持つ接点の削除. - PowerPoint PPT Presentation

TRANSCRIPT

Page 1: アルゴリズムとデータ構造 補足資料 13-3 「 2 分探索木からの節点の削除」

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

「 2 分探索木からの節点の削除」

横浜国立大学理工学部

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

Page 2: アルゴリズムとデータ構造 補足資料 13-3 「 2 分探索木からの節点の削除」

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

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

• 探索木から節点を削除する1. 端点(葉: leaf )の削除2. 一つの子孫しか持たない節点の削除3. 二つの子孫を持つ接点の削除

Page 3: アルゴリズムとデータ構造 補足資料 13-3 「 2 分探索木からの節点の削除」

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

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

• 探索木から節点を削除する1. 端点(葉: leaf )の削除2. 一つの子孫しか持たない節点の削除3. 二つの子孫を持つ接点の削除

Page 4: アルゴリズムとデータ構造 補足資料 13-3 「 2 分探索木からの節点の削除」

端点(葉: leaf )の削除main()

… root = delete(8, root);...

root

1NULL

NULL

4

3NULL

NULL

5NULL

NULL

6 NULL

2

8NULL

NULL

10NULL

NULL

9

7

※ メンバ count は省略

delete(8, root)

Page 5: アルゴリズムとデータ構造 補足資料 13-3 「 2 分探索木からの節点の削除」

端点(葉: leaf )の削除main()

… root = delete(8, root);...

root

1NULL

NULL

4

3NULL

NULL

5NULL

NULL

6 NULL

2

8NULL

NULL

10NULL

NULL

9

7

※ メンバ count は省略

if ( t == NULL ) printf(“ 見つかりませんでした \n”);else if ( x < t->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

Page 6: アルゴリズムとデータ構造 補足資料 13-3 「 2 分探索木からの節点の削除」

端点(葉: leaf )の削除main()

… root = delete(8, root);...

root

1NULL

NULL

4

3NULL

NULL

5NULL

NULL

6 NULL

2

8NULL

NULL

10NULL

NULL

9

7

※ メンバ count は省略

if ( t == NULL ) printf(“ 見つかりませんでした \n”);else if ( x < t->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

Page 7: アルゴリズムとデータ構造 補足資料 13-3 「 2 分探索木からの節点の削除」

端点(葉: leaf )の削除main()

… root = delete(8, root);...

root

1NULL

NULL

4

3NULL

NULL

5NULL

NULL

6 NULL

2

8NULL

NULL

10NULL

NULL

9

7

※ メンバ count は省略

if ( t == NULL ) printf(“ 見つかりませんでした \n”);else if ( x < t->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 < t->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 q

delete(8, t->right)

Page 8: アルゴリズムとデータ構造 補足資料 13-3 「 2 分探索木からの節点の削除」

端点(葉: leaf )の削除main()

… root = delete(8, root);...

root

1NULL

NULL

4

3NULL

NULL

5NULL

NULL

6 NULL

2

8NULL

NULL

10NULL

NULL

9

7

※ メンバ count は省略

if ( t == NULL ) printf(“ 見つかりませんでした \n”);else if ( x < t->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 < t->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 q

delete(8, t->right)

Page 9: アルゴリズムとデータ構造 補足資料 13-3 「 2 分探索木からの節点の削除」

端点(葉: leaf )の削除main()

… root = delete(8, root);...

root

1NULL

NULL

4

3NULL

NULL

5NULL

NULL

6 NULL

2

8NULL

NULL

10NULL

NULL

9

7

※ メンバ count は省略

if ( t == NULL ) printf(“ 見つかりませんでした \n”);else if ( x < t->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 < t->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 q

delete(8, t->right)

if ( t == NULL ) printf(“ 見つかりませんでした \n”);else if ( x < t->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 q

delete(8, t->right)

Page 10: アルゴリズムとデータ構造 補足資料 13-3 「 2 分探索木からの節点の削除」

端点(葉: leaf )の削除main()

… root = delete(8, root);...

root

1NULL

NULL

4

3NULL

NULL

5NULL

NULL

6 NULL

2

8NULL

NULL

10NULL

NULL

9

7

※ メンバ count は省略

if ( t == NULL ) printf(“ 見つかりませんでした \n”);else if ( x < t->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 < t->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 q

delete(8, t->right)

if ( t == NULL ) printf(“ 見つかりませんでした \n”);else if ( x < t->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 q

delete(8, t->right)

Page 11: アルゴリズムとデータ構造 補足資料 13-3 「 2 分探索木からの節点の削除」

端点(葉: leaf )の削除main()

… root = delete(8, root);...

root

1NULL

NULL

4

3NULL

NULL

5NULL

NULL

6 NULL

2

8NULL

NULL

10NULL

NULL

9

7

※ メンバ count は省略

if ( t == NULL ) printf(“ 見つかりませんでした \n”);else if ( x < t->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 < t->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 q

delete(8, t->right)

if ( t == NULL ) printf(“ 見つかりませんでした \n”);else if ( x < t->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 q

delete(8, t->right)

Page 12: アルゴリズムとデータ構造 補足資料 13-3 「 2 分探索木からの節点の削除」

端点(葉: leaf )の削除main()

… root = delete(8, root);...

root

1NULL

NULL

4

3NULL

NULL

5NULL

NULL

6 NULL

2

8NULL

NULL

10NULL

NULL

9

7

※ メンバ count は省略

if ( t == NULL ) printf(“ 見つかりませんでした \n”);else if ( x < t->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 < t->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 q

delete(8, t->right)

if ( t == NULL ) printf(“ 見つかりませんでした \n”);else if ( x < t->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 NULL q

delete(8, t->right)

Page 13: アルゴリズムとデータ構造 補足資料 13-3 「 2 分探索木からの節点の削除」

端点(葉: leaf )の削除main()

… root = delete(8, root);...

root

1NULL

NULL

4

3NULL

NULL

5NULL

NULL

6 NULL

2

8NULL

NULL

10NULL

NULL

9

7

※ メンバ count は省略

if ( t == NULL ) printf(“ 見つかりませんでした \n”);else if ( x < t->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 < t->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 q

delete(8, t->right)

if ( t == NULL ) printf(“ 見つかりませんでした \n”);else if ( x < t->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 NULL q

delete(8, t->right)

Page 14: アルゴリズムとデータ構造 補足資料 13-3 「 2 分探索木からの節点の削除」

端点(葉: leaf )の削除main()

… root = delete(8, root);...

root

1NULL

NULL

4

3NULL

NULL

5NULL

NULL

6 NULL

2

8NULL

NULL

10NULL

NULL

9

7

※ メンバ count は省略

if ( t == NULL ) printf(“ 見つかりませんでした \n”);else if ( x < t->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 < t->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 q

delete(8, t->right)

if ( t == NULL ) printf(“ 見つかりませんでした \n”);else if ( x < t->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 NULL q

delete(8, t->right)

Page 15: アルゴリズムとデータ構造 補足資料 13-3 「 2 分探索木からの節点の削除」

端点(葉: leaf )の削除main()

… root = delete(8, root);...

root

1NULL

NULL

4

3NULL

NULL

5NULL

NULL

6 NULL

2

8NULL

NULL

10NULL

NULL

9

7

※ メンバ count は省略

if ( t == NULL ) printf(“ 見つかりませんでした \n”);else if ( x < t->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 < t->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 q

delete(8, t->right)

if ( t == NULL ) printf(“ 見つかりませんでした \n”);else if ( x < t->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 NULL q

delete(8, t->right) NULL

Page 16: アルゴリズムとデータ構造 補足資料 13-3 「 2 分探索木からの節点の削除」

端点(葉: leaf )の削除main()

… root = delete(8, root);...

root

1NULL

NULL

4

3NULL

NULL

5NULL

NULL

6 NULL

2

10NULL

NULL

9NULL

7

※ メンバ count は省略

if ( t == NULL ) printf(“ 見つかりませんでした \n”);else if ( x < t->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 < t->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 q

delete(8, t->right) NULL

Page 17: アルゴリズムとデータ構造 補足資料 13-3 「 2 分探索木からの節点の削除」

端点(葉: leaf )の削除main()

… root = delete(8, root);...

root

1NULL

NULL

4

3NULL

NULL

5NULL

NULL

6 NULL

2

10NULL

NULL

9NULL

7

※ メンバ count は省略

if ( t == NULL ) printf(“ 見つかりませんでした \n”);else if ( x < t->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 < t->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 q

delete(8, t->right)

Page 18: アルゴリズムとデータ構造 補足資料 13-3 「 2 分探索木からの節点の削除」

端点(葉: leaf )の削除main()

… root = delete(8, root);...

root

1NULL

NULL

4

3NULL

NULL

5NULL

NULL

6 NULL

2

10NULL

NULL

9NULL

7

※ メンバ count は省略

if ( t == NULL ) printf(“ 見つかりませんでした \n”);else if ( x < t->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

Page 19: アルゴリズムとデータ構造 補足資料 13-3 「 2 分探索木からの節点の削除」

端点(葉: leaf )の削除main()

… root = delete(8, root);...

root

1NULL

NULL

4

3NULL

NULL

5NULL

NULL

6 NULL

2

10NULL

NULL

9NULL

7

※ メンバ count は省略

if ( t == NULL ) printf(“ 見つかりませんでした \n”);else if ( x < t->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

Page 20: アルゴリズムとデータ構造 補足資料 13-3 「 2 分探索木からの節点の削除」

端点(葉: leaf )の削除main()

… root = delete(8, root);...

root

1NULL

NULL

4

3NULL

NULL

5NULL

NULL

6 NULL

2

10NULL

NULL

9NULL

7

※ メンバ count は省略

Page 21: アルゴリズムとデータ構造 補足資料 13-3 「 2 分探索木からの節点の削除」

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

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

• 探索木から節点を削除する1. 端点(葉: leaf )の削除2. 一つの子孫しか持たない節点の削除3. 二つの子孫を持つ接点の削除

Page 22: アルゴリズムとデータ構造 補足資料 13-3 「 2 分探索木からの節点の削除」

一つの子孫しか持たない節点の削除main()

… root = delete(6, root);...

root

1NULL

NULL

4

3NULL

NULL

5NULL

NULL

6 NULL

2

8NULL

NULL

10NULL

NULL

9

7

※ メンバ count は省略

delete(6, root)

Page 23: アルゴリズムとデータ構造 補足資料 13-3 「 2 分探索木からの節点の削除」

一つの子孫しか持たない節点の削除main()

… root = delete(6, root);...

root

1NULL

NULL

4

3NULL

NULL

5NULL

NULL

2

8NULL

NULL

10NULL

NULL

9

7

※ メンバ count は省略

ゴールのイメージ

6 NULL

Page 24: アルゴリズムとデータ構造 補足資料 13-3 「 2 分探索木からの節点の削除」

一つの子孫しか持たない節点の削除main()

… root = delete(6, root);...

root

1NULL

NULL

4

3NULL

NULL

5NULL

NULL

6 NULL

2

8NULL

NULL

10NULL

NULL

9

7

※ メンバ count は省略

delete(6, root)

Page 25: アルゴリズムとデータ構造 補足資料 13-3 「 2 分探索木からの節点の削除」

一つの子孫しか持たない節点の削除main()

… root = delete(6, root);...

root

1NULL

NULL

4

3NULL

NULL

5NULL

NULL

6 NULL

2

8NULL

NULL

10NULL

NULL

9

7

※ メンバ count は省略

if ( t == NULL ) printf(“ 見つかりませんでした \n”);else if ( x < t->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 t q

delete(6, root)

Page 26: アルゴリズムとデータ構造 補足資料 13-3 「 2 分探索木からの節点の削除」

一つの子孫しか持たない節点の削除main()

… root = delete(6, root);...

root

1NULL

NULL

4

3NULL

NULL

5NULL

NULL

6 NULL

2

8NULL

NULL

10NULL

NULL

9

7

※ メンバ count は省略

if ( t == NULL ) printf(“ 見つかりませんでした \n”);else if ( x < t->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 t q

delete(6, root)

Page 27: アルゴリズムとデータ構造 補足資料 13-3 「 2 分探索木からの節点の削除」

一つの子孫しか持たない節点の削除main()

… root = delete(6, root);...

root

1NULL

NULL

4

3NULL

NULL

5NULL

NULL

6 NULL

2

8NULL

NULL

10NULL

NULL

9

7

※ メンバ count は省略

if ( t == NULL ) printf(“ 見つかりませんでした \n”);else if ( x < t->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 t q

delete(6, root)

if ( t == NULL ) printf(“ 見つかりませんでした \n”);else if ( x < t->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 t q

delete(6, t->left)

Page 28: アルゴリズムとデータ構造 補足資料 13-3 「 2 分探索木からの節点の削除」

一つの子孫しか持たない節点の削除main()

… root = delete(6, root);...

root

1NULL

NULL

4

3NULL

NULL

5NULL

NULL

6 NULL

2

8NULL

NULL

10NULL

NULL

9

7

※ メンバ count は省略

if ( t == NULL ) printf(“ 見つかりませんでした \n”);else if ( x < t->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 t q

delete(6, root)

if ( t == NULL ) printf(“ 見つかりませんでした \n”);else if ( x < t->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 t q

delete(6, t->left)

Page 29: アルゴリズムとデータ構造 補足資料 13-3 「 2 分探索木からの節点の削除」

一つの子孫しか持たない節点の削除main()

… root = delete(6, root);...

root

1NULL

NULL

4

3NULL

NULL

5NULL

NULL

6 NULL

2

8NULL

NULL

10NULL

NULL

9

7

※ メンバ count は省略

if ( t == NULL ) printf(“ 見つかりませんでした \n”);else if ( x < t->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 t q

delete(6, root)

if ( t == NULL ) printf(“ 見つかりませんでした \n”);else if ( x < t->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 t q

delete(6, t->left)

if ( t == NULL ) printf(“ 見つかりませんでした \n”);else if ( x < t->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 t q

delete(6, t->left)

Page 30: アルゴリズムとデータ構造 補足資料 13-3 「 2 分探索木からの節点の削除」

一つの子孫しか持たない節点の削除main()

… root = delete(6, root);...

root

1NULL

NULL

4

3NULL

NULL

5NULL

NULL

6 NULL

2

8NULL

NULL

10NULL

NULL

9

7

※ メンバ count は省略

if ( t == NULL ) printf(“ 見つかりませんでした \n”);else if ( x < t->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 t q

delete(6, root)

if ( t == NULL ) printf(“ 見つかりませんでした \n”);else if ( x < t->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 t q

delete(6, t->left)

if ( t == NULL ) printf(“ 見つかりませんでした \n”);else if ( x < t->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 t q

delete(6, t->left)

Page 31: アルゴリズムとデータ構造 補足資料 13-3 「 2 分探索木からの節点の削除」

一つの子孫しか持たない節点の削除main()

… root = delete(6, root);...

root

1NULL

NULL

4

3NULL

NULL

5NULL

NULL

6 NULL

2

8NULL

NULL

10NULL

NULL

9

7

※ メンバ count は省略

if ( t == NULL ) printf(“ 見つかりませんでした \n”);else if ( x < t->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 t q

delete(6, root)

if ( t == NULL ) printf(“ 見つかりませんでした \n”);else if ( x < t->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 t q

delete(6, t->left)

if ( t == NULL ) printf(“ 見つかりませんでした \n”);else if ( x < t->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 t q

delete(6, t->left)

Page 32: アルゴリズムとデータ構造 補足資料 13-3 「 2 分探索木からの節点の削除」

一つの子孫しか持たない節点の削除main()

… root = delete(6, root);...

root

1NULL

NULL

4

3NULL

NULL

5NULL

NULL

6 NULL

2

8NULL

NULL

10NULL

NULL

9

7

※ メンバ count は省略

if ( t == NULL ) printf(“ 見つかりませんでした \n”);else if ( x < t->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 t q

delete(6, root)

if ( t == NULL ) printf(“ 見つかりませんでした \n”);else if ( x < t->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 t q

delete(6, t->left)

if ( t == NULL ) printf(“ 見つかりませんでした \n”);else if ( x < t->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 t q

delete(6, t->left)

Page 33: アルゴリズムとデータ構造 補足資料 13-3 「 2 分探索木からの節点の削除」

一つの子孫しか持たない節点の削除main()

… root = delete(6, root);...

root

1NULL

NULL

4

3NULL

NULL

5NULL

NULL

6 NULL

2

8NULL

NULL

10NULL

NULL

9

7

※ メンバ count は省略

if ( t == NULL ) printf(“ 見つかりませんでした \n”);else if ( x < t->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 t q

delete(6, root)

if ( t == NULL ) printf(“ 見つかりませんでした \n”);else if ( x < t->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 t q

delete(6, t->left)

if ( t == NULL ) printf(“ 見つかりませんでした \n”);else if ( x < t->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 t q

delete(6, t->left)

Page 34: アルゴリズムとデータ構造 補足資料 13-3 「 2 分探索木からの節点の削除」

一つの子孫しか持たない節点の削除main()

… root = delete(6, root);...

root

1NULL

NULL

4

3NULL

NULL

5NULL

NULL

6 NULL

2

8NULL

NULL

10NULL

NULL

9

7

※ メンバ count は省略

if ( t == NULL ) printf(“ 見つかりませんでした \n”);else if ( x < t->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 t q

delete(6, root)

if ( t == NULL ) printf(“ 見つかりませんでした \n”);else if ( x < t->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 t q

delete(6, t->left)

if ( t == NULL ) printf(“ 見つかりませんでした \n”);else if ( x < t->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 t q

delete(6, t->left)

Page 35: アルゴリズムとデータ構造 補足資料 13-3 「 2 分探索木からの節点の削除」

一つの子孫しか持たない節点の削除main()

… root = delete(6, root);...

root

1NULL

NULL

4

3NULL

NULL

5NULL

NULL

6 NULL

2

8NULL

NULL

10NULL

NULL

9

7

※ メンバ count は省略

if ( t == NULL ) printf(“ 見つかりませんでした \n”);else if ( x < t->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 t q

delete(6, root)

if ( t == NULL ) printf(“ 見つかりませんでした \n”);else if ( x < t->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 t q

delete(6, t->left)

Page 36: アルゴリズムとデータ構造 補足資料 13-3 「 2 分探索木からの節点の削除」

一つの子孫しか持たない節点の削除main()

… root = delete(6, root);...

root

1NULL

NULL

4

3NULL

NULL

5NULL

NULL

2

8NULL

NULL

10NULL

NULL

9

7

※ メンバ count は省略

if ( t == NULL ) printf(“ 見つかりませんでした \n”);else if ( x < t->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 t q

delete(6, root)

if ( t == NULL ) printf(“ 見つかりませんでした \n”);else if ( x < t->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 t q

delete(6, t->left)

Page 37: アルゴリズムとデータ構造 補足資料 13-3 「 2 分探索木からの節点の削除」

一つの子孫しか持たない節点の削除main()

… root = delete(6, root);...

root

1NULL

NULL

4

3NULL

NULL

5NULL

NULL

2

8NULL

NULL

10NULL

NULL

9

7

※ メンバ count は省略

if ( t == NULL ) printf(“ 見つかりませんでした \n”);else if ( x < t->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 t q

delete(6, root)

if ( t == NULL ) printf(“ 見つかりませんでした \n”);else if ( x < t->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 t q

delete(6, t->left)

Page 38: アルゴリズムとデータ構造 補足資料 13-3 「 2 分探索木からの節点の削除」

一つの子孫しか持たない節点の削除main()

… root = delete(6, root);...

root

1NULL

NULL

4

3NULL

NULL

5NULL

NULL

2

8NULL

NULL

10NULL

NULL

9

7

※ メンバ count は省略

if ( t == NULL ) printf(“ 見つかりませんでした \n”);else if ( x < t->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 t q

delete(6, root)

Page 39: アルゴリズムとデータ構造 補足資料 13-3 「 2 分探索木からの節点の削除」

一つの子孫しか持たない節点の削除main()

… root = delete(6, root);...

root

1NULL

NULL

4

3NULL

NULL

5NULL

NULL

2

8NULL

NULL

10NULL

NULL

9

7

※ メンバ count は省略

if ( t == NULL ) printf(“ 見つかりませんでした \n”);else if ( x < t->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 t q

delete(6, root)

Page 40: アルゴリズムとデータ構造 補足資料 13-3 「 2 分探索木からの節点の削除」

一つの子孫しか持たない節点の削除main()

… root = delete(6, root);...

root

1NULL

NULL

4

3NULL

NULL

5NULL

NULL

2

8NULL

NULL

10NULL

NULL

9

7

※ メンバ count は省略

Page 41: アルゴリズムとデータ構造 補足資料 13-3 「 2 分探索木からの節点の削除」

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

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

• 探索木から節点を削除する1. 端点(葉: leaf )の削除2. 一つの子孫しか持たない節点の削除3. 二つの子孫を持つ接点の削除

Page 42: アルゴリズムとデータ構造 補足資料 13-3 「 2 分探索木からの節点の削除」

二つの子孫を持つ接点の削除main()

… root = delete(7, root);...

root

1NULL

NULL

4

3NULL

NULL

5NULL

NULL

6 NULL

2

8NULL

NULL

10NULL

NULL

9

7

※ メンバ count は省略

delete(7, root)

Page 43: アルゴリズムとデータ構造 補足資料 13-3 「 2 分探索木からの節点の削除」

二つの子孫を持つ接点の削除main()

… root = delete(7, root);...

root

1NULL

NULL

4

3NULL

NULL

5NULL

NULL

2

8NULL

NULL

10NULL

NULL

9

6

※ メンバ count は省略

6 NULL

値を

コピ

左部分木の最右要素で置き換える

ゴールのイメージ

Page 44: アルゴリズムとデータ構造 補足資料 13-3 「 2 分探索木からの節点の削除」

二つの子孫を持つ接点の削除main()

… root = delete(7, root);...

root

1NULL

NULL

4

3NULL

NULL

5NULL

NULL

6 NULL

2

8NULL

NULL

10NULL

NULL

9

7

※ メンバ count は省略

delete(7, root)

Page 45: アルゴリズムとデータ構造 補足資料 13-3 「 2 分探索木からの節点の削除」

二つの子孫を持つ接点の削除main()

… root = delete(7, root);...

root

1NULL

NULL

4

3NULL

NULL

5NULL

NULL

6 NULL

2

8NULL

NULL

10NULL

NULL

9

7

※ メンバ count は省略

if ( t == NULL ) printf(“ 見つかりませんでした \n”);else if ( x < t->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 t q

delete(7, root)

Page 46: アルゴリズムとデータ構造 補足資料 13-3 「 2 分探索木からの節点の削除」

二つの子孫を持つ接点の削除main()

… root = delete(7, root);...

root

1NULL

NULL

4

3NULL

NULL

5NULL

NULL

6 NULL

2

8NULL

NULL

10NULL

NULL

9

7

※ メンバ count は省略

if ( t == NULL ) printf(“ 見つかりませんでした \n”);else if ( x < t->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 t q

delete(7, root)

Page 47: アルゴリズムとデータ構造 補足資料 13-3 「 2 分探索木からの節点の削除」

二つの子孫を持つ接点の削除main()

… root = delete(7, root);...

root

1NULL

NULL

4

3NULL

NULL

5NULL

NULL

6 NULL

2

8NULL

NULL

10NULL

NULL

9

7

※ メンバ count は省略

if ( t == NULL ) printf(“ 見つかりませんでした \n”);else if ( x < t->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 t q

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

Page 48: アルゴリズムとデータ構造 補足資料 13-3 「 2 分探索木からの節点の削除」

二つの子孫を持つ接点の削除main()

… root = delete(7, root);...

root

1NULL

NULL

4

3NULL

NULL

5NULL

NULL

6 NULL

2

8NULL

NULL

10NULL

NULL

9

7

※ メンバ count は省略

if ( t == NULL ) printf(“ 見つかりませんでした \n”);else if ( x < t->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 t q

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

Page 49: アルゴリズムとデータ構造 補足資料 13-3 「 2 分探索木からの節点の削除」

二つの子孫を持つ接点の削除main()

… root = delete(7, root);...

root

1NULL

NULL

4

3NULL

NULL

5NULL

NULL

6 NULL

2

8NULL

NULL

10NULL

NULL

9

7

※ メンバ count は省略

if ( t == NULL ) printf(“ 見つかりませんでした \n”);else if ( x < t->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 t q

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

Page 50: アルゴリズムとデータ構造 補足資料 13-3 「 2 分探索木からの節点の削除」

二つの子孫を持つ接点の削除main()

… root = delete(7, root);...

root

1NULL

NULL

4

3NULL

NULL

5NULL

NULL

6 NULL

2

8NULL

NULL

10NULL

NULL

9

7

※ メンバ count は省略

if ( t == NULL ) printf(“ 見つかりませんでした \n”);else if ( x < t->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 t q

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

値を

コピ

Page 51: アルゴリズムとデータ構造 補足資料 13-3 「 2 分探索木からの節点の削除」

二つの子孫を持つ接点の削除main()

… root = delete(7, root);...

root

1NULL

NULL

4

3NULL

NULL

5NULL

NULL

6 NULL

2

8NULL

NULL

10NULL

NULL

9

6

※ メンバ count は省略

if ( t == NULL ) printf(“ 見つかりませんでした \n”);else if ( x < t->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 t q

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

値を

コピ

Page 52: アルゴリズムとデータ構造 補足資料 13-3 「 2 分探索木からの節点の削除」

二つの子孫を持つ接点の削除main()

… root = delete(7, root);...

root

1NULL

NULL

4

3NULL

NULL

5NULL

NULL

6 NULL

2

8NULL

NULL

10NULL

NULL

9

6

※ メンバ count は省略

if ( t == NULL ) printf(“ 見つかりませんでした \n”);else if ( x < t->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 t q

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

Page 53: アルゴリズムとデータ構造 補足資料 13-3 「 2 分探索木からの節点の削除」

二つの子孫を持つ接点の削除main()

… root = delete(7, root);...

root

1NULL

NULL

4

3NULL

NULL

5NULL

NULL

6 NULL

2

8NULL

NULL

10NULL

NULL

9

6

※ メンバ count は省略

if ( t == NULL ) printf(“ 見つかりませんでした \n”);else if ( x < t->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 t q

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

Page 54: アルゴリズムとデータ構造 補足資料 13-3 「 2 分探索木からの節点の削除」

二つの子孫を持つ接点の削除main()

… root = delete(7, root);...

root

1NULL

NULL

4

3NULL

NULL

5NULL

NULL

6 NULL

2

8NULL

NULL

10NULL

NULL

9

6

※ メンバ count は省略

if ( t == NULL ) printf(“ 見つかりませんでした \n”);else if ( x < t->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 t q

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

Page 55: アルゴリズムとデータ構造 補足資料 13-3 「 2 分探索木からの節点の削除」

二つの子孫を持つ接点の削除main()

… root = delete(7, root);...

root

1NULL

NULL

4

3NULL

NULL

5NULL

NULL

6 NULL

2

8NULL

NULL

10NULL

NULL

9

6

※ メンバ count は省略

if ( t == NULL ) printf(“ 見つかりませんでした \n”);else if ( x < t->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 t q

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

Page 56: アルゴリズムとデータ構造 補足資料 13-3 「 2 分探索木からの節点の削除」

二つの子孫を持つ接点の削除main()

… root = delete(7, root);...

root

1NULL

NULL

4

3NULL

NULL

5NULL

NULL

6 NULL

2

8NULL

NULL

10NULL

NULL

9

6

※ メンバ count は省略

if ( t == NULL ) printf(“ 見つかりませんでした \n”);else if ( x < t->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 t q

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

Page 57: アルゴリズムとデータ構造 補足資料 13-3 「 2 分探索木からの節点の削除」

二つの子孫を持つ接点の削除main()

… root = delete(7, root);...

root

1NULL

NULL

4

3NULL

NULL

5NULL

NULL

2

8NULL

NULL

10NULL

NULL

9

6

※ メンバ count は省略

if ( t == NULL ) printf(“ 見つかりませんでした \n”);else if ( x < t->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 t q

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

Page 58: アルゴリズムとデータ構造 補足資料 13-3 「 2 分探索木からの節点の削除」

二つの子孫を持つ接点の削除main()

… root = delete(7, root);...

root

1NULL

NULL

4

3NULL

NULL

5NULL

NULL

2

8NULL

NULL

10NULL

NULL

9

6

※ メンバ count は省略

if ( t == NULL ) printf(“ 見つかりませんでした \n”);else if ( x < t->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 t q

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

Page 59: アルゴリズムとデータ構造 補足資料 13-3 「 2 分探索木からの節点の削除」

二つの子孫を持つ接点の削除main()

… root = delete(7, root);...

root

1NULL

NULL

4

3NULL

NULL

5NULL

NULL

2

8NULL

NULL

10NULL

NULL

9

6

※ メンバ count は省略

if ( t == NULL ) printf(“ 見つかりませんでした \n”);else if ( x < t->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 t q

delete(7, root)

Page 60: アルゴリズムとデータ構造 補足資料 13-3 「 2 分探索木からの節点の削除」

二つの子孫を持つ接点の削除main()

… root = delete(7, root);...

root

1NULL

NULL

4

3NULL

NULL

5NULL

NULL

2

8NULL

NULL

10NULL

NULL

9

6

※ メンバ count は省略

if ( t == NULL ) printf(“ 見つかりませんでした \n”);else if ( x < t->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 t q

delete(7, root)

Page 61: アルゴリズムとデータ構造 補足資料 13-3 「 2 分探索木からの節点の削除」

二つの子孫を持つ接点の削除main()

… root = delete(7, root);...

root

1NULL

NULL

4

3NULL

NULL

5NULL

NULL

2

8NULL

NULL

10NULL

NULL

9

6

※ メンバ count は省略

if ( t == NULL ) printf(“ 見つかりませんでした \n”);else if ( x < t->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 t q

delete(7, root)

Page 62: アルゴリズムとデータ構造 補足資料 13-3 「 2 分探索木からの節点の削除」

二つの子孫を持つ接点の削除main()

… root = delete(7, root);...

root

1NULL

NULL

4

3NULL

NULL

5NULL

NULL

2

8NULL

NULL

10NULL

NULL

9

6

※ メンバ count は省略