This commit is contained in:
krahets
2023-11-09 05:13:54 +08:00
parent 9a09f9407e
commit 3f666fa676
85 changed files with 619 additions and 610 deletions

View File

@@ -3652,7 +3652,7 @@
</div>
<p>每个节点都有两个引用(指针),分别指向「左子节点 left-child node」和「右子节点 right-child node」该节点被称为这两个子节点的「父节点 parent node」。当给定一个二叉树的节点时我们将该节点的左子节点及其以下节点形成的树称为该节点的「左子树 left subtree」同理可得「右子树 right subtree」。</p>
<p><strong>在二叉树中,除叶节点外,其他所有节点都包含子节点和非空子树</strong>。如图 7-1 所示,如果将“节点 2”视为父节点则其左子节点和右子节点分别是“节点 4”和“节点 5”左子树是“节点 4 及其以下节点形成的树”,右子树是“节点 5 及其以下节点形成的树”。</p>
<p><a class="glightbox" href="../binary_tree.assets/binary_tree_definition.png" data-type="image" data-width="100%" data-height="auto" data-desc-position="bottom"><img alt="父节点、子节点、子树" src="../binary_tree.assets/binary_tree_definition.png" /></a></p>
<p><a class="glightbox" href="../binary_tree.assets/binary_tree_definition.png" data-type="image" data-width="100%" data-height="auto" data-desc-position="bottom"><img alt="父节点、子节点、子树" class="animation-figure" src="../binary_tree.assets/binary_tree_definition.png" /></a></p>
<p align="center"> 图 7-1 &nbsp; 父节点、子节点、子树 </p>
<h2 id="711">7.1.1 &nbsp; 二叉树常见术语<a class="headerlink" href="#711" title="Permanent link">&para;</a></h2>
@@ -3667,7 +3667,7 @@
<li>节点的「深度 depth」从根节点到该节点所经过的边的数量。</li>
<li>节点的「高度 height」从距离该节点最远的叶节点到该节点所经过的边的数量。</li>
</ul>
<p><a class="glightbox" href="../binary_tree.assets/binary_tree_terminology.png" data-type="image" data-width="100%" data-height="auto" data-desc-position="bottom"><img alt="二叉树的常用术语" src="../binary_tree.assets/binary_tree_terminology.png" /></a></p>
<p><a class="glightbox" href="../binary_tree.assets/binary_tree_terminology.png" data-type="image" data-width="100%" data-height="auto" data-desc-position="bottom"><img alt="二叉树的常用术语" class="animation-figure" src="../binary_tree.assets/binary_tree_terminology.png" /></a></p>
<p align="center"> 图 7-2 &nbsp; 二叉树的常用术语 </p>
<div class="admonition tip">
@@ -3849,7 +3849,7 @@
</div>
<h3 id="2">2. &nbsp; 插入与删除节点<a class="headerlink" href="#2" title="Permanent link">&para;</a></h3>
<p>与链表类似,在二叉树中插入与删除节点可以通过修改指针来实现。图 7-3 给出了一个示例。</p>
<p><a class="glightbox" href="../binary_tree.assets/binary_tree_add_remove.png" data-type="image" data-width="100%" data-height="auto" data-desc-position="bottom"><img alt="在二叉树中插入与删除节点" src="../binary_tree.assets/binary_tree_add_remove.png" /></a></p>
<p><a class="glightbox" href="../binary_tree.assets/binary_tree_add_remove.png" data-type="image" data-width="100%" data-height="auto" data-desc-position="bottom"><img alt="在二叉树中插入与删除节点" class="animation-figure" src="../binary_tree.assets/binary_tree_add_remove.png" /></a></p>
<p align="center"> 图 7-3 &nbsp; 在二叉树中插入与删除节点 </p>
<div class="tabbed-set tabbed-alternate" data-tabs="3:12"><input checked="checked" id="__tabbed_3_1" name="__tabbed_3" type="radio" /><input id="__tabbed_3_2" name="__tabbed_3" type="radio" /><input id="__tabbed_3_3" name="__tabbed_3" type="radio" /><input id="__tabbed_3_4" name="__tabbed_3" type="radio" /><input id="__tabbed_3_5" name="__tabbed_3" type="radio" /><input id="__tabbed_3_6" name="__tabbed_3" type="radio" /><input id="__tabbed_3_7" name="__tabbed_3" type="radio" /><input id="__tabbed_3_8" name="__tabbed_3" type="radio" /><input id="__tabbed_3_9" name="__tabbed_3" type="radio" /><input id="__tabbed_3_10" name="__tabbed_3" type="radio" /><input id="__tabbed_3_11" name="__tabbed_3" type="radio" /><input id="__tabbed_3_12" name="__tabbed_3" type="radio" /><div class="tabbed-labels"><label for="__tabbed_3_1">Python</label><label for="__tabbed_3_2">C++</label><label for="__tabbed_3_3">Java</label><label for="__tabbed_3_4">C#</label><label for="__tabbed_3_5">Go</label><label for="__tabbed_3_6">Swift</label><label for="__tabbed_3_7">JS</label><label for="__tabbed_3_8">TS</label><label for="__tabbed_3_9">Dart</label><label for="__tabbed_3_10">Rust</label><label for="__tabbed_3_11">C</label><label for="__tabbed_3_12">Zig</label></div>
@@ -3978,22 +3978,22 @@
<p class="admonition-title">Tip</p>
<p>请注意,在中文社区中,完美二叉树常被称为「满二叉树」。</p>
</div>
<p><a class="glightbox" href="../binary_tree.assets/perfect_binary_tree.png" data-type="image" data-width="100%" data-height="auto" data-desc-position="bottom"><img alt="完美二叉树" src="../binary_tree.assets/perfect_binary_tree.png" /></a></p>
<p><a class="glightbox" href="../binary_tree.assets/perfect_binary_tree.png" data-type="image" data-width="100%" data-height="auto" data-desc-position="bottom"><img alt="完美二叉树" class="animation-figure" src="../binary_tree.assets/perfect_binary_tree.png" /></a></p>
<p align="center"> 图 7-4 &nbsp; 完美二叉树 </p>
<h3 id="2_1">2. &nbsp; 完全二叉树<a class="headerlink" href="#2_1" title="Permanent link">&para;</a></h3>
<p>如图 7-5 所示,「完全二叉树 complete binary tree」只有最底层的节点未被填满且最底层节点尽量靠左填充。</p>
<p><a class="glightbox" href="../binary_tree.assets/complete_binary_tree.png" data-type="image" data-width="100%" data-height="auto" data-desc-position="bottom"><img alt="完全二叉树" src="../binary_tree.assets/complete_binary_tree.png" /></a></p>
<p><a class="glightbox" href="../binary_tree.assets/complete_binary_tree.png" data-type="image" data-width="100%" data-height="auto" data-desc-position="bottom"><img alt="完全二叉树" class="animation-figure" src="../binary_tree.assets/complete_binary_tree.png" /></a></p>
<p align="center"> 图 7-5 &nbsp; 完全二叉树 </p>
<h3 id="3">3. &nbsp; 完满二叉树<a class="headerlink" href="#3" title="Permanent link">&para;</a></h3>
<p>如图 7-6 所示,「完满二叉树 full binary tree」除了叶节点之外其余所有节点都有两个子节点。</p>
<p><a class="glightbox" href="../binary_tree.assets/full_binary_tree.png" data-type="image" data-width="100%" data-height="auto" data-desc-position="bottom"><img alt="完满二叉树" src="../binary_tree.assets/full_binary_tree.png" /></a></p>
<p><a class="glightbox" href="../binary_tree.assets/full_binary_tree.png" data-type="image" data-width="100%" data-height="auto" data-desc-position="bottom"><img alt="完满二叉树" class="animation-figure" src="../binary_tree.assets/full_binary_tree.png" /></a></p>
<p align="center"> 图 7-6 &nbsp; 完满二叉树 </p>
<h3 id="4">4. &nbsp; 平衡二叉树<a class="headerlink" href="#4" title="Permanent link">&para;</a></h3>
<p>如图 7-7 所示,「平衡二叉树 balanced binary tree」中任意节点的左子树和右子树的高度之差的绝对值不超过 1 。</p>
<p><a class="glightbox" href="../binary_tree.assets/balanced_binary_tree.png" data-type="image" data-width="100%" data-height="auto" data-desc-position="bottom"><img alt="平衡二叉树" src="../binary_tree.assets/balanced_binary_tree.png" /></a></p>
<p><a class="glightbox" href="../binary_tree.assets/balanced_binary_tree.png" data-type="image" data-width="100%" data-height="auto" data-desc-position="bottom"><img alt="平衡二叉树" class="animation-figure" src="../binary_tree.assets/balanced_binary_tree.png" /></a></p>
<p align="center"> 图 7-7 &nbsp; 平衡二叉树 </p>
<h2 id="714">7.1.4 &nbsp; 二叉树的退化<a class="headerlink" href="#714" title="Permanent link">&para;</a></h2>
@@ -4002,7 +4002,7 @@
<li>完美二叉树是理想情况,可以充分发挥二叉树“分治”的优势。</li>
<li>链表则是另一个极端,各项操作都变为线性操作,时间复杂度退化至 <span class="arithmatex">\(O(n)\)</span></li>
</ul>
<p><a class="glightbox" href="../binary_tree.assets/binary_tree_best_worst_cases.png" data-type="image" data-width="100%" data-height="auto" data-desc-position="bottom"><img alt="二叉树的最佳与最差结构" src="../binary_tree.assets/binary_tree_best_worst_cases.png" /></a></p>
<p><a class="glightbox" href="../binary_tree.assets/binary_tree_best_worst_cases.png" data-type="image" data-width="100%" data-height="auto" data-desc-position="bottom"><img alt="二叉树的最佳与最差结构" class="animation-figure" src="../binary_tree.assets/binary_tree_best_worst_cases.png" /></a></p>
<p align="center"> 图 7-8 &nbsp; 二叉树的最佳与最差结构 </p>
<p>如表 7-1 所示,在最佳和最差结构下,二叉树的叶节点数量、节点总数、高度等达到极大或极小值。</p>