Release reviewed gh-pages candidate

This commit is contained in:
krahets
2026-11-18 16:34:49 +08:00
parent 8966617032
commit 451a6b692c
822 changed files with 3585 additions and 3541 deletions

View File

@@ -1929,14 +1929,14 @@
<h2 id="731">7.3.1   パーフェクト二分木の表現<a class="headerlink" href="#731" title="この見出しへのリンク"></a></h2>
<p>まず単純な場合を考えます。パーフェクト二分木の全ノードをレベル順に配列へ格納すると、各ノードが一意のインデックスに対応します。</p>
<p>レベル順走査の特徴から、親と子のインデックスの対応式を導けます。<strong>ノードのインデックスが <span class="arithmatex">\(i\)</span> なら、左の子は <span class="arithmatex">\(2i + 1\)</span>、右の子は <span class="arithmatex">\(2i + 2\)</span> です</strong>。図 7-12 に対応関係を示します。</p>
<p><ha-svg-figure class="animation-figure" data-book-key="ja/chapter_tree/array_representation_of_tree.assets/array_representation_binary_tree.png" data-dark-src="/assets/figure-svg/figures/ce454349d63aba71b7fc636c.json" data-src="/assets/figure-svg/figures/4320813b99decc05f097ddc5.json" style=";--figure-ratio:960.0/540.0"><object data="/ja/chapter_tree/array_representation_of_tree.assets/array_representation_binary_tree.svg" data-theme="light" tabindex="-1" type="image/svg+xml">パーフェクト二分木の配列表現</object><object data="/ja/chapter_tree/array_representation_of_tree.assets/array_representation_binary_tree.dark.svg" data-theme="dark" tabindex="-1" type="image/svg+xml">パーフェクト二分木の配列表現</object></ha-svg-figure></p>
<p align="center"> 図 7-12   パーフェクト二分木の配列表現 </p>
<p><ha-svg-figure aria-label="パーフェクト二分木の配列表現" class="animation-figure" data-book-key="ja/chapter_tree/array_representation_of_tree.assets/array_representation_binary_tree.png" data-dark-src="/assets/figure-svg/figures/ce454349d63aba71b7fc636c.json" data-figure-accessible="" data-src="/assets/figure-svg/figures/4320813b99decc05f097ddc5.json" role="img" style=";--figure-ratio:960.0/540.0"><object aria-hidden="true" data="/ja/chapter_tree/array_representation_of_tree.assets/array_representation_binary_tree.svg" data-theme="light" inert="" tabindex="-1" type="image/svg+xml">パーフェクト二分木の配列表現</object><object aria-hidden="true" data="/ja/chapter_tree/array_representation_of_tree.assets/array_representation_binary_tree.dark.svg" data-theme="dark" inert="" tabindex="-1" type="image/svg+xml">パーフェクト二分木の配列表現</object></ha-svg-figure></p>
<p align="center"> 図 7-12   パーフェクト二分木の配列表現 </p><div class="ha-sr-only figure-description"><h3>図解 7-12:パーフェクト二分木の配列表現</h3><p>パーフェクト二分木をレベル順に 0 始まりの配列に格納し、2i+1 と 2i+2 で左右の子のインデックスを求めます。</p><p>4 層のパーフェクト二分木に 15 個のノードがあります。レベル順の値は 1~15 で、第 1 層の根が 1、第 2 層が 2、3、第 3 層が 4~7、第 4 層の葉が 8~15 です。これらを配列のインデックス 0~14 に順に格納します。表に全ノードのインデックスと左右の子の位置を示します。</p><table><caption>各ノードのインデックスと左右の子の位置</caption><thead><tr><th scope="col">現在のインデックス</th><th scope="col">ノードの値</th><th scope="col">左の子のインデックスと値</th><th scope="col">右の子のインデックスと値</th></tr></thead><tbody><tr><th scope="row">0</th><td>1</td><td>1:2</td><td>2:3</td></tr><tr><th scope="row">1</th><td>2</td><td>3:4</td><td>4:5</td></tr><tr><th scope="row">2</th><td>3</td><td>5:6</td><td>6:7</td></tr><tr><th scope="row">3</th><td>4</td><td>7:8</td><td>8:9</td></tr><tr><th scope="row">4</th><td>5</td><td>9:10</td><td>10:11</td></tr><tr><th scope="row">5</th><td>6</td><td>11:12</td><td>12:13</td></tr><tr><th scope="row">6</th><td>7</td><td>13:14</td><td>14:15</td></tr><tr><th scope="row">7</th><td>8</td><td>15:範囲外、子なし</td><td>16:範囲外、子なし</td></tr><tr><th scope="row">8</th><td>9</td><td>17:範囲外、子なし</td><td>18:範囲外、子なし</td></tr><tr><th scope="row">9</th><td>10</td><td>19:範囲外、子なし</td><td>20:範囲外、子なし</td></tr><tr><th scope="row">10</th><td>11</td><td>21:範囲外、子なし</td><td>22:範囲外、子なし</td></tr><tr><th scope="row">11</th><td>12</td><td>23:範囲外、子なし</td><td>24:範囲外、子なし</td></tr><tr><th scope="row">12</th><td>13</td><td>25:範囲外、子なし</td><td>26:範囲外、子なし</td></tr><tr><th scope="row">13</th><td>14</td><td>27:範囲外、子なし</td><td>28:範囲外、子なし</td></tr><tr><th scope="row">14</th><td>15</td><td>29:範囲外、子なし</td><td>30:範囲外、子なし</td></tr></tbody></table><p>式に代入する i はノードの値ではなく配列のインデックスです。たとえばノード 6 はインデックス 5 にあり、左の子はインデックス 11(値 12)、右の子は 12(値 13)です。</p><p>子へのポインタの代わりに式で位置を求められますが、アクセス前に配列の範囲内かを確認します。インデックス 7~14(ノード 8~15)は葉です。計算した子の位置 15~30 はすべて範囲外で、子はありません。</p></div>
<p><strong>この対応式は、連結リストのノード参照(ポインタ)と同じ役割を果たします</strong>。配列内のどのノードからでも、式を使って左右の子にアクセスできます。</p>
<h2 id="732">7.3.2   任意の二分木の表現<a class="headerlink" href="#732" title="この見出しへのリンク"></a></h2>
<p>パーフェクト二分木は特殊な場合です。一般の二分木では、中間の層にも多くの <code>None</code> があります。レベル順の列にはそれらが含まれないため、その列だけでは <code>None</code> の数や位置を推測できません。<strong>つまり、同じレベル順の列に対応する二分木の構造が複数あります</strong>。</p>
<p>図 7-13 のように、パーフェクト二分木でない木には、上の配列表現をそのまま使えません。</p>
<p><ha-svg-figure class="animation-figure" data-book-key="ja/chapter_tree/array_representation_of_tree.assets/array_representation_without_empty.png" data-dark-src="/assets/figure-svg/figures/252de8e31444aea78006799f.json" data-src="/assets/figure-svg/figures/e479908caafb3038fb4ce8cf.json" style=";--figure-ratio:960.0/540.0"><object data="/ja/chapter_tree/array_representation_of_tree.assets/array_representation_without_empty.svg" data-theme="light" tabindex="-1" type="image/svg+xml">同じレベル順の列に対応する複数の二分木</object><object data="/ja/chapter_tree/array_representation_of_tree.assets/array_representation_without_empty.dark.svg" data-theme="dark" tabindex="-1" type="image/svg+xml">同じレベル順の列に対応する複数の二分木</object></ha-svg-figure></p>
<p align="center"> 図 7-13   同じレベル順の列に対応する複数の二分木 </p>
<p><ha-svg-figure aria-label="途中の空き位置を省略したレベル順配列" class="animation-figure" data-book-key="ja/chapter_tree/array_representation_of_tree.assets/array_representation_without_empty.png" data-dark-src="/assets/figure-svg/figures/252de8e31444aea78006799f.json" data-figure-accessible="" data-src="/assets/figure-svg/figures/e479908caafb3038fb4ce8cf.json" role="img" style=";--figure-ratio:960.0/540.0"><object aria-hidden="true" data="/ja/chapter_tree/array_representation_of_tree.assets/array_representation_without_empty.svg" data-theme="light" inert="" tabindex="-1" type="image/svg+xml">同じレベル順の列に対応する複数の二分木</object><object aria-hidden="true" data="/ja/chapter_tree/array_representation_of_tree.assets/array_representation_without_empty.dark.svg" data-theme="dark" inert="" tabindex="-1" type="image/svg+xml">同じレベル順の列に対応する複数の二分木</object></ha-svg-figure></p>
<p align="center"> 図 7-13   同じレベル順の列に対応する複数の二分木 </p><div class="ha-sr-only figure-description"><h3>図解 7-13:途中の空き位置を省略したレベル順配列</h3><p>木の途中の空き位置を飛ばしてレベル順の値を詰めると、インデックスがずれます。同じ値の列が異なる形の木に対応しえます。</p><p>表の「元の木での位置」は、空き位置を残してレベル順に 0 から数えた番号で、根は位置 0 です。「詰めた配列のインデックス」は空きを飛ばした後の位置です。図に描かれているのは次に述べる元の木だけです。</p><p>元の木には 10 個のノードがあります。根 1(位置 0)の左右の子は 2(位置 1)と 3(位置 2)です。2 の子は左の 4(位置 3)だけです。3 の左右の子は 6(位置 5)と 7(位置 6)、4 の左右の子は 8(位置 7)と 9(位置 8)です。6 の子は左の 12(位置 11)だけ、7 の子は右の 15(位置 14)だけです。8、9、12、15 は葉です。詰めた後のインデックス 0~9 と元の位置の対応を表に示します。</p><table><caption>詰めた配列と元の木での位置の対応</caption><thead><tr><th scope="col">詰めた配列のインデックス</th><th scope="col">ノードの値</th><th scope="col">元の木での位置</th></tr></thead><tbody><tr><th scope="row">0</th><td>1</td><td>0</td></tr><tr><th scope="row">1</th><td>2</td><td>1</td></tr><tr><th scope="row">2</th><td>3</td><td>2</td></tr><tr><th scope="row">3</th><td>4</td><td>3</td></tr><tr><th scope="row">4</th><td>6</td><td>5</td></tr><tr><th scope="row">5</th><td>7</td><td>6</td></tr><tr><th scope="row">6</th><td>8</td><td>7</td></tr><tr><th scope="row">7</th><td>9</td><td>8</td></tr><tr><th scope="row">8</th><td>12</td><td>11</td></tr><tr><th scope="row">9</th><td>15</td><td>14</td></tr></tbody></table><p>元の木では位置 4、9、10、12、13 が空いています。これらを飛ばして [1, 2, 3, 4, 6, 7, 8, 9, 12, 15] に詰めると、ノード 6 は元の位置 5 からインデックス 4 に移ります。ここで 2i+2 を使うと、6 はインデックス 1 のノード 2 の右の子になります。元の木では 2 に右の子はなく、6 は 3 の左の子です。詰めた配列に式を当てはめて木を復元すると、1 の左右の子は 2 と 3、2 の左右の子は 4 と 6、3 の左右の子は 7 と 8、4 の左右の子は 9 と 12、6 の子は左の 15 だけで、残りは葉です。この木は親子関係が違ってもレベル順の値は元の木と同じです。この比較用の木は式から求めたもので、図には描かれていません。</p><p>途中の空きを省くと、異なる親子関係の木でもレベル順の値が同じになることがあります。これは途中に空きがある場合の問題です。完全二分木の空きはすべてレベル順の末尾にあり、その省略で対応は崩れません。</p></div>
<p>解決するには、<strong>レベル順の列にすべての <code>None</code> を明示的に書き込みます</strong>。図 7-14 のように、これで二分木を一意に表せます。コード例を示します。</p>
<div class="tabbed-set tabbed-alternate" data-tabs="1:13"><input checked="checked" id="__tabbed_1_1" name="__tabbed_1" type="radio"/><input id="__tabbed_1_2" name="__tabbed_1" type="radio"/><input id="__tabbed_1_3" name="__tabbed_1" type="radio"/><input id="__tabbed_1_4" name="__tabbed_1" type="radio"/><input id="__tabbed_1_5" name="__tabbed_1" type="radio"/><input id="__tabbed_1_6" name="__tabbed_1" type="radio"/><input id="__tabbed_1_7" name="__tabbed_1" type="radio"/><input id="__tabbed_1_8" name="__tabbed_1" type="radio"/><input id="__tabbed_1_9" name="__tabbed_1" type="radio"/><input id="__tabbed_1_10" name="__tabbed_1" type="radio"/><input id="__tabbed_1_11" name="__tabbed_1" type="radio"/><input id="__tabbed_1_12" name="__tabbed_1" type="radio"/><input id="__tabbed_1_13" name="__tabbed_1" type="radio"/><div class="tabbed-labels"><label for="__tabbed_1_1">Python</label><label for="__tabbed_1_2">C++</label><label for="__tabbed_1_3">Java</label><label for="__tabbed_1_4">C#</label><label for="__tabbed_1_5">Go</label><label for="__tabbed_1_6">Swift</label><label for="__tabbed_1_7">JS</label><label for="__tabbed_1_8">TS</label><label for="__tabbed_1_9">Dart</label><label for="__tabbed_1_10">Rust</label><label for="__tabbed_1_11">C</label><label for="__tabbed_1_12">Kotlin</label><label for="__tabbed_1_13">Ruby</label></div>
<div class="tabbed-content">
@@ -2020,12 +2020,12 @@
</div>
</div>
</div>
<p><ha-svg-figure class="animation-figure" data-book-key="ja/chapter_tree/array_representation_of_tree.assets/array_representation_with_empty.png" data-dark-src="/assets/figure-svg/figures/cc77c7c6d2ec1e5c6c93df2e.json" data-src="/assets/figure-svg/figures/469a2875cc6f5ab4d91a967f.json" style=";--figure-ratio:960.0/540.0"><object data="/ja/chapter_tree/array_representation_of_tree.assets/array_representation_with_empty.svg" data-theme="light" tabindex="-1" type="image/svg+xml">任意の二分木の配列表現</object><object data="/ja/chapter_tree/array_representation_of_tree.assets/array_representation_with_empty.dark.svg" data-theme="dark" tabindex="-1" type="image/svg+xml">任意の二分木の配列表現</object></ha-svg-figure></p>
<p align="center"> 図 7-14   任意の二分木の配列表現 </p>
<p><ha-svg-figure aria-label="空き位置を残した二分木の配列" class="animation-figure" data-book-key="ja/chapter_tree/array_representation_of_tree.assets/array_representation_with_empty.png" data-dark-src="/assets/figure-svg/figures/cc77c7c6d2ec1e5c6c93df2e.json" data-figure-accessible="" data-src="/assets/figure-svg/figures/469a2875cc6f5ab4d91a967f.json" role="img" style=";--figure-ratio:960.0/540.0"><object aria-hidden="true" data="/ja/chapter_tree/array_representation_of_tree.assets/array_representation_with_empty.svg" data-theme="light" inert="" tabindex="-1" type="image/svg+xml">任意の二分木の配列表現</object><object aria-hidden="true" data="/ja/chapter_tree/array_representation_of_tree.assets/array_representation_with_empty.dark.svg" data-theme="dark" inert="" tabindex="-1" type="image/svg+xml">任意の二分木の配列表現</object></ha-svg-figure></p>
<p align="center"> 図 7-14   任意の二分木の配列表現 </p><div class="ha-sr-only figure-description"><h3>図解 7-14:空き位置を残した二分木の配列</h3><p>レベル順の配列で途中に欠けたノードの位置を残すと、2i+1、2i+2 の対応から木の形を一意に復元できます。</p><p>実在するノードは 10 個です。根 1 の左右の子は 2 と 3、2 には左の子 4 だけがあります。3 の左右の子は 6 と 7、4 の左右の子は 8 と 9 です。6 には左の子 12 だけ、7 には右の子 15 だけがあり、8、9、12、15 は葉です。配列はインデックス 0~14 の 15 マスで、10 個の値と、インデックス 4、9、10、12、13 の 5 個の空き位置を格納します。各マスの内容は表に示します。</p><table><caption>配列のインデックスとノードの値</caption><thead><tr><th scope="col">インデックス</th><th scope="col">ノードの値または空き位置</th></tr></thead><tbody><tr><th scope="row">0</th><td>1</td></tr><tr><th scope="row">1</th><td>2</td></tr><tr><th scope="row">2</th><td>3</td></tr><tr><th scope="row">3</th><td>4</td></tr><tr><th scope="row">4</th><td>空き</td></tr><tr><th scope="row">5</th><td>6</td></tr><tr><th scope="row">6</th><td>7</td></tr><tr><th scope="row">7</th><td>8</td></tr><tr><th scope="row">8</th><td>9</td></tr><tr><th scope="row">9</th><td>空き</td></tr><tr><th scope="row">10</th><td>空き</td></tr><tr><th scope="row">11</th><td>12</td></tr><tr><th scope="row">12</th><td>空き</td></tr><tr><th scope="row">13</th><td>空き</td></tr><tr><th scope="row">14</th><td>15</td></tr></tbody></table><p>インデックス 4 にノードがないときは、次の層でその子に当たる 9、10 も空けておきます。そうしないと後ろのノードが前にずれます。たとえばインデックス 5 のノード 6 は、左の子の位置 11 に 12 があり、右の子の位置 12 は空です。</p><p>途中の空き位置を残せば、インデックス i の子の位置 2i+1 と 2i+2 からこの木を一意に復元できます。空き位置はノードが存在しないことを表し、コードでは None や null などで表します。値 0 とは区別します。</p></div>
<p><strong>完全二分木は、配列で表すのに非常に適しています</strong>。定義を思い出すと、<code>None</code> は最下層の右側にしかありません。<strong>したがって、すべての <code>None</code> はレベル順の列の末尾に現れます</strong>。</p>
<p>つまり、完全二分木を配列で表す場合は、<code>None</code> の格納をすべて省けるので便利です。図 7-15 に例を示します。</p>
<p><ha-svg-figure class="animation-figure" data-book-key="ja/chapter_tree/array_representation_of_tree.assets/array_representation_complete_binary_tree.png" data-dark-src="/assets/figure-svg/figures/2e97f4da39fa81736bc20d6f.json" data-src="/assets/figure-svg/figures/4cf82cb52608fba11b591826.json" style=";--figure-ratio:960.0/540.0"><object data="/ja/chapter_tree/array_representation_of_tree.assets/array_representation_complete_binary_tree.svg" data-theme="light" tabindex="-1" type="image/svg+xml">完全二分木の配列表現</object><object data="/ja/chapter_tree/array_representation_of_tree.assets/array_representation_complete_binary_tree.dark.svg" data-theme="dark" tabindex="-1" type="image/svg+xml">完全二分木の配列表現</object></ha-svg-figure></p>
<p align="center"> 図 7-15   完全二分木の配列表現 </p>
<p><ha-svg-figure aria-label="完全二分木の配列では末尾の空き位置を省略できる:完全二分木のレベル順配列で末尾の 5 個の空き位置を省略" class="animation-figure" data-book-key="ja/chapter_tree/array_representation_of_tree.assets/array_representation_complete_binary_tree.png" data-dark-src="/assets/figure-svg/figures/2e97f4da39fa81736bc20d6f.json" data-figure-accessible="" data-src="/assets/figure-svg/figures/4cf82cb52608fba11b591826.json" role="img" style=";--figure-ratio:960.0/540.0"><object aria-hidden="true" data="/ja/chapter_tree/array_representation_of_tree.assets/array_representation_complete_binary_tree.svg" data-theme="light" inert="" tabindex="-1" type="image/svg+xml">完全二分木の配列表現</object><object aria-hidden="true" data="/ja/chapter_tree/array_representation_of_tree.assets/array_representation_complete_binary_tree.dark.svg" data-theme="dark" inert="" tabindex="-1" type="image/svg+xml">完全二分木の配列表現</object></ha-svg-figure></p>
<p align="center"> 図 7-15   完全二分木の配列表現 </p><div class="ha-sr-only figure-description"><h3>図解 7-15:完全二分木の配列では末尾の空き位置を省略できる</h3><p>完全二分木の空き位置はレベル順配列の末尾に集まります。それらを省略しても、存在するノードのインデックスは変わりません。</p><p>完全二分木では最下層以外のすべての層が埋まり、最下層も左から右へ隙間なく埋まっています。図の配列のインデックスは 0 始まりで、10 個のノードが 0~9 を占めます。</p><p>左の 4 層の完全二分木には、実在する 10 個のノードが塗りつぶされています。根は 1、その左右の子は 2 と 3 です。2 の左右の子は 4 と 5、3 の左右の子は 6 と 7、4 の左右の子は 8 と 9 です。5 の子は左の 10 だけで、6、7、8、9、10 は葉です。第 4 層の 10 の右には灰色の破線の円が 5 個あり、末尾の空き位置を示します。</p><p>右上の「レベル順走査の列(配列)」は [1, 2, 3, 4, 5, 6, 7, 8, 9, 10] の後ろに破線の空きマスを 5 個並べています。中央の下向き矢印に「完全二分木なら、空き位置を格納する必要はない」と添えられ、右下の最終配列 [1, 2, 3, 4, 5, 6, 7, 8, 9, 10] を指します。省く 5 マスはすべて末尾にあるので、実在する 10 個のノードのインデックスは変わりません。</p><p>たとえばノード 5 はインデックス 4 にあります。左の子の位置は 2×4+1 = 9 で、ノード 10 が入っています。右の子の位置は 2×4+2 = 10 で、長さ 10 の配列の範囲外なので、右の子はありません。</p><p>末尾の空き位置を省いても、子のインデックスを求める式 2i+1 と 2i+2 は使えます。求めた位置にアクセスする前に、配列の範囲内かを確認します。</p></div>
<p>以下は配列で二分木を実装したコードで、次の操作を含みます。</p>
<ul>
<li>指定したノードの値、左右の子、親を取得します。</li>
@@ -3323,5 +3323,5 @@
<script src="../../javascripts/mathjax.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/mathjax/3.2.2/es5/tex-mml-chtml.min.js"></script>
<script src="../../javascripts/animation_player.js"></script>
<script defer="" src="/assets/figure-svg/js/bcdea376f01bc2bc0cac7c6c.js"></script><script defer="" src="/assets/figure-svg/js/e84f59519170c0f5ef238548.js"></script><script defer="" src="/assets/figure-svg/js/a704d8e80a80ddc71f2a03a7.js"></script><script defer="" src="/assets/figure-svg/js/bff17b8259339758cb1c4a06.js"></script></body>
<script defer="" src="/assets/figure-svg/js/00174ff861e190f5c1f09e34.js"></script><script defer="" src="/assets/figure-svg/js/e84f59519170c0f5ef238548.js"></script><script defer="" src="/assets/figure-svg/js/a704d8e80a80ddc71f2a03a7.js"></script><script defer="" src="/assets/figure-svg/js/70ae82c6406c9bdbda8e8362.js"></script></body>
</html>