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

Post on 18-Jan-2016

57 Views

Category:

Documents

0 Downloads

Preview:

Click to see full reader

DESCRIPTION

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

TRANSCRIPT

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

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

横浜国立大学理工学部

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

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

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

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

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

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

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

端点(葉: 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)

端点(葉: 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

端点(葉: 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

端点(葉: 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)

端点(葉: 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)

端点(葉: 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)

端点(葉: 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)

端点(葉: 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)

端点(葉: 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)

端点(葉: 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)

端点(葉: 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)

端点(葉: 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

端点(葉: 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

端点(葉: 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)

端点(葉: 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

端点(葉: 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

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

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

root

1NULL

NULL

4

3NULL

NULL

5NULL

NULL

6 NULL

2

10NULL

NULL

9NULL

7

※ メンバ count は省略

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

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

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

一つの子孫しか持たない節点の削除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)

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

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

root

1NULL

NULL

4

3NULL

NULL

5NULL

NULL

2

8NULL

NULL

10NULL

NULL

9

7

※ メンバ count は省略

ゴールのイメージ

6 NULL

一つの子孫しか持たない節点の削除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)

一つの子孫しか持たない節点の削除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)

一つの子孫しか持たない節点の削除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)

一つの子孫しか持たない節点の削除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)

一つの子孫しか持たない節点の削除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)

一つの子孫しか持たない節点の削除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)

一つの子孫しか持たない節点の削除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)

一つの子孫しか持たない節点の削除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)

一つの子孫しか持たない節点の削除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)

一つの子孫しか持たない節点の削除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)

一つの子孫しか持たない節点の削除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)

一つの子孫しか持たない節点の削除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)

一つの子孫しか持たない節点の削除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)

一つの子孫しか持たない節点の削除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)

一つの子孫しか持たない節点の削除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)

一つの子孫しか持たない節点の削除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)

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

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

root

1NULL

NULL

4

3NULL

NULL

5NULL

NULL

2

8NULL

NULL

10NULL

NULL

9

7

※ メンバ count は省略

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

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

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

二つの子孫を持つ接点の削除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)

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

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

root

1NULL

NULL

4

3NULL

NULL

5NULL

NULL

2

8NULL

NULL

10NULL

NULL

9

6

※ メンバ count は省略

6 NULL

値を

コピ

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

ゴールのイメージ

二つの子孫を持つ接点の削除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)

二つの子孫を持つ接点の削除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)

二つの子孫を持つ接点の削除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)

二つの子孫を持つ接点の削除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

二つの子孫を持つ接点の削除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

二つの子孫を持つ接点の削除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

二つの子孫を持つ接点の削除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

値を

コピ

二つの子孫を持つ接点の削除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

値を

コピ

二つの子孫を持つ接点の削除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

二つの子孫を持つ接点の削除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

二つの子孫を持つ接点の削除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

二つの子孫を持つ接点の削除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

二つの子孫を持つ接点の削除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

二つの子孫を持つ接点の削除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

二つの子孫を持つ接点の削除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

二つの子孫を持つ接点の削除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)

二つの子孫を持つ接点の削除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)

二つの子孫を持つ接点の削除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)

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

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

root

1NULL

NULL

4

3NULL

NULL

5NULL

NULL

2

8NULL

NULL

10NULL

NULL

9

6

※ メンバ count は省略

top related