This commit is contained in:
krahets
2026-11-04 05:26:02 +08:00
parent 10963b9c01
commit 81e6fc70d7
30312 changed files with 24023 additions and 14508 deletions

View File

@@ -47,7 +47,7 @@
set(key, value, storage, key === "__palette" ? shared : scope);
})();
</script>
<link href="/assets/figure-svg/css/e5c971c8d6831a5e9f627022.css" rel="stylesheet"/><link href="/assets/figure-svg/css/320d37a71f8e9962e76c8fc3.css" rel="stylesheet"/><link href="/assets/figure-svg/css/c053e5d2507ba0f764c3d1cf.css" rel="stylesheet"/></head>
<link href="/assets/figure-svg/css/e5c971c8d6831a5e9f627022.css" rel="stylesheet"/><link href="/assets/figure-svg/css/c053e5d2507ba0f764c3d1cf.css" rel="stylesheet"/></head>
<body data-md-color-accent="teal" data-md-color-primary="white" data-md-color-scheme="default" dir="ltr">
<input autocomplete="off" class="md-toggle" data-md-toggle="drawer" id="__drawer" type="checkbox"/>
<input autocomplete="off" class="md-toggle" data-md-toggle="search" id="__search" type="checkbox"/>
@@ -1929,13 +1929,13 @@
<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/cde8bfdccf20b162c22a8c3a.json" data-src="/assets/figure-svg/figures/ae1b3c792b819931fbfa4bda.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><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><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/934ce21653bc996d3b272ac7.json" data-src="/assets/figure-svg/figures/75453347aa44eef7ead214fe.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><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>解決するには、<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>
@@ -2020,11 +2020,11 @@
</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/e57c6d6620b5415aeccaf204.json" data-src="/assets/figure-svg/figures/35e68870ea57df2cd9545979.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><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><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/acc56e8f2b74fffb31a7ab15.json" data-src="/assets/figure-svg/figures/00039863de1f59768fd949a3.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><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>以下は配列で二分木を実装したコードで、次の操作を含みます。</p>
<ul>
@@ -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/99a707e9f0a14d8280f16808.js"></script><script defer="" src="/assets/figure-svg/js/d706a8d4337d8499273c0243.js"></script></body>
<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/99a707e9f0a14d8280f16808.js"></script><script defer="" src="/assets/figure-svg/js/855fb5bb73c22cf5c2cea474.js"></script></body>
</html>