---
title: 40 分钟速通《数据结构》
author: 萑澈
pubDatetime: 2026-11-09T00:00:00Z
featured: false
draft: false
printable: true
tags:
  - 数据结构
  - cs408
  - 考研
  - 速通
description: "B站《40 分钟速通数据结构》视频笔记：基本概念、复杂度、线性表、栈与队列、串与 KMP、数组、树与图、最短路径、查找、排序。视频：https://www.bilibili.com/video/BV1eZpP6bE58/"
---

## 一、基本概念

### 数据、数据元素、数据项

| 概念 | 含义 | 表格中的对应 |
|:----:|:-----|:-------------|
| 数据 | 描述客观事物的符号集合 | 整张表 |
| 数据元素 | 数据的基本单位 | 一行（一条完整记录，如「李雷 139xxxx0002 上海」） |
| 数据项 | 最小单位，不可再分 | 一个格（如「139xxxx0002」） |

<figure class="cpd-figure not-content" id="ds40-img5-fig-1">
  <svg viewBox="0 0 700 220" role="img" aria-label="数据、数据对象、数据元素与数据项的四层包含关系图">
    <rect class="fig-box-subtle" x="18" y="16" width="664" height="188" rx="8"/>
    <rect class="fig-box-blue" x="36" y="48" width="628" height="142" rx="8"/>
    <rect class="fig-box-green" x="54" y="80" width="592" height="96" rx="8"/>
    <rect class="fig-box-orange" x="72" y="114" width="270" height="48" rx="6"/>
    <rect class="fig-box-purple" x="358" y="114" width="270" height="48" rx="6"/>
    <text class="fig-title" x="34" y="38" font-size="12">第 1 层 · 数据（Data）：计算机能接收并处理的符号总集（文本、整数、图像等）</text>
    <text class="fig-blue" x="52" y="69" font-size="11.5">第 2 层 · 数据对象（Data Object）：性质相同的数据元素集合（如：同一张学生成绩表）</text>
    <text class="fig-green" x="70" y="101" font-size="11.5">第 3 层 · 数据元素（Data Element）：数据的基本单位，作为整体参与运算（如：一条学生记录）</text>
    <text class="fig-orange" x="207" y="134" font-size="11" text-anchor="middle">第 4 层 · 数据项 1：学号 = 202601</text>
    <text class="fig-muted" x="207" y="152" font-size="10" text-anchor="middle">最小不可分单位（原子字段）</text>
    <text class="fig-purple" x="493" y="134" font-size="11" text-anchor="middle">第 4 层 · 数据项 2：总评成绩 = 92</text>
    <text class="fig-muted" x="493" y="152" font-size="10" text-anchor="middle">最小不可分单位（基本属性）</text>
  </svg>
  <figcaption>图 1-1a：数据概念四层嵌套包含关系——数据 ⊃ 数据对象 ⊃ 数据元素（基本单位）⊃ 数据项（最小单位）</figcaption>
</figure>

<figure class="cpd-figure not-content" id="ds40-w1-fig-1">
  <svg viewBox="0 0 720 294" role="img" aria-label="数据、数据对象、数据元素与数据项（含原子项与组合项）层级解剖图">
    <defs>
      <marker id="ds40-w1-f1-arr" viewBox="0 0 10 10" refX="8.5" refY="5" markerWidth="6.5" markerHeight="6.5" markerUnits="userSpaceOnUse" orient="auto-start-reverse">
        <path d="M 1 1.5 L 8.5 5 L 1 8.5 z" fill="context-stroke" stroke="none"/>
      </marker>
    </defs>
    <rect class="fig-box-subtle" x="18" y="16" width="684" height="222" rx="8"/>
    <rect class="fig-box-dashed" x="306" y="178" width="382" height="48" rx="6"/>
    <rect class="fig-box" x="32" y="46" width="436" height="26" rx="4"/>
    <rect class="fig-box" x="32" y="74" width="436" height="24" rx="3"/>
    <rect class="fig-box" x="32" y="100" width="436" height="24" rx="3"/>
    <rect class="fig-box-accent" x="32" y="126" width="436" height="30" rx="4"/>
    <rect class="fig-box-blue" x="158" y="128" width="116" height="26" rx="3"/>
    <rect class="fig-box-purple" x="278" y="128" width="118" height="26" rx="3"/>
    <rect class="fig-box-accent" x="498" y="60" width="190" height="64" rx="6"/>
    <rect class="fig-box-blue" x="42" y="178" width="248" height="48" rx="6"/>
    <rect class="fig-box-purple" x="318" y="200" width="110" height="22" rx="4"/>
    <rect class="fig-box-purple" x="438" y="200" width="110" height="22" rx="4"/>
    <rect class="fig-box-purple" x="558" y="200" width="118" height="22" rx="4"/>
    <rect class="fig-box-orange" x="18" y="246" width="684" height="32" rx="6"/>
    <line class="fig-divider" x1="92" y1="46" x2="92" y2="124"/>
    <line class="fig-divider" x1="156" y1="46" x2="156" y2="124"/>
    <line class="fig-divider" x1="276" y1="46" x2="276" y2="124"/>
    <line class="fig-divider" x1="398" y1="46" x2="398" y2="124"/>
    <path class="fig-line-accent" fill="none" d="M 468 141 L 482 141 L 482 92 L 496 92" marker-end="url(#ds40-w1-f1-arr)"/>
    <line class="fig-line" x1="216" y1="156" x2="216" y2="176" stroke="var(--sl-color-blue)" style="stroke: var(--sl-color-blue)" marker-end="url(#ds40-w1-f1-arr)"/>
    <line class="fig-line" x1="337" y1="156" x2="337" y2="176" stroke="var(--sl-color-purple)" style="stroke: var(--sl-color-purple)" marker-end="url(#ds40-w1-f1-arr)"/>
    <text class="fig-title" x="32" y="36" font-size="12.5">数据（Data）：通讯录符号总集  ⊃  数据对象：性质相同的学生记录集合</text>
    <text class="fig-muted" x="62" y="63" font-size="11" text-anchor="middle">学号</text>
    <text class="fig-muted" x="124" y="63" font-size="11" text-anchor="middle">姓名</text>
    <text class="fig-muted" x="216" y="63" font-size="11" text-anchor="middle">联系电话</text>
    <text class="fig-muted" x="337" y="63" font-size="11" text-anchor="middle">出生日期</text>
    <text class="fig-muted" x="433" y="63" font-size="11" text-anchor="middle">城市</text>
    <text class="fig-muted" x="62" y="90" font-size="11" text-anchor="middle">1001</text>
    <text class="fig-muted" x="124" y="90" font-size="11" text-anchor="middle">韩梅</text>
    <text class="fig-muted" x="216" y="90" font-size="11" text-anchor="middle">138xxxx0001</text>
    <text class="fig-muted" x="337" y="90" font-size="11" text-anchor="middle">2004-03-12</text>
    <text class="fig-muted" x="433" y="90" font-size="11" text-anchor="middle">北京</text>
    <text class="fig-muted" x="62" y="116" font-size="11" text-anchor="middle">1003</text>
    <text class="fig-muted" x="124" y="116" font-size="11" text-anchor="middle">张伟</text>
    <text class="fig-muted" x="216" y="116" font-size="11" text-anchor="middle">137xxxx0003</text>
    <text class="fig-muted" x="337" y="116" font-size="11" text-anchor="middle">2003-11-06</text>
    <text class="fig-muted" x="433" y="116" font-size="11" text-anchor="middle">广州</text>
    <text class="fig-accent" x="62" y="145" font-size="11.5" text-anchor="middle">1002</text>
    <text class="fig-accent" x="124" y="145" font-size="11.5" text-anchor="middle">李雷</text>
    <text class="fig-blue" x="216" y="145" font-size="11.5" text-anchor="middle">139xxxx0002</text>
    <text class="fig-purple" x="337" y="145" font-size="11.5" text-anchor="middle">2004-05-18</text>
    <text class="fig-accent" x="433" y="145" font-size="11.5" text-anchor="middle">上海</text>
    <text class="fig-accent" x="593" y="80" font-size="11.5" text-anchor="middle">数据元素（基本单位）</text>
    <text class="fig-label" x="593" y="96" font-size="11" text-anchor="middle">整行记录作为个体</text>
    <text class="fig-muted" x="593" y="113" font-size="10.5" text-anchor="middle">参与查找 / 插入 / 删除</text>
    <text class="fig-blue" x="166" y="198" font-size="11.5" text-anchor="middle">原子数据项（最小不可分单位）</text>
    <text class="fig-label" x="166" y="215" font-size="10.5" text-anchor="middle">如「139xxxx0002」，不可再分</text>
    <text class="fig-purple" x="318" y="195" font-size="11">组合数据项（可拆分子项）：</text>
    <text class="fig-purple" x="373" y="215" font-size="10.5" text-anchor="middle">年：2004</text>
    <text class="fig-purple" x="493" y="215" font-size="10.5" text-anchor="middle">月：05</text>
    <text class="fig-purple" x="617" y="215" font-size="10.5" text-anchor="middle">日：18</text>
    <text class="fig-orange" x="360" y="266" font-size="11" text-anchor="middle">★ 408 考点：数据元素 = 基本单位（个体）；数据项 = 最小不可分单位；数据对象 = 同性质元素集合</text>
  </svg>
  <figcaption>图 1-1：数据层级解剖——数据 ⊃ 数据对象 ⊃ 数据元素（基本单位）⊃ 数据项（最小不可分单位，组合项可拆分为原子子项）</figcaption>
</figure>

### 数据结构三要素

- **逻辑结构**：元素之间是什么关系
- **存储结构**：在内存里实际怎么放
- **数据的运算**：在这批数据上能做哪些操作

同一批数据，可以用不同逻辑结构组织，也可以用不同存储结构实现。

<figure class="cpd-figure not-content" id="ds40-img5-fig-2">
  <svg viewBox="0 0 700 245" role="img" aria-label="同一线性队列逻辑结构在顺序存储与链式存储下的不同物理实现对比">
    <defs>
      <marker id="ds40-img5-arr-2" viewBox="0 0 10 10" refX="8.5" refY="5" markerWidth="6.5" markerHeight="6.5" markerUnits="userSpaceOnUse" orient="auto-start-reverse">
        <path d="M 1 1.5 L 8.5 5 L 1 8.5 z" fill="context-stroke" stroke="none"/>
      </marker>
    </defs>
    <line class="fig-line-accent" x1="210" y1="78" x2="180" y2="102" marker-end="url(#ds40-img5-arr-2)"/>
    <line class="fig-line-accent" x1="490" y1="78" x2="520" y2="102" marker-end="url(#ds40-img5-arr-2)"/>
    <rect class="fig-box-blue" x="18" y="16" width="664" height="62" rx="8"/>
    <rect class="fig-box-green" x="18" y="104" width="320" height="92" rx="8"/>
    <rect class="fig-box-purple" x="362" y="104" width="320" height="92" rx="8"/>
    <rect class="fig-box" x="36" y="136" width="66" height="28" rx="3"/>
    <rect class="fig-box" x="102" y="136" width="66" height="28" rx="3"/>
    <rect class="fig-box" x="168" y="136" width="66" height="28" rx="3"/>
    <rect class="fig-box-dashed" x="234" y="136" width="66" height="28" rx="3"/>
    <line class="fig-line" x1="448" y1="150" x2="472" y2="150" marker-end="url(#ds40-img5-arr-2)"/>
    <line class="fig-line" x1="544" y1="150" x2="568" y2="150" marker-end="url(#ds40-img5-arr-2)"/>
    <rect class="fig-box" x="380" y="136" width="68" height="28" rx="3"/>
    <rect class="fig-box" x="476" y="136" width="68" height="28" rx="3"/>
    <rect class="fig-box" x="572" y="136" width="68" height="28" rx="3"/>
    <rect class="fig-box-accent" x="18" y="206" width="664" height="24" rx="5"/>
    <text class="fig-blue" x="350" y="38" font-size="12" text-anchor="middle">同一逻辑结构：队列 Q = (a₁, a₂, a₃) —— 元素呈 1 对 1 线性关系</text>
    <text class="fig-label" x="350" y="60" font-size="10.5" text-anchor="middle">运算定义固定：EnQueue(Q, x) 队尾入队 · DeQueue(Q, &amp;x) 队头出队</text>
    <text class="fig-green" x="178" y="124" font-size="11" text-anchor="middle">存储实现 A：顺序队列（连续数组）</text>
    <text class="fig-label" x="69" y="154" font-size="10.5" text-anchor="middle">[0] a₁</text>
    <text class="fig-label" x="135" y="154" font-size="10.5" text-anchor="middle">[1] a₂</text>
    <text class="fig-label" x="201" y="154" font-size="10.5" text-anchor="middle">[2] a₃</text>
    <text class="fig-muted" x="267" y="154" font-size="10" text-anchor="middle">[3] 空</text>
    <text class="fig-muted" x="178" y="184" font-size="10" text-anchor="middle">内存连续 · 无指针开销 · 容量固定</text>
    <text class="fig-purple" x="522" y="124" font-size="11" text-anchor="middle">存储实现 B：链式队列（单链表）</text>
    <text class="fig-label" x="414" y="154" font-size="10.5" text-anchor="middle">a₁ | •</text>
    <text class="fig-label" x="510" y="154" font-size="10.5" text-anchor="middle">a₂ | •</text>
    <text class="fig-label" x="606" y="154" font-size="10.5" text-anchor="middle">a₃ | ∧</text>
    <text class="fig-muted" x="522" y="184" font-size="10" text-anchor="middle">内存可分散 · 动态扩缩 · 需存指针域</text>
    <text class="fig-accent" x="350" y="222" font-size="10.5" text-anchor="middle">要点：一种逻辑结构可以对应多种存储结构，而运算的具体实现取决于所选存储结构</text>
  </svg>
  <figcaption>图 1-2a：逻辑与存储的“一对多”映射——同一队列逻辑结构可分别采用顺序数组或链表实现</figcaption>
</figure>

<figure class="cpd-figure not-content" id="ds40-w1-fig-2">
  <svg viewBox="0 0 720 286" role="img" aria-label="数据结构三要素（逻辑结构、存储结构、数据的运算）协作闭环图">
    <defs>
      <marker id="ds40-w1-f2-arr" viewBox="0 0 10 10" refX="8.5" refY="5" markerWidth="6.5" markerHeight="6.5" markerUnits="userSpaceOnUse" orient="auto-start-reverse">
        <path d="M 1 1.5 L 8.5 5 L 1 8.5 z" fill="context-stroke" stroke="none"/>
      </marker>
    </defs>
    <rect class="fig-box-subtle" x="18" y="18" width="310" height="120" rx="8"/>
    <rect class="fig-box-subtle" x="392" y="18" width="310" height="120" rx="8"/>
    <rect class="fig-box-subtle" x="18" y="162" width="684" height="108" rx="8"/>
    <line class="fig-line-accent" x1="328" y1="78" x2="390" y2="78" marker-end="url(#ds40-w1-f2-arr)"/>
    <line class="fig-line" x1="173" y1="138" x2="173" y2="192" stroke="var(--sl-color-purple)" style="stroke: var(--sl-color-purple)" marker-end="url(#ds40-w1-f2-arr)"/>
    <line class="fig-line" x1="547" y1="138" x2="547" y2="192" stroke="var(--sl-color-orange)" style="stroke: var(--sl-color-orange)" marker-end="url(#ds40-w1-f2-arr)"/>
    <line class="fig-line-accent" x1="312" y1="225" x2="406" y2="225" marker-end="url(#ds40-w1-f2-arr)"/>
    <rect class="fig-box-blue" x="30" y="28" width="286" height="30" rx="6"/>
    <rect class="fig-box" x="30" y="68" width="138" height="58" rx="6"/>
    <rect class="fig-box" x="178" y="68" width="138" height="58" rx="6"/>
    <rect class="fig-box-green" x="404" y="28" width="286" height="30" rx="6"/>
    <rect class="fig-box" x="404" y="68" width="138" height="58" rx="6"/>
    <rect class="fig-box" x="552" y="68" width="138" height="58" rx="6"/>
    <rect class="fig-box-purple" x="34" y="194" width="278" height="62" rx="6"/>
    <rect class="fig-box-orange" x="408" y="194" width="278" height="62" rx="6"/>
    <rect class="fig-box" x="336" y="52" width="48" height="20" rx="4"/>
    <rect class="fig-box" x="328" y="202" width="64" height="18" rx="4"/>
    <text class="fig-blue" x="173" y="47" font-size="11.5" text-anchor="middle">① 逻辑结构（独立于计算机，面向问题）</text>
    <text class="fig-label" x="99" y="93" font-size="11.5" text-anchor="middle">线性结构 (1:1)</text>
    <text class="fig-muted" x="99" y="110" font-size="10" text-anchor="middle">线性表·栈·队·串</text>
    <text class="fig-label" x="247" y="93" font-size="11.5" text-anchor="middle">非线性结构</text>
    <text class="fig-muted" x="247" y="110" font-size="10" text-anchor="middle">集合·树·图结构</text>
    <text class="fig-green" x="547" y="47" font-size="11.5" text-anchor="middle">② 存储结构 / 物理结构（面向内存映像）</text>
    <text class="fig-label" x="473" y="93" font-size="11.5" text-anchor="middle">连续空间映像</text>
    <text class="fig-muted" x="473" y="110" font-size="10" text-anchor="middle">顺序存储（相邻）</text>
    <text class="fig-label" x="621" y="93" font-size="11.5" text-anchor="middle">非连续/计算映像</text>
    <text class="fig-muted" x="621" y="110" font-size="10" text-anchor="middle">链式·索引·散列</text>
    <text class="fig-accent" x="360" y="66" font-size="10.5" text-anchor="middle">映像</text>
    <text class="fig-muted" x="360" y="98" font-size="10" text-anchor="middle">1对多实现</text>
    <text class="fig-title" x="360" y="182" font-size="12" text-anchor="middle">③ 数据的运算（施加在数据上的操作：增 · 删 · 改 · 查 · 排序）</text>
    <text class="fig-purple" x="173" y="212" font-size="11.5" text-anchor="middle">★ 运算的定义（针对逻辑结构）</text>
    <text class="fig-label" x="173" y="229" font-size="10.5" text-anchor="middle">指出运算的「功能契约」（做什么）</text>
    <text class="fig-muted" x="173" y="246" font-size="10" text-anchor="middle">例：出队 = 删除并返回队头元素</text>
    <text class="fig-accent" x="360" y="215" font-size="10" text-anchor="middle">落地实现</text>
    <text class="fig-orange" x="547" y="212" font-size="11.5" text-anchor="middle">★ 运算的实现（针对存储结构）</text>
    <text class="fig-label" x="547" y="229" font-size="10.5" text-anchor="middle">指出具体「操作步骤」（怎么做）</text>
    <text class="fig-muted" x="547" y="246" font-size="10" text-anchor="middle">例：front = (front + 1) % MaxSize</text>
  </svg>
  <figcaption>图 1-2：数据结构三要素闭环——运算的定义依附于逻辑结构（做什么），运算的实现依赖于存储结构（怎么做）</figcaption>
</figure>

### 逻辑结构

只描述元素之间的关系，与内存无关。

| 逻辑结构 | 关系 | 直观例子 |
|:--------:|:----:|:---------|
| 集合 | 同属一个集合，别无其他关系 | 一堆无序的点 |
| 线性结构 | 一对一 | 排队；线性表、栈、队列、串 |
| 树形结构 | 一对多 | 家谱 |
| 图状结构 | 多对多 | 社交网络 |

<figure class="cpd-figure not-content" id="ds40-img5-fig-3">
  <svg viewBox="0 0 700 240" role="img" aria-label="四种逻辑结构的现实映射例子与前驱后继度数对比">
    <defs>
      <marker id="ds40-img5-arr-3" viewBox="0 0 10 10" refX="8.5" refY="5" markerWidth="6" markerHeight="6" markerUnits="userSpaceOnUse" orient="auto-start-reverse">
        <path d="M 1 1.5 L 8.5 5 L 1 8.5 z" fill="context-stroke" stroke="none"/>
      </marker>
    </defs>
    <rect class="fig-box-subtle" x="18" y="16" width="154" height="208" rx="8"/>
    <rect class="fig-box-subtle" x="188" y="16" width="154" height="208" rx="8"/>
    <rect class="fig-box-subtle" x="358" y="16" width="154" height="208" rx="8"/>
    <rect class="fig-box-subtle" x="528" y="16" width="154" height="208" rx="8"/>
    <rect class="fig-box" x="28" y="24" width="134" height="26" rx="5"/>
    <rect class="fig-box-blue" x="198" y="24" width="134" height="26" rx="5"/>
    <rect class="fig-box-green" x="368" y="24" width="134" height="26" rx="5"/>
    <rect class="fig-box-purple" x="538" y="24" width="134" height="26" rx="5"/>
    <circle class="fig-box-dashed" cx="95" cy="108" r="42"/>
    <circle class="fig-box" cx="76" cy="96" r="13"/>
    <circle class="fig-box" cx="114" cy="94" r="13"/>
    <circle class="fig-box" cx="95" cy="126" r="13"/>
    <line class="fig-line-accent" x1="226" y1="108" x2="244" y2="108" marker-end="url(#ds40-img5-arr-3)"/>
    <line class="fig-line-accent" x1="286" y1="108" x2="304" y2="108" marker-end="url(#ds40-img5-arr-3)"/>
    <rect class="fig-box-blue" x="198" y="94" width="28" height="28" rx="4"/>
    <rect class="fig-box-blue" x="248" y="94" width="38" height="28" rx="4"/>
    <rect class="fig-box-blue" x="308" y="94" width="24" height="28" rx="4"/>
    <line class="fig-line" x1="435" y1="78" x2="400" y2="112" stroke="var(--sl-color-green)" style="stroke: var(--sl-color-green)"/>
    <line class="fig-line" x1="435" y1="78" x2="470" y2="112" stroke="var(--sl-color-green)" style="stroke: var(--sl-color-green)"/>
    <line class="fig-line" x1="400" y1="112" x2="386" y2="144" stroke="var(--sl-color-green)" style="stroke: var(--sl-color-green)"/>
    <line class="fig-line" x1="400" y1="112" x2="418" y2="144" stroke="var(--sl-color-green)" style="stroke: var(--sl-color-green)"/>
    <rect class="fig-box-green" x="415" y="64" width="40" height="20" rx="4"/>
    <rect class="fig-box-green" x="380" y="102" width="40" height="20" rx="4"/>
    <rect class="fig-box-green" x="450" y="102" width="40" height="20" rx="4"/>
    <rect class="fig-box-green" x="368" y="136" width="36" height="18" rx="4"/>
    <rect class="fig-box-green" x="408" y="136" width="36" height="18" rx="4"/>
    <line class="fig-line" x1="576" y1="84" x2="634" y2="84" stroke="var(--sl-color-purple)" style="stroke: var(--sl-color-purple)"/>
    <line class="fig-line" x1="576" y1="84" x2="576" y2="132" stroke="var(--sl-color-purple)" style="stroke: var(--sl-color-purple)"/>
    <line class="fig-line" x1="634" y1="84" x2="634" y2="132" stroke="var(--sl-color-purple)" style="stroke: var(--sl-color-purple)"/>
    <line class="fig-line" x1="576" y1="132" x2="634" y2="132" stroke="var(--sl-color-purple)" style="stroke: var(--sl-color-purple)"/>
    <line class="fig-line" x1="576" y1="84" x2="634" y2="132" stroke="var(--sl-color-purple)" style="stroke: var(--sl-color-purple)"/>
    <circle class="fig-box-purple" cx="576" cy="84" r="12"/>
    <circle class="fig-box-purple" cx="634" cy="84" r="12"/>
    <circle class="fig-box-purple" cx="576" cy="132" r="12"/>
    <circle class="fig-box-purple" cx="634" cy="132" r="12"/>
    <line class="fig-divider" x1="28" y1="168" x2="162" y2="168"/>
    <line class="fig-divider" x1="198" y1="168" x2="332" y2="168"/>
    <line class="fig-divider" x1="368" y1="168" x2="502" y2="168"/>
    <line class="fig-divider" x1="538" y1="168" x2="672" y2="168"/>
    <text class="fig-label" x="95" y="41" font-size="11" text-anchor="middle">集合（无序关系）</text>
    <text class="fig-blue" x="265" y="41" font-size="11" text-anchor="middle">线性结构（1 : 1）</text>
    <text class="fig-green" x="435" y="41" font-size="11" text-anchor="middle">树形结构（1 : N）</text>
    <text class="fig-purple" x="605" y="41" font-size="11" text-anchor="middle">图状结构（M : N）</text>
    <text class="fig-label" x="76" y="100" font-size="10" text-anchor="middle">x₁</text>
    <text class="fig-label" x="114" y="98" font-size="10" text-anchor="middle">x₂</text>
    <text class="fig-label" x="95" y="130" font-size="10" text-anchor="middle">x₃</text>
    <text class="fig-label" x="212" y="112" font-size="10" text-anchor="middle">甲</text>
    <text class="fig-label" x="267" y="112" font-size="10" text-anchor="middle">乙</text>
    <text class="fig-label" x="320" y="112" font-size="10" text-anchor="middle">丙</text>
    <text class="fig-label" x="435" y="78" font-size="9.5" text-anchor="middle">/root</text>
    <text class="fig-label" x="400" y="116" font-size="9.5" text-anchor="middle">/src</text>
    <text class="fig-label" x="470" y="116" font-size="9.5" text-anchor="middle">/doc</text>
    <text class="fig-label" x="386" y="149" font-size="9" text-anchor="middle">a.c</text>
    <text class="fig-label" x="426" y="149" font-size="9" text-anchor="middle">b.c</text>
    <text class="fig-label" x="576" y="88" font-size="10" text-anchor="middle">A</text>
    <text class="fig-label" x="634" y="88" font-size="10" text-anchor="middle">B</text>
    <text class="fig-label" x="576" y="136" font-size="10" text-anchor="middle">C</text>
    <text class="fig-label" x="634" y="136" font-size="10" text-anchor="middle">D</text>
    <text class="fig-label" x="95" y="186" font-size="10" text-anchor="middle">例：抽奖箱 / 集合</text>
    <text class="fig-muted" x="95" y="204" font-size="9.5" text-anchor="middle">前驱 = 0 · 后继 = 0</text>
    <text class="fig-label" x="265" y="186" font-size="10" text-anchor="middle">例：窗口排队序列</text>
    <text class="fig-muted" x="265" y="204" font-size="9.5" text-anchor="middle">除首尾外 1 前驱 1 后继</text>
    <text class="fig-label" x="435" y="186" font-size="10" text-anchor="middle">例：目录树 / 家谱</text>
    <text class="fig-muted" x="435" y="204" font-size="9.5" text-anchor="middle">1 个双亲 · 多个孩子</text>
    <text class="fig-label" x="605" y="186" font-size="10" text-anchor="middle">例：地铁网 / 社交圈</text>
    <text class="fig-muted" x="605" y="204" font-size="9.5" text-anchor="middle">任意多个前驱与后继</text>
  </svg>
  <figcaption>图 1-3a：四种逻辑结构的直觉模型与结点度数（前驱数、后继数）特征对比</figcaption>
</figure>

<figure class="cpd-figure not-content" id="ds40-w1-fig-3">
  <svg viewBox="0 0 720 236" role="img" aria-label="四种基本逻辑结构（集合、线性、树形、图状）的结点关系拓扑对比">
    <defs>
      <marker id="ds40-w1-f3-arr" viewBox="0 0 10 10" refX="8.5" refY="5" markerWidth="6" markerHeight="6" markerUnits="userSpaceOnUse" orient="auto-start-reverse">
        <path d="M 1 1.5 L 8.5 5 L 1 8.5 z" fill="context-stroke" stroke="none"/>
      </marker>
    </defs>
    <rect class="fig-box-subtle" x="18" y="16" width="162" height="204" rx="8"/>
    <rect class="fig-box-subtle" x="192" y="16" width="162" height="204" rx="8"/>
    <rect class="fig-box-subtle" x="366" y="16" width="162" height="204" rx="8"/>
    <rect class="fig-box-subtle" x="540" y="16" width="162" height="204" rx="8"/>
    <circle class="fig-box-dashed" cx="99" cy="112" r="44"/>
    <line class="fig-line-accent" x1="227" y1="112" x2="243" y2="112" marker-end="url(#ds40-w1-f3-arr)"/>
    <line class="fig-line-accent" x1="265" y1="112" x2="281" y2="112" marker-end="url(#ds40-w1-f3-arr)"/>
    <line class="fig-line-accent" x1="303" y1="112" x2="319" y2="112" marker-end="url(#ds40-w1-f3-arr)"/>
    <line class="fig-line" x1="447" y1="76" x2="412" y2="114" stroke="var(--sl-color-green)" style="stroke: var(--sl-color-green)"/>
    <line class="fig-line" x1="447" y1="76" x2="482" y2="114" stroke="var(--sl-color-green)" style="stroke: var(--sl-color-green)"/>
    <line class="fig-line" x1="412" y1="114" x2="394" y2="148" stroke="var(--sl-color-green)" style="stroke: var(--sl-color-green)"/>
    <line class="fig-line" x1="412" y1="114" x2="430" y2="148" stroke="var(--sl-color-green)" style="stroke: var(--sl-color-green)"/>
    <line class="fig-line" x1="482" y1="114" x2="482" y2="148" stroke="var(--sl-color-green)" style="stroke: var(--sl-color-green)"/>
    <line class="fig-line" x1="621" y1="74" x2="578" y2="104" stroke="var(--sl-color-purple)" style="stroke: var(--sl-color-purple)"/>
    <line class="fig-line" x1="621" y1="74" x2="664" y2="104" stroke="var(--sl-color-purple)" style="stroke: var(--sl-color-purple)"/>
    <line class="fig-line" x1="578" y1="104" x2="664" y2="104" stroke="var(--sl-color-purple)" style="stroke: var(--sl-color-purple)"/>
    <line class="fig-line" x1="578" y1="104" x2="592" y2="148" stroke="var(--sl-color-purple)" style="stroke: var(--sl-color-purple)"/>
    <line class="fig-line" x1="664" y1="104" x2="650" y2="148" stroke="var(--sl-color-purple)" style="stroke: var(--sl-color-purple)"/>
    <line class="fig-line" x1="592" y1="148" x2="650" y2="148" stroke="var(--sl-color-purple)" style="stroke: var(--sl-color-purple)"/>
    <line class="fig-line" x1="621" y1="74" x2="592" y2="148" stroke="var(--sl-color-purple)" style="stroke: var(--sl-color-purple)"/>
    <line class="fig-divider" x1="28" y1="170" x2="170" y2="170"/>
    <line class="fig-divider" x1="202" y1="170" x2="344" y2="170"/>
    <line class="fig-divider" x1="376" y1="170" x2="518" y2="170"/>
    <line class="fig-divider" x1="550" y1="170" x2="692" y2="170"/>
    <rect class="fig-box" x="28" y="24" width="142" height="26" rx="5"/>
    <rect class="fig-box-blue" x="202" y="24" width="142" height="26" rx="5"/>
    <rect class="fig-box-green" x="376" y="24" width="142" height="26" rx="5"/>
    <rect class="fig-box-purple" x="550" y="24" width="142" height="26" rx="5"/>
    <circle class="fig-box" cx="80" cy="96" r="11"/>
    <circle class="fig-box" cx="118" cy="93" r="11"/>
    <circle class="fig-box" cx="83" cy="128" r="11"/>
    <circle class="fig-box" cx="116" cy="129" r="11"/>
    <circle class="fig-box-blue" cx="216" cy="112" r="11"/>
    <circle class="fig-box-blue" cx="254" cy="112" r="11"/>
    <circle class="fig-box-blue" cx="292" cy="112" r="11"/>
    <circle class="fig-box-blue" cx="330" cy="112" r="11"/>
    <circle class="fig-box-green" cx="447" cy="76" r="11"/>
    <circle class="fig-box-green" cx="412" cy="114" r="11"/>
    <circle class="fig-box-green" cx="482" cy="114" r="11"/>
    <circle class="fig-box-green" cx="394" cy="148" r="11"/>
    <circle class="fig-box-green" cx="430" cy="148" r="11"/>
    <circle class="fig-box-green" cx="482" cy="148" r="11"/>
    <circle class="fig-box-purple" cx="621" cy="74" r="11"/>
    <circle class="fig-box-purple" cx="578" cy="104" r="11"/>
    <circle class="fig-box-purple" cx="664" cy="104" r="11"/>
    <circle class="fig-box-purple" cx="592" cy="148" r="11"/>
    <circle class="fig-box-purple" cx="650" cy="148" r="11"/>
    <text class="fig-label" x="99" y="41" font-size="11.5" text-anchor="middle">① 集合 (无关系)</text>
    <text class="fig-blue" x="273" y="41" font-size="11.5" text-anchor="middle">② 线性结构 (1:1)</text>
    <text class="fig-green" x="447" y="41" font-size="11.5" text-anchor="middle">③ 树形结构 (1:N)</text>
    <text class="fig-purple" x="621" y="41" font-size="11.5" text-anchor="middle">④ 图状结构 (M:N)</text>
    <text class="fig-label" x="80" y="100" font-size="10" text-anchor="middle">a</text>
    <text class="fig-label" x="118" y="97" font-size="10" text-anchor="middle">b</text>
    <text class="fig-label" x="83" y="132" font-size="10" text-anchor="middle">c</text>
    <text class="fig-label" x="116" y="133" font-size="10" text-anchor="middle">d</text>
    <text class="fig-label" x="216" y="116" font-size="10" text-anchor="middle">a</text>
    <text class="fig-label" x="254" y="116" font-size="10" text-anchor="middle">b</text>
    <text class="fig-label" x="292" y="116" font-size="10" text-anchor="middle">c</text>
    <text class="fig-label" x="330" y="116" font-size="10" text-anchor="middle">d</text>
    <text class="fig-label" x="447" y="80" font-size="10" text-anchor="middle">A</text>
    <text class="fig-label" x="412" y="118" font-size="10" text-anchor="middle">B</text>
    <text class="fig-label" x="482" y="118" font-size="10" text-anchor="middle">C</text>
    <text class="fig-label" x="394" y="152" font-size="10" text-anchor="middle">D</text>
    <text class="fig-label" x="430" y="152" font-size="10" text-anchor="middle">E</text>
    <text class="fig-label" x="482" y="152" font-size="10" text-anchor="middle">F</text>
    <text class="fig-label" x="621" y="78" font-size="10" text-anchor="middle">1</text>
    <text class="fig-label" x="578" y="108" font-size="10" text-anchor="middle">2</text>
    <text class="fig-label" x="664" y="108" font-size="10" text-anchor="middle">3</text>
    <text class="fig-label" x="592" y="152" font-size="10" text-anchor="middle">4</text>
    <text class="fig-label" x="650" y="152" font-size="10" text-anchor="middle">5</text>
    <text class="fig-label" x="99" y="188" font-size="10.5" text-anchor="middle">无前驱 · 无后继</text>
    <text class="fig-muted" x="99" y="205" font-size="9.5" text-anchor="middle">仅同属一个集合（非线性）</text>
    <text class="fig-label" x="273" y="188" font-size="10.5" text-anchor="middle">首无前驱 · 尾无后继</text>
    <text class="fig-muted" x="273" y="205" font-size="9.5" text-anchor="middle">中间元素唯一前驱与后继</text>
    <text class="fig-label" x="447" y="188" font-size="10.5" text-anchor="middle">根无前驱 · 叶无后继</text>
    <text class="fig-muted" x="447" y="205" font-size="9.5" text-anchor="middle">其余 1 个双亲、多个孩子</text>
    <text class="fig-label" x="621" y="188" font-size="10.5" text-anchor="middle">任意两顶点均可关联</text>
    <text class="fig-muted" x="621" y="205" font-size="9.5" text-anchor="middle">多个直接前驱与直接后继</text>
  </svg>
  <figcaption>图 1-3：四种逻辑结构拓扑特征——线性结构严格满足一对一，集合、树、图统称非线性结构</figcaption>
</figure>

### 存储结构

在内存里实际怎么放。

| 存储结构 | 做法 | 特点 |
|:--------:|:-----|:-----|
| 顺序存储 | 挨着放，用一段连续内存 | 逻辑相邻 = 物理相邻 |
| 链式存储 | 结点内存指针，把元素串起来 | 内存可以不连续 |
| 索引存储 | 额外建索引表（关键字 → 地址） | 查表再定位 |
| 散列存储 | 用哈希函数 `H(key)` 直接算位置 | 查找快，需处理冲突 |

<figure class="cpd-figure not-content" id="ds40-img5-fig-4">
  <svg viewBox="0 0 700 254" role="img" aria-label="四种物理存储结构的查找定位流程与核心权衡对比图">
    <defs>
      <marker id="ds40-img5-arr-4" viewBox="0 0 10 10" refX="8.5" refY="5" markerWidth="6" markerHeight="6" markerUnits="userSpaceOnUse" orient="auto-start-reverse">
        <path d="M 1 1.5 L 8.5 5 L 1 8.5 z" fill="context-stroke" stroke="none"/>
      </marker>
    </defs>
    <rect class="fig-box-subtle" x="18" y="16" width="664" height="48" rx="6"/>
    <rect class="fig-box-subtle" x="18" y="74" width="664" height="48" rx="6"/>
    <rect class="fig-box-subtle" x="18" y="132" width="664" height="48" rx="6"/>
    <rect class="fig-box-subtle" x="18" y="190" width="664" height="48" rx="6"/>
    <line class="fig-line-accent" x1="208" y1="40" x2="232" y2="40" marker-end="url(#ds40-img5-arr-4)"/>
    <line class="fig-line-accent" x1="358" y1="40" x2="382" y2="40" marker-end="url(#ds40-img5-arr-4)"/>
    <line class="fig-line" x1="208" y1="98" x2="232" y2="98" stroke="var(--sl-color-green)" style="stroke: var(--sl-color-green)" marker-end="url(#ds40-img5-arr-4)"/>
    <line class="fig-line" x1="358" y1="98" x2="382" y2="98" stroke="var(--sl-color-green)" style="stroke: var(--sl-color-green)" marker-end="url(#ds40-img5-arr-4)"/>
    <line class="fig-line" x1="208" y1="156" x2="232" y2="156" stroke="var(--sl-color-purple)" style="stroke: var(--sl-color-purple)" marker-end="url(#ds40-img5-arr-4)"/>
    <line class="fig-line" x1="358" y1="156" x2="382" y2="156" stroke="var(--sl-color-purple)" style="stroke: var(--sl-color-purple)" marker-end="url(#ds40-img5-arr-4)"/>
    <line class="fig-line" x1="208" y1="214" x2="232" y2="214" stroke="var(--sl-color-orange)" style="stroke: var(--sl-color-orange)" marker-end="url(#ds40-img5-arr-4)"/>
    <line class="fig-line" x1="358" y1="214" x2="382" y2="214" stroke="var(--sl-color-orange)" style="stroke: var(--sl-color-orange)" marker-end="url(#ds40-img5-arr-4)"/>
    <rect class="fig-box-blue" x="26" y="24" width="94" height="32" rx="5"/>
    <rect class="fig-box" x="134" y="26" width="74" height="28" rx="4"/>
    <rect class="fig-box" x="234" y="26" width="124" height="28" rx="4"/>
    <rect class="fig-box-blue" x="384" y="26" width="90" height="28" rx="4"/>
    <rect class="fig-box-green" x="26" y="82" width="94" height="32" rx="5"/>
    <rect class="fig-box" x="134" y="84" width="74" height="28" rx="4"/>
    <rect class="fig-box" x="234" y="84" width="124" height="28" rx="4"/>
    <rect class="fig-box-green" x="384" y="84" width="90" height="28" rx="4"/>
    <rect class="fig-box-purple" x="26" y="140" width="94" height="32" rx="5"/>
    <rect class="fig-box" x="134" y="142" width="74" height="28" rx="4"/>
    <rect class="fig-box" x="234" y="142" width="124" height="28" rx="4"/>
    <rect class="fig-box-purple" x="384" y="142" width="90" height="28" rx="4"/>
    <rect class="fig-box-orange" x="26" y="198" width="94" height="32" rx="5"/>
    <rect class="fig-box" x="134" y="200" width="74" height="28" rx="4"/>
    <rect class="fig-box" x="234" y="200" width="124" height="28" rx="4"/>
    <rect class="fig-box-orange" x="384" y="200" width="90" height="28" rx="4"/>
    <text class="fig-blue" x="73" y="44" font-size="11" text-anchor="middle">1. 顺序存储</text>
    <text class="fig-label" x="171" y="44" font-size="10" text-anchor="middle">给定位序 i</text>
    <text class="fig-label" x="296" y="44" font-size="10" text-anchor="middle">Base + (i - 1) × L</text>
    <text class="fig-blue" x="429" y="44" font-size="10" text-anchor="middle">O(1) 直达</text>
    <text class="fig-muted" x="488" y="44" font-size="10">密度 = 1，插删需搬移元素</text>
    <text class="fig-green" x="73" y="102" font-size="11" text-anchor="middle">2. 链式存储</text>
    <text class="fig-label" x="171" y="102" font-size="10" text-anchor="middle">从 head 起</text>
    <text class="fig-label" x="296" y="102" font-size="10" text-anchor="middle">沿 next 逐结点跳</text>
    <text class="fig-green" x="429" y="102" font-size="10" text-anchor="middle">O(n) 查找</text>
    <text class="fig-muted" x="488" y="102" font-size="10">可离散分配，需额外指针域</text>
    <text class="fig-purple" x="73" y="160" font-size="11" text-anchor="middle">3. 索引存储</text>
    <text class="fig-label" x="171" y="160" font-size="10" text-anchor="middle">关键字 key</text>
    <text class="fig-label" x="296" y="160" font-size="10" text-anchor="middle">查索引表得物理地址</text>
    <text class="fig-purple" x="429" y="160" font-size="10" text-anchor="middle">跳跃读取</text>
    <text class="fig-muted" x="488" y="160" font-size="10">检索快，增删需同步改索引</text>
    <text class="fig-orange" x="73" y="218" font-size="11" text-anchor="middle">4. 散列存储</text>
    <text class="fig-label" x="171" y="218" font-size="10" text-anchor="middle">关键字 key</text>
    <text class="fig-label" x="296" y="218" font-size="10" text-anchor="middle">计算地址 H(key)</text>
    <text class="fig-orange" x="429" y="218" font-size="10" text-anchor="middle">近似 O(1)</text>
    <text class="fig-muted" x="488" y="218" font-size="10">定位最快，冲突时需再探测</text>
  </svg>
  <figcaption>图 1-4a：四种存储结构的寻址路径与时空权衡对照</figcaption>
</figure>

<figure class="cpd-figure not-content" id="ds40-w1-fig-4">
  <svg viewBox="0 0 720 272" role="img" aria-label="四种物理存储结构（顺序、链式、索引、散列）在内存中的映射方式对比">
    <defs>
      <marker id="ds40-w1-f4-arr" viewBox="0 0 10 10" refX="8.5" refY="5" markerWidth="6" markerHeight="6" markerUnits="userSpaceOnUse" orient="auto-start-reverse">
        <path d="M 1 1.5 L 8.5 5 L 1 8.5 z" fill="context-stroke" stroke="none"/>
      </marker>
    </defs>
    <rect class="fig-box-subtle" x="18" y="16" width="334" height="114" rx="8"/>
    <rect class="fig-box-subtle" x="368" y="16" width="334" height="114" rx="8"/>
    <rect class="fig-box-subtle" x="18" y="142" width="334" height="114" rx="8"/>
    <rect class="fig-box-subtle" x="368" y="142" width="334" height="114" rx="8"/>
    <line class="fig-line-accent" x1="462" y1="74" x2="496" y2="74" marker-end="url(#ds40-w1-f4-arr)"/>
    <line class="fig-line-accent" x1="576" y1="74" x2="610" y2="74" marker-end="url(#ds40-w1-f4-arr)"/>
    <line class="fig-line" x1="138" y1="184" x2="204" y2="224" stroke="var(--sl-color-purple)" style="stroke: var(--sl-color-purple)" marker-end="url(#ds40-w1-f4-arr)"/>
    <line class="fig-line" x1="138" y1="204" x2="204" y2="184" stroke="var(--sl-color-purple)" style="stroke: var(--sl-color-purple)" marker-end="url(#ds40-w1-f4-arr)"/>
    <line class="fig-line" x1="138" y1="224" x2="204" y2="204" stroke="var(--sl-color-purple)" style="stroke: var(--sl-color-purple)" marker-end="url(#ds40-w1-f4-arr)"/>
    <line class="fig-line" x1="440" y1="186" x2="462" y2="196" stroke="var(--sl-color-orange)" style="stroke: var(--sl-color-orange)" marker-end="url(#ds40-w1-f4-arr)"/>
    <line class="fig-line" x1="440" y1="218" x2="462" y2="208" stroke="var(--sl-color-orange)" style="stroke: var(--sl-color-orange)" marker-end="url(#ds40-w1-f4-arr)"/>
    <line class="fig-line" x1="544" y1="194" x2="578" y2="179" stroke="var(--sl-color-orange)" style="stroke: var(--sl-color-orange)" marker-end="url(#ds40-w1-f4-arr)"/>
    <line class="fig-line" x1="544" y1="210" x2="578" y2="233" stroke="var(--sl-color-orange)" style="stroke: var(--sl-color-orange)" marker-end="url(#ds40-w1-f4-arr)"/>
    <rect class="fig-box-blue" x="42" y="56" width="70" height="36" rx="3"/>
    <rect class="fig-box-blue" x="112" y="56" width="70" height="36" rx="3"/>
    <rect class="fig-box-blue" x="182" y="56" width="70" height="36" rx="3"/>
    <rect class="fig-box-blue" x="252" y="56" width="70" height="36" rx="3"/>
    <rect class="fig-box-green" x="384" y="58" width="44" height="32" rx="3"/>
    <rect class="fig-box" x="428" y="58" width="34" height="32" rx="3"/>
    <rect class="fig-box-green" x="498" y="58" width="44" height="32" rx="3"/>
    <rect class="fig-box" x="542" y="58" width="34" height="32" rx="3"/>
    <rect class="fig-box-green" x="612" y="58" width="42" height="32" rx="3"/>
    <rect class="fig-box" x="654" y="58" width="32" height="32" rx="3"/>
    <rect class="fig-box-purple" x="32" y="174" width="106" height="20" rx="2"/>
    <rect class="fig-box-purple" x="32" y="194" width="106" height="20" rx="2"/>
    <rect class="fig-box-purple" x="32" y="214" width="106" height="20" rx="2"/>
    <line class="fig-divider" x1="84" y1="174" x2="84" y2="234"/>
    <rect class="fig-box" x="206" y="174" width="128" height="20" rx="2"/>
    <rect class="fig-box" x="206" y="194" width="128" height="20" rx="2"/>
    <rect class="fig-box" x="206" y="214" width="128" height="20" rx="2"/>
    <rect class="fig-box" x="380" y="175" width="60" height="22" rx="4"/>
    <rect class="fig-box" x="380" y="207" width="60" height="22" rx="4"/>
    <rect class="fig-box-orange" x="464" y="182" width="80" height="38" rx="6"/>
    <rect class="fig-box-orange" x="580" y="170" width="108" height="18" rx="2"/>
    <rect class="fig-box" x="580" y="188" width="108" height="18" rx="2"/>
    <rect class="fig-box" x="580" y="206" width="108" height="18" rx="2"/>
    <rect class="fig-box-orange" x="580" y="224" width="108" height="18" rx="2"/>
    <text class="fig-blue" x="32" y="38" font-size="11.5">① 顺序存储：连续单元，逻辑相邻 ⇔ 物理相邻</text>
    <text class="fig-label" x="77" y="79" font-size="12" text-anchor="middle">a</text>
    <text class="fig-label" x="147" y="79" font-size="12" text-anchor="middle">b</text>
    <text class="fig-label" x="217" y="79" font-size="12" text-anchor="middle">c</text>
    <text class="fig-label" x="287" y="79" font-size="12" text-anchor="middle">d</text>
    <text class="fig-muted" x="77" y="110" font-size="10" text-anchor="middle">1000</text>
    <text class="fig-muted" x="147" y="110" font-size="10" text-anchor="middle">1004</text>
    <text class="fig-muted" x="217" y="110" font-size="10" text-anchor="middle">1008</text>
    <text class="fig-muted" x="287" y="110" font-size="10" text-anchor="middle">1012</text>
    <text class="fig-green" x="382" y="38" font-size="11.5">② 链式存储：物理地址可离散，靠指针串联</text>
    <text class="fig-label" x="406" y="78" font-size="11.5" text-anchor="middle">a</text>
    <text class="fig-muted" x="445" y="78" font-size="9.5" text-anchor="middle">108</text>
    <text class="fig-label" x="520" y="78" font-size="11.5" text-anchor="middle">b</text>
    <text class="fig-muted" x="559" y="78" font-size="9.5" text-anchor="middle">310</text>
    <text class="fig-label" x="633" y="78" font-size="11.5" text-anchor="middle">c</text>
    <text class="fig-muted" x="670" y="78" font-size="10.5" text-anchor="middle">∧</text>
    <text class="fig-muted" x="423" y="110" font-size="10" text-anchor="middle">@240</text>
    <text class="fig-muted" x="537" y="110" font-size="10" text-anchor="middle">@108 (乱序)</text>
    <text class="fig-muted" x="649" y="110" font-size="10" text-anchor="middle">@310</text>
    <text class="fig-purple" x="32" y="162" font-size="11.5">③ 索引存储：附加索引表（关键字 → 地址）</text>
    <text class="fig-label" x="58" y="188" font-size="10" text-anchor="middle">k1</text>
    <text class="fig-muted" x="111" y="188" font-size="10" text-anchor="middle">208</text>
    <text class="fig-label" x="58" y="208" font-size="10" text-anchor="middle">k2</text>
    <text class="fig-muted" x="111" y="208" font-size="10" text-anchor="middle">200</text>
    <text class="fig-label" x="58" y="228" font-size="10" text-anchor="middle">k3</text>
    <text class="fig-muted" x="111" y="228" font-size="10" text-anchor="middle">204</text>
    <text class="fig-label" x="270" y="188" font-size="10" text-anchor="middle">@200: 元素 b</text>
    <text class="fig-label" x="270" y="208" font-size="10" text-anchor="middle">@204: 元素 c</text>
    <text class="fig-label" x="270" y="228" font-size="10" text-anchor="middle">@208: 元素 a</text>
    <text class="fig-muted" x="85" y="248" font-size="9.5" text-anchor="middle">索引表（占额外空间）</text>
    <text class="fig-muted" x="270" y="248" font-size="9.5" text-anchor="middle">主数据区</text>
    <text class="fig-orange" x="382" y="162" font-size="11.5">④ 散列存储：H(key) 直接计算存储位置</text>
    <text class="fig-label" x="410" y="190" font-size="10" text-anchor="middle">key=12</text>
    <text class="fig-label" x="410" y="222" font-size="10" text-anchor="middle">key=19</text>
    <text class="fig-orange" x="504" y="199" font-size="10.5" text-anchor="middle">H(k)</text>
    <text class="fig-muted" x="504" y="213" font-size="9.5" text-anchor="middle">k % 4</text>
    <text class="fig-label" x="634" y="183" font-size="10" text-anchor="middle">[0] : 12</text>
    <text class="fig-muted" x="634" y="201" font-size="10" text-anchor="middle">[1] : 空</text>
    <text class="fig-muted" x="634" y="219" font-size="10" text-anchor="middle">[2] : 空</text>
    <text class="fig-label" x="634" y="237" font-size="10" text-anchor="middle">[3] : 19 (需防冲突)</text>
  </svg>
  <figcaption>图 1-4：四种存储结构的内存布局对比——非顺序存储（链式、索引、散列）均不要求逻辑相邻元素物理连续</figcaption>
</figure>

### ★考点：名字里带「表」不一定是存储结构

- **只描述关系 → 逻辑结构**：栈、队列、有序表、线性表、树、图
- **规定了怎么存 → 存储结构相关**：顺序表、链表、散列表、循环队列

关键区分：**有序表**是逻辑结构（只说明元素按关键字有序），**顺序表**才是存储结构（说明用连续内存存放）。

<figure class="cpd-figure not-content" id="ds40-img5-fig-5">
  <svg viewBox="0 0 700 240" role="img" aria-label="408高频易错点：名字带表的结构归属判定决策流程">
    <defs>
      <marker id="ds40-img5-arr-5" viewBox="0 0 10 10" refX="8.5" refY="5" markerWidth="6.5" markerHeight="6.5" markerUnits="userSpaceOnUse" orient="auto-start-reverse">
        <path d="M 1 1.5 L 8.5 5 L 1 8.5 z" fill="context-stroke" stroke="none"/>
      </marker>
    </defs>
    <line class="fig-line-accent" x1="170" y1="120" x2="196" y2="120" marker-end="url(#ds40-img5-arr-5)"/>
    <path class="fig-line" fill="none" d="M 358 100 L 380 100 L 380 64 L 404 64" stroke="var(--sl-color-blue)" style="stroke: var(--sl-color-blue)" marker-end="url(#ds40-img5-arr-5)"/>
    <path class="fig-line" fill="none" d="M 358 140 L 380 140 L 380 176 L 404 176" stroke="var(--sl-color-green)" style="stroke: var(--sl-color-green)" marker-end="url(#ds40-img5-arr-5)"/>
    <rect class="fig-box" x="18" y="84" width="152" height="72" rx="8"/>
    <rect class="fig-box-accent" x="198" y="84" width="160" height="72" rx="8"/>
    <rect class="fig-box" x="366" y="54" width="28" height="20" rx="4"/>
    <rect class="fig-box" x="366" y="166" width="28" height="20" rx="4"/>
    <rect class="fig-box-blue" x="406" y="16" width="276" height="96" rx="8"/>
    <rect class="fig-box-orange" x="418" y="48" width="252" height="26" rx="4"/>
    <rect class="fig-box" x="418" y="80" width="252" height="24" rx="4"/>
    <rect class="fig-box-green" x="406" y="128" width="276" height="96" rx="8"/>
    <rect class="fig-box-purple" x="418" y="160" width="252" height="26" rx="4"/>
    <rect class="fig-box" x="418" y="192" width="252" height="24" rx="4"/>
    <text class="fig-label" x="94" y="114" font-size="11" text-anchor="middle">遇到一个结构名词</text>
    <text class="fig-muted" x="94" y="134" font-size="10" text-anchor="middle">如“有序表”“循环队列”</text>
    <text class="fig-accent" x="278" y="112" font-size="11" text-anchor="middle">是否规定了底层存法？</text>
    <text class="fig-muted" x="278" y="132" font-size="9.5" text-anchor="middle">（连续数组 / 指针 / 散列）</text>
    <text class="fig-blue" x="380" y="68" font-size="10" text-anchor="middle">否</text>
    <text class="fig-green" x="380" y="180" font-size="10" text-anchor="middle">是</text>
    <text class="fig-blue" x="544" y="38" font-size="11.5" text-anchor="middle">判定为：纯逻辑结构（只谈逻辑关系）</text>
    <text class="fig-orange" x="544" y="65" font-size="10.5" text-anchor="middle">★ 有序表：仅规定关键字递增/递减，未定存法</text>
    <text class="fig-label" x="544" y="96" font-size="10" text-anchor="middle">同类：线性表 · 栈 · 队列 · 串 · 树 · 图</text>
    <text class="fig-green" x="544" y="150" font-size="11.5" text-anchor="middle">判定为：与存储结构相关（绑定了内存）</text>
    <text class="fig-purple" x="544" y="177" font-size="10.5" text-anchor="middle">★ 顺序表 · 循环队列（连续数组取模）</text>
    <text class="fig-label" x="544" y="208" font-size="10" text-anchor="middle">同类：链表 · 散列表 · 索引表 · 线索二叉树</text>
  </svg>
  <figcaption>图 1-5a：408 高频陷阱判定树——紧盯“是否绑定物理存法”，秒分“有序表”与“顺序表”</figcaption>
</figure>

<figure class="cpd-figure not-content" id="ds40-w1-fig-5">
  <svg viewBox="0 0 720 248" role="img" aria-label="408高频考点：逻辑结构名词与存储结构相关名词辨析对照图">
    <defs>
      <marker id="ds40-w1-f5-arr" viewBox="0 0 10 10" refX="8.5" refY="5" markerWidth="6" markerHeight="6" markerUnits="userSpaceOnUse" orient="auto-start-reverse">
        <path d="M 1 1.5 L 8.5 5 L 1 8.5 z" fill="context-stroke" stroke="none"/>
      </marker>
    </defs>
    <rect class="fig-box-subtle" x="18" y="16" width="318" height="216" rx="8"/>
    <rect class="fig-box-subtle" x="384" y="16" width="318" height="216" rx="8"/>
    <line class="fig-line-accent" x1="324" y1="78" x2="394" y2="76" marker-end="url(#ds40-w1-f5-arr)"/>
    <line class="fig-line-accent" x1="324" y1="96" x2="394" y2="110" marker-end="url(#ds40-w1-f5-arr)"/>
    <rect class="fig-box-blue" x="30" y="26" width="294" height="26" rx="5"/>
    <rect class="fig-box-orange" x="30" y="62" width="294" height="50" rx="5"/>
    <rect class="fig-box" x="30" y="120" width="142" height="44" rx="5"/>
    <rect class="fig-box" x="182" y="120" width="142" height="44" rx="5"/>
    <rect class="fig-box" x="30" y="172" width="294" height="48" rx="5"/>
    <rect class="fig-box" x="338" y="80" width="44" height="18" rx="4"/>
    <rect class="fig-box-green" x="396" y="26" width="294" height="26" rx="5"/>
    <rect class="fig-box-green" x="396" y="62" width="294" height="28" rx="5"/>
    <rect class="fig-box-green" x="396" y="96" width="294" height="28" rx="5"/>
    <rect class="fig-box" x="396" y="132" width="142" height="42" rx="5"/>
    <rect class="fig-box" x="548" y="132" width="142" height="42" rx="5"/>
    <rect class="fig-box-purple" x="396" y="182" width="294" height="38" rx="5"/>
    <text class="fig-blue" x="177" y="43" font-size="11.5" text-anchor="middle">纯逻辑结构（只谈关系，不绑内存）</text>
    <text class="fig-orange" x="177" y="82" font-size="11.5" text-anchor="middle">★ 有序表（高频易错：属于逻辑结构！）</text>
    <text class="fig-muted" x="177" y="101" font-size="10" text-anchor="middle">仅要求关键字递增/递减，既可顺序存也可链式存</text>
    <text class="fig-label" x="101" y="139" font-size="11" text-anchor="middle">线性表 · 串</text>
    <text class="fig-muted" x="101" y="155" font-size="9.5" text-anchor="middle">一对一序列</text>
    <text class="fig-label" x="253" y="139" font-size="11" text-anchor="middle">栈 · 队列</text>
    <text class="fig-muted" x="253" y="155" font-size="9.5" text-anchor="middle">操作受限线性表</text>
    <text class="fig-label" x="177" y="193" font-size="11" text-anchor="middle">树 · 二叉树 · 森林 · 图</text>
    <text class="fig-muted" x="177" y="210" font-size="9.5" text-anchor="middle">一对多 / 多对多非线性逻辑关系</text>
    <text class="fig-accent" x="360" y="93" font-size="9.5" text-anchor="middle">实现</text>
    <text class="fig-green" x="543" y="43" font-size="11.5" text-anchor="middle">存储结构相关（绑定了物理存储方式）</text>
    <text class="fig-label" x="543" y="80" font-size="10.5" text-anchor="middle">顺序有序表 → 连续数组（支持折半查找）</text>
    <text class="fig-label" x="543" y="114" font-size="10.5" text-anchor="middle">链式有序表 → 单/双链表（仅能顺序查找）</text>
    <text class="fig-label" x="467" y="150" font-size="11" text-anchor="middle">顺序表 · 链表</text>
    <text class="fig-muted" x="467" y="166" font-size="9.5" text-anchor="middle">连续 vs 指针串联</text>
    <text class="fig-label" x="619" y="150" font-size="11" text-anchor="middle">顺序栈 · 链栈</text>
    <text class="fig-muted" x="619" y="166" font-size="9.5" text-anchor="middle">数组 vs 头插单链表</text>
    <text class="fig-purple" x="543" y="199" font-size="10.5" text-anchor="middle">★ 循环队列（顺序数组取模）· 散列表 · 线索树</text>
    <text class="fig-muted" x="543" y="214" font-size="9.5" text-anchor="middle">均明确规定了底层存储映像机制</text>
  </svg>
  <figcaption>图 1-5：408 真题辨析——“有序表、栈、队列”是逻辑结构；“顺序表、循环队列、散列表”涉及具体存储结构</figcaption>
</figure>

### 算法的五个特性

- **有穷性**：执行有限步后结束，不能死循环
- **确定性**：每条指令无歧义，相同输入必得相同输出
- **可行性**：每一步都能通过基本运算有限次实现
- **输入**：有零个或多个输入
- **输出**：有一个或多个输出

好算法的目标：**正确性、可读性、健壮性、高效性与低存储需求**。

<figure class="cpd-figure not-content" id="ds40-img5-fig-6">
  <svg viewBox="0 0 700 256" role="img" aria-label="算法五大特性的合法判据与典型反例对照表">
    <rect class="fig-box-subtle" x="18" y="16" width="664" height="224" rx="8"/>
    <rect class="fig-box-accent" x="26" y="22" width="648" height="26" rx="5"/>
    <rect class="fig-box" x="26" y="54" width="648" height="32" rx="4"/>
    <rect class="fig-box" x="26" y="90" width="648" height="32" rx="4"/>
    <rect class="fig-box" x="26" y="126" width="648" height="32" rx="4"/>
    <rect class="fig-box" x="26" y="162" width="648" height="32" rx="4"/>
    <rect class="fig-box" x="26" y="198" width="648" height="32" rx="4"/>
    <line class="fig-divider" x1="136" y1="22" x2="136" y2="230"/>
    <line class="fig-divider" x1="406" y1="22" x2="406" y2="230"/>
    <text class="fig-accent" x="81" y="39" font-size="11" text-anchor="middle">必要特性</text>
    <text class="fig-accent" x="271" y="39" font-size="11" text-anchor="middle">合法判据（必须满足）</text>
    <text class="fig-accent" x="540" y="39" font-size="11" text-anchor="middle">典型反例（是程序但不是算法）</text>
    <text class="fig-orange" x="81" y="74" font-size="11" text-anchor="middle">1. 有穷性</text>
    <text class="fig-label" x="150" y="74" font-size="10.5">有限步后必结束，且耗时在可接受内</text>
    <text class="fig-red" x="420" y="74" font-size="10.5">while (1) 死循环 / 常驻操作系统</text>
    <text class="fig-purple" x="81" y="110" font-size="11" text-anchor="middle">2. 确定性</text>
    <text class="fig-label" x="150" y="110" font-size="10.5">每条指令无歧义，同输入必得同输出</text>
    <text class="fig-red" x="420" y="110" font-size="10.5">依赖未定义变量或随机跳转分支</text>
    <text class="fig-green" x="81" y="146" font-size="11" text-anchor="middle">3. 可行性</text>
    <text class="fig-label" x="150" y="146" font-size="10.5">每步可拆解为基本运算有限次完成</text>
    <text class="fig-red" x="420" y="146" font-size="10.5">出现除数为 0 或要求列出全部无穷素数</text>
    <text class="fig-blue" x="81" y="182" font-size="11" text-anchor="middle">4. 输入 ≥ 0</text>
    <text class="fig-label" x="150" y="182" font-size="10.5">接收 0 个或多个外部输入（常量内置）</text>
    <text class="fig-green" x="420" y="182" font-size="10.5">注意：0 个输入仍是合法算法（如打印乘法表）</text>
    <text class="fig-accent" x="81" y="218" font-size="11" text-anchor="middle">5. 输出 ≥ 1</text>
    <text class="fig-label" x="150" y="218" font-size="10.5">至少有 1 个返回值或状态输出</text>
    <text class="fig-red" x="420" y="218" font-size="10.5">无任何返回值且不改变外部状态的空跑代码</text>
  </svg>
  <figcaption>图 1-6a：算法五大特性的“合法 vs 反例”速查表——操作系统不是算法，0 输入允许但 0 输出禁止</figcaption>
</figure>

<figure class="cpd-figure not-content" id="ds40-w1-fig-6">
  <svg viewBox="0 0 720 244" role="img" aria-label="算法五大必要特性流水线与四大好算法质量评价指标">
    <defs>
      <marker id="ds40-w1-f6-arr" viewBox="0 0 10 10" refX="8.5" refY="5" markerWidth="6" markerHeight="6" markerUnits="userSpaceOnUse" orient="auto-start-reverse">
        <path d="M 1 1.5 L 8.5 5 L 1 8.5 z" fill="context-stroke" stroke="none"/>
      </marker>
    </defs>
    <rect class="fig-box-subtle" x="18" y="16" width="684" height="122" rx="8"/>
    <rect class="fig-box-subtle" x="18" y="150" width="684" height="78" rx="8"/>
    <line class="fig-line-accent" x1="148" y1="84" x2="182" y2="84" marker-end="url(#ds40-w1-f6-arr)"/>
    <line class="fig-line-accent" x1="538" y1="84" x2="570" y2="84" marker-end="url(#ds40-w1-f6-arr)"/>
    <rect class="fig-box-blue" x="30" y="50" width="118" height="68" rx="6"/>
    <rect class="fig-box" x="184" y="44" width="354" height="82" rx="6"/>
    <rect class="fig-box-orange" x="194" y="52" width="108" height="66" rx="5"/>
    <rect class="fig-box-purple" x="310" y="52" width="108" height="66" rx="5"/>
    <rect class="fig-box-green" x="426" y="52" width="102" height="66" rx="5"/>
    <rect class="fig-box-accent" x="572" y="50" width="118" height="68" rx="6"/>
    <rect class="fig-box-green" x="30" y="178" width="156" height="40" rx="5"/>
    <rect class="fig-box-blue" x="198" y="178" width="156" height="40" rx="5"/>
    <rect class="fig-box-orange" x="366" y="178" width="156" height="40" rx="5"/>
    <rect class="fig-box-purple" x="534" y="178" width="156" height="40" rx="5"/>
    <text class="fig-title" x="360" y="34" font-size="11.5" text-anchor="middle">算法的 5 个必要特性（缺一不可，否则不能称为算法）</text>
    <text class="fig-blue" x="89" y="70" font-size="11.5" text-anchor="middle">④ 输入</text>
    <text class="fig-label" x="89" y="88" font-size="11" text-anchor="middle">≥ 0 个</text>
    <text class="fig-muted" x="89" y="106" font-size="9.5" text-anchor="middle">可内置常量无输入</text>
    <text class="fig-orange" x="248" y="71" font-size="11" text-anchor="middle">① 有穷性</text>
    <text class="fig-label" x="248" y="88" font-size="10" text-anchor="middle">有限步+有限时</text>
    <text class="fig-muted" x="248" y="106" font-size="9.5" text-anchor="middle">死循环不是算法</text>
    <text class="fig-purple" x="364" y="71" font-size="11" text-anchor="middle">② 确定性</text>
    <text class="fig-label" x="364" y="88" font-size="10" text-anchor="middle">指令无二义性</text>
    <text class="fig-muted" x="364" y="106" font-size="9.5" text-anchor="middle">同输入必同输出</text>
    <text class="fig-green" x="477" y="71" font-size="11" text-anchor="middle">③ 可行性</text>
    <text class="fig-label" x="477" y="88" font-size="10" text-anchor="middle">基本运算可解</text>
    <text class="fig-muted" x="477" y="106" font-size="9.5" text-anchor="middle">有限次内可实现</text>
    <text class="fig-accent" x="631" y="70" font-size="11.5" text-anchor="middle">⑤ 输出</text>
    <text class="fig-label" x="631" y="88" font-size="11" text-anchor="middle">★ ≥ 1 个</text>
    <text class="fig-muted" x="631" y="106" font-size="9.5" text-anchor="middle">必有至少1个输出</text>
    <text class="fig-title" x="360" y="168" font-size="11.5" text-anchor="middle">好算法的 4 大设计目标（判定优劣的工程准则）</text>
    <text class="fig-green" x="108" y="195" font-size="11" text-anchor="middle">正确性（首要）</text>
    <text class="fig-muted" x="108" y="211" font-size="9.5" text-anchor="middle">合法输入得正确结果</text>
    <text class="fig-blue" x="276" y="195" font-size="11" text-anchor="middle">可读性</text>
    <text class="fig-muted" x="276" y="211" font-size="9.5" text-anchor="middle">逻辑清晰便于阅读维护</text>
    <text class="fig-orange" x="444" y="195" font-size="11" text-anchor="middle">健壮性（容错）</text>
    <text class="fig-muted" x="444" y="211" font-size="9.5" text-anchor="middle">非法输入能妥善处理</text>
    <text class="fig-purple" x="612" y="195" font-size="11" text-anchor="middle">高效与低存储</text>
    <text class="fig-muted" x="612" y="211" font-size="9.5" text-anchor="middle">时间复杂度低·省空间</text>
  </svg>
  <figcaption>图 1-6：算法五大必要特性（注意输入 ≥ 0 而输出必须 ≥ 1）与四大评价目标对照</figcaption>
</figure>

### 抽象数据类型（ADT）

- **ADT = 数据对象 + 数据关系 + 基本操作**
- 只关心「能做什么」，不关心「怎么实现」
- 例：ADT 栈对外提供 `Push(x)`、`Pop()`、`GetTop()`、`IsEmpty()`；内部用数组还是链表实现，使用者不需要知道
- 价值：接口与实现分离，同一个 ADT 可以有不同的存储实现

<figure class="cpd-figure not-content" id="ds40-img5-fig-7">
  <svg viewBox="0 0 700 236" role="img" aria-label="ADT栈的LIFO黑盒操作序列与内部状态变化轨迹图">
    <defs>
      <marker id="ds40-img5-arr-7" viewBox="0 0 10 10" refX="8.5" refY="5" markerWidth="6.5" markerHeight="6.5" markerUnits="userSpaceOnUse" orient="auto-start-reverse">
        <path d="M 1 1.5 L 8.5 5 L 1 8.5 z" fill="context-stroke" stroke="none"/>
      </marker>
    </defs>
    <line class="fig-line-accent" x1="172" y1="118" x2="186" y2="118" marker-end="url(#ds40-img5-arr-7)"/>
    <line class="fig-line-accent" x1="342" y1="118" x2="356" y2="118" marker-end="url(#ds40-img5-arr-7)"/>
    <line class="fig-line-accent" x1="512" y1="118" x2="526" y2="118" marker-end="url(#ds40-img5-arr-7)"/>
    <rect class="fig-box-accent" x="18" y="16" width="664" height="28" rx="6"/>
    <rect class="fig-box-subtle" x="18" y="54" width="154" height="128" rx="8"/>
    <rect class="fig-box-subtle" x="188" y="54" width="154" height="128" rx="8"/>
    <rect class="fig-box-subtle" x="358" y="54" width="154" height="128" rx="8"/>
    <rect class="fig-box-subtle" x="528" y="54" width="154" height="128" rx="8"/>
    <rect class="fig-box-dashed" x="48" y="96" width="94" height="60" rx="4"/>
    <rect class="fig-box-blue" x="218" y="128" width="94" height="26" rx="4"/>
    <rect class="fig-box-green" x="218" y="96" width="94" height="26" rx="4"/>
    <rect class="fig-box-blue" x="388" y="128" width="94" height="26" rx="4"/>
    <rect class="fig-box-orange" x="388" y="96" width="94" height="26" rx="4"/>
    <rect class="fig-box-blue" x="558" y="128" width="94" height="26" rx="4"/>
    <rect class="fig-box-dashed" x="558" y="96" width="94" height="26" rx="4"/>
    <rect class="fig-box-purple" x="18" y="192" width="664" height="28" rx="6"/>
    <text class="fig-accent" x="350" y="34" font-size="11.5" text-anchor="middle">ADT 栈（Stack）黑盒契约：仅通过栈顶接口读写，严格遵守后进先出（LIFO）</text>
    <text class="fig-label" x="95" y="76" font-size="10.5" text-anchor="middle">1. InitStack(&amp;S)</text>
    <text class="fig-muted" x="95" y="130" font-size="10" text-anchor="middle">空栈（无元素）</text>
    <text class="fig-muted" x="95" y="172" font-size="9.5" text-anchor="middle">IsEmpty(S) = true</text>
    <text class="fig-green" x="265" y="76" font-size="10.5" text-anchor="middle">2. Push(A), Push(B)</text>
    <text class="fig-green" x="265" y="113" font-size="10.5" text-anchor="middle">B（栈顶）</text>
    <text class="fig-blue" x="265" y="145" font-size="10.5" text-anchor="middle">A（栈底）</text>
    <text class="fig-muted" x="265" y="172" font-size="9.5" text-anchor="middle">B 后入，压在 A 上</text>
    <text class="fig-orange" x="435" y="76" font-size="10.5" text-anchor="middle">3. GetTop(S) → B</text>
    <text class="fig-orange" x="435" y="113" font-size="10.5" text-anchor="middle">B（只读不出）</text>
    <text class="fig-blue" x="435" y="145" font-size="10.5" text-anchor="middle">A</text>
    <text class="fig-muted" x="435" y="172" font-size="9.5" text-anchor="middle">栈内元素个数不变</text>
    <text class="fig-purple" x="605" y="76" font-size="10.5" text-anchor="middle">4. Pop(&amp;S, &amp;x) → B</text>
    <text class="fig-muted" x="605" y="113" font-size="10" text-anchor="middle">（已弹出 x = B）</text>
    <text class="fig-blue" x="605" y="145" font-size="10.5" text-anchor="middle">A（新栈顶）</text>
    <text class="fig-muted" x="605" y="172" font-size="9.5" text-anchor="middle">栈顶回退到 A</text>
    <text class="fig-purple" x="350" y="210" font-size="10.5" text-anchor="middle">封装价值：无论内部用数组 top-- 还是链表头删，外部只感知上述统一语义</text>
  </svg>
  <figcaption>图 1-7a：ADT 栈的黑盒状态转移轨迹——接口只暴露逻辑行为，屏蔽顺序栈与链栈的底层实现细节</figcaption>
</figure>

<figure class="cpd-figure not-content" id="ds40-w1-fig-7">
  <svg viewBox="0 0 720 262" role="img" aria-label="抽象数据类型ADT栈的外部接口契约与底层顺序栈、链栈双实现解耦架构图">
    <defs>
      <marker id="ds40-w1-f7-arr" viewBox="0 0 10 10" refX="8.5" refY="5" markerWidth="6" markerHeight="6" markerUnits="userSpaceOnUse" orient="auto-start-reverse">
        <path d="M 1 1.5 L 8.5 5 L 1 8.5 z" fill="context-stroke" stroke="none"/>
      </marker>
    </defs>
    <rect class="fig-box-subtle" x="18" y="16" width="684" height="104" rx="8"/>
    <rect class="fig-box-subtle" x="18" y="156" width="334" height="90" rx="8"/>
    <rect class="fig-box-subtle" x="368" y="156" width="334" height="90" rx="8"/>
    <line class="fig-line-accent" x1="150" y1="68" x2="180" y2="68" marker-end="url(#ds40-w1-f7-arr)"/>
    <line class="fig-line-dashed" x1="24" y1="138" x2="696" y2="138"/>
    <line class="fig-line" x1="220" y1="110" x2="220" y2="164" stroke="var(--sl-color-blue)" style="stroke: var(--sl-color-blue)" marker-end="url(#ds40-w1-f7-arr)"/>
    <line class="fig-line" x1="540" y1="110" x2="540" y2="164" stroke="var(--sl-color-green)" style="stroke: var(--sl-color-green)" marker-end="url(#ds40-w1-f7-arr)"/>
    <rect class="fig-box" x="30" y="38" width="120" height="60" rx="6"/>
    <rect class="fig-box-accent" x="182" y="26" width="506" height="84" rx="6"/>
    <rect class="fig-box" x="196" y="56" width="112" height="42" rx="4"/>
    <rect class="fig-box" x="318" y="56" width="112" height="42" rx="4"/>
    <rect class="fig-box" x="440" y="56" width="112" height="42" rx="4"/>
    <rect class="fig-box" x="562" y="56" width="112" height="42" rx="4"/>
    <rect class="fig-box-purple" x="256" y="128" width="208" height="20" rx="4"/>
    <rect class="fig-box-blue" x="30" y="166" width="310" height="70" rx="6"/>
    <rect class="fig-box-green" x="380" y="166" width="310" height="70" rx="6"/>
    <text class="fig-label" x="90" y="62" font-size="11" text-anchor="middle">上层应用程序</text>
    <text class="fig-muted" x="90" y="80" font-size="9.5" text-anchor="middle">只调接口，不问实现</text>
    <text class="fig-accent" x="435" y="45" font-size="11.5" text-anchor="middle">ADT Stack = (数据对象 D, 栈顶关系 S, 基本操作集 P)</text>
    <text class="fig-label" x="252" y="74" font-size="10.5" text-anchor="middle">Push(&amp;S, x)</text>
    <text class="fig-muted" x="252" y="90" font-size="9.5" text-anchor="middle">新元素入栈</text>
    <text class="fig-label" x="374" y="74" font-size="10.5" text-anchor="middle">Pop(&amp;S, &amp;x)</text>
    <text class="fig-muted" x="374" y="90" font-size="9.5" text-anchor="middle">栈顶出栈</text>
    <text class="fig-label" x="496" y="74" font-size="10.5" text-anchor="middle">GetTop(S)</text>
    <text class="fig-muted" x="496" y="90" font-size="9.5" text-anchor="middle">读栈顶元素</text>
    <text class="fig-label" x="618" y="74" font-size="10.5" text-anchor="middle">IsEmpty(S)</text>
    <text class="fig-muted" x="618" y="90" font-size="9.5" text-anchor="middle">判断栈是否空</text>
    <text class="fig-purple" x="360" y="142" font-size="10" text-anchor="middle">★ 抽象屏障：接口与底层存储完全分离</text>
    <text class="fig-blue" x="185" y="186" font-size="11" text-anchor="middle">底层实现 A：顺序栈（连续数组）</text>
    <text class="fig-label" x="185" y="205" font-size="10" text-anchor="middle">data[MaxSize] + top 游标</text>
    <text class="fig-muted" x="185" y="223" font-size="9.5" text-anchor="middle">存储密度=1，存取快；预分配固定容量</text>
    <text class="fig-green" x="535" y="186" font-size="11" text-anchor="middle">底层实现 B：链栈（单链表头插）</text>
    <text class="fig-label" x="535" y="205" font-size="10" text-anchor="middle">LNode *head 指向栈顶结点</text>
    <text class="fig-muted" x="535" y="223" font-size="9.5" text-anchor="middle">动态扩缩无上溢；每个结点含额外指针域</text>
  </svg>
  <figcaption>图 1-7：ADT 抽象屏障——对外暴露统一的“数据对象+关系+基本操作”，底层存储可在数组与链表间透明替换</figcaption>
</figure>

## 二、复杂度

### 时间复杂度

- 看基本操作的执行次数随 $n$ 的增长趋势，不数具体秒数
- 大 $O$ 只保留最高阶项、忽略系数
- 常见量级（从小到大，分两行连读）：

$$
O(1) < O(\log n) < O(n) < O(n\log n)
$$

$$
O(n^2) < O(n^3) < O(2^n) < O(n!)
$$

- 口诀：**常对幂指阶**
- 典型例子：
  - 单层循环累加 $1 \sim n$：执行 $n$ 次 → $O(n)$
  - 每轮 `i *= 2`：$1,2,4,\dots$ 增长到 $n$ 约需 $\log_2 n$ 次 → $O(\log n)$

<figure class="cpd-figure not-content" id="ds40-img5-fig-8">
  <svg viewBox="0 0 700 264" role="img" aria-label="时间复杂度增长曲线坐标图与408嵌套循环加法乘法法则速算模板">
    <defs>
      <marker id="ds40-img5-arr-8" viewBox="0 0 10 10" refX="8.5" refY="5" markerWidth="6.5" markerHeight="6.5" markerUnits="userSpaceOnUse" orient="auto-start-reverse">
        <path d="M 1 1.5 L 8.5 5 L 1 8.5 z" fill="context-stroke" stroke="none"/>
      </marker>
    </defs>
    <rect class="fig-box-subtle" x="18" y="16" width="330" height="232" rx="8"/>
    <rect class="fig-box-subtle" x="362" y="16" width="320" height="232" rx="8"/>
    <line class="fig-line" x1="56" y1="218" x2="56" y2="52" marker-end="url(#ds40-img5-arr-8)"/>
    <line class="fig-line" x1="56" y1="218" x2="322" y2="218" marker-end="url(#ds40-img5-arr-8)"/>
    <path class="fig-line" fill="none" d="M 56 206 L 314 206" stroke="var(--sl-color-green)" style="stroke: var(--sl-color-green)"/>
    <path class="fig-line" fill="none" d="M 56 206 Q 132 184 314 176" stroke="var(--sl-color-green)" style="stroke: var(--sl-color-green)"/>
    <path class="fig-line" fill="none" d="M 56 218 L 314 128" stroke="var(--sl-color-blue)" style="stroke: var(--sl-color-blue)"/>
    <path class="fig-line" fill="none" d="M 56 218 Q 186 168 314 78" stroke="var(--sl-color-orange)" style="stroke: var(--sl-color-orange)"/>
    <path class="fig-line" fill="none" d="M 56 218 Q 170 194 246 58" stroke="var(--sl-color-purple)" style="stroke: var(--sl-color-purple)"/>
    <path class="fig-line" fill="none" d="M 56 218 Q 114 208 142 58" stroke="var(--sl-color-red)" style="stroke: var(--sl-color-red)"/>
    <rect class="fig-box-green" x="374" y="48" width="296" height="58" rx="6"/>
    <rect class="fig-box-blue" x="374" y="114" width="296" height="60" rx="6"/>
    <rect class="fig-box-orange" x="374" y="182" width="296" height="56" rx="6"/>
    <text class="fig-title" x="183" y="36" font-size="11.5" text-anchor="middle">随输入规模 n 增大的增长曲线分化</text>
    <text class="fig-muted" x="48" y="64" font-size="9.5" text-anchor="end">T(n)</text>
    <text class="fig-muted" x="330" y="232" font-size="9.5" text-anchor="end">规模 n</text>
    <text class="fig-red" x="150" y="66" font-size="10">O(2ⁿ)</text>
    <text class="fig-purple" x="254" y="66" font-size="10">O(n²)</text>
    <text class="fig-orange" x="316" y="76" font-size="10" text-anchor="end">O(n log n)</text>
    <text class="fig-blue" x="316" y="122" font-size="10" text-anchor="end">O(n)</text>
    <text class="fig-green" x="316" y="170" font-size="10" text-anchor="end">O(log n)</text>
    <text class="fig-green" x="316" y="198" font-size="10" text-anchor="end">O(1)</text>
    <text class="fig-muted" x="183" y="238" font-size="9.5" text-anchor="middle">n 较大时，指数与平方级迅速拉开与对数/线性的差距</text>
    <text class="fig-title" x="522" y="36" font-size="11.5" text-anchor="middle">408 代码时间复杂度三大速算模板</text>
    <text class="fig-green" x="388" y="68" font-size="10.5">1. 顺序并列（加法法则：取最高阶）</text>
    <text class="fig-label" x="388" y="88" font-size="10">T(n) = O(n) + O(n²) ⇒ O(n²)（省去低阶与常数）</text>
    <text class="fig-blue" x="388" y="134" font-size="10.5">2. 独立嵌套（乘法法则：内外相乘）</text>
    <text class="fig-label" x="388" y="152" font-size="10">for(i=0; i&lt;n; i++) for(j=0; j&lt;n; j++)</text>
    <text class="fig-muted" x="388" y="167" font-size="9.5">外层 n 次 × 内层 n 次 ⇒ O(n²)</text>
    <text class="fig-orange" x="388" y="201" font-size="10.5">3. 依赖内层（等差求和：看频度总和）</text>
    <text class="fig-label" x="388" y="218" font-size="10">for(i=0; i&lt;n; i++) for(j=0; j&lt;i; j++)</text>
    <text class="fig-muted" x="388" y="232" font-size="9.5">0 + 1 + ··· + (n - 1) = n(n - 1) / 2 ⇒ O(n²)</text>
  </svg>
  <figcaption>图 2-1a：时间复杂度增长曲线对比与“加法取最大、乘法看嵌套、依赖用求和”三大速算模型</figcaption>
</figure>

<figure class="cpd-figure not-content" id="ds40-w1-fig-8">
  <svg viewBox="0 0 720 278" role="img" aria-label="时间复杂度常对幂指阶增长阶梯与O(n)及O(log n)循环步进轨迹对比">
    <defs>
      <marker id="ds40-w1-f8-arr" viewBox="0 0 10 10" refX="8.5" refY="5" markerWidth="6" markerHeight="6" markerUnits="userSpaceOnUse" orient="auto-start-reverse">
        <path d="M 1 1.5 L 8.5 5 L 1 8.5 z" fill="context-stroke" stroke="none"/>
      </marker>
    </defs>
    <rect class="fig-box-subtle" x="18" y="16" width="332" height="246" rx="8"/>
    <rect class="fig-box-subtle" x="366" y="16" width="336" height="246" rx="8"/>
    <line class="fig-line" x1="426" y1="88" x2="440" y2="88" stroke="var(--sl-color-blue)" style="stroke: var(--sl-color-blue)" marker-end="url(#ds40-w1-f8-arr)"/>
    <line class="fig-line" x1="476" y1="88" x2="490" y2="88" stroke="var(--sl-color-blue)" style="stroke: var(--sl-color-blue)" marker-end="url(#ds40-w1-f8-arr)"/>
    <line class="fig-line" x1="526" y1="88" x2="542" y2="88" stroke="var(--sl-color-blue)" style="stroke: var(--sl-color-blue)" marker-end="url(#ds40-w1-f8-arr)"/>
    <line class="fig-line" x1="586" y1="88" x2="602" y2="88" stroke="var(--sl-color-blue)" style="stroke: var(--sl-color-blue)" marker-end="url(#ds40-w1-f8-arr)"/>
    <line class="fig-line" x1="434" y1="194" x2="448" y2="194" stroke="var(--sl-color-green)" style="stroke: var(--sl-color-green)" marker-end="url(#ds40-w1-f8-arr)"/>
    <line class="fig-line" x1="494" y1="194" x2="508" y2="194" stroke="var(--sl-color-green)" style="stroke: var(--sl-color-green)" marker-end="url(#ds40-w1-f8-arr)"/>
    <line class="fig-line" x1="554" y1="194" x2="568" y2="194" stroke="var(--sl-color-green)" style="stroke: var(--sl-color-green)" marker-end="url(#ds40-w1-f8-arr)"/>
    <line class="fig-line" x1="614" y1="194" x2="628" y2="194" stroke="var(--sl-color-green)" style="stroke: var(--sl-color-green)" marker-end="url(#ds40-w1-f8-arr)"/>
    <rect class="fig-box-green" x="28" y="48" width="88" height="20" rx="3"/>
    <rect class="fig-box-green" x="122" y="51" width="12" height="14" rx="2"/>
    <rect class="fig-box-green" x="28" y="73" width="88" height="20" rx="3"/>
    <rect class="fig-box-green" x="122" y="76" width="24" height="14" rx="2"/>
    <rect class="fig-box-blue" x="28" y="98" width="88" height="20" rx="3"/>
    <rect class="fig-box-blue" x="122" y="101" width="40" height="14" rx="2"/>
    <rect class="fig-box-blue" x="28" y="123" width="88" height="20" rx="3"/>
    <rect class="fig-box-blue" x="122" y="126" width="56" height="14" rx="2"/>
    <rect class="fig-box-orange" x="28" y="148" width="88" height="20" rx="3"/>
    <rect class="fig-box-orange" x="122" y="151" width="74" height="14" rx="2"/>
    <rect class="fig-box-orange" x="28" y="173" width="88" height="20" rx="3"/>
    <rect class="fig-box-orange" x="122" y="176" width="92" height="14" rx="2"/>
    <rect class="fig-box-red" x="28" y="198" width="88" height="20" rx="3"/>
    <rect class="fig-box-red" x="122" y="201" width="114" height="14" rx="2"/>
    <rect class="fig-box-red" x="28" y="223" width="88" height="20" rx="3"/>
    <rect class="fig-box-red" x="122" y="226" width="136" height="14" rx="2"/>
    <rect class="fig-box" x="378" y="46" width="312" height="92" rx="6"/>
    <rect class="fig-box-blue" x="392" y="76" width="34" height="24" rx="3"/>
    <rect class="fig-box-blue" x="442" y="76" width="34" height="24" rx="3"/>
    <rect class="fig-box-blue" x="492" y="76" width="34" height="24" rx="3"/>
    <rect class="fig-box" x="544" y="76" width="42" height="24" rx="3"/>
    <rect class="fig-box-blue" x="604" y="76" width="46" height="24" rx="3"/>
    <rect class="fig-box" x="378" y="150" width="312" height="100" rx="6"/>
    <rect class="fig-box-green" x="390" y="182" width="44" height="24" rx="3"/>
    <rect class="fig-box-green" x="450" y="182" width="44" height="24" rx="3"/>
    <rect class="fig-box-green" x="510" y="182" width="44" height="24" rx="3"/>
    <rect class="fig-box-green" x="570" y="182" width="44" height="24" rx="3"/>
    <rect class="fig-box-green" x="630" y="182" width="48" height="24" rx="3"/>
    <text class="fig-title" x="184" y="36" font-size="11.5" text-anchor="middle">“常·对·幂·指·阶”增长阶梯 (n=16)</text>
    <text class="fig-label" x="72" y="62" font-size="10" text-anchor="middle">O(1)</text>
    <text class="fig-muted" x="338" y="62" font-size="9.5" text-anchor="end">常 · 1 次</text>
    <text class="fig-label" x="72" y="87" font-size="10" text-anchor="middle">O(log₂n)</text>
    <text class="fig-muted" x="338" y="87" font-size="9.5" text-anchor="end">对 · 4 次</text>
    <text class="fig-label" x="72" y="112" font-size="10" text-anchor="middle">O(n)</text>
    <text class="fig-muted" x="338" y="112" font-size="9.5" text-anchor="end">幂 · 16 次</text>
    <text class="fig-label" x="72" y="137" font-size="10" text-anchor="middle">O(n log₂n)</text>
    <text class="fig-muted" x="338" y="137" font-size="9.5" text-anchor="end">幂对 · 64 次</text>
    <text class="fig-label" x="72" y="162" font-size="10" text-anchor="middle">O(n²)</text>
    <text class="fig-muted" x="338" y="162" font-size="9.5" text-anchor="end">平方 · 256 次</text>
    <text class="fig-label" x="72" y="187" font-size="10" text-anchor="middle">O(n³)</text>
    <text class="fig-muted" x="338" y="187" font-size="9.5" text-anchor="end">立方 · 4096 次</text>
    <text class="fig-label" x="72" y="212" font-size="10" text-anchor="middle">O(2ⁿ)</text>
    <text class="fig-muted" x="338" y="212" font-size="9.5" text-anchor="end">指 · 65536 次</text>
    <text class="fig-label" x="72" y="237" font-size="10" text-anchor="middle">O(n!)</text>
    <text class="fig-muted" x="338" y="237" font-size="9.5" text-anchor="end">阶 · 2×10¹³</text>
    <text class="fig-title" x="534" y="36" font-size="11.5" text-anchor="middle">循环步长决定执行次数（以 n=16 为例）</text>
    <text class="fig-blue" x="390" y="65" font-size="10.5">① 逐一累加：for (i = 1; i &lt;= n; i++)</text>
    <text class="fig-label" x="409" y="92" font-size="10" text-anchor="middle">1</text>
    <text class="fig-label" x="459" y="92" font-size="10" text-anchor="middle">2</text>
    <text class="fig-label" x="509" y="92" font-size="10" text-anchor="middle">3</text>
    <text class="fig-muted" x="565" y="92" font-size="10" text-anchor="middle">···</text>
    <text class="fig-label" x="627" y="92" font-size="10" text-anchor="middle">16</text>
    <text class="fig-muted" x="534" y="122" font-size="10" text-anchor="middle">每步 +1，共走 n = 16 步 ⇒ T(n) = O(n)</text>
    <text class="fig-green" x="390" y="170" font-size="10.5">② 指数倍增：for (i = 1; i &lt;= n; i *= 2)</text>
    <text class="fig-label" x="412" y="198" font-size="10" text-anchor="middle">i=1</text>
    <text class="fig-label" x="472" y="198" font-size="10" text-anchor="middle">i=2</text>
    <text class="fig-label" x="532" y="198" font-size="10" text-anchor="middle">i=4</text>
    <text class="fig-label" x="592" y="198" font-size="10" text-anchor="middle">i=8</text>
    <text class="fig-label" x="654" y="198" font-size="10" text-anchor="middle">i=16</text>
    <text class="fig-label" x="534" y="224" font-size="10" text-anchor="middle">设走 k 步满足 2ᵏ = n ⇒ k = log₂n</text>
    <text class="fig-muted" x="534" y="241" font-size="9.5" text-anchor="middle">每步 ×2，迅速逼近上界 ⇒ T(n) = O(log n)</text>
  </svg>
  <figcaption>图 2-1：时间复杂度量级对比（记住口诀“常对幂指阶”）与循环变量倍增带来的对数级加速原理</figcaption>
</figure>

### 空间复杂度

- 只算算法额外占用的**辅助空间**，不算输入本身
- $S(n)=O(1)$：只用到常数个额外变量，称**原地工作**
- $S(n)=O(n)$：额外开了与 $n$ 成正比的数组
- 递归：系统栈也要计入，递归深度为 $d$ 时空间为 $O(d)$

<figure class="cpd-figure not-content" id="ds40-img5-fig-9">
  <svg viewBox="0 0 700 256" role="img" aria-label="递归调用栈随深度压栈达到峰值再逐层弹栈释放的过程与迭代空间对比">
    <defs>
      <marker id="ds40-img5-arr-9" viewBox="0 0 10 10" refX="8.5" refY="5" markerWidth="6.5" markerHeight="6.5" markerUnits="userSpaceOnUse" orient="auto-start-reverse">
        <path d="M 1 1.5 L 8.5 5 L 1 8.5 z" fill="context-stroke" stroke="none"/>
      </marker>
    </defs>
    <rect class="fig-box-subtle" x="18" y="16" width="448" height="224" rx="8"/>
    <rect class="fig-box-subtle" x="480" y="16" width="202" height="224" rx="8"/>
    <line class="fig-line-accent" x1="126" y1="138" x2="138" y2="138" marker-end="url(#ds40-img5-arr-9)"/>
    <line class="fig-line-accent" x1="234" y1="138" x2="246" y2="138" marker-end="url(#ds40-img5-arr-9)"/>
    <line class="fig-line-accent" x1="342" y1="138" x2="354" y2="138" marker-end="url(#ds40-img5-arr-9)"/>
    <rect class="fig-box-blue" x="30" y="176" width="94" height="26" rx="4"/>
    <rect class="fig-box-blue" x="140" y="176" width="92" height="26" rx="4"/>
    <rect class="fig-box-purple" x="140" y="144" width="92" height="26" rx="4"/>
    <rect class="fig-box-blue" x="248" y="176" width="92" height="26" rx="4"/>
    <rect class="fig-box-purple" x="248" y="144" width="92" height="26" rx="4"/>
    <rect class="fig-box-orange" x="248" y="112" width="92" height="26" rx="4"/>
    <rect class="fig-box-blue" x="356" y="176" width="96" height="26" rx="4"/>
    <rect class="fig-box-purple" x="356" y="144" width="96" height="26" rx="4"/>
    <rect class="fig-box-orange" x="356" y="112" width="96" height="26" rx="4"/>
    <rect class="fig-box-accent" x="356" y="80" width="96" height="26" rx="4"/>
    <rect class="fig-box-purple" x="492" y="48" width="178" height="72" rx="6"/>
    <rect class="fig-box-green" x="492" y="130" width="178" height="72" rx="6"/>
    <rect class="fig-box-accent" x="492" y="210" width="178" height="22" rx="4"/>
    <text class="fig-title" x="242" y="36" font-size="11.5" text-anchor="middle">递归调用栈轨迹（以 fact(4) 为例）：峰值深度决定空间</text>
    <text class="fig-muted" x="77" y="64" font-size="9.5" text-anchor="middle">第 1 步：入口</text>
    <text class="fig-label" x="77" y="193" font-size="10" text-anchor="middle">fact(4)</text>
    <text class="fig-muted" x="186" y="64" font-size="9.5" text-anchor="middle">第 2 步：压栈</text>
    <text class="fig-purple" x="186" y="161" font-size="10" text-anchor="middle">fact(3)</text>
    <text class="fig-label" x="186" y="193" font-size="10" text-anchor="middle">fact(4)</text>
    <text class="fig-muted" x="294" y="64" font-size="9.5" text-anchor="middle">第 3 步：压栈</text>
    <text class="fig-orange" x="294" y="129" font-size="10" text-anchor="middle">fact(2)</text>
    <text class="fig-purple" x="294" y="161" font-size="10" text-anchor="middle">fact(3)</text>
    <text class="fig-label" x="294" y="193" font-size="10" text-anchor="middle">fact(4)</text>
    <text class="fig-accent" x="404" y="64" font-size="9.5" text-anchor="middle">峰值深度 d = 4</text>
    <text class="fig-accent" x="404" y="97" font-size="10" text-anchor="middle">fact(1) → 弹栈</text>
    <text class="fig-orange" x="404" y="129" font-size="10" text-anchor="middle">fact(2)</text>
    <text class="fig-purple" x="404" y="161" font-size="10" text-anchor="middle">fact(3)</text>
    <text class="fig-label" x="404" y="193" font-size="10" text-anchor="middle">fact(4)</text>
    <text class="fig-muted" x="242" y="224" font-size="9.5" text-anchor="middle">返回阶段自顶向下逐帧释放；最深时同时存活 n 个活动记录 ⇒ S(n) = O(n)</text>
    <text class="fig-title" x="581" y="36" font-size="11" text-anchor="middle">递归 vs 原地迭代对比</text>
    <text class="fig-purple" x="581" y="68" font-size="10.5" text-anchor="middle">递归版 fact(n)</text>
    <text class="fig-label" x="581" y="88" font-size="10" text-anchor="middle">系统栈同时存 n 帧</text>
    <text class="fig-muted" x="581" y="106" font-size="9.5" text-anchor="middle">空间复杂度 S(n) = O(n)</text>
    <text class="fig-green" x="581" y="150" font-size="10.5" text-anchor="middle">迭代版 for(i=1..n)</text>
    <text class="fig-label" x="581" y="170" font-size="10" text-anchor="middle">仅用 i, ans 两个变量</text>
    <text class="fig-muted" x="581" y="188" font-size="9.5" text-anchor="middle">空间复杂度 S(n) = O(1)</text>
    <text class="fig-accent" x="581" y="225" font-size="9.5" text-anchor="middle">★ 递归栈算“最大同时存活数”</text>
  </svg>
  <figcaption>图 2-2a：递归空间复杂度核算原理——栈空间按调用树的最大同时存活深度 d 计算，而非累计调用次数</figcaption>
</figure>

<figure class="cpd-figure not-content" id="ds40-w1-fig-9">
  <svg viewBox="0 0 720 268" role="img" aria-label="空间复杂度核算边界与原地工作O(1)、辅助数组O(n)、递归调用栈O(d)对比图">
    <defs>
      <marker id="ds40-w1-f9-arr" viewBox="0 0 10 10" refX="8.5" refY="5" markerWidth="6" markerHeight="6" markerUnits="userSpaceOnUse" orient="auto-start-reverse">
        <path d="M 1 1.5 L 8.5 5 L 1 8.5 z" fill="context-stroke" stroke="none"/>
      </marker>
    </defs>
    <rect class="fig-box-subtle" x="18" y="16" width="684" height="44" rx="8"/>
    <rect class="fig-box-subtle" x="18" y="72" width="220" height="180" rx="8"/>
    <rect class="fig-box-subtle" x="250" y="72" width="220" height="180" rx="8"/>
    <rect class="fig-box-subtle" x="482" y="72" width="220" height="180" rx="8"/>
    <line class="fig-line-accent" x1="276" y1="38" x2="304" y2="38" marker-end="url(#ds40-w1-f9-arr)"/>
    <line class="fig-line" x1="664" y1="184" x2="664" y2="120" stroke="var(--sl-color-purple)" style="stroke: var(--sl-color-purple)" marker-end="url(#ds40-w1-f9-arr)"/>
    <rect class="fig-box" x="28" y="24" width="248" height="28" rx="5"/>
    <rect class="fig-box-accent" x="306" y="24" width="384" height="28" rx="5"/>
    <rect class="fig-box-green" x="28" y="82" width="200" height="26" rx="5"/>
    <rect class="fig-box-green" x="34" y="126" width="56" height="32" rx="4"/>
    <rect class="fig-box-green" x="100" y="126" width="56" height="32" rx="4"/>
    <rect class="fig-box-green" x="166" y="126" width="56" height="32" rx="4"/>
    <rect class="fig-box-blue" x="260" y="82" width="200" height="26" rx="5"/>
    <rect class="fig-box-blue" x="264" y="126" width="38" height="32" rx="2"/>
    <rect class="fig-box-blue" x="302" y="126" width="38" height="32" rx="2"/>
    <rect class="fig-box-blue" x="340" y="126" width="38" height="32" rx="2"/>
    <rect class="fig-box" x="378" y="126" width="34" height="32" rx="2"/>
    <rect class="fig-box-blue" x="412" y="126" width="44" height="32" rx="2"/>
    <rect class="fig-box-purple" x="492" y="82" width="200" height="26" rx="5"/>
    <rect class="fig-box-purple" x="498" y="118" width="150" height="20" rx="3"/>
    <rect class="fig-box" x="498" y="142" width="150" height="20" rx="3"/>
    <rect class="fig-box-purple" x="498" y="166" width="150" height="20" rx="3"/>
    <text class="fig-muted" x="152" y="42" font-size="10.5" text-anchor="middle">原输入数据 A[n]（不计入空间复杂度）</text>
    <text class="fig-accent" x="498" y="42" font-size="10.5" text-anchor="middle">★ S(n) 仅算：运行期额外申请的辅助变量 + 数组 + 递归工作栈</text>
    <text class="fig-green" x="128" y="99" font-size="11" text-anchor="middle">① 原地工作 S(n) = O(1)</text>
    <text class="fig-label" x="62" y="146" font-size="10.5" text-anchor="middle">i</text>
    <text class="fig-label" x="128" y="146" font-size="10.5" text-anchor="middle">j</text>
    <text class="fig-label" x="194" y="146" font-size="10.5" text-anchor="middle">temp</text>
    <text class="fig-label" x="128" y="186" font-size="10.5" text-anchor="middle">仅常数个辅助标量</text>
    <text class="fig-muted" x="128" y="205" font-size="9.5" text-anchor="middle">辅助空间不随 n 增大而增长</text>
    <text class="fig-muted" x="128" y="224" font-size="9.5" text-anchor="middle">例：顺序表双指针原地逆置</text>
    <text class="fig-blue" x="360" y="99" font-size="11" text-anchor="middle">② 辅助数组 S(n) = O(n)</text>
    <text class="fig-label" x="283" y="146" font-size="9.5" text-anchor="middle">B[0]</text>
    <text class="fig-label" x="321" y="146" font-size="9.5" text-anchor="middle">B[1]</text>
    <text class="fig-label" x="359" y="146" font-size="9.5" text-anchor="middle">B[2]</text>
    <text class="fig-muted" x="395" y="146" font-size="9.5" text-anchor="middle">···</text>
    <text class="fig-label" x="434" y="146" font-size="9.5" text-anchor="middle">B[n-1]</text>
    <text class="fig-label" x="360" y="186" font-size="10.5" text-anchor="middle">动态/静态开辟 n 规模空间</text>
    <text class="fig-muted" x="360" y="205" font-size="9.5" text-anchor="middle">空间开销与输入规模成正比</text>
    <text class="fig-muted" x="360" y="224" font-size="9.5" text-anchor="middle">例：归并排序辅助数组 B[n]</text>
    <text class="fig-purple" x="592" y="99" font-size="11" text-anchor="middle">③ 递归调用栈 S(n) = O(d)</text>
    <text class="fig-label" x="573" y="132" font-size="9.5" text-anchor="middle">栈顶：第 d 层递归帧</text>
    <text class="fig-muted" x="573" y="156" font-size="9.5" text-anchor="middle">··· 中间活动记录 ···</text>
    <text class="fig-label" x="573" y="180" font-size="9.5" text-anchor="middle">栈底：第 1 层入口帧</text>
    <text class="fig-purple" x="680" y="156" font-size="10" text-anchor="middle">深d</text>
    <text class="fig-label" x="592" y="206" font-size="10.5" text-anchor="middle">取决于最大递归调用深度 d</text>
    <text class="fig-muted" x="592" y="224" font-size="9.5" text-anchor="middle">例：快排最好 O(log n) · 最坏 O(n)</text>
  </svg>
  <figcaption>图 2-2：空间复杂度三类典型模型——牢记“输入数组本身不算，递归栈深度必须算”</figcaption>
</figure>

## 三、线性表：顺序表与链表

### 顺序表

- 用一段**地址连续**的存储单元依次存放元素：逻辑相邻 = 物理相邻
- 地址计算：

$$
\mathrm{LOC}(a_i) = \mathrm{LOC}(a_1) + (i-1) \times L
$$

其中 $L$ 为每个元素占用的字节数。例：$L=4$，$a_1$ 地址为 1000，则 $a_5$ 地址为 $1000 + 4 \times 4 = 1016$。
- **随机访问 $O(1)$**：按下标直接算出地址
- **插入 / 删除 $O(n)$**：需要移动元素
  - 插入：从后往前依次后移，平均移动 $n/2$ 个元素
  - 删除：从前往后依次前移，平均移动 $(n-1)/2$ 个元素
  - 表尾插入 / 删除为 $O(1)$
- 特点：支持随机访问、存储密度高；插入删除代价大，容量固定（动态扩容有额外开销）

<figure class="cpd-figure not-content" id="ds40-img6-fig-1">
  <svg viewBox="0 0 700 185" role="img" aria-label="顺序表的连续内存布局与地址计算公式">
    <defs>
      <marker id="ds40-img6-arr-1" viewBox="0 0 10 10" refX="8.5" refY="5" markerWidth="6" markerHeight="6" markerUnits="userSpaceOnUse" orient="auto-start-reverse">
        <path d="M 1 1.5 L 8.5 5 L 1 8.5 z" fill="context-stroke" stroke="none"/>
      </marker>
    </defs>
    <rect class="fig-box-subtle" x="16" y="12" width="668" height="161" rx="8"/>
    <text class="fig-title" x="350" y="34" font-size="11.5" text-anchor="middle">顺序表：逻辑相邻 = 物理连续，按下标直接计算物理地址 O(1)</text>
    <rect class="fig-box-accent" x="96" y="54" width="76" height="40" rx="3"/>
    <rect class="fig-box-blue" x="172" y="54" width="76" height="40" rx="3"/>
    <rect class="fig-box-blue" x="248" y="54" width="76" height="40" rx="3"/>
    <rect class="fig-box-blue" x="324" y="54" width="76" height="40" rx="3"/>
    <rect class="fig-box-orange" x="400" y="54" width="76" height="40" rx="3"/>
    <rect class="fig-box-blue" x="476" y="54" width="76" height="40" rx="3"/>
    <rect class="fig-box-dashed" x="552" y="54" width="108" height="40" rx="3"/>
    <text class="fig-muted" x="84" y="78" font-size="10" text-anchor="end">元素:</text>
    <text class="fig-muted" x="84" y="114" font-size="10" text-anchor="end">地址:</text>
    <text class="fig-label" x="134" y="79" font-size="11.5" text-anchor="middle">a₁</text>
    <text class="fig-label" x="210" y="79" font-size="11.5" text-anchor="middle">a₂</text>
    <text class="fig-label" x="286" y="79" font-size="11.5" text-anchor="middle">a₃</text>
    <text class="fig-label" x="362" y="79" font-size="11.5" text-anchor="middle">a₄</text>
    <text class="fig-orange" x="438" y="79" font-size="11.5" text-anchor="middle">a₅</text>
    <text class="fig-label" x="514" y="79" font-size="11.5" text-anchor="middle">a₆</text>
    <text class="fig-muted" x="606" y="79" font-size="10" text-anchor="middle">空闲单元</text>
    <text class="fig-accent" x="134" y="114" font-size="10" text-anchor="middle">1000</text>
    <text class="fig-muted" x="210" y="114" font-size="10" text-anchor="middle">1004</text>
    <text class="fig-muted" x="286" y="114" font-size="10" text-anchor="middle">1008</text>
    <text class="fig-muted" x="362" y="114" font-size="10" text-anchor="middle">1012</text>
    <text class="fig-orange" x="438" y="114" font-size="10" text-anchor="middle">1016</text>
    <text class="fig-muted" x="514" y="114" font-size="10" text-anchor="middle">1020</text>
    <text class="fig-muted" x="606" y="114" font-size="10" text-anchor="middle">1024 ...</text>
    <path class="fig-line" d="M 134 122 L 134 134 L 438 134 L 438 122" fill="none" stroke="var(--sl-color-orange)" marker-end="url(#ds40-img6-arr-1)"/>
    <rect class="fig-box" x="158" y="142" width="384" height="22" rx="4"/>
    <text class="fig-label" x="350" y="157" font-size="10" text-anchor="middle">LOC(a₅) = LOC(a₁) + (5 - 1) × 4 = 1000 + 16 = 1016</text>
  </svg>
  <figcaption>图 10：顺序表的连续内存布局与地址计算</figcaption>
</figure>

<figure class="cpd-figure not-content" id="ds40-img6-fig-2">
  <svg viewBox="0 0 700 200" role="img" aria-label="顺序表在 1008 处插入 X 时元素从后往前依次后移">
    <defs>
      <marker id="ds40-img6-arr-2" viewBox="0 0 10 10" refX="8.5" refY="5" markerWidth="6" markerHeight="6" markerUnits="userSpaceOnUse" orient="auto-start-reverse">
        <path d="M 1 1.5 L 8.5 5 L 1 8.5 z" fill="context-stroke" stroke="none"/>
      </marker>
    </defs>
    <rect class="fig-box-subtle" x="16" y="12" width="668" height="176" rx="8"/>
    <text class="fig-title" x="350" y="34" font-size="11.5" text-anchor="middle">顺序表插入 X（i = 3，地址 1008）：从后往前依次后移 j = length-1 downto i-1</text>
    <path class="fig-line" d="M 520 76 Q 558 52 594 76" fill="none" stroke="var(--sl-color-orange)" marker-end="url(#ds40-img6-arr-2)"/>
    <path class="fig-line" d="M 440 76 Q 478 52 514 76" fill="none" stroke="var(--sl-color-orange)" marker-end="url(#ds40-img6-arr-2)"/>
    <path class="fig-line" d="M 360 76 Q 398 52 434 76" fill="none" stroke="var(--sl-color-orange)" marker-end="url(#ds40-img6-arr-2)"/>
    <path class="fig-line" d="M 280 76 Q 318 52 354 76" fill="none" stroke="var(--sl-color-orange)" marker-end="url(#ds40-img6-arr-2)"/>
    <rect class="fig-box" x="80" y="80" width="76" height="38" rx="3"/>
    <rect class="fig-box" x="160" y="80" width="76" height="38" rx="3"/>
    <rect class="fig-box-orange" x="240" y="80" width="76" height="38" rx="3"/>
    <rect class="fig-box-blue" x="320" y="80" width="76" height="38" rx="3"/>
    <rect class="fig-box-blue" x="400" y="80" width="76" height="38" rx="3"/>
    <rect class="fig-box-blue" x="480" y="80" width="76" height="38" rx="3"/>
    <rect class="fig-box-green" x="560" y="80" width="76" height="38" rx="3"/>
    <text class="fig-label" x="118" y="103" font-size="11" text-anchor="middle">a₁</text>
    <text class="fig-label" x="198" y="103" font-size="11" text-anchor="middle">a₂</text>
    <text class="fig-orange" x="278" y="103" font-size="11" text-anchor="middle">X (1008)</text>
    <text class="fig-label" x="358" y="103" font-size="11" text-anchor="middle">a₃</text>
    <text class="fig-label" x="438" y="103" font-size="11" text-anchor="middle">a₄</text>
    <text class="fig-label" x="518" y="103" font-size="11" text-anchor="middle">a₅</text>
    <text class="fig-green" x="598" y="103" font-size="11" text-anchor="middle">a₆</text>
    <text class="fig-muted" x="278" y="136" font-size="9.5" text-anchor="middle">腾出空位</text>
    <text class="fig-orange" x="478" y="136" font-size="9.5" text-anchor="middle">a₃ ～ a₆ 依次后移 1 位（先移 a₆，再移 a₅…）</text>
    <text class="fig-muted" x="350" y="168" font-size="10" text-anchor="middle">共有 n+1 个可插位置，等概率平均移动次数 = n / 2，时间复杂度 O(n)</text>
  </svg>
  <figcaption>图 11：在 1008 处插入 X，a₃～a₆ 依次后移；平均移动次数与 O(n)</figcaption>
</figure>

<figure class="cpd-figure not-content" id="ds40-w1-fig-10">
  <svg viewBox="0 0 720 296" role="img" aria-label="顺序表连续地址计算公式LOC(ai)与插入后移、删除前移方向对比">
    <defs>
      <marker id="ds40-w1-f10-arr" viewBox="0 0 10 10" refX="8.5" refY="5" markerWidth="6" markerHeight="6" markerUnits="userSpaceOnUse" orient="auto-start-reverse">
        <path d="M 1 1.5 L 8.5 5 L 1 8.5 z" fill="context-stroke" stroke="none"/>
      </marker>
    </defs>
    <rect class="fig-box-subtle" x="18" y="16" width="684" height="118" rx="8"/>
    <rect class="fig-box-subtle" x="18" y="146" width="334" height="134" rx="8"/>
    <rect class="fig-box-subtle" x="368" y="146" width="334" height="134" rx="8"/>
    <path class="fig-line" d="M 248 194 Q 272 176 294 194" stroke="var(--sl-color-orange)" style="stroke: var(--sl-color-orange)" marker-end="url(#ds40-w1-f10-arr)"/>
    <path class="fig-line" d="M 200 194 Q 224 176 246 194" stroke="var(--sl-color-orange)" style="stroke: var(--sl-color-orange)" marker-end="url(#ds40-w1-f10-arr)"/>
    <path class="fig-line" d="M 152 194 Q 176 176 198 194" stroke="var(--sl-color-orange)" style="stroke: var(--sl-color-orange)" marker-end="url(#ds40-w1-f10-arr)"/>
    <path class="fig-line" d="M 556 194 Q 533 176 510 194" stroke="var(--sl-color-purple)" style="stroke: var(--sl-color-purple)" marker-end="url(#ds40-w1-f10-arr)"/>
    <path class="fig-line" d="M 606 194 Q 583 176 560 194" stroke="var(--sl-color-purple)" style="stroke: var(--sl-color-purple)" marker-end="url(#ds40-w1-f10-arr)"/>
    <path class="fig-line" d="M 656 194 Q 633 176 610 194" stroke="var(--sl-color-purple)" style="stroke: var(--sl-color-purple)" marker-end="url(#ds40-w1-f10-arr)"/>
    <rect class="fig-box-accent" x="114" y="64" width="76" height="36" rx="2"/>
    <rect class="fig-box-blue" x="190" y="64" width="76" height="36" rx="2"/>
    <rect class="fig-box-blue" x="266" y="64" width="76" height="36" rx="2"/>
    <rect class="fig-box-blue" x="342" y="64" width="76" height="36" rx="2"/>
    <rect class="fig-box-orange" x="418" y="64" width="76" height="36" rx="2"/>
    <rect class="fig-box-blue" x="494" y="64" width="76" height="36" rx="2"/>
    <rect class="fig-box-dashed" x="570" y="64" width="84" height="36" rx="2"/>
    <rect class="fig-box" x="34" y="196" width="48" height="28" rx="2"/>
    <rect class="fig-box" x="82" y="196" width="48" height="28" rx="2"/>
    <rect class="fig-box-orange" x="130" y="196" width="48" height="28" rx="2"/>
    <rect class="fig-box-blue" x="178" y="196" width="48" height="28" rx="2"/>
    <rect class="fig-box-blue" x="226" y="196" width="48" height="28" rx="2"/>
    <rect class="fig-box-blue" x="274" y="196" width="48" height="28" rx="2"/>
    <rect class="fig-box" x="386" y="196" width="50" height="28" rx="2"/>
    <rect class="fig-box" x="436" y="196" width="50" height="28" rx="2"/>
    <rect class="fig-box-purple" x="486" y="196" width="50" height="28" rx="2"/>
    <rect class="fig-box-purple" x="536" y="196" width="50" height="28" rx="2"/>
    <rect class="fig-box-purple" x="586" y="196" width="50" height="28" rx="2"/>
    <rect class="fig-box-dashed" x="636" y="196" width="48" height="28" rx="2"/>
    <text class="fig-title" x="360" y="34" font-size="11.5" text-anchor="middle">★ 随机存取 O(1)：LOC(a₅) = LOC(a₁) + (5 - 1)×4B = 1000 + 16 = 1016（位序 i 比下标大 1）</text>
    <text class="fig-muted" x="104" y="55" font-size="10" text-anchor="end">位序/下标:</text>
    <text class="fig-muted" x="104" y="86" font-size="10" text-anchor="end">数据元素:</text>
    <text class="fig-muted" x="104" y="118" font-size="10" text-anchor="end">物理地址:</text>
    <text class="fig-muted" x="152" y="55" font-size="9.5" text-anchor="middle">i=1 [0]</text>
    <text class="fig-muted" x="228" y="55" font-size="9.5" text-anchor="middle">i=2 [1]</text>
    <text class="fig-muted" x="304" y="55" font-size="9.5" text-anchor="middle">i=3 [2]</text>
    <text class="fig-muted" x="380" y="55" font-size="9.5" text-anchor="middle">i=4 [3]</text>
    <text class="fig-orange" x="456" y="55" font-size="9.5" text-anchor="middle">i=5 [4]</text>
    <text class="fig-muted" x="532" y="55" font-size="9.5" text-anchor="middle">i=6 [5]</text>
    <text class="fig-muted" x="612" y="55" font-size="9.5" text-anchor="middle">空闲 [6]</text>
    <text class="fig-label" x="152" y="86" font-size="11.5" text-anchor="middle">a₁</text>
    <text class="fig-label" x="228" y="86" font-size="11.5" text-anchor="middle">a₂</text>
    <text class="fig-label" x="304" y="86" font-size="11.5" text-anchor="middle">a₃</text>
    <text class="fig-label" x="380" y="86" font-size="11.5" text-anchor="middle">a₄</text>
    <text class="fig-orange" x="456" y="86" font-size="11.5" text-anchor="middle">a₅ ★</text>
    <text class="fig-label" x="532" y="86" font-size="11.5" text-anchor="middle">a₆</text>
    <text class="fig-muted" x="612" y="86" font-size="10" text-anchor="middle">MaxSize</text>
    <text class="fig-accent" x="152" y="118" font-size="10" text-anchor="middle">1000</text>
    <text class="fig-muted" x="228" y="118" font-size="10" text-anchor="middle">1004</text>
    <text class="fig-muted" x="304" y="118" font-size="10" text-anchor="middle">1008</text>
    <text class="fig-muted" x="380" y="118" font-size="10" text-anchor="middle">1012</text>
    <text class="fig-orange" x="456" y="118" font-size="10" text-anchor="middle">1016</text>
    <text class="fig-muted" x="532" y="118" font-size="10" text-anchor="middle">1020</text>
    <text class="fig-muted" x="612" y="118" font-size="10" text-anchor="middle">1024</text>
    <text class="fig-orange" x="185" y="166" font-size="11" text-anchor="middle">插入 X (i=3)：必须「从后往前」依次后移</text>
    <text class="fig-label" x="58" y="214" font-size="10" text-anchor="middle">a₁</text>
    <text class="fig-label" x="106" y="214" font-size="10" text-anchor="middle">a₂</text>
    <text class="fig-orange" x="154" y="214" font-size="10.5" text-anchor="middle">X</text>
    <text class="fig-label" x="202" y="214" font-size="10" text-anchor="middle">a₃→</text>
    <text class="fig-label" x="250" y="214" font-size="10" text-anchor="middle">a₄→</text>
    <text class="fig-label" x="298" y="214" font-size="10" text-anchor="middle">a₅</text>
    <text class="fig-label" x="185" y="246" font-size="10" text-anchor="middle">n+1 个插入位，平均移动 E_ins = n / 2</text>
    <text class="fig-muted" x="185" y="265" font-size="9.5" text-anchor="middle">防覆盖口诀：插元素先腾尾，复杂度 O(n)</text>
    <text class="fig-purple" x="535" y="166" font-size="11" text-anchor="middle">删除 a₃ (i=3)：必须「从前往后」依次前移</text>
    <text class="fig-label" x="411" y="214" font-size="10" text-anchor="middle">a₁</text>
    <text class="fig-label" x="461" y="214" font-size="10" text-anchor="middle">a₂</text>
    <text class="fig-label" x="511" y="214" font-size="10" text-anchor="middle">←a₄</text>
    <text class="fig-label" x="561" y="214" font-size="10" text-anchor="middle">←a₅</text>
    <text class="fig-label" x="611" y="214" font-size="10" text-anchor="middle">←a₆</text>
    <text class="fig-muted" x="660" y="214" font-size="9.5" text-anchor="middle">空</text>
    <text class="fig-label" x="535" y="246" font-size="10" text-anchor="middle">n 个可删位，平均移动 E_del = (n - 1) / 2</text>
    <text class="fig-muted" x="535" y="265" font-size="9.5" text-anchor="middle">仅表尾插删为 O(1)；其余位置均需搬移 O(n)</text>
  </svg>
  <figcaption>图 3-1：顺序表连续地址计算（注意位序 i 从 1 开始）与插入后移、删除前移的平均移动次数</figcaption>
</figure>

### 单链表

每个结点由**数据域**和**指针域**组成，靠指针把分散的结点串起来。

<figure class="cpd-figure not-content" id="ds40-img6-fig-3">
  <svg viewBox="0 0 700 175" role="img" aria-label="单链表的头结点与空表对比">
    <defs>
      <marker id="ds40-img6-arr-3" viewBox="0 0 10 10" refX="8.5" refY="5" markerWidth="6" markerHeight="6" markerUnits="userSpaceOnUse" orient="auto-start-reverse">
        <path d="M 1 1.5 L 8.5 5 L 1 8.5 z" fill="context-stroke" stroke="none"/>
      </marker>
    </defs>
    <rect class="fig-box-subtle" x="16" y="12" width="668" height="72" rx="8"/>
    <rect class="fig-box-subtle" x="16" y="94" width="668" height="68" rx="8"/>
    <line class="fig-line-accent" x1="80" y1="48" x2="118" y2="48" marker-end="url(#ds40-img6-arr-3)"/>
    <line class="fig-line-accent" x1="214" y1="48" x2="252" y2="48" marker-end="url(#ds40-img6-arr-3)"/>
    <line class="fig-line-accent" x1="340" y1="48" x2="378" y2="48" marker-end="url(#ds40-img6-arr-3)"/>
    <line class="fig-line-accent" x1="466" y1="48" x2="504" y2="48" marker-end="url(#ds40-img6-arr-3)"/>
    <rect class="fig-box-purple" x="34" y="34" width="46" height="28" rx="4"/>
    <rect class="fig-box-orange" x="120" y="32" width="64" height="32" rx="3"/>
    <rect class="fig-box" x="184" y="32" width="30" height="32" rx="3"/>
    <rect class="fig-box-blue" x="254" y="32" width="56" height="32" rx="3"/>
    <rect class="fig-box" x="310" y="32" width="30" height="32" rx="3"/>
    <rect class="fig-box-blue" x="380" y="32" width="56" height="32" rx="3"/>
    <rect class="fig-box" x="436" y="32" width="30" height="32" rx="3"/>
    <rect class="fig-box-blue" x="506" y="32" width="56" height="32" rx="3"/>
    <rect class="fig-box" x="562" y="32" width="30" height="32" rx="3"/>
    <text class="fig-purple" x="57" y="52" font-size="11" text-anchor="middle">L</text>
    <text class="fig-orange" x="152" y="52" font-size="10" text-anchor="middle">头结点</text>
    <text class="fig-muted" x="199" y="52" font-size="10" text-anchor="middle">·</text>
    <text class="fig-label" x="282" y="52" font-size="11" text-anchor="middle">a₁</text>
    <text class="fig-muted" x="325" y="52" font-size="10" text-anchor="middle">·</text>
    <text class="fig-label" x="408" y="52" font-size="11" text-anchor="middle">a₂</text>
    <text class="fig-muted" x="451" y="52" font-size="10" text-anchor="middle">·</text>
    <text class="fig-label" x="534" y="52" font-size="11" text-anchor="middle">aₙ</text>
    <text class="fig-muted" x="577" y="52" font-size="10.5" text-anchor="middle">∧</text>
    <text class="fig-muted" x="636" y="52" font-size="9.5" text-anchor="middle">非空表</text>
    <line class="fig-line-accent" x1="80" y1="128" x2="118" y2="128" marker-end="url(#ds40-img6-arr-3)"/>
    <rect class="fig-box-purple" x="34" y="114" width="46" height="28" rx="4"/>
    <rect class="fig-box-orange" x="120" y="112" width="64" height="32" rx="3"/>
    <rect class="fig-box" x="184" y="112" width="30" height="32" rx="3"/>
    <text class="fig-purple" x="57" y="132" font-size="11" text-anchor="middle">L</text>
    <text class="fig-orange" x="152" y="132" font-size="10" text-anchor="middle">头结点</text>
    <text class="fig-muted" x="199" y="132" font-size="10.5" text-anchor="middle">∧</text>
    <text class="fig-green" x="430" y="132" font-size="10.5" text-anchor="middle">空表判定：L-&gt;next == NULL（头结点始终存在，首元操作与其余结点完全统一）</text>
  </svg>
  <figcaption>图 12：头结点不存数据，让「第一个结点」和其他结点操作统一</figcaption>
</figure>

**头结点**不存数据，作用是让第一个数据结点和其他结点的插入、删除操作统一，不必特判「在表头操作」。

<figure class="cpd-figure not-content" id="ds40-w1-fig-11">
  <svg viewBox="0 0 720 236" role="img" aria-label="带头结点与不带头结点单链表的头指针指向、判空条件及首元操作对比">
    <defs>
      <marker id="ds40-w1-f11-arr" viewBox="0 0 10 10" refX="8.5" refY="5" markerWidth="6" markerHeight="6" markerUnits="userSpaceOnUse" orient="auto-start-reverse">
        <path d="M 1 1.5 L 8.5 5 L 1 8.5 z" fill="context-stroke" stroke="none"/>
      </marker>
    </defs>
    <rect class="fig-box-subtle" x="18" y="16" width="684" height="96" rx="8"/>
    <rect class="fig-box-subtle" x="18" y="124" width="684" height="96" rx="8"/>
    <line class="fig-line-accent" x1="74" y1="70" x2="106" y2="70" marker-end="url(#ds40-w1-f11-arr)"/>
    <line class="fig-line-accent" x1="196" y1="70" x2="228" y2="70" marker-end="url(#ds40-w1-f11-arr)"/>
    <line class="fig-line-accent" x1="306" y1="70" x2="338" y2="70" marker-end="url(#ds40-w1-f11-arr)"/>
    <line class="fig-line-accent" x1="416" y1="70" x2="448" y2="70" marker-end="url(#ds40-w1-f11-arr)"/>
    <line class="fig-line" x1="74" y1="178" x2="228" y2="178" stroke="var(--sl-color-orange)" style="stroke: var(--sl-color-orange)" marker-end="url(#ds40-w1-f11-arr)"/>
    <line class="fig-line" x1="306" y1="178" x2="338" y2="178" stroke="var(--sl-color-orange)" style="stroke: var(--sl-color-orange)" marker-end="url(#ds40-w1-f11-arr)"/>
    <line class="fig-line" x1="416" y1="178" x2="448" y2="178" stroke="var(--sl-color-orange)" style="stroke: var(--sl-color-orange)" marker-end="url(#ds40-w1-f11-arr)"/>
    <rect class="fig-box-purple" x="30" y="56" width="44" height="28" rx="4"/>
    <rect class="fig-box-orange" x="108" y="54" width="58" height="32" rx="3"/>
    <rect class="fig-box" x="166" y="54" width="30" height="32" rx="3"/>
    <rect class="fig-box-green" x="230" y="54" width="46" height="32" rx="3"/>
    <rect class="fig-box" x="276" y="54" width="30" height="32" rx="3"/>
    <rect class="fig-box-blue" x="340" y="54" width="46" height="32" rx="3"/>
    <rect class="fig-box" x="386" y="54" width="30" height="32" rx="3"/>
    <rect class="fig-box-blue" x="450" y="54" width="46" height="32" rx="3"/>
    <rect class="fig-box" x="496" y="54" width="30" height="32" rx="3"/>
    <rect class="fig-box-green" x="542" y="34" width="148" height="66" rx="5"/>
    <rect class="fig-box-purple" x="30" y="164" width="44" height="28" rx="4"/>
    <rect class="fig-box" x="108" y="168" width="90" height="20" rx="4"/>
    <rect class="fig-box-orange" x="230" y="162" width="46" height="32" rx="3"/>
    <rect class="fig-box" x="276" y="162" width="30" height="32" rx="3"/>
    <rect class="fig-box-blue" x="340" y="162" width="46" height="32" rx="3"/>
    <rect class="fig-box" x="386" y="162" width="30" height="32" rx="3"/>
    <rect class="fig-box-blue" x="450" y="162" width="46" height="32" rx="3"/>
    <rect class="fig-box" x="496" y="162" width="30" height="32" rx="3"/>
    <rect class="fig-box-orange" x="542" y="142" width="148" height="66" rx="5"/>
    <text class="fig-green" x="30" y="36" font-size="11">① 带头结点单链表（408 默认结构：头结点本身固定充当 a₁ 的前驱）</text>
    <text class="fig-purple" x="52" y="74" font-size="11" text-anchor="middle">L</text>
    <text class="fig-orange" x="137" y="74" font-size="10" text-anchor="middle">头结点</text>
    <text class="fig-muted" x="181" y="74" font-size="10" text-anchor="middle">·</text>
    <text class="fig-label" x="253" y="74" font-size="11" text-anchor="middle">a₁</text>
    <text class="fig-muted" x="291" y="74" font-size="10" text-anchor="middle">·</text>
    <text class="fig-label" x="363" y="74" font-size="11" text-anchor="middle">a₂</text>
    <text class="fig-muted" x="401" y="74" font-size="10" text-anchor="middle">·</text>
    <text class="fig-label" x="473" y="74" font-size="11" text-anchor="middle">aₙ</text>
    <text class="fig-muted" x="511" y="74" font-size="10.5" text-anchor="middle">∧</text>
    <text class="fig-green" x="616" y="53" font-size="10" text-anchor="middle">判空: L-&gt;next == NULL</text>
    <text class="fig-label" x="616" y="71" font-size="10" text-anchor="middle">表头插删无需改指针 L</text>
    <text class="fig-muted" x="616" y="88" font-size="9.5" text-anchor="middle">所有数据结点操作完全统一</text>
    <text class="fig-orange" x="30" y="144" font-size="11">② 不带头结点单链表（头指针 L 直接指向首元结点 a₁）</text>
    <text class="fig-purple" x="52" y="182" font-size="11" text-anchor="middle">L</text>
    <text class="fig-muted" x="153" y="182" font-size="9.5" text-anchor="middle">无虚拟前驱</text>
    <text class="fig-orange" x="253" y="182" font-size="11" text-anchor="middle">a₁</text>
    <text class="fig-muted" x="291" y="182" font-size="10" text-anchor="middle">·</text>
    <text class="fig-label" x="363" y="182" font-size="11" text-anchor="middle">a₂</text>
    <text class="fig-muted" x="401" y="182" font-size="10" text-anchor="middle">·</text>
    <text class="fig-label" x="473" y="182" font-size="11" text-anchor="middle">aₙ</text>
    <text class="fig-muted" x="511" y="182" font-size="10.5" text-anchor="middle">∧</text>
    <text class="fig-orange" x="616" y="161" font-size="10" text-anchor="middle">判空: L == NULL</text>
    <text class="fig-label" x="616" y="179" font-size="10" text-anchor="middle">在 i=1 插删必须修改 L</text>
    <text class="fig-muted" x="616" y="196" font-size="9.5" text-anchor="middle">需额外写 if (i==1) 分支</text>
  </svg>
  <figcaption>图 3-2：带头结点 vs 不带头结点单链表——审题务必先看清“是否带头结点”以防判空和首元边界写错</figcaption>
</figure>

**插入**：把 `s` 插到 `p` 之后，必须**先连后断**。

```c
s->next = p->next;  // ① 先让 s 接上 p 的后继
p->next = s;        // ② 再让 p 指向 s
```

顺序不能颠倒：若先执行 ②，`p->next` 已经被改成 `s`，`a2` 及其之后的整段链表就再也找不到了。

<figure class="cpd-figure not-content" id="ds40-img6-fig-4">
  <svg viewBox="0 0 700 185" role="img" aria-label="单链表后插操作先连后断两步指针修改">
    <defs>
      <marker id="ds40-img6-arr-4" viewBox="0 0 10 10" refX="8.5" refY="5" markerWidth="6" markerHeight="6" markerUnits="userSpaceOnUse" orient="auto-start-reverse">
        <path d="M 1 1.5 L 8.5 5 L 1 8.5 z" fill="context-stroke" stroke="none"/>
      </marker>
    </defs>
    <rect class="fig-box-subtle" x="16" y="12" width="668" height="161" rx="8"/>
    <line class="fig-line-dashed" x1="176" y1="56" x2="438" y2="56"/>
    <path class="fig-line" d="M 358 134 Q 418 130 448 76" fill="none" stroke="var(--sl-color-green)" marker-end="url(#ds40-img6-arr-4)"/>
    <path class="fig-line" d="M 160 72 Q 170 128 248 134" fill="none" stroke="var(--sl-color-orange)" marker-end="url(#ds40-img6-arr-4)"/>
    <rect class="fig-box-blue" x="80" y="40" width="66" height="32" rx="3"/>
    <rect class="fig-box" x="146" y="40" width="30" height="32" rx="3"/>
    <rect class="fig-box" x="260" y="28" width="90" height="18" rx="4"/>
    <rect class="fig-box-blue" x="440" y="40" width="66" height="32" rx="3"/>
    <rect class="fig-box" x="506" y="40" width="30" height="32" rx="3"/>
    <rect class="fig-box-green" x="250" y="118" width="76" height="32" rx="3"/>
    <rect class="fig-box" x="326" y="118" width="30" height="32" rx="3"/>
    <text class="fig-label" x="113" y="60" font-size="11" text-anchor="middle">p (a₁)</text>
    <text class="fig-muted" x="161" y="60" font-size="10" text-anchor="middle">·</text>
    <text class="fig-muted" x="305" y="41" font-size="9.5" text-anchor="middle">原链断开</text>
    <text class="fig-label" x="473" y="60" font-size="11" text-anchor="middle">a₂</text>
    <text class="fig-muted" x="521" y="60" font-size="10" text-anchor="middle">·</text>
    <text class="fig-green" x="288" y="138" font-size="11" text-anchor="middle">s (新结点)</text>
    <text class="fig-muted" x="341" y="138" font-size="10" text-anchor="middle">·</text>
    <text class="fig-green" x="466" y="128" font-size="10.5" text-anchor="middle">① s-&gt;next = p-&gt;next</text>
    <text class="fig-orange" x="130" y="128" font-size="10.5" text-anchor="middle">② p-&gt;next = s</text>
  </svg>
  <figcaption>图 13：先连后断，① → ②</figcaption>
</figure>

<figure class="cpd-figure not-content" id="ds40-w1-fig-12">
  <svg viewBox="0 0 720 256" role="img" aria-label="单链表后插操作先连后断正确指针顺序与颠倒顺序导致断链自环的错误对比">
    <defs>
      <marker id="ds40-w1-f12-arr" viewBox="0 0 10 10" refX="8.5" refY="5" markerWidth="6" markerHeight="6" markerUnits="userSpaceOnUse" orient="auto-start-reverse">
        <path d="M 1 1.5 L 8.5 5 L 1 8.5 z" fill="context-stroke" stroke="none"/>
      </marker>
    </defs>
    <rect class="fig-box-subtle" x="18" y="16" width="344" height="224" rx="8"/>
    <rect class="fig-box-subtle" x="376" y="16" width="326" height="224" rx="8"/>
    <line class="fig-line-dashed" x1="118" y1="80" x2="242" y2="80"/>
    <path class="fig-line" d="M 222 152 Q 258 148 272 98" stroke="var(--sl-color-green)" style="stroke: var(--sl-color-green)" marker-end="url(#ds40-w1-f12-arr)"/>
    <path class="fig-line" d="M 102 96 Q 108 142 136 150" stroke="var(--sl-color-orange)" style="stroke: var(--sl-color-orange)" marker-end="url(#ds40-w1-f12-arr)"/>
    <line class="fig-line-dashed" x1="472" y1="80" x2="582" y2="80"/>
    <path class="fig-line" d="M 456 96 Q 460 142 486 150" stroke="var(--sl-color-red)" style="stroke: var(--sl-color-red)" marker-end="url(#ds40-w1-f12-arr)"/>
    <path class="fig-line" d="M 572 146 C 606 130 606 174 574 158" stroke="var(--sl-color-red)" style="stroke: var(--sl-color-red)" marker-end="url(#ds40-w1-f12-arr)"/>
    <rect class="fig-box-blue" x="34" y="64" width="54" height="32" rx="3"/>
    <rect class="fig-box" x="88" y="64" width="30" height="32" rx="3"/>
    <rect class="fig-box" x="138" y="48" width="84" height="18" rx="4"/>
    <rect class="fig-box-blue" x="244" y="64" width="58" height="32" rx="3"/>
    <rect class="fig-box" x="302" y="64" width="30" height="32" rx="3"/>
    <rect class="fig-box-green" x="138" y="136" width="54" height="32" rx="3"/>
    <rect class="fig-box" x="192" y="136" width="30" height="32" rx="3"/>
    <rect class="fig-box-green" x="234" y="114" width="116" height="20" rx="4"/>
    <rect class="fig-box-orange" x="28" y="114" width="96" height="20" rx="4"/>
    <rect class="fig-box" x="30" y="184" width="320" height="44" rx="5"/>
    <rect class="fig-box-blue" x="392" y="64" width="50" height="32" rx="3"/>
    <rect class="fig-box" x="442" y="64" width="30" height="32" rx="3"/>
    <rect class="fig-box-red" x="488" y="48" width="80" height="18" rx="4"/>
    <rect class="fig-box-red" x="584" y="64" width="72" height="32" rx="3"/>
    <rect class="fig-box" x="656" y="64" width="30" height="32" rx="3"/>
    <rect class="fig-box-orange" x="488" y="136" width="54" height="32" rx="3"/>
    <rect class="fig-box" x="542" y="136" width="30" height="32" rx="3"/>
    <rect class="fig-box-red" x="386" y="114" width="90" height="20" rx="4"/>
    <rect class="fig-box-red" x="604" y="142" width="84" height="20" rx="4"/>
    <rect class="fig-box-red" x="390" y="184" width="298" height="44" rx="5"/>
    <text class="fig-green" x="190" y="36" font-size="11.5" text-anchor="middle">✓ 正确时序：先连后断（① → ②）</text>
    <text class="fig-label" x="61" y="84" font-size="10.5" text-anchor="middle">p (a₁)</text>
    <text class="fig-muted" x="103" y="84" font-size="10" text-anchor="middle">·</text>
    <text class="fig-muted" x="180" y="61" font-size="9.5" text-anchor="middle">原链断开</text>
    <text class="fig-label" x="273" y="84" font-size="10.5" text-anchor="middle">a₂</text>
    <text class="fig-muted" x="317" y="84" font-size="10" text-anchor="middle">·</text>
    <text class="fig-label" x="165" y="156" font-size="10.5" text-anchor="middle">s (X)</text>
    <text class="fig-muted" x="207" y="156" font-size="10" text-anchor="middle">·</text>
    <text class="fig-green" x="292" y="128" font-size="9.5" text-anchor="middle">① s-&gt;next = p-&gt;next</text>
    <text class="fig-orange" x="76" y="128" font-size="9.5" text-anchor="middle">② p-&gt;next = s</text>
    <text class="fig-label" x="190" y="202" font-size="10" text-anchor="middle">先保存后继地址到 s-&gt;next，再改 p-&gt;next</text>
    <text class="fig-muted" x="190" y="219" font-size="9.5" text-anchor="middle">已知 p 指针时后插复杂度 O(1)</text>
    <text class="fig-red" x="539" y="36" font-size="11.5" text-anchor="middle">✗ 错误颠倒：若先写 p-&gt;next = s</text>
    <text class="fig-label" x="417" y="84" font-size="10.5" text-anchor="middle">p (a₁)</text>
    <text class="fig-muted" x="457" y="84" font-size="10" text-anchor="middle">·</text>
    <text class="fig-red" x="528" y="61" font-size="9.5" text-anchor="middle">✗ 彻底断链</text>
    <text class="fig-red" x="620" y="84" font-size="10" text-anchor="middle">a₂..aₙ丢失</text>
    <text class="fig-muted" x="671" y="84" font-size="10" text-anchor="middle">∧</text>
    <text class="fig-label" x="515" y="156" font-size="10.5" text-anchor="middle">s (X)</text>
    <text class="fig-muted" x="557" y="156" font-size="10" text-anchor="middle">·</text>
    <text class="fig-red" x="431" y="128" font-size="9.5" text-anchor="middle">① p-&gt;next=s</text>
    <text class="fig-red" x="646" y="156" font-size="9.5" text-anchor="middle">② 自环 s!</text>
    <text class="fig-red" x="539" y="202" font-size="10" text-anchor="middle">p-&gt;next 已被覆盖为 s，后半段链表永久丢失</text>
    <text class="fig-muted" x="539" y="219" font-size="9.5" text-anchor="middle">且 s-&gt;next = p-&gt;next 变成 s 指向自己！</text>
  </svg>
  <figcaption>图 3-3：单链表后插铁律“先连后断”——必须先让新结点 s 接住后继，否则原后继地址被覆盖引发断链与自环</figcaption>
</figure>

**删除** `p` 的后继：

```c
q = p->next;        // q 指向待删除结点
p->next = q->next;  // 前驱绕过 q
free(q);            // 释放 q
```

<figure class="cpd-figure not-content" id="ds40-img6-fig-5">
  <svg viewBox="0 0 700 140" role="img" aria-label="单链表删除 p 的后继结点 q 的三步操作">
    <defs>
      <marker id="ds40-img6-arr-5" viewBox="0 0 10 10" refX="8.5" refY="5" markerWidth="6" markerHeight="6" markerUnits="userSpaceOnUse" orient="auto-start-reverse">
        <path d="M 1 1.5 L 8.5 5 L 1 8.5 z" fill="context-stroke" stroke="none"/>
      </marker>
    </defs>
    <rect class="fig-box-subtle" x="16" y="12" width="668" height="116" rx="8"/>
    <line class="fig-line-dashed" x1="166" y1="68" x2="268" y2="68"/>
    <line class="fig-line-dashed" x1="368" y1="68" x2="470" y2="68"/>
    <path class="fig-line" d="M 150 52 Q 318 16 488 50" fill="none" stroke="var(--sl-color-green)" marker-end="url(#ds40-img6-arr-5)"/>
    <rect class="fig-box-blue" x="70" y="52" width="66" height="32" rx="3"/>
    <rect class="fig-box" x="136" y="52" width="30" height="32" rx="3"/>
    <rect class="fig-box-red" x="270" y="52" width="68" height="32" rx="3"/>
    <rect class="fig-box" x="338" y="52" width="30" height="32" rx="3"/>
    <rect class="fig-box-blue" x="472" y="52" width="66" height="32" rx="3"/>
    <rect class="fig-box" x="538" y="52" width="30" height="32" rx="3"/>
    <text class="fig-label" x="103" y="72" font-size="11" text-anchor="middle">p (a₁)</text>
    <text class="fig-muted" x="151" y="72" font-size="10" text-anchor="middle">·</text>
    <text class="fig-red" x="304" y="72" font-size="11" text-anchor="middle">q (a₂)</text>
    <text class="fig-muted" x="353" y="72" font-size="10" text-anchor="middle">·</text>
    <text class="fig-label" x="505" y="72" font-size="11" text-anchor="middle">a₃</text>
    <text class="fig-muted" x="553" y="72" font-size="10" text-anchor="middle">·</text>
    <text class="fig-green" x="318" y="28" font-size="10" text-anchor="middle">② p-&gt;next = q-&gt;next（绕过 q）</text>
    <text class="fig-red" x="318" y="106" font-size="10" text-anchor="middle">① q = p-&gt;next   |   ③ free(q) 释放空间</text>
  </svg>
  <figcaption>图 14：删除 a₂：p-&gt;next = q-&gt;next，再 free(q)</figcaption>
</figure>

**查找**：链表只能**顺序存取**。找第 5 个结点必须从头指针开始数到第 5 个，不能像顺序表那样直接算地址，时间复杂度 $O(n)$。

<figure class="cpd-figure not-content" id="ds40-img6-fig-6">
  <svg viewBox="0 0 700 132" role="img" aria-label="单链表按位查找与按值查找的顺序存取过程">
    <defs>
      <marker id="ds40-img6-arr-6" viewBox="0 0 10 10" refX="8.5" refY="5" markerWidth="6" markerHeight="6" markerUnits="userSpaceOnUse" orient="auto-start-reverse">
        <path d="M 1 1.5 L 8.5 5 L 1 8.5 z" fill="context-stroke" stroke="none"/>
      </marker>
    </defs>
    <rect class="fig-box-subtle" x="16" y="12" width="668" height="108" rx="8"/>
    <text class="fig-title" x="350" y="34" font-size="11" text-anchor="middle">顺序存取 O(n)：GetElem(L, 5) 需从头数到第 5 个；LocateElem(L, e) 逐个比对值</text>
    <line class="fig-line-accent" x1="94" y1="68" x2="122" y2="68" marker-end="url(#ds40-img6-arr-6)"/>
    <line class="fig-line-accent" x1="196" y1="68" x2="224" y2="68" marker-end="url(#ds40-img6-arr-6)"/>
    <line class="fig-line-accent" x1="298" y1="68" x2="326" y2="68" marker-end="url(#ds40-img6-arr-6)"/>
    <line class="fig-line-accent" x1="400" y1="68" x2="428" y2="68" marker-end="url(#ds40-img6-arr-6)"/>
    <line class="fig-line-accent" x1="502" y1="68" x2="530" y2="68" marker-end="url(#ds40-img6-arr-6)"/>
    <rect class="fig-box-purple" x="32" y="52" width="62" height="32" rx="3"/>
    <rect class="fig-box-blue" x="124" y="52" width="72" height="32" rx="3"/>
    <rect class="fig-box-blue" x="226" y="52" width="72" height="32" rx="3"/>
    <rect class="fig-box-blue" x="328" y="52" width="72" height="32" rx="3"/>
    <rect class="fig-box-blue" x="430" y="52" width="72" height="32" rx="3"/>
    <rect class="fig-box-orange" x="532" y="52" width="84" height="32" rx="3"/>
    <text class="fig-purple" x="63" y="72" font-size="10" text-anchor="middle">头 j=0</text>
    <text class="fig-label" x="160" y="72" font-size="10" text-anchor="middle">a₁ (j=1)</text>
    <text class="fig-label" x="262" y="72" font-size="10" text-anchor="middle">a₂ (j=2)</text>
    <text class="fig-label" x="364" y="72" font-size="10" text-anchor="middle">a₃ (j=3)</text>
    <text class="fig-label" x="466" y="72" font-size="10" text-anchor="middle">a₄ (j=4)</text>
    <text class="fig-orange" x="574" y="72" font-size="10" text-anchor="middle">a₅ (j=5) ★</text>
    <text class="fig-muted" x="350" y="106" font-size="9.5" text-anchor="middle">每次指针移动 p = p-&gt;next; j++; 无法像顺序表那样通过下标直接算出地址</text>
  </svg>
  <figcaption>图 15：找第 5 个 → 数到 5，顺序存取 O(n)</figcaption>
</figure>

<figure class="cpd-figure not-content" id="ds40-w1-fig-13">
  <svg viewBox="0 0 720 264" role="img" aria-label="单链表删除后继结点的三步指针绕过操作与按位顺序存取O(n)查找轨迹">
    <defs>
      <marker id="ds40-w1-f13-arr" viewBox="0 0 10 10" refX="8.5" refY="5" markerWidth="6" markerHeight="6" markerUnits="userSpaceOnUse" orient="auto-start-reverse">
        <path d="M 1 1.5 L 8.5 5 L 1 8.5 z" fill="context-stroke" stroke="none"/>
      </marker>
    </defs>
    <rect class="fig-box-subtle" x="18" y="16" width="684" height="114" rx="8"/>
    <rect class="fig-box-subtle" x="18" y="142" width="684" height="106" rx="8"/>
    <line class="fig-line-dashed" x1="126" y1="80" x2="208" y2="80"/>
    <line class="fig-line-dashed" x1="300" y1="80" x2="382" y2="80"/>
    <path class="fig-line" d="M 112 64 Q 252 28 404 62" stroke="var(--sl-color-green)" style="stroke: var(--sl-color-green)" marker-end="url(#ds40-w1-f13-arr)"/>
    <line class="fig-line-accent" x1="92" y1="196" x2="116" y2="196" marker-end="url(#ds40-w1-f13-arr)"/>
    <line class="fig-line-accent" x1="182" y1="196" x2="206" y2="196" marker-end="url(#ds40-w1-f13-arr)"/>
    <line class="fig-line-accent" x1="272" y1="196" x2="296" y2="196" marker-end="url(#ds40-w1-f13-arr)"/>
    <line class="fig-line-accent" x1="362" y1="196" x2="386" y2="196" marker-end="url(#ds40-w1-f13-arr)"/>
    <line class="fig-line-accent" x1="452" y1="196" x2="476" y2="196" marker-end="url(#ds40-w1-f13-arr)"/>
    <rect class="fig-box-blue" x="42" y="64" width="54" height="32" rx="3"/>
    <rect class="fig-box" x="96" y="64" width="30" height="32" rx="3"/>
    <rect class="fig-box-green" x="192" y="34" width="128" height="18" rx="4"/>
    <rect class="fig-box-red" x="210" y="64" width="60" height="32" rx="3"/>
    <rect class="fig-box" x="270" y="64" width="30" height="32" rx="3"/>
    <rect class="fig-box-red" x="190" y="102" width="132" height="18" rx="4"/>
    <rect class="fig-box-green" x="384" y="64" width="54" height="32" rx="3"/>
    <rect class="fig-box" x="438" y="64" width="30" height="32" rx="3"/>
    <rect class="fig-box" x="488" y="34" width="198" height="84" rx="5"/>
    <rect class="fig-box-purple" x="34" y="182" width="58" height="28" rx="3"/>
    <rect class="fig-box-blue" x="118" y="182" width="64" height="28" rx="3"/>
    <rect class="fig-box-blue" x="208" y="182" width="64" height="28" rx="3"/>
    <rect class="fig-box-blue" x="298" y="182" width="64" height="28" rx="3"/>
    <rect class="fig-box-blue" x="388" y="182" width="64" height="28" rx="3"/>
    <rect class="fig-box-orange" x="478" y="182" width="74" height="28" rx="3"/>
    <rect class="fig-box-orange" x="568" y="166" width="118" height="68" rx="5"/>
    <text class="fig-title" x="32" y="34" font-size="11">删除后继结点</text>
    <text class="fig-label" x="69" y="84" font-size="10.5" text-anchor="middle">p (a₁)</text>
    <text class="fig-muted" x="111" y="84" font-size="10" text-anchor="middle">·</text>
    <text class="fig-green" x="256" y="47" font-size="9.5" text-anchor="middle">② p-&gt;next = q-&gt;next</text>
    <text class="fig-red" x="240" y="84" font-size="10.5" text-anchor="middle">q (a₂)</text>
    <text class="fig-muted" x="285" y="84" font-size="10" text-anchor="middle">·</text>
    <text class="fig-red" x="256" y="115" font-size="9.5" text-anchor="middle">① q=p-&gt;next · ③ free(q)</text>
    <text class="fig-label" x="411" y="84" font-size="10.5" text-anchor="middle">a₃</text>
    <text class="fig-muted" x="453" y="84" font-size="10" text-anchor="middle">·</text>
    <text class="fig-label" x="587" y="54" font-size="10" text-anchor="middle">已知前驱 p 删后继：O(1)</text>
    <text class="fig-orange" x="587" y="74" font-size="9.5" text-anchor="middle">★ 若仅给待删结点 *p（非尾）：</text>
    <text class="fig-muted" x="587" y="91" font-size="9.5" text-anchor="middle">偷天换日拷贝后继值再删后继 O(1)</text>
    <text class="fig-muted" x="587" y="108" font-size="9.5" text-anchor="middle">若 *p 为尾结点仍需从头遍历 O(n)</text>
    <text class="fig-title" x="32" y="164" font-size="11">按位查找 GetElem(L, 5)：链表不支持下标算地址，必须从头顺藤摸瓜逐跳计数</text>
    <text class="fig-purple" x="63" y="200" font-size="10" text-anchor="middle">头 j=0</text>
    <text class="fig-label" x="150" y="200" font-size="10" text-anchor="middle">a₁ (j=1)</text>
    <text class="fig-label" x="240" y="200" font-size="10" text-anchor="middle">a₂ (j=2)</text>
    <text class="fig-label" x="330" y="200" font-size="10" text-anchor="middle">a₃ (j=3)</text>
    <text class="fig-label" x="420" y="200" font-size="10" text-anchor="middle">a₄ (j=4)</text>
    <text class="fig-orange" x="515" y="200" font-size="10" text-anchor="middle">a₅ (j=5) ★</text>
    <text class="fig-muted" x="292" y="232" font-size="9.5" text-anchor="middle">每轮执行 p = p-&gt;next; j++; 直至 j == 5 命中目标</text>
    <text class="fig-orange" x="627" y="186" font-size="10.5" text-anchor="middle">顺序存取</text>
    <text class="fig-label" x="627" y="204" font-size="10" text-anchor="middle">不可随机跳</text>
    <text class="fig-muted" x="627" y="222" font-size="10" text-anchor="middle">查找 O(n)</text>
  </svg>
  <figcaption>图 3-4：单链表删除后继结点（绕过 + 释放）与顺序存取查找轨迹（无论有序与否，单链表折半查找均退化不可用）</figcaption>
</figure>

### 双链表、循环链表与静态链表

| 变体 | 结构 | 要点 |
|:----:|:-----|:-----|
| 双链表 | `prior` \| `data` \| `next` | 每个结点存前驱和后继两个指针，可双向遍历；插入删除要同时改两个方向的指针 |
| 循环链表 | 尾结点 `next` 指回头部 | 从任一结点出发都能遍历整张表；常配合尾指针使用 |
| 静态链表 | 数组 + 游标 `cur` | 用数组下标模拟指针，`cur` 存下一个元素的下标；`cur = -1` 表示链表结束 |

静态链表示例（数组 + 游标模拟指针）：

| 下标 | data | cur |
|:----:|:----:|:---:|
| 0 | — | 2 |
| 1 | b | 3 |
| 2 | a | 1 |
| 3 | c | -1 |

按 `cur` 串起来就是 `0 → 2(a) → 1(b) → 3(c) → -1`。

<figure class="cpd-figure not-content" id="ds40-img6-fig-7">
  <svg viewBox="0 0 700 220" role="img" aria-label="双链表、循环链表与静态链表结构示意图">
    <defs>
      <marker id="ds40-img6-arr-7" viewBox="0 0 10 10" refX="8.5" refY="5" markerWidth="6" markerHeight="6" markerUnits="userSpaceOnUse" orient="auto-start-reverse">
        <path d="M 1 1.5 L 8.5 5 L 1 8.5 z" fill="context-stroke" stroke="none"/>
      </marker>
    </defs>
    <rect class="fig-box-subtle" x="16" y="12" width="326" height="96" rx="8"/>
    <rect class="fig-box-subtle" x="358" y="12" width="326" height="96" rx="8"/>
    <rect class="fig-box-subtle" x="16" y="118" width="668" height="90" rx="8"/>
    <line class="fig-line-accent" x1="112" y1="62" x2="144" y2="62" marker-end="url(#ds40-img6-arr-7)"/>
    <line class="fig-line-accent" x1="224" y1="62" x2="256" y2="62" marker-end="url(#ds40-img6-arr-7)"/>
    <line class="fig-line" x1="146" y1="76" x2="114" y2="76" stroke="var(--sl-color-purple)" marker-end="url(#ds40-img6-arr-7)"/>
    <line class="fig-line" x1="258" y1="76" x2="226" y2="76" stroke="var(--sl-color-purple)" marker-end="url(#ds40-img6-arr-7)"/>
    <rect class="fig-box" x="34" y="54" width="18" height="30" rx="2"/>
    <rect class="fig-box-blue" x="52" y="54" width="42" height="30" rx="2"/>
    <rect class="fig-box" x="94" y="54" width="18" height="30" rx="2"/>
    <rect class="fig-box" x="146" y="54" width="18" height="30" rx="2"/>
    <rect class="fig-box-blue" x="164" y="54" width="42" height="30" rx="2"/>
    <rect class="fig-box" x="206" y="54" width="18" height="30" rx="2"/>
    <rect class="fig-box" x="258" y="54" width="18" height="30" rx="2"/>
    <rect class="fig-box-blue" x="276" y="54" width="42" height="30" rx="2"/>
    <rect class="fig-box" x="318" y="54" width="18" height="30" rx="2"/>
    <text class="fig-blue" x="179" y="34" font-size="11" text-anchor="middle">双链表：prior | data | next</text>
    <text class="fig-label" x="73" y="73" font-size="10" text-anchor="middle">a₁</text>
    <text class="fig-label" x="185" y="73" font-size="10" text-anchor="middle">a₂</text>
    <text class="fig-label" x="297" y="73" font-size="10" text-anchor="middle">a₃</text>
    <line class="fig-line-accent" x1="438" y1="66" x2="468" y2="66" marker-end="url(#ds40-img6-arr-7)"/>
    <line class="fig-line-accent" x1="532" y1="66" x2="562" y2="66" marker-end="url(#ds40-img6-arr-7)"/>
    <path class="fig-line" d="M 596 82 L 596 96 L 406 96 L 406 82" fill="none" stroke="var(--sl-color-green)" marker-end="url(#ds40-img6-arr-7)"/>
    <rect class="fig-box-orange" x="376" y="50" width="62" height="30" rx="3"/>
    <rect class="fig-box-blue" x="470" y="50" width="62" height="30" rx="3"/>
    <rect class="fig-box-green" x="564" y="50" width="64" height="30" rx="3"/>
    <text class="fig-green" x="521" y="34" font-size="11" text-anchor="middle">循环链表：尾结点 next 指回头部</text>
    <text class="fig-orange" x="407" y="69" font-size="10" text-anchor="middle">头结点</text>
    <text class="fig-label" x="501" y="69" font-size="10" text-anchor="middle">a₁ ···</text>
    <text class="fig-green" x="596" y="69" font-size="10" text-anchor="middle">aₙ (尾)</text>
    <line class="fig-line-accent" x1="170" y1="170" x2="210" y2="170" marker-end="url(#ds40-img6-arr-7)"/>
    <line class="fig-line-accent" x1="330" y1="170" x2="370" y2="170" marker-end="url(#ds40-img6-arr-7)"/>
    <line class="fig-line-accent" x1="490" y1="170" x2="530" y2="170" marker-end="url(#ds40-img6-arr-7)"/>
    <rect class="fig-box-orange" x="50" y="152" width="120" height="36" rx="4"/>
    <rect class="fig-box-green" x="212" y="152" width="118" height="36" rx="4"/>
    <rect class="fig-box-blue" x="372" y="152" width="118" height="36" rx="4"/>
    <rect class="fig-box-purple" x="532" y="152" width="118" height="36" rx="4"/>
    <text class="fig-title" x="350" y="138" font-size="11" text-anchor="middle">静态链表：数组 + 游标 cur 模拟指针（cur = -1 结束）</text>
    <text class="fig-orange" x="110" y="174" font-size="10" text-anchor="middle">[0] — | cur=2</text>
    <text class="fig-green" x="271" y="174" font-size="10" text-anchor="middle">[2] a | cur=1</text>
    <text class="fig-blue" x="431" y="174" font-size="10" text-anchor="middle">[1] b | cur=3</text>
    <text class="fig-purple" x="591" y="174" font-size="10" text-anchor="middle">[3] c | cur=-1</text>
  </svg>
  <figcaption>图 16：双链表（prior/data/next）、循环链表（尾指回头）、静态链表（数组+游标）</figcaption>
</figure>

<figure class="cpd-figure not-content" id="ds40-w1-fig-14">
  <svg viewBox="0 0 720 304" role="img" aria-label="双链表双向指针、循环单链表尾指针R与静态链表数组游标跳转轨迹对比图">
    <defs>
      <marker id="ds40-w1-f14-arr" viewBox="0 0 10 10" refX="8.5" refY="5" markerWidth="6" markerHeight="6" markerUnits="userSpaceOnUse" orient="auto-start-reverse">
        <path d="M 1 1.5 L 8.5 5 L 1 8.5 z" fill="context-stroke" stroke="none"/>
      </marker>
    </defs>
    <rect class="fig-box-subtle" x="18" y="16" width="336" height="130" rx="8"/>
    <rect class="fig-box-subtle" x="368" y="16" width="334" height="130" rx="8"/>
    <rect class="fig-box-subtle" x="18" y="158" width="684" height="130" rx="8"/>
    <line class="fig-line-accent" x1="110" y1="73" x2="144" y2="73" marker-end="url(#ds40-w1-f14-arr)"/>
    <line class="fig-line-accent" x1="224" y1="73" x2="258" y2="73" marker-end="url(#ds40-w1-f14-arr)"/>
    <line class="fig-line" x1="146" y1="87" x2="112" y2="87" stroke="var(--sl-color-purple)" style="stroke: var(--sl-color-purple)" marker-end="url(#ds40-w1-f14-arr)"/>
    <line class="fig-line" x1="260" y1="87" x2="226" y2="87" stroke="var(--sl-color-purple)" style="stroke: var(--sl-color-purple)" marker-end="url(#ds40-w1-f14-arr)"/>
    <line class="fig-line-accent" x1="444" y1="73" x2="468" y2="73" marker-end="url(#ds40-w1-f14-arr)"/>
    <line class="fig-line-accent" x1="526" y1="73" x2="550" y2="73" marker-end="url(#ds40-w1-f14-arr)"/>
    <line class="fig-line" x1="646" y1="73" x2="622" y2="73" stroke="var(--sl-color-purple)" style="stroke: var(--sl-color-purple)" marker-end="url(#ds40-w1-f14-arr)"/>
    <path class="fig-line" d="M 608 88 L 608 110 L 415 110 L 415 90" stroke="var(--sl-color-green)" style="stroke: var(--sl-color-green)" marker-end="url(#ds40-w1-f14-arr)"/>
    <line class="fig-line-accent" x1="358" y1="228" x2="380" y2="228" marker-end="url(#ds40-w1-f14-arr)"/>
    <line class="fig-line-accent" x1="462" y1="228" x2="484" y2="228" marker-end="url(#ds40-w1-f14-arr)"/>
    <line class="fig-line-accent" x1="566" y1="228" x2="588" y2="228" marker-end="url(#ds40-w1-f14-arr)"/>
    <rect class="fig-box" x="32" y="64" width="18" height="32" rx="2"/>
    <rect class="fig-box-blue" x="50" y="64" width="42" height="32" rx="2"/>
    <rect class="fig-box" x="92" y="64" width="18" height="32" rx="2"/>
    <rect class="fig-box" x="146" y="64" width="18" height="32" rx="2"/>
    <rect class="fig-box-blue" x="164" y="64" width="42" height="32" rx="2"/>
    <rect class="fig-box" x="206" y="64" width="18" height="32" rx="2"/>
    <rect class="fig-box" x="260" y="64" width="18" height="32" rx="2"/>
    <rect class="fig-box-blue" x="278" y="64" width="42" height="32" rx="2"/>
    <rect class="fig-box" x="320" y="64" width="18" height="32" rx="2"/>
    <rect class="fig-box-orange" x="386" y="58" width="58" height="30" rx="3"/>
    <rect class="fig-box-blue" x="470" y="58" width="56" height="30" rx="3"/>
    <rect class="fig-box-green" x="552" y="58" width="68" height="30" rx="3"/>
    <rect class="fig-box-purple" x="648" y="58" width="40" height="30" rx="4"/>
    <rect class="fig-box" x="466" y="101" width="92" height="18" rx="4"/>
    <rect class="fig-box" x="32" y="184" width="210" height="18" rx="2"/>
    <rect class="fig-box-orange" x="32" y="202" width="210" height="18" rx="2"/>
    <rect class="fig-box-blue" x="32" y="220" width="210" height="18" rx="2"/>
    <rect class="fig-box-green" x="32" y="238" width="210" height="18" rx="2"/>
    <rect class="fig-box-purple" x="32" y="256" width="210" height="18" rx="2"/>
    <line class="fig-divider" x1="88" y1="184" x2="88" y2="274"/>
    <line class="fig-divider" x1="168" y1="184" x2="168" y2="274"/>
    <rect class="fig-box-orange" x="276" y="208" width="82" height="40" rx="4"/>
    <rect class="fig-box-green" x="382" y="208" width="80" height="40" rx="4"/>
    <rect class="fig-box-blue" x="486" y="208" width="80" height="40" rx="4"/>
    <rect class="fig-box-purple" x="590" y="208" width="96" height="40" rx="4"/>
    <text class="fig-blue" x="186" y="36" font-size="11" text-anchor="middle">① 双链表：prior | data | next 双向互指</text>
    <text class="fig-muted" x="41" y="84" font-size="9.5" text-anchor="middle">∧</text>
    <text class="fig-label" x="71" y="84" font-size="10.5" text-anchor="middle">a₁</text>
    <text class="fig-muted" x="101" y="84" font-size="9.5" text-anchor="middle">·</text>
    <text class="fig-muted" x="155" y="84" font-size="9.5" text-anchor="middle">·</text>
    <text class="fig-label" x="185" y="84" font-size="10.5" text-anchor="middle">a₂</text>
    <text class="fig-muted" x="215" y="84" font-size="9.5" text-anchor="middle">·</text>
    <text class="fig-muted" x="269" y="84" font-size="9.5" text-anchor="middle">·</text>
    <text class="fig-label" x="299" y="84" font-size="10.5" text-anchor="middle">a₃</text>
    <text class="fig-muted" x="329" y="84" font-size="9.5" text-anchor="middle">∧</text>
    <text class="fig-label" x="186" y="116" font-size="9.5" text-anchor="middle">访问直接前驱 O(1)；插删需同步改 4 个指针</text>
    <text class="fig-muted" x="186" y="133" font-size="9.5" text-anchor="middle">先连新结点双翼，再改原前后结点指针防断链</text>
    <text class="fig-green" x="535" y="36" font-size="11" text-anchor="middle">② 循环单链表：尾指回头 + 尾指针 R 优化</text>
    <text class="fig-orange" x="415" y="77" font-size="10" text-anchor="middle">头结点</text>
    <text class="fig-label" x="498" y="77" font-size="10" text-anchor="middle">a₁ ···</text>
    <text class="fig-green" x="586" y="77" font-size="10" text-anchor="middle">aₙ (尾)</text>
    <text class="fig-purple" x="668" y="77" font-size="10.5" text-anchor="middle">R</text>
    <text class="fig-green" x="512" y="114" font-size="9.5" text-anchor="middle">next 指回头</text>
    <text class="fig-muted" x="535" y="134" font-size="9.5" text-anchor="middle">★ 设尾指针 R：访问表尾 R 与表头 R-&gt;next 皆 O(1)</text>
    <text class="fig-title" x="360" y="176" font-size="11" text-anchor="middle">③ 静态链表：用连续数组下标 cur 模拟指针（无指针语言实现链表，容量固定）</text>
    <text class="fig-muted" x="60" y="197" font-size="9.5" text-anchor="middle">下标</text>
    <text class="fig-muted" x="128" y="197" font-size="9.5" text-anchor="middle">data</text>
    <text class="fig-muted" x="205" y="197" font-size="9.5" text-anchor="middle">cur</text>
    <text class="fig-label" x="60" y="215" font-size="9.5" text-anchor="middle">0</text>
    <text class="fig-muted" x="128" y="215" font-size="9.5" text-anchor="middle">—(头)</text>
    <text class="fig-orange" x="205" y="215" font-size="9.5" text-anchor="middle">2</text>
    <text class="fig-label" x="60" y="233" font-size="9.5" text-anchor="middle">1</text>
    <text class="fig-label" x="128" y="233" font-size="9.5" text-anchor="middle">b</text>
    <text class="fig-blue" x="205" y="233" font-size="9.5" text-anchor="middle">3</text>
    <text class="fig-label" x="60" y="251" font-size="9.5" text-anchor="middle">2</text>
    <text class="fig-label" x="128" y="251" font-size="9.5" text-anchor="middle">a</text>
    <text class="fig-green" x="205" y="251" font-size="9.5" text-anchor="middle">1</text>
    <text class="fig-label" x="60" y="269" font-size="9.5" text-anchor="middle">3</text>
    <text class="fig-label" x="128" y="269" font-size="9.5" text-anchor="middle">c</text>
    <text class="fig-purple" x="205" y="269" font-size="9.5" text-anchor="middle">-1</text>
    <text class="fig-muted" x="481" y="197" font-size="9.5" text-anchor="middle">按游标 cur 展开的逻辑序列：0 → 2(a) → 1(b) → 3(c) → -1 (表尾)</text>
    <text class="fig-orange" x="317" y="225" font-size="10" text-anchor="middle">[0] 头结点</text>
    <text class="fig-muted" x="317" y="241" font-size="9.5" text-anchor="middle">cur = 2</text>
    <text class="fig-green" x="422" y="225" font-size="10" text-anchor="middle">[2] data=a</text>
    <text class="fig-muted" x="422" y="241" font-size="9.5" text-anchor="middle">cur = 1</text>
    <text class="fig-blue" x="526" y="225" font-size="10" text-anchor="middle">[1] data=b</text>
    <text class="fig-muted" x="526" y="241" font-size="9.5" text-anchor="middle">cur = 3</text>
    <text class="fig-purple" x="638" y="225" font-size="10" text-anchor="middle">[3] data=c</text>
    <text class="fig-muted" x="638" y="241" font-size="9.5" text-anchor="middle">cur = -1 (结束)</text>
    <text class="fig-muted" x="481" y="270" font-size="9.5" text-anchor="middle">增删仅需改游标 cur 无需搬移元素，但失去了随机存取能力且需预分配数组大小</text>
  </svg>
  <figcaption>图 3-5：双链表、带尾指针循环单链表与静态链表（游标模拟指针）核心机制对照</figcaption>
</figure>

### 顺序表 vs 链表

| 对比项 | 顺序表 | 链表 |
|:------:|:-------|:-----|
| 存取方式 | 随机存取 $O(1)$ | 顺序存取 $O(n)$ |
| 插入 / 删除 | 移动元素 $O(n)$ | 改指针 $O(1)$* |
| 存储密度 | $=1$ | $<1$（指针占空间） |
| 适用场景 | 多查少改 | 频繁插入删除 |

\* 已知插入位置（前驱结点）的前提下；找位置本身仍需 $O(n)$。

<figure class="cpd-figure not-content" id="ds40-img6-fig-8">
  <svg viewBox="0 0 700 180" role="img" aria-label="顺序表与链表的四维对比矩阵">
    <rect class="fig-box-subtle" x="16" y="12" width="668" height="156" rx="8"/>
    <rect class="fig-box" x="28" y="24" width="144" height="28" rx="4"/>
    <rect class="fig-box-blue" x="180" y="24" width="242" height="28" rx="4"/>
    <rect class="fig-box-green" x="430" y="24" width="242" height="28" rx="4"/>
    <rect class="fig-box" x="28" y="58" width="144" height="24" rx="3"/>
    <rect class="fig-box-green" x="180" y="58" width="242" height="24" rx="3"/>
    <rect class="fig-box-orange" x="430" y="58" width="242" height="24" rx="3"/>
    <rect class="fig-box" x="28" y="86" width="144" height="24" rx="3"/>
    <rect class="fig-box-orange" x="180" y="86" width="242" height="24" rx="3"/>
    <rect class="fig-box-green" x="430" y="86" width="242" height="24" rx="3"/>
    <rect class="fig-box" x="28" y="114" width="144" height="24" rx="3"/>
    <rect class="fig-box-green" x="180" y="114" width="242" height="24" rx="3"/>
    <rect class="fig-box-orange" x="430" y="114" width="242" height="24" rx="3"/>
    <rect class="fig-box-purple" x="28" y="142" width="144" height="20" rx="3"/>
    <rect class="fig-box-blue" x="180" y="142" width="242" height="20" rx="3"/>
    <rect class="fig-box-blue" x="430" y="142" width="242" height="20" rx="3"/>
    <text class="fig-title" x="100" y="42" font-size="10.5" text-anchor="middle">对比维度</text>
    <text class="fig-blue" x="301" y="42" font-size="11" text-anchor="middle">顺序表</text>
    <text class="fig-green" x="551" y="42" font-size="11" text-anchor="middle">链表</text>
    <text class="fig-label" x="100" y="74" font-size="10" text-anchor="middle">存取方式</text>
    <text class="fig-green" x="301" y="74" font-size="10" text-anchor="middle">随机存取 O(1)</text>
    <text class="fig-orange" x="551" y="74" font-size="10" text-anchor="middle">顺序存取 O(n)</text>
    <text class="fig-label" x="100" y="102" font-size="10" text-anchor="middle">插入 / 删除</text>
    <text class="fig-orange" x="301" y="102" font-size="10" text-anchor="middle">移动元素 O(n)</text>
    <text class="fig-green" x="551" y="102" font-size="10" text-anchor="middle">改指针 O(1)（已知前驱）</text>
    <text class="fig-label" x="100" y="130" font-size="10" text-anchor="middle">存储密度</text>
    <text class="fig-green" x="301" y="130" font-size="10" text-anchor="middle">= 1（无指针开销）</text>
    <text class="fig-orange" x="551" y="130" font-size="10" text-anchor="middle">&lt; 1（指针占空间）</text>
    <text class="fig-purple" x="100" y="156" font-size="10" text-anchor="middle">适用场景</text>
    <text class="fig-label" x="301" y="156" font-size="10" text-anchor="middle">多查少改、长度较固定</text>
    <text class="fig-label" x="551" y="156" font-size="10" text-anchor="middle">频繁插入删除、长度多变</text>
  </svg>
  <figcaption>图 17：顺序表与链表的四维对比</figcaption>
</figure>

<figure class="cpd-figure not-content" id="ds40-w1-fig-15">
  <svg viewBox="0 0 720 278" role="img" aria-label="顺序表与链表的存储密度、查找复杂度、增删代价拆解及工程选型四维对比矩阵">
    <rect class="fig-box-subtle" x="12" y="12" width="696" height="254" rx="8"/>
    <rect class="fig-box" x="20" y="20" width="134" height="28" rx="4"/>
    <rect class="fig-box-blue" x="166" y="20" width="260" height="28" rx="4"/>
    <rect class="fig-box-green" x="438" y="20" width="262" height="28" rx="4"/>
    <rect class="fig-box" x="20" y="56" width="134" height="44" rx="4"/>
    <rect class="fig-box-green" x="166" y="56" width="260" height="44" rx="4"/>
    <rect class="fig-box-orange" x="438" y="56" width="262" height="44" rx="4"/>
    <rect class="fig-box" x="20" y="108" width="134" height="44" rx="4"/>
    <rect class="fig-box-green" x="166" y="108" width="260" height="44" rx="4"/>
    <rect class="fig-box-orange" x="438" y="108" width="262" height="44" rx="4"/>
    <rect class="fig-box" x="20" y="160" width="134" height="50" rx="4"/>
    <rect class="fig-box-orange" x="166" y="160" width="260" height="50" rx="4"/>
    <rect class="fig-box-green" x="438" y="160" width="262" height="50" rx="4"/>
    <rect class="fig-box-purple" x="20" y="218" width="134" height="42" rx="4"/>
    <rect class="fig-box-blue" x="166" y="218" width="260" height="42" rx="4"/>
    <rect class="fig-box-blue" x="438" y="218" width="262" height="42" rx="4"/>
    <text class="fig-title" x="87" y="38" font-size="11" text-anchor="middle">对比维度</text>
    <text class="fig-blue" x="296" y="38" font-size="11.5" text-anchor="middle">顺序表 SqList（连续地址映像）</text>
    <text class="fig-green" x="569" y="38" font-size="11.5" text-anchor="middle">链表 LinkList（离散结点 + 指针）</text>
    <text class="fig-label" x="87" y="75" font-size="10.5" text-anchor="middle">① 存储密度</text>
    <text class="fig-muted" x="87" y="91" font-size="9.5" text-anchor="middle">数据域 / 结点总字节</text>
    <text class="fig-green" x="296" y="74" font-size="10.5" text-anchor="middle">存储密度 = 1 ★（空间全存数据）</text>
    <text class="fig-muted" x="296" y="91" font-size="9.5" text-anchor="middle">无指针开销，CPU Cache 连续预取友好</text>
    <text class="fig-orange" x="569" y="74" font-size="10.5" text-anchor="middle">存储密度 &lt; 1（指针域占额外空间）</text>
    <text class="fig-muted" x="569" y="91" font-size="9.5" text-anchor="middle">结点分散于堆区，指针跳转存在访存开销</text>
    <text class="fig-label" x="87" y="127" font-size="10.5" text-anchor="middle">② 存取与查找</text>
    <text class="fig-muted" x="87" y="143" font-size="9.5" text-anchor="middle">按位序 vs 按值</text>
    <text class="fig-green" x="296" y="126" font-size="10.5" text-anchor="middle">按位随机存取 O(1) ★</text>
    <text class="fig-muted" x="296" y="143" font-size="9.5" text-anchor="middle">按值查找：无序 O(n) · 有序折半 O(log n)</text>
    <text class="fig-orange" x="569" y="126" font-size="10.5" text-anchor="middle">仅支持顺序存取 O(n)</text>
    <text class="fig-muted" x="569" y="143" font-size="9.5" text-anchor="middle">有序链表亦无法 O(1) 取中点，不能折半查找</text>
    <text class="fig-label" x="87" y="181" font-size="10.5" text-anchor="middle">③ 插入与删除</text>
    <text class="fig-muted" x="87" y="198" font-size="9.5" text-anchor="middle">★ 定位 + 修改拆解</text>
    <text class="fig-orange" x="296" y="180" font-size="10" text-anchor="middle">定位 O(1) + 搬移元素 O(n) ⇒ 总 O(n)</text>
    <text class="fig-muted" x="296" y="198" font-size="9.5" text-anchor="middle">平均移动约 n/2 个元素，大结构体复制代价高</text>
    <text class="fig-green" x="569" y="180" font-size="10" text-anchor="middle">已知前驱指针时：改指针 O(1) ★（零搬移）</text>
    <text class="fig-muted" x="569" y="198" font-size="9.5" text-anchor="middle">注意：若给定按位序 i 插删，遍历找前驱仍需 O(n)</text>
    <text class="fig-purple" x="87" y="236" font-size="10.5" text-anchor="middle">④ 容量与选型</text>
    <text class="fig-muted" x="87" y="252" font-size="9.5" text-anchor="middle">适用业务特征</text>
    <text class="fig-label" x="296" y="236" font-size="10" text-anchor="middle">预分配大片连续空间 · 适合「表长固定、多查少改」</text>
    <text class="fig-muted" x="296" y="252" font-size="9.5" text-anchor="middle">动态顺序表扩容需申请新空间并整表搬迁</text>
    <text class="fig-label" x="569" y="236" font-size="10" text-anchor="middle">动态按需分配结点 · 适合「长度多变、频繁增删」</text>
    <text class="fig-muted" x="569" y="252" font-size="9.5" text-anchor="middle">无需预先估计最大规模，碎片空间即可容纳</text>
  </svg>
  <figcaption>图 3-6：顺序表与链表四维选型决策矩阵——把握“按位查选顺序表，已知指针频增删选链表”的核心准则</figcaption>
</figure>

## 四、栈与队列

### 栈

**定义**：只允许在**一端**进行插入和删除的线性表，这一端叫**栈顶**，另一端叫栈底。

**特点**：后进先出（LIFO），最后放进去的最先被拿走。入栈叫 `push`，出栈叫 `pop`。

**顺序栈**：用数组存放元素，`top` 记录栈顶位置。
- 栈空：`top == -1`
- 栈满：`top == MaxSize - 1`

<figure class="cpd-figure not-content" id="ds40-w2-fig-1">
  <svg viewBox="0 0 700 250" role="img" aria-label="顺序栈 LIFO 进出栈演化与两种 top 指针初值约定对比">
    <defs>
      <marker id="ds40-w2-arr-1" viewBox="0 0 10 10" refX="8.5" refY="5" markerWidth="6.5" markerHeight="6.5" markerUnits="userSpaceOnUse" orient="auto-start-reverse">
        <path d="M 1 1.5 L 8.5 5 L 1 8.5 z" fill="context-stroke" stroke="none"/>
      </marker>
    </defs>
    <rect class="fig-box-subtle" x="16" y="14" width="668" height="222" rx="8"/>
    <rect class="fig-box" x="28" y="42" width="402" height="182" rx="6"/>
    <rect class="fig-box-purple" x="442" y="42" width="230" height="182" rx="6"/>
    <line class="fig-line" stroke="var(--sl-color-orange)" stroke-width="1.5" x1="126" y1="194" x2="100" y2="194" marker-end="url(#ds40-w2-arr-1)"/>
    <line class="fig-line" stroke="var(--sl-color-blue)" stroke-width="1.5" x1="254" y1="138" x2="228" y2="138" marker-end="url(#ds40-w2-arr-1)"/>
    <line class="fig-line" stroke="var(--sl-color-red)" stroke-width="1.5" x1="382" y1="82" x2="356" y2="82" marker-end="url(#ds40-w2-arr-1)"/>
    <path class="fig-line" stroke="var(--sl-color-green)" stroke-width="1.4" d="M 192 66 L 192 108" marker-end="url(#ds40-w2-arr-1)"/>
    <path class="fig-line" stroke="var(--sl-color-orange)" stroke-width="1.4" d="M 212 108 L 212 66" marker-end="url(#ds40-w2-arr-1)"/>
    <rect class="fig-box-subtle" x="52" y="68" width="46" height="28" rx="3"/>
    <rect class="fig-box-subtle" x="52" y="96" width="46" height="28" rx="3"/>
    <rect class="fig-box-subtle" x="52" y="124" width="46" height="28" rx="3"/>
    <rect class="fig-box-subtle" x="52" y="152" width="46" height="28" rx="3"/>
    <rect class="fig-box-dashed" x="52" y="180" width="46" height="26" rx="3"/>
    <rect class="fig-box-subtle" x="180" y="68" width="46" height="28" rx="3"/>
    <rect class="fig-box-subtle" x="180" y="96" width="46" height="28" rx="3"/>
    <rect class="fig-box-blue" x="180" y="124" width="46" height="28" rx="3"/>
    <rect class="fig-box-green" x="180" y="152" width="46" height="28" rx="3"/>
    <rect class="fig-box-red" x="308" y="68" width="46" height="28" rx="3"/>
    <rect class="fig-box-green" x="308" y="96" width="46" height="28" rx="3"/>
    <rect class="fig-box-green" x="308" y="124" width="46" height="28" rx="3"/>
    <rect class="fig-box-green" x="308" y="152" width="46" height="28" rx="3"/>
    <line class="fig-divider" x1="454" y1="74" x2="660" y2="74"/>
    <line class="fig-divider" x1="454" y1="192" x2="660" y2="192"/>
    <text class="fig-title" x="350" y="32" font-size="12.5" text-anchor="middle">顺序栈 LIFO 状态演化与 408 两种 top 指针约定辨析（MaxSize = 4）</text>
    <text class="fig-blue" x="229" y="55" font-size="11.5" text-anchor="middle">① 默认约定：top 指向栈顶元素（初值 -1）</text>
    <text class="fig-muted" x="44" y="86" font-size="10" text-anchor="end">3</text>
    <text class="fig-muted" x="44" y="114" font-size="10" text-anchor="end">2</text>
    <text class="fig-muted" x="44" y="142" font-size="10" text-anchor="end">1</text>
    <text class="fig-muted" x="44" y="170" font-size="10" text-anchor="end">0</text>
    <text class="fig-muted" x="44" y="197" font-size="10" text-anchor="end">-1</text>
    <text class="fig-orange" x="130" y="198" font-size="10.5">top = -1</text>
    <text class="fig-muted" x="75" y="217" font-size="10.5" text-anchor="middle">栈空</text>
    <text class="fig-green" x="175" y="84" font-size="9.5" text-anchor="end">push</text>
    <text class="fig-orange" x="230" y="84" font-size="9.5">pop</text>
    <text class="fig-blue" x="203" y="143" font-size="12" text-anchor="middle">B</text>
    <text class="fig-green" x="203" y="171" font-size="12" text-anchor="middle">A</text>
    <text class="fig-blue" x="258" y="141" font-size="10.5">top = 1</text>
    <text class="fig-muted" x="203" y="198" font-size="10" text-anchor="middle">进: ++top</text>
    <text class="fig-muted" x="203" y="214" font-size="10" text-anchor="middle">出: top--</text>
    <text class="fig-red" x="331" y="87" font-size="12" text-anchor="middle">D</text>
    <text class="fig-green" x="331" y="115" font-size="12" text-anchor="middle">C</text>
    <text class="fig-green" x="331" y="143" font-size="12" text-anchor="middle">B</text>
    <text class="fig-green" x="331" y="171" font-size="12" text-anchor="middle">A</text>
    <text class="fig-red" x="386" y="86" font-size="10.5">top = 3</text>
    <text class="fig-red" x="331" y="214" font-size="10.5" text-anchor="middle">栈满 (Max-1)</text>
    <text class="fig-purple" x="557" y="63" font-size="11.5" text-anchor="middle">② 变体考点：top 指栈顶下一空位</text>
    <text class="fig-label" x="456" y="94" font-size="10.5">• 栈空条件：top == 0</text>
    <text class="fig-label" x="456" y="116" font-size="10.5">• 进栈先存后加：data[top++] = x</text>
    <text class="fig-label" x="456" y="138" font-size="10.5">• 出栈先减后取：x = data[--top]</text>
    <text class="fig-label" x="456" y="160" font-size="10.5">• 栈满条件：top == MaxSize</text>
    <text class="fig-label" x="456" y="182" font-size="10.5">• 当前元素个数：直接等于 top</text>
    <text class="fig-orange" x="557" y="211" font-size="10.5" text-anchor="middle">★ 审题先看初值是 -1 还是 0！</text>
  </svg>
  <figcaption>顺序栈机制与高频审题陷阱：默认 top 初值为 -1（先加后压），若题干设初值为 0 则变为先压后加</figcaption>
</figure>

**共享栈**：两个栈共享同一个数组，栈底分别在数组两端，从两端向中间生长。
- 栈满条件：`top0 + 1 == top1`
- 好处：两个栈的空间可以互相调剂，提高空间利用率。

<figure class="cpd-figure not-content" id="ds40-fig-s1">
  <svg viewBox="0 0 680 186" role="img" aria-label="共享栈：两端向中间生长与栈满判据">
    <defs>
      <marker id="ds40-arr-s1" viewBox="0 0 10 10" refX="8" refY="5" markerWidth="6" markerHeight="6" markerUnits="userSpaceOnUse" orient="auto-start-reverse">
        <path d="M1.5,1.5 L8.5,5 L1.5,8.5" fill="none" stroke="context-stroke" stroke-width="1.5" stroke-linecap="round" stroke-linejoin="round"/>
      </marker>
    </defs>
    <line class="fig-line" stroke="var(--sl-color-blue)" stroke-width="1.5" x1="235" y1="24" x2="235" y2="52" marker-end="url(#ds40-arr-s1)"/>
    <line class="fig-line" stroke="var(--sl-color-green)" stroke-width="1.5" x1="375" y1="24" x2="375" y2="52" marker-end="url(#ds40-arr-s1)"/>
    <line class="fig-line" stroke="var(--sl-color-blue)" stroke-width="1.4" x1="72" y1="138" x2="255" y2="138" marker-end="url(#ds40-arr-s1)"/>
    <line class="fig-line" stroke="var(--sl-color-green)" stroke-width="1.4" x1="608" y1="138" x2="355" y2="138" marker-end="url(#ds40-arr-s1)"/>
    <rect class="fig-box-blue" x="60" y="56" width="70" height="44" rx="4"/>
    <rect class="fig-box-blue" x="130" y="56" width="70" height="44" rx="4"/>
    <rect class="fig-box-blue" x="200" y="56" width="70" height="44" rx="4"/>
    <rect class="fig-box-subtle" x="270" y="56" width="70" height="44" rx="4"/>
    <rect class="fig-box-green" x="340" y="56" width="70" height="44" rx="4"/>
    <rect class="fig-box-green" x="410" y="56" width="70" height="44" rx="4"/>
    <rect class="fig-box-green" x="480" y="56" width="70" height="44" rx="4"/>
    <rect class="fig-box-green" x="550" y="56" width="70" height="44" rx="4"/>
    <text class="fig-blue" x="235" y="19" text-anchor="middle" font-size="12">top0 = 2</text>
    <text class="fig-green" x="375" y="19" text-anchor="middle" font-size="12">top1 = 4</text>
    <g font-size="13" text-anchor="middle">
      <text class="fig-blue" x="95" y="83">a₁</text>
      <text class="fig-blue" x="165" y="83">a₂</text>
      <text class="fig-blue" x="235" y="83">a₃</text>
      <text class="fig-muted" x="305" y="83">空闲</text>
      <text class="fig-green" x="375" y="83">b₄</text>
      <text class="fig-green" x="445" y="83">b₃</text>
      <text class="fig-green" x="515" y="83">b₂</text>
      <text class="fig-green" x="585" y="83">b₁</text>
    </g>
    <g class="fig-muted" font-size="11" text-anchor="middle">
      <text x="95" y="116">0（栈0底）</text>
      <text x="165" y="116">1</text>
      <text x="235" y="116">2</text>
      <text x="305" y="116">3</text>
      <text x="375" y="116">4</text>
      <text x="445" y="116">5</text>
      <text x="515" y="116">6</text>
      <text x="585" y="116">7（栈1底）</text>
    </g>
    <text class="fig-blue" x="160" y="155" text-anchor="middle" font-size="12">0 号栈向右进栈（top0++）</text>
    <text class="fig-green" x="485" y="155" text-anchor="middle" font-size="12">1 号栈向左进栈（top1--）</text>
    <text class="fig-muted" x="340" y="172" text-anchor="middle" font-size="11.5">两栈迎面生长，当 top0 + 1 == top1 时共享空间耗尽（栈满）</text>
  </svg>
  <figcaption>共享栈内存布局：两栈共享同一数组，栈底分居两端，空间互补调剂</figcaption>
</figure>

> **★考点**：出栈序列合法性。

入栈顺序为 1、2、3，出栈序列 `312` 不合法。

理由：3 最先出栈，说明 1、2 都已经入栈，而且 2 压在 1 上面；3 出栈后栈顶是 2，下一个只能出 2，不可能出 1。

合法序列共 5 种：`123`、`132`、`213`、`231`、`321`。

<figure class="cpd-figure not-content" id="ds40-img4-fig-1">
  <svg viewBox="0 0 700 220" role="img" aria-label="入栈序列 1、2、3 下出栈序列 312 非法的逐步栈状态追踪">
    <defs>
      <marker id="ds40-img4-arr-1" viewBox="0 0 10 10" refX="8.5" refY="5" markerWidth="6.5" markerHeight="6.5" markerUnits="userSpaceOnUse" orient="auto-start-reverse">
        <path d="M 1 1.5 L 8.5 5 L 1 8.5 z" fill="context-stroke" stroke="none"/>
      </marker>
    </defs>
    <rect class="fig-box-subtle" x="16" y="14" width="668" height="192" rx="8"/>
    <line class="fig-line" stroke="var(--sl-color-blue)" stroke-width="1.5" x1="128" y1="116" x2="152" y2="116" marker-end="url(#ds40-img4-arr-1)"/>
    <line class="fig-line" stroke="var(--sl-color-blue)" stroke-width="1.5" x1="256" y1="116" x2="280" y2="116" marker-end="url(#ds40-img4-arr-1)"/>
    <line class="fig-line" stroke="var(--sl-color-green)" stroke-width="1.5" x1="384" y1="116" x2="408" y2="116" marker-end="url(#ds40-img4-arr-1)"/>
    <line class="fig-line" stroke="var(--sl-color-red)" stroke-width="1.5" x1="512" y1="116" x2="536" y2="116" marker-end="url(#ds40-img4-arr-1)"/>
    <rect class="fig-box" x="28" y="48" width="98" height="144" rx="6"/>
    <rect class="fig-box" x="156" y="48" width="98" height="144" rx="6"/>
    <rect class="fig-box" x="284" y="48" width="98" height="144" rx="6"/>
    <rect class="fig-box" x="412" y="48" width="98" height="144" rx="6"/>
    <rect class="fig-box-red" x="540" y="48" width="132" height="144" rx="6"/>
    <rect class="fig-box-subtle" x="52" y="76" width="50" height="24" rx="3"/>
    <rect class="fig-box-subtle" x="52" y="102" width="50" height="24" rx="3"/>
    <rect class="fig-box-blue" x="52" y="128" width="50" height="24" rx="3"/>
    <rect class="fig-box-subtle" x="180" y="76" width="50" height="24" rx="3"/>
    <rect class="fig-box-orange" x="180" y="102" width="50" height="24" rx="3"/>
    <rect class="fig-box-blue" x="180" y="128" width="50" height="24" rx="3"/>
    <rect class="fig-box-green" x="308" y="76" width="50" height="24" rx="3"/>
    <rect class="fig-box-orange" x="308" y="102" width="50" height="24" rx="3"/>
    <rect class="fig-box-blue" x="308" y="128" width="50" height="24" rx="3"/>
    <rect class="fig-box-dashed" x="436" y="76" width="50" height="24" rx="3"/>
    <rect class="fig-box-orange" x="436" y="102" width="50" height="24" rx="3"/>
    <rect class="fig-box-blue" x="436" y="128" width="50" height="24" rx="3"/>
    <rect class="fig-box-orange" x="581" y="96" width="50" height="24" rx="3"/>
    <rect class="fig-box-blue" x="581" y="122" width="50" height="24" rx="3"/>
    <text class="fig-title" x="350" y="34" font-size="12.5" text-anchor="middle">图 18：出栈序列 312 非法追踪（入栈顺序：1 → 2 → 3）</text>
    <text class="fig-blue" x="77" y="65" font-size="10.5" text-anchor="middle">① push(1)</text>
    <text class="fig-blue" x="77" y="144" font-size="11.5" text-anchor="middle">1 (顶)</text>
    <text class="fig-muted" x="77" y="176" font-size="9.5" text-anchor="middle">栈：[1]</text>
    <text class="fig-blue" x="205" y="65" font-size="10.5" text-anchor="middle">② push(2)</text>
    <text class="fig-orange" x="205" y="118" font-size="11.5" text-anchor="middle">2 (顶)</text>
    <text class="fig-blue" x="205" y="144" font-size="11.5" text-anchor="middle">1</text>
    <text class="fig-muted" x="205" y="176" font-size="9.5" text-anchor="middle">栈：[1, 2]</text>
    <text class="fig-green" x="333" y="65" font-size="10.5" text-anchor="middle">③ push(3)</text>
    <text class="fig-green" x="333" y="92" font-size="11.5" text-anchor="middle">3 (顶)</text>
    <text class="fig-orange" x="333" y="118" font-size="11.5" text-anchor="middle">2</text>
    <text class="fig-blue" x="333" y="144" font-size="11.5" text-anchor="middle">1</text>
    <text class="fig-muted" x="333" y="176" font-size="9.5" text-anchor="middle">栈：[1, 2, 3]</text>
    <text class="fig-green" x="461" y="65" font-size="10.5" text-anchor="middle">④ pop → 3</text>
    <text class="fig-muted" x="461" y="92" font-size="10" text-anchor="middle">3 已出</text>
    <text class="fig-orange" x="461" y="118" font-size="11.5" text-anchor="middle">2 (顶)</text>
    <text class="fig-blue" x="461" y="144" font-size="11.5" text-anchor="middle">1 (底)</text>
    <text class="fig-muted" x="461" y="176" font-size="9.5" text-anchor="middle">已出：3</text>
    <text class="fig-red" x="606" y="65" font-size="10.5" text-anchor="middle">⑤ 想出 1？✗ 非法！</text>
    <text class="fig-muted" x="606" y="84" font-size="9.5" text-anchor="middle">2 压在 1 上方</text>
    <text class="fig-orange" x="606" y="112" font-size="11.5" text-anchor="middle">2 (顶)</text>
    <text class="fig-blue" x="606" y="138" font-size="11.5" text-anchor="middle">1 (被压)</text>
    <text class="fig-red" x="606" y="164" font-size="10" text-anchor="middle">此时只能 pop 出 2</text>
    <text class="fig-red" x="606" y="180" font-size="10" text-anchor="middle">1 绝不可能先于 2 出</text>
  </svg>
  <figcaption>图 18：3 最先出栈后，2 压在 1 上，下一个不可能是 1</figcaption>
</figure>

**出栈序列总数**：$n$ 个元素依次入栈，不同出栈序列的个数为卡特兰数：

$$
\frac{1}{n+1}\binom{2n}{n}
$$

$n=3$ 时：$\binom{6}{3}/4 = 20/4 = 5$。

<figure class="cpd-figure not-content" id="ds40-img4-fig-2">
  <svg viewBox="0 0 700 226" role="img" aria-label="卡特兰数出栈序列总数公式与 n=3 时 5 种合法出栈序列">
    <rect class="fig-box-subtle" x="16" y="14" width="668" height="198" rx="8"/>
    <rect class="fig-box" x="28" y="44" width="256" height="154" rx="6"/>
    <rect class="fig-box" x="296" y="44" width="376" height="154" rx="6"/>
    <rect class="fig-box-accent" x="42" y="72" width="228" height="44" rx="5"/>
    <rect class="fig-box-blue" x="42" y="126" width="228" height="58" rx="5"/>
    <rect class="fig-box-green" x="310" y="70" width="168" height="32" rx="4"/>
    <rect class="fig-box-green" x="490" y="70" width="168" height="32" rx="4"/>
    <rect class="fig-box-green" x="310" y="110" width="168" height="32" rx="4"/>
    <rect class="fig-box-green" x="490" y="110" width="168" height="32" rx="4"/>
    <rect class="fig-box-green" x="310" y="150" width="168" height="32" rx="4"/>
    <rect class="fig-box-red" x="490" y="150" width="168" height="32" rx="4"/>
    <text class="fig-title" x="350" y="34" font-size="12.5" text-anchor="middle">图 19：出栈序列总数公式（卡特兰数）与 n = 3 的 5 种合法序列</text>
    <text class="fig-title" x="156" y="63" font-size="11.5" text-anchor="middle">① 卡特兰数公式</text>
    <text class="fig-accent" x="156" y="99" font-size="13" text-anchor="middle">Cₙ = C(2n, n) / (n + 1)</text>
    <text class="fig-blue" x="156" y="144" font-size="10.5" text-anchor="middle">n = 3 代入计算：</text>
    <text class="fig-label" x="156" y="162" font-size="10.5" text-anchor="middle">C₃ = C(6, 3) / 4 = 20 / 4 = 5</text>
    <text class="fig-muted" x="156" y="177" font-size="9.5" text-anchor="middle">总排列 3! = 6 种，仅 312 非法</text>
    <text class="fig-green" x="484" y="63" font-size="11.5" text-anchor="middle">② n = 3 全部 5 种合法出栈序列（3! - 1 = 5）</text>
    <text class="fig-green" x="394" y="90" font-size="11" text-anchor="middle">1. 1 2 3（随进随出）</text>
    <text class="fig-green" x="574" y="90" font-size="11" text-anchor="middle">2. 1 3 2（1 出后 3,2 出）</text>
    <text class="fig-green" x="394" y="130" font-size="11" text-anchor="middle">3. 2 1 3（1,2 进后出）</text>
    <text class="fig-green" x="574" y="130" font-size="11" text-anchor="middle">4. 2 3 1（1 垫底最后出）</text>
    <text class="fig-green" x="394" y="170" font-size="11" text-anchor="middle">5. 3 2 1（全进再全出）</text>
    <text class="fig-red" x="574" y="170" font-size="11" text-anchor="middle">✗ 3 1 2（唯一非法排列）</text>
  </svg>
  <figcaption>图 19：出栈序列数公式与 n = 3 的 5 种合法序列</figcaption>
</figure>

<figure class="cpd-figure not-content" id="ds40-w2-fig-2">
  <svg viewBox="0 0 700 266" role="img" aria-label="出栈序列 312 非法反证与卡特兰数 5 种合法操作轨迹全解">
    <defs>
      <marker id="ds40-w2-arr-2" viewBox="0 0 10 10" refX="8.5" refY="5" markerWidth="6.5" markerHeight="6.5" markerUnits="userSpaceOnUse" orient="auto-start-reverse">
        <path d="M 1 1.5 L 8.5 5 L 1 8.5 z" fill="context-stroke" stroke="none"/>
      </marker>
    </defs>
    <rect class="fig-box-subtle" x="16" y="14" width="668" height="238" rx="8"/>
    <rect class="fig-box" x="28" y="42" width="322" height="198" rx="6"/>
    <rect class="fig-box" x="362" y="42" width="310" height="198" rx="6"/>
    <line class="fig-line" stroke="var(--sl-color-green)" stroke-width="1.5" x1="102" y1="88" x2="148" y2="88" marker-end="url(#ds40-w2-arr-2)"/>
    <line class="fig-line" stroke="var(--sl-color-red)" stroke-width="1.5" stroke-dasharray="4 3" x1="102" y1="148" x2="156" y2="148" marker-end="url(#ds40-w2-arr-2)"/>
    <rect class="fig-box-dashed" x="48" y="72" width="52" height="30" rx="4"/>
    <rect class="fig-box-orange" x="48" y="102" width="52" height="30" rx="4"/>
    <rect class="fig-box-blue" x="48" y="132" width="52" height="30" rx="4"/>
    <rect class="fig-box-green" x="152" y="73" width="184" height="28" rx="4"/>
    <rect class="fig-box-red" x="160" y="133" width="176" height="30" rx="4"/>
    <rect class="fig-box-purple" x="40" y="176" width="298" height="54" rx="5"/>
    <rect class="fig-box-green" x="374" y="68" width="286" height="22" rx="4"/>
    <rect class="fig-box-green" x="374" y="94" width="286" height="22" rx="4"/>
    <rect class="fig-box-green" x="374" y="120" width="286" height="22" rx="4"/>
    <rect class="fig-box-green" x="374" y="146" width="286" height="22" rx="4"/>
    <rect class="fig-box-green" x="374" y="172" width="286" height="22" rx="4"/>
    <rect class="fig-box-accent" x="374" y="202" width="286" height="28" rx="4"/>
    <text class="fig-title" x="350" y="32" font-size="12.5" text-anchor="middle">出栈序列合法性秒杀法则（入栈次序 1 → 2 → 3）与卡特兰数速查</text>
    <text class="fig-red" x="189" y="60" font-size="11.5" text-anchor="middle">① 反证：为何 312 绝对非法？</text>
    <text class="fig-muted" x="74" y="91" font-size="11" text-anchor="middle">3 已出</text>
    <text class="fig-orange" x="74" y="122" font-size="12" text-anchor="middle">2 (顶)</text>
    <text class="fig-blue" x="74" y="152" font-size="12" text-anchor="middle">1 (底)</text>
    <text class="fig-green" x="244" y="91" font-size="10.5" text-anchor="middle">首出 3 ⇒ 1、2 必已压在栈内</text>
    <text class="fig-red" x="248" y="152" font-size="10.5" text-anchor="middle">✗ 2 封住栈顶，1 无法越过 2 先出</text>
    <text class="fig-purple" x="189" y="194" font-size="10.5" text-anchor="middle">★ 408 秒杀通法（逆序判据）：</text>
    <text class="fig-label" x="189" y="210" font-size="10" text-anchor="middle">对任一元素 x，其后所有 &lt; x 的元素必按递减逆序排列</text>
    <text class="fig-muted" x="189" y="224" font-size="9.5" text-anchor="middle">例：312 中 3 后面有 1、2（均 &lt; 3），但 1→2 递增 ⇒ 非法！</text>
    <text class="fig-green" x="517" y="60" font-size="11.5" text-anchor="middle">② n = 3 全部 5 种合法出栈轨迹（P=进, O=出）</text>
    <text class="fig-label" x="386" y="83" font-size="10.5">123 ：P1 O1 P2 O2 P3 O3（随进随出）</text>
    <text class="fig-label" x="386" y="109" font-size="10.5">132 ：P1 O1 P2 P3 O3 O2（1 出后 2、3 逆序出）</text>
    <text class="fig-label" x="386" y="135" font-size="10.5">213 ：P1 P2 O2 O1 P3 O3（1、2 逆序出后进出 3）</text>
    <text class="fig-label" x="386" y="161" font-size="10.5">231 ：P1 P2 O2 P3 O3 O1（1 垫底最后出）</text>
    <text class="fig-label" x="386" y="187" font-size="10.5">321 ：P1 P2 P3 O3 O2 O1（全进再全出，完全逆序）</text>
    <text class="fig-accent" x="517" y="220" font-size="10.5" text-anchor="middle">高频卡特兰数：C₁=1，C₂=2，C₃=5，C₄=14，C₅=42</text>
  </svg>
  <figcaption>出栈合法性判据与卡特兰数：任一元素 x 出栈后，所有比 x 早入栈且尚未出栈的元素必按逆序依次弹出</figcaption>
</figure>

**栈的应用**：

1. **括号匹配**：遇左括号入栈，遇右括号弹出栈顶检查是否配对；扫描结束后栈为空才算全部匹配。
2. **表达式求值**：平时写的 `A+B*C` 是中缀表达式，计算机更喜欢后缀表达式 `ABC*+`。从左到右扫描，操作数入栈；遇运算符弹出两个操作数计算，再把结果压回栈中。
3. **递归**：函数调用时系统把参数和返回地址压入调用栈，返回时再弹出；递归太深会栈溢出。

<figure class="cpd-figure not-content" id="ds40-w2-fig-3">
  <svg viewBox="0 0 700 282" role="img" aria-label="栈的括号匹配判定、中缀转后缀运算符栈规则与后缀表达式操作数栈求值步骤">
    <rect class="fig-box-subtle" x="16" y="14" width="668" height="254" rx="8"/>
    <rect class="fig-box" x="28" y="42" width="644" height="58" rx="6"/>
    <rect class="fig-box-green" x="40" y="64" width="196" height="28" rx="4"/>
    <rect class="fig-box-red" x="248" y="64" width="202" height="28" rx="4"/>
    <rect class="fig-box-orange" x="462" y="64" width="198" height="28" rx="4"/>
    <rect class="fig-box" x="28" y="108" width="308" height="148" rx="6"/>
    <rect class="fig-box" x="348" y="108" width="324" height="148" rx="6"/>
    <rect class="fig-box-blue" x="40" y="132" width="284" height="26" rx="4"/>
    <rect class="fig-box-purple" x="40" y="164" width="284" height="52" rx="4"/>
    <rect class="fig-box-accent" x="40" y="222" width="284" height="24" rx="4"/>
    <rect class="fig-box-blue" x="360" y="132" width="64" height="84" rx="4"/>
    <rect class="fig-box-orange" x="432" y="132" width="76" height="84" rx="4"/>
    <rect class="fig-box-purple" x="516" y="132" width="70" height="84" rx="4"/>
    <rect class="fig-box-green" x="594" y="132" width="66" height="84" rx="4"/>
    <rect class="fig-box-red" x="360" y="222" width="300" height="24" rx="4"/>
    <text class="fig-title" x="350" y="32" font-size="12.5" text-anchor="middle">栈的核心应用一：括号匹配三态判定 &amp; 中缀转后缀与后缀表达式求值</text>
    <text class="fig-label" x="40" y="57" font-size="11">① 括号匹配（左括号入栈，右括号弹栈比对）：</text>
    <text class="fig-green" x="138" y="82" font-size="10.5" text-anchor="middle">[ ( ) ] → 扫描完栈空 ✓ 合法</text>
    <text class="fig-red" x="349" y="82" font-size="10.5" text-anchor="middle">[ ( ] ) → 弹 ( 遇 ] 失配 ✗</text>
    <text class="fig-orange" x="561" y="82" font-size="10.5" text-anchor="middle">( ( ) → 扫描完栈非空（缺右）✗</text>
    <text class="fig-blue" x="182" y="125" font-size="11.5" text-anchor="middle">② 中缀转后缀（用运算符栈）</text>
    <text class="fig-blue" x="182" y="149" font-size="10.5" text-anchor="middle">3 + 4 * ( 5 - 3 )  ⇒  3 4 5 3 - * +</text>
    <text class="fig-purple" x="50" y="181" font-size="10">• 操作数：直接输出到后缀序列</text>
    <text class="fig-purple" x="50" y="196" font-size="10">• 界限符：( 直接入栈；遇 ) 持续弹栈直至弹出 (</text>
    <text class="fig-purple" x="50" y="211" font-size="10">• 运算符：先弹出栈内优先级 &gt;= 它的，再入栈</text>
    <text class="fig-accent" x="182" y="238" font-size="10" text-anchor="middle">左优先原则：同级运算符先算左边，保证手算结果唯一</text>
    <text class="fig-green" x="510" y="125" font-size="11.5" text-anchor="middle">③ 后缀求值：3 4 5 3 - * +（用操作数栈）</text>
    <text class="fig-blue" x="392" y="149" font-size="10" text-anchor="middle">压 3,4,5,3</text>
    <text class="fig-label" x="392" y="167" font-size="10.5" text-anchor="middle">[3,4,5,3]</text>
    <text class="fig-muted" x="392" y="185" font-size="9.5" text-anchor="middle">栈顶为右</text>
    <text class="fig-muted" x="392" y="203" font-size="9.5" text-anchor="middle">操作数 3</text>
    <text class="fig-orange" x="470" y="149" font-size="10" text-anchor="middle">遇 “-”</text>
    <text class="fig-label" x="470" y="167" font-size="10.5" text-anchor="middle">5 - 3 = 2</text>
    <text class="fig-muted" x="470" y="185" font-size="9.5" text-anchor="middle">弹 b=3, a=5</text>
    <text class="fig-muted" x="470" y="203" font-size="9.5" text-anchor="middle">栈: [3,4,2]</text>
    <text class="fig-purple" x="551" y="149" font-size="10" text-anchor="middle">遇 “*”</text>
    <text class="fig-label" x="551" y="167" font-size="10.5" text-anchor="middle">4 * 2 = 8</text>
    <text class="fig-muted" x="551" y="185" font-size="9.5" text-anchor="middle">弹 b=2, a=4</text>
    <text class="fig-muted" x="551" y="203" font-size="9.5" text-anchor="middle">栈: [3,8]</text>
    <text class="fig-green" x="627" y="149" font-size="10" text-anchor="middle">遇 “+”</text>
    <text class="fig-green" x="627" y="167" font-size="10.5" text-anchor="middle">3 + 8 = 11</text>
    <text class="fig-muted" x="627" y="185" font-size="9.5" text-anchor="middle">弹 b=8, a=3</text>
    <text class="fig-green" x="627" y="203" font-size="9.5" text-anchor="middle">终值: 11</text>
    <text class="fig-red" x="510" y="238" font-size="10" text-anchor="middle">★ 易错点：先弹出的是右操作数 b，后弹出左操作数 a（算 a - b 或 a / b）</text>
  </svg>
  <figcaption>栈在编译求值中的流水线：中缀转后缀维护“运算符栈”，后缀求值维护“操作数栈”（注意减/除法的左右操作数弹出次序）</figcaption>
</figure>

<figure class="cpd-figure not-content" id="ds40-w2-fig-4">
  <svg viewBox="0 0 700 226" role="img" aria-label="递归函数调用栈帧压入与弹出生命周期及栈帧三要素结构">
    <defs>
      <marker id="ds40-w2-arr-4" viewBox="0 0 10 10" refX="8.5" refY="5" markerWidth="6.5" markerHeight="6.5" markerUnits="userSpaceOnUse" orient="auto-start-reverse">
        <path d="M 1 1.5 L 8.5 5 L 1 8.5 z" fill="context-stroke" stroke="none"/>
      </marker>
    </defs>
    <rect class="fig-box-subtle" x="16" y="14" width="668" height="198" rx="8"/>
    <rect class="fig-box" x="28" y="42" width="412" height="158" rx="6"/>
    <rect class="fig-box-purple" x="452" y="42" width="220" height="158" rx="6"/>
    <path class="fig-line" stroke="var(--sl-color-blue)" stroke-width="1.5" d="M 52 172 L 52 72" marker-end="url(#ds40-w2-arr-4)"/>
    <path class="fig-line" stroke="var(--sl-color-green)" stroke-width="1.5" d="M 384 72 L 384 172" marker-end="url(#ds40-w2-arr-4)"/>
    <rect class="fig-box-orange" x="88" y="64" width="260" height="34" rx="4"/>
    <rect class="fig-box-blue" x="88" y="106" width="260" height="34" rx="4"/>
    <rect class="fig-box-green" x="88" y="148" width="260" height="34" rx="4"/>
    <rect class="fig-box-red" x="464" y="156" width="196" height="34" rx="4"/>
    <line class="fig-divider" x1="464" y1="70" x2="660" y2="70"/>
    <text class="fig-title" x="350" y="32" font-size="12.5" text-anchor="middle">栈的核心应用二：递归调用工作栈（以 fact(3) = 3! = 6 为例）</text>
    <text class="fig-blue" x="52" y="58" font-size="10" text-anchor="middle">递推压栈</text>
    <text class="fig-green" x="384" y="58" font-size="10" text-anchor="middle">回归弹栈</text>
    <text class="fig-orange" x="218" y="85" font-size="11" text-anchor="middle">栈顶帧：fact(1) ｜ 触底边界 n=1，返回 1</text>
    <text class="fig-blue" x="218" y="127" font-size="11" text-anchor="middle">中间帧：fact(2) ｜ 挂起等返回值，算 2 × 1 = 2</text>
    <text class="fig-green" x="218" y="169" font-size="11" text-anchor="middle">栈底帧：fact(3) ｜ 挂起等返回值，算 3 × 2 = 6</text>
    <text class="fig-purple" x="562" y="61" font-size="11.5" text-anchor="middle">每层栈帧保存三要素</text>
    <text class="fig-label" x="468" y="90" font-size="10.5">① 调用实参 / 形参（如 n）</text>
    <text class="fig-label" x="468" y="114" font-size="10.5">② 本层局部变量（独立副本）</text>
    <text class="fig-label" x="468" y="138" font-size="10.5">③ 返回地址 RA（上一层断点）</text>
    <text class="fig-red" x="562" y="177" font-size="10" text-anchor="middle">无出口或层数过深 ⇒ Stack Overflow</text>
  </svg>
  <figcaption>递归调用栈原理：每深入一层压入独立栈帧，触及递归边界后按 LIFO 逆序弹出并回代结果</figcaption>
</figure>

### 队列

**定义**：只允许在**队尾**插入、在**队头**删除的线性表。

**特点**：先进先出（FIFO），先来的先出队。

**顺序队列的假溢出**：元素不断从队头出队，front 前面空出来的位置再也用不上；明明还有空间，却因为 rear 到达数组末尾而显示队满。

**循环队列**：把数组首尾相接，指针走到末尾后绕回开头。
- 指针移动：`rear = (rear + 1) % MaxSize`，`front = (front + 1) % MaxSize`
- 下标 7 再加 1 取模后回到 0

约定 `front` 指向队头元素，`rear` 指向队尾元素的下一个位置（下一个空位），并**牺牲一个存储单元**区分队空和队满：

- 队空：`front == rear`
- 队满：`(rear + 1) % MaxSize == front`
- 元素个数：`(rear - front + MaxSize) % MaxSize`
- 例：`(5 - 6 + 8) % 8 = 7`

<figure class="cpd-figure not-content" id="ds40-img4-fig-3">
  <svg viewBox="0 0 700 236" role="img" aria-label="循环队列牺牲一个单元的队空、队满与元素个数三个核心公式示意图">
    <defs>
      <marker id="ds40-img4-arr-2" viewBox="0 0 10 10" refX="8.5" refY="5" markerWidth="6.5" markerHeight="6.5" markerUnits="userSpaceOnUse" orient="auto-start-reverse">
        <path d="M 1 1.5 L 8.5 5 L 1 8.5 z" fill="context-stroke" stroke="none"/>
      </marker>
    </defs>
    <rect class="fig-box-subtle" x="16" y="14" width="668" height="208" rx="8"/>
    <rect class="fig-box" x="28" y="44" width="332" height="164" rx="6"/>
    <rect class="fig-box" x="372" y="44" width="300" height="164" rx="6"/>
    <line class="fig-line" stroke="var(--sl-color-red)" stroke-width="1.5" x1="252" y1="84" x2="252" y2="104" marker-end="url(#ds40-img4-arr-2)"/>
    <line class="fig-line" stroke="var(--sl-color-blue)" stroke-width="1.5" x1="290" y1="84" x2="290" y2="104" marker-end="url(#ds40-img4-arr-2)"/>
    <path class="fig-line-dashed" stroke="var(--sl-color-accent)" d="M 346 122 C 362 122 362 164 194 164 C 34 164 34 140 42 132" marker-end="url(#ds40-img4-arr-2)"/>
    <rect class="fig-box-green" x="44" y="106" width="38" height="32" rx="3"/>
    <rect class="fig-box-green" x="82" y="106" width="38" height="32" rx="3"/>
    <rect class="fig-box-green" x="120" y="106" width="38" height="32" rx="3"/>
    <rect class="fig-box-green" x="158" y="106" width="38" height="32" rx="3"/>
    <rect class="fig-box-green" x="196" y="106" width="38" height="32" rx="3"/>
    <rect class="fig-box-red" x="234" y="106" width="38" height="32" rx="3"/>
    <rect class="fig-box-blue" x="272" y="106" width="38" height="32" rx="3"/>
    <rect class="fig-box-green" x="310" y="106" width="38" height="32" rx="3"/>
    <rect class="fig-box" x="124" y="155" width="140" height="18" rx="3"/>
    <rect class="fig-box-blue" x="384" y="68" width="276" height="40" rx="4"/>
    <rect class="fig-box-red" x="384" y="114" width="276" height="40" rx="4"/>
    <rect class="fig-box-accent" x="384" y="160" width="276" height="40" rx="4"/>
    <text class="fig-title" x="350" y="34" font-size="12.5" text-anchor="middle">图 20：循环队列三大公式（牺牲 1 个红色空单元区分空与满，MaxSize = 8）</text>
    <text class="fig-red" x="234" y="76" font-size="10.5" text-anchor="middle">rear = 5（空）</text>
    <text class="fig-blue" x="304" y="76" font-size="10.5" text-anchor="middle">front = 6</text>
    <text class="fig-green" x="63" y="126" font-size="11" text-anchor="middle">d</text>
    <text class="fig-green" x="101" y="126" font-size="11" text-anchor="middle">e</text>
    <text class="fig-green" x="139" y="126" font-size="11" text-anchor="middle">f</text>
    <text class="fig-green" x="177" y="126" font-size="11" text-anchor="middle">g</text>
    <text class="fig-green" x="215" y="126" font-size="11" text-anchor="middle">h</text>
    <text class="fig-red" x="253" y="126" font-size="10.5" text-anchor="middle">牺牲</text>
    <text class="fig-blue" x="291" y="126" font-size="11" text-anchor="middle">b</text>
    <text class="fig-green" x="329" y="126" font-size="11" text-anchor="middle">c</text>
    <text class="fig-muted" x="63" y="150" font-size="9.5" text-anchor="middle">0</text>
    <text class="fig-muted" x="101" y="150" font-size="9.5" text-anchor="middle">1</text>
    <text class="fig-muted" x="139" y="150" font-size="9.5" text-anchor="middle">2</text>
    <text class="fig-muted" x="177" y="150" font-size="9.5" text-anchor="middle">3</text>
    <text class="fig-muted" x="215" y="150" font-size="9.5" text-anchor="middle">4</text>
    <text class="fig-red" x="253" y="150" font-size="9.5" text-anchor="middle">5</text>
    <text class="fig-blue" x="291" y="150" font-size="9.5" text-anchor="middle">6</text>
    <text class="fig-muted" x="329" y="150" font-size="9.5" text-anchor="middle">7</text>
    <text class="fig-accent" x="194" y="168" font-size="9.5" text-anchor="middle">绕回：(7 + 1) % 8 == 0</text>
    <text class="fig-muted" x="194" y="193" font-size="10" text-anchor="middle">最多存 MaxSize - 1 = 7 个元素</text>
    <text class="fig-blue" x="522" y="60" font-size="11.5" text-anchor="middle">三个必背公式</text>
    <text class="fig-blue" x="396" y="85" font-size="10.5">① 队空条件：front == rear</text>
    <text class="fig-muted" x="396" y="101" font-size="9.5">两指针重合且无有效元素</text>
    <text class="fig-red" x="396" y="131" font-size="10.5">② 队满条件：(rear + 1) % MaxSize == front</text>
    <text class="fig-muted" x="396" y="147" font-size="9.5">例：(5 + 1) % 8 == 6，红色单元被牺牲</text>
    <text class="fig-accent" x="396" y="177" font-size="10.5">③ 元素个数：(rear - front + MaxSize) % MaxSize</text>
    <text class="fig-muted" x="396" y="193" font-size="9.5">例：(5 - 6 + 8) % 8 = 7 个</text>
  </svg>
  <figcaption>图 20：队空、队满、元素个数公式；红色位置为牺牲的空单元</figcaption>
</figure>

<figure class="cpd-figure not-content" id="ds40-w2-fig-5">
  <svg viewBox="0 0 700 286" role="img" aria-label="顺序队列假溢出对比循环队列取模环及三种队空队满判据全表">
    <defs>
      <marker id="ds40-w2-arr-5" viewBox="0 0 10 10" refX="8.5" refY="5" markerWidth="6.5" markerHeight="6.5" markerUnits="userSpaceOnUse" orient="auto-start-reverse">
        <path d="M 1 1.5 L 8.5 5 L 1 8.5 z" fill="context-stroke" stroke="none"/>
      </marker>
    </defs>
    <rect class="fig-box-subtle" x="16" y="14" width="668" height="258" rx="8"/>
    <rect class="fig-box" x="28" y="42" width="322" height="218" rx="6"/>
    <rect class="fig-box" x="362" y="42" width="310" height="218" rx="6"/>
    <line class="fig-line" stroke="var(--sl-color-blue)" stroke-width="1.4" x1="258" y1="126" x2="258" y2="146" marker-end="url(#ds40-w2-arr-5)"/>
    <line class="fig-line" stroke="var(--sl-color-red)" stroke-width="1.4" x1="224" y1="126" x2="224" y2="146" marker-end="url(#ds40-w2-arr-5)"/>
    <path class="fig-line" stroke="var(--sl-color-accent)" stroke-width="1.4" stroke-dasharray="4 3" d="M 326 164 C 344 164 344 204 189 204 C 42 204 42 184 52 178" marker-end="url(#ds40-w2-arr-5)"/>
    <rect class="fig-box-dashed" x="44" y="76" width="38" height="26" rx="3"/>
    <rect class="fig-box-dashed" x="82" y="76" width="38" height="26" rx="3"/>
    <rect class="fig-box-green" x="120" y="76" width="38" height="26" rx="3"/>
    <rect class="fig-box-green" x="158" y="76" width="38" height="26" rx="3"/>
    <rect class="fig-box-orange" x="204" y="76" width="134" height="26" rx="4"/>
    <rect class="fig-box-green" x="46" y="150" width="34" height="28" rx="3"/>
    <rect class="fig-box-green" x="80" y="150" width="34" height="28" rx="3"/>
    <rect class="fig-box-green" x="114" y="150" width="34" height="28" rx="3"/>
    <rect class="fig-box-green" x="148" y="150" width="34" height="28" rx="3"/>
    <rect class="fig-box-green" x="182" y="150" width="34" height="28" rx="3"/>
    <rect class="fig-box-red" x="216" y="150" width="34" height="28" rx="3"/>
    <rect class="fig-box-blue" x="250" y="150" width="34" height="28" rx="3"/>
    <rect class="fig-box-green" x="284" y="150" width="34" height="28" rx="3"/>
    <rect class="fig-box" x="132" y="194" width="114" height="18" rx="3"/>
    <rect class="fig-box-purple" x="44" y="220" width="290" height="30" rx="4"/>
    <rect class="fig-box-orange" x="374" y="68" width="286" height="56" rx="4"/>
    <rect class="fig-box-blue" x="374" y="130" width="286" height="56" rx="4"/>
    <rect class="fig-box-green" x="374" y="192" width="286" height="58" rx="4"/>
    <text class="fig-title" x="350" y="32" font-size="12.5" text-anchor="middle">顺序队列“假溢出” vs 循环队列取模环 &amp; 408 三种判空/判满方案</text>
    <text class="fig-orange" x="189" y="59" font-size="11" text-anchor="middle">① 假溢出 vs 循环绕回（MaxSize = 8）</text>
    <text class="fig-muted" x="63" y="93" font-size="9.5" text-anchor="middle">空</text>
    <text class="fig-muted" x="101" y="93" font-size="9.5" text-anchor="middle">空</text>
    <text class="fig-green" x="139" y="93" font-size="10.5" text-anchor="middle">c</text>
    <text class="fig-green" x="177" y="93" font-size="10.5" text-anchor="middle">d</text>
    <text class="fig-orange" x="271" y="93" font-size="10" text-anchor="middle">rear 撞底，前空却假满</text>
    <text class="fig-red" x="210" y="121" font-size="10" text-anchor="end">rear=5(空)</text>
    <text class="fig-blue" x="246" y="121" font-size="10">front=6</text>
    <text class="fig-green" x="63" y="168" font-size="10.5" text-anchor="middle">e₃</text>
    <text class="fig-green" x="97" y="168" font-size="10.5" text-anchor="middle">e₄</text>
    <text class="fig-green" x="131" y="168" font-size="10.5" text-anchor="middle">e₅</text>
    <text class="fig-green" x="165" y="168" font-size="10.5" text-anchor="middle">e₆</text>
    <text class="fig-green" x="199" y="168" font-size="10.5" text-anchor="middle">e₇</text>
    <text class="fig-red" x="233" y="168" font-size="10" text-anchor="middle">牺牲</text>
    <text class="fig-blue" x="267" y="168" font-size="10.5" text-anchor="middle">e₁</text>
    <text class="fig-green" x="301" y="168" font-size="10.5" text-anchor="middle">e₂</text>
    <text class="fig-muted" x="63" y="190" font-size="9" text-anchor="middle">0</text>
    <text class="fig-muted" x="97" y="190" font-size="9" text-anchor="middle">1</text>
    <text class="fig-muted" x="131" y="190" font-size="9" text-anchor="middle">2</text>
    <text class="fig-muted" x="165" y="190" font-size="9" text-anchor="middle">3</text>
    <text class="fig-muted" x="199" y="190" font-size="9" text-anchor="middle">4</text>
    <text class="fig-red" x="233" y="190" font-size="9" text-anchor="middle">5</text>
    <text class="fig-blue" x="267" y="190" font-size="9" text-anchor="middle">6</text>
    <text class="fig-muted" x="301" y="190" font-size="9" text-anchor="middle">7</text>
    <text class="fig-accent" x="189" y="207" font-size="9.5" text-anchor="middle">(7 + 1) % 8 == 0</text>
    <text class="fig-purple" x="189" y="239" font-size="10" text-anchor="middle">长度：(5 - 6 + 8) % 8 = 7 ｜ 满：(5 + 1) % 8 == 6</text>
    <text class="fig-blue" x="517" y="59" font-size="11" text-anchor="middle">② 区分 front == rear 的 3 种考法</text>
    <text class="fig-orange" x="384" y="85" font-size="10.5">方案 A：牺牲 1 个存储单元（最常考，最多存 Max-1 个）</text>
    <text class="fig-label" x="384" y="102" font-size="10">队空：front == rear</text>
    <text class="fig-label" x="384" y="117" font-size="10">队满：(rear + 1) % MaxSize == front</text>
    <text class="fig-blue" x="384" y="147" font-size="10.5">方案 B：增设数据成员 size（可存满 MaxSize 个）</text>
    <text class="fig-label" x="384" y="164" font-size="10">队空：size == 0 ｜ 队满：size == MaxSize</text>
    <text class="fig-muted" x="384" y="179" font-size="9.5">队空与队满时均有 front == rear</text>
    <text class="fig-green" x="384" y="209" font-size="10.5">方案 C：增设最近操作标志 tag（可存满 MaxSize 个）</text>
    <text class="fig-label" x="384" y="226" font-size="10">出队置 tag=0，入队置 tag=1</text>
    <text class="fig-muted" x="384" y="242" font-size="9.5">空：front==rear &amp;&amp; tag==0 ｜ 满：front==rear &amp;&amp; tag==1</text>
  </svg>
  <figcaption>循环队列模运算与判满全解：牺牲一单元法最多装 MaxSize-1 个元素，配合 size 或 tag 则可装满全部单元</figcaption>
</figure>

**链队列**：用链表实现，`front` 指向队头，`rear` 指向队尾；一般不存在队满问题。

**双端队列**：两端都能插入和删除；栈（只在一端进出）和队列（一端进、另一端出）都是受限的双端队列。

<figure class="cpd-figure not-content" id="ds40-w2-fig-6">
  <svg viewBox="0 0 700 258" role="img" aria-label="带头结点链队列尾指针置空边界与受限双端队列合法出队序列判定">
    <defs>
      <marker id="ds40-w2-arr-6" viewBox="0 0 10 10" refX="8.5" refY="5" markerWidth="6.5" markerHeight="6.5" markerUnits="userSpaceOnUse" orient="auto-start-reverse">
        <path d="M 1 1.5 L 8.5 5 L 1 8.5 z" fill="context-stroke" stroke="none"/>
      </marker>
    </defs>
    <rect class="fig-box-subtle" x="16" y="14" width="668" height="230" rx="8"/>
    <rect class="fig-box" x="28" y="42" width="308" height="190" rx="6"/>
    <rect class="fig-box" x="348" y="42" width="324" height="190" rx="6"/>
    <line class="fig-line" stroke="var(--sl-color-blue)" stroke-width="1.5" x1="74" y1="74" x2="74" y2="96" marker-end="url(#ds40-w2-arr-6)"/>
    <line class="fig-line" stroke="var(--sl-color-green)" stroke-width="1.5" x1="270" y1="74" x2="270" y2="96" marker-end="url(#ds40-w2-arr-6)"/>
    <line class="fig-line" stroke-width="1.5" x1="108" y1="114" x2="140" y2="114" marker-end="url(#ds40-w2-arr-6)"/>
    <line class="fig-line" stroke-width="1.5" x1="206" y1="114" x2="238" y2="114" marker-end="url(#ds40-w2-arr-6)"/>
    <rect class="fig-box-purple" x="42" y="98" width="66" height="32" rx="4"/>
    <rect class="fig-box-blue" x="142" y="98" width="64" height="32" rx="4"/>
    <rect class="fig-box-green" x="240" y="98" width="64" height="32" rx="4"/>
    <rect class="fig-box-red" x="40" y="146" width="284" height="74" rx="4"/>
    <rect class="fig-box-blue" x="360" y="68" width="300" height="74" rx="4"/>
    <rect class="fig-box-green" x="360" y="148" width="300" height="74" rx="4"/>
    <text class="fig-title" x="350" y="32" font-size="12.5" text-anchor="middle">链队列边界陷阱 &amp; 受限双端队列合法序列秒杀（入队序 1, 2, 3, 4）</text>
    <text class="fig-purple" x="182" y="59" font-size="11.5" text-anchor="middle">① 带头结点链队列结构</text>
    <text class="fig-blue" x="74" y="70" font-size="10.5" text-anchor="middle">front</text>
    <text class="fig-green" x="270" y="70" font-size="10.5" text-anchor="middle">rear</text>
    <text class="fig-purple" x="75" y="118" font-size="10.5" text-anchor="middle">头结点</text>
    <text class="fig-blue" x="174" y="118" font-size="11" text-anchor="middle">a₁</text>
    <text class="fig-green" x="272" y="118" font-size="11" text-anchor="middle">a₂ | ∧</text>
    <text class="fig-red" x="182" y="165" font-size="10.5" text-anchor="middle">★ 链队列删空特判（高频代码填空）：</text>
    <text class="fig-label" x="182" y="184" font-size="10" text-anchor="middle">普通出队仅改 front-&gt;next = p-&gt;next；</text>
    <text class="fig-label" x="182" y="200" font-size="10" text-anchor="middle">但若被删 p == rear（唯一元素出队），</text>
    <text class="fig-red" x="182" y="214" font-size="10" text-anchor="middle">必须同步置 rear = front，防尾指针悬空！</text>
    <text class="fig-blue" x="510" y="59" font-size="11.5" text-anchor="middle">② 受限双端队列经典对称反例（408 真题）</text>
    <text class="fig-blue" x="372" y="86" font-size="10.5">输入受限（仅右端进，左右两端皆可出）：</text>
    <text class="fig-label" x="372" y="104" font-size="10">• 4 1 3 2 ✓（全进成 [1,2,3,4] → 右4、左1、右3、2）</text>
    <text class="fig-red" x="372" y="121" font-size="10">• 4 2 1 3 ✗（4 出后剩 [1,2,3]，2 夹在中间无法先出）</text>
    <text class="fig-green" x="372" y="166" font-size="10.5">输出受限（左右两端皆可进，仅左端出）：</text>
    <text class="fig-label" x="372" y="184" font-size="10">• 4 2 1 3 ✓（1进→2左进→3右进→4左进 ⇒ [4,2,1,3]）</text>
    <text class="fig-red" x="372" y="201" font-size="10">• 4 1 3 2 ✗（3 入队时只能在 {1,2} 外侧，插不进中间）</text>
  </svg>
  <figcaption>链队列与双端队列考点：链队列删独苗必重置 rear = front；受限双端队列抓住“4 先出则 1,2,3 必已在队内”倒推</figcaption>
</figure>

**队列的应用**：打印任务排队、操作系统进程调度、树的层序遍历、图的广度优先搜索。

## 五、串与 KMP

### 串与模式匹配

**串**（字符串）：由零个或多个字符组成的有限序列。

最重要的操作是**模式匹配**：在主串 $S$ 中找到子串 $T$ 第一次出现的位置。

**暴力匹配（BF）**：把子串和主串逐个字符比较，一旦失配，主串回退到下一个起点，子串从头再来。

最坏情况为 $O(m \times n)$：例如主串 `aaaaaaab`、模式串 `aab`，每一趟都要比到最后一个字符才失配。

<figure class="cpd-figure not-content" id="ds40-img3-fig-1">
  <svg viewBox="0 0 700 274" role="img" aria-label="BF 暴力匹配最坏情况逐趟回退过程示意图">
    <defs>
      <marker id="ds40-img3-arr-1" viewBox="0 0 10 10" refX="8.5" refY="5" markerWidth="6.5" markerHeight="6.5" markerUnits="userSpaceOnUse" orient="auto-start-reverse">
        <path d="M 1 1.5 L 8.5 5 L 1 8.5 z" fill="context-stroke" stroke="none"/>
      </marker>
    </defs>
    <rect class="fig-box-subtle" x="16" y="14" width="668" height="246" rx="8"/>
    <rect class="fig-box" x="28" y="42" width="644" height="172" rx="6"/>
    <path class="fig-line" stroke="var(--sl-color-red)" stroke-dasharray="4 3" d="M 206 128 C 190 135 174 135 158 142" marker-end="url(#ds40-img3-arr-1)"/>
    <path class="fig-line" stroke="var(--sl-color-red)" stroke-dasharray="4 3" d="M 254 166 C 238 173 222 173 206 180" marker-end="url(#ds40-img3-arr-1)"/>
    <g class="fig-box-subtle">
      <rect x="88" y="62" width="44" height="26" rx="3"/>
      <rect x="136" y="62" width="44" height="26" rx="3"/>
      <rect x="184" y="62" width="44" height="26" rx="3"/>
      <rect x="232" y="62" width="44" height="26" rx="3"/>
      <rect x="280" y="62" width="44" height="26" rx="3"/>
      <rect x="328" y="62" width="44" height="26" rx="3"/>
    </g>
    <rect class="fig-box-blue" x="376" y="62" width="44" height="26" rx="3"/>
    <rect class="fig-box-green" x="88" y="100" width="44" height="26" rx="3"/>
    <rect class="fig-box-green" x="136" y="100" width="44" height="26" rx="3"/>
    <rect class="fig-box-red" x="184" y="100" width="44" height="26" rx="3"/>
    <rect class="fig-box-green" x="136" y="140" width="44" height="26" rx="3"/>
    <rect class="fig-box-green" x="184" y="140" width="44" height="26" rx="3"/>
    <rect class="fig-box-red" x="232" y="140" width="44" height="26" rx="3"/>
    <rect class="fig-box-green" x="280" y="180" width="44" height="26" rx="3"/>
    <rect class="fig-box-green" x="328" y="180" width="44" height="26" rx="3"/>
    <rect class="fig-box-green" x="376" y="180" width="44" height="26" rx="3"/>
    <rect class="fig-box-orange" x="28" y="222" width="644" height="28" rx="5"/>
    <text class="fig-title" x="350" y="32" font-size="12.5" text-anchor="middle">BF 暴力匹配最坏情况逐趟轨迹（S = “aaaaaab”，n = 7；T = “aab”，m = 3）</text>
    <g class="fig-muted" font-size="9.5" text-anchor="middle">
      <text x="110" y="57">1</text><text x="158" y="57">2</text><text x="206" y="57">3</text>
      <text x="254" y="57">4</text><text x="302" y="57">5</text><text x="350" y="57">6</text><text x="398" y="57">7</text>
    </g>
    <text class="fig-muted" x="78" y="79" font-size="11" text-anchor="end">主串 S</text>
    <g class="fig-label" font-size="12" text-anchor="middle">
      <text x="110" y="79">a</text><text x="158" y="79">a</text><text x="206" y="79">a</text>
      <text x="254" y="79">a</text><text x="302" y="79">a</text><text x="350" y="79">a</text>
    </g>
    <text class="fig-blue" x="398" y="79" font-size="12" text-anchor="middle">b</text>
    <text class="fig-muted" x="78" y="117" font-size="10.5" text-anchor="end">第 1 趟</text>
    <text class="fig-green" x="110" y="117" font-size="11.5" text-anchor="middle">a ✓</text>
    <text class="fig-green" x="158" y="117" font-size="11.5" text-anchor="middle">a ✓</text>
    <text class="fig-red" x="206" y="117" font-size="11.5" text-anchor="middle">b ✗</text>
    <text class="fig-red" x="434" y="117" font-size="10.5">比 m = 3 次失配 → i 回退到 2，j = 1</text>
    <text class="fig-muted" x="78" y="157" font-size="10.5" text-anchor="end">第 2 趟</text>
    <text class="fig-green" x="158" y="157" font-size="11.5" text-anchor="middle">a ✓</text>
    <text class="fig-green" x="206" y="157" font-size="11.5" text-anchor="middle">a ✓</text>
    <text class="fig-red" x="254" y="157" font-size="11.5" text-anchor="middle">b ✗</text>
    <text class="fig-red" x="434" y="157" font-size="10.5">比 m = 3 次失配 → i 回退到 3，j = 1</text>
    <text class="fig-muted" x="210" y="176" font-size="10" text-anchor="middle">…… 第 3、4 趟均在末尾失配 ……</text>
    <text class="fig-muted" x="78" y="197" font-size="10.5" text-anchor="end">第 5 趟</text>
    <text class="fig-green" x="302" y="197" font-size="11.5" text-anchor="middle">a ✓</text>
    <text class="fig-green" x="350" y="197" font-size="11.5" text-anchor="middle">a ✓</text>
    <text class="fig-green" x="398" y="197" font-size="11.5" text-anchor="middle">b ✓</text>
    <text class="fig-green" x="434" y="197" font-size="10.5">匹配成功！返回起始位置 i - m + 1 = 5</text>
    <text class="fig-orange" x="350" y="240" font-size="11" text-anchor="middle">★ 最坏比较次数 = (n - m + 1) × m = 5 × 3 = 15 次 ⇒ 时间复杂度 O(m × n)</text>
  </svg>
  <figcaption>图 21：BF 每趟都可能走到最后才失配，最坏 $O(m \times n)$</figcaption>
</figure>

<figure class="cpd-figure not-content" id="ds40-w2-fig-7">
  <svg viewBox="0 0 700 252" role="img" aria-label="暴力匹配 BF 算法主串指针回退最坏情况与 KMP 复杂度对比">
    <defs>
      <marker id="ds40-w2-arr-7" viewBox="0 0 10 10" refX="8.5" refY="5" markerWidth="6.5" markerHeight="6.5" markerUnits="userSpaceOnUse" orient="auto-start-reverse">
        <path d="M 1 1.5 L 8.5 5 L 1 8.5 z" fill="context-stroke" stroke="none"/>
      </marker>
    </defs>
    <rect class="fig-box-subtle" x="16" y="14" width="668" height="224" rx="8"/>
    <rect class="fig-box" x="28" y="42" width="406" height="184" rx="6"/>
    <rect class="fig-box" x="446" y="42" width="226" height="184" rx="6"/>
    <path class="fig-line" stroke="var(--sl-color-red)" stroke-width="1.4" stroke-dasharray="4 3" d="M 184 120 L 146 136" marker-end="url(#ds40-w2-arr-7)"/>
    <path class="fig-line" stroke="var(--sl-color-red)" stroke-width="1.4" stroke-dasharray="4 3" d="M 220 162 L 182 178" marker-end="url(#ds40-w2-arr-7)"/>
    <rect class="fig-box-subtle" x="92" y="54" width="34" height="24" rx="3"/>
    <rect class="fig-box-subtle" x="128" y="54" width="34" height="24" rx="3"/>
    <rect class="fig-box-subtle" x="164" y="54" width="34" height="24" rx="3"/>
    <rect class="fig-box-subtle" x="200" y="54" width="34" height="24" rx="3"/>
    <rect class="fig-box-subtle" x="236" y="54" width="34" height="24" rx="3"/>
    <rect class="fig-box-subtle" x="272" y="54" width="34" height="24" rx="3"/>
    <rect class="fig-box-subtle" x="308" y="54" width="34" height="24" rx="3"/>
    <rect class="fig-box-blue" x="344" y="54" width="34" height="24" rx="3"/>
    <rect class="fig-box-green" x="92" y="94" width="34" height="24" rx="3"/>
    <rect class="fig-box-green" x="128" y="94" width="34" height="24" rx="3"/>
    <rect class="fig-box-red" x="164" y="94" width="34" height="24" rx="3"/>
    <rect class="fig-box-green" x="128" y="138" width="34" height="24" rx="3"/>
    <rect class="fig-box-green" x="164" y="138" width="34" height="24" rx="3"/>
    <rect class="fig-box-red" x="200" y="138" width="34" height="24" rx="3"/>
    <rect class="fig-box-green" x="272" y="186" width="34" height="24" rx="3"/>
    <rect class="fig-box-green" x="308" y="186" width="34" height="24" rx="3"/>
    <rect class="fig-box-green" x="344" y="186" width="34" height="24" rx="3"/>
    <rect class="fig-box-orange" x="458" y="54" width="202" height="78" rx="4"/>
    <rect class="fig-box-green" x="458" y="140" width="202" height="74" rx="4"/>
    <text class="fig-title" x="350" y="32" font-size="12.5" text-anchor="middle">朴素暴力匹配（BF）主串指针反复回退退化机理（S = “aaaaaaab”, T = “aab”）</text>
    <text class="fig-muted" x="82" y="70" font-size="10.5" text-anchor="end">主串 S</text>
    <text class="fig-label" x="109" y="70" font-size="11" text-anchor="middle">a</text>
    <text class="fig-label" x="145" y="70" font-size="11" text-anchor="middle">a</text>
    <text class="fig-label" x="181" y="70" font-size="11" text-anchor="middle">a</text>
    <text class="fig-label" x="217" y="70" font-size="11" text-anchor="middle">a</text>
    <text class="fig-label" x="253" y="70" font-size="11" text-anchor="middle">a</text>
    <text class="fig-label" x="289" y="70" font-size="11" text-anchor="middle">a</text>
    <text class="fig-label" x="325" y="70" font-size="11" text-anchor="middle">a</text>
    <text class="fig-blue" x="361" y="70" font-size="11" text-anchor="middle">b</text>
    <text class="fig-muted" x="82" y="110" font-size="10" text-anchor="end">第 1 趟</text>
    <text class="fig-green" x="109" y="110" font-size="11" text-anchor="middle">a</text>
    <text class="fig-green" x="145" y="110" font-size="11" text-anchor="middle">a</text>
    <text class="fig-red" x="181" y="110" font-size="11" text-anchor="middle">b✗</text>
    <text class="fig-red" x="208" y="110" font-size="10">i=3 失配 → 回退至 i=2</text>
    <text class="fig-muted" x="82" y="154" font-size="10" text-anchor="end">第 2 趟</text>
    <text class="fig-green" x="145" y="154" font-size="11" text-anchor="middle">a</text>
    <text class="fig-green" x="181" y="154" font-size="11" text-anchor="middle">a</text>
    <text class="fig-red" x="217" y="154" font-size="11" text-anchor="middle">b✗</text>
    <text class="fig-red" x="244" y="154" font-size="10">i=4 失配 → 回退至 i=3</text>
    <text class="fig-muted" x="217" y="177" font-size="10">…… 连续回退 5 趟 ……</text>
    <text class="fig-muted" x="82" y="202" font-size="10" text-anchor="end">第 6 趟</text>
    <text class="fig-green" x="289" y="202" font-size="11" text-anchor="middle">a</text>
    <text class="fig-green" x="325" y="202" font-size="11" text-anchor="middle">a</text>
    <text class="fig-green" x="361" y="202" font-size="11" text-anchor="middle">b✓</text>
    <text class="fig-green" x="262" y="202" font-size="10" text-anchor="end">终匹配成功 →</text>
    <text class="fig-orange" x="559" y="72" font-size="11" text-anchor="middle">BF 失配回退公式（1-based）</text>
    <text class="fig-label" x="559" y="92" font-size="10.5" text-anchor="middle">i = i - j + 2 ， j = 1</text>
    <text class="fig-muted" x="559" y="109" font-size="9.5" text-anchor="middle">（若 0-based 则 i = i - j + 1, j = 0）</text>
    <text class="fig-red" x="559" y="124" font-size="10" text-anchor="middle">最坏时间：O((n-m+1)m) = O(nm)</text>
    <text class="fig-green" x="559" y="158" font-size="11" text-anchor="middle">KMP 本质突破</text>
    <text class="fig-label" x="559" y="177" font-size="10" text-anchor="middle">主串 i 永不回退！仅跳 j = next[j]</text>
    <text class="fig-muted" x="559" y="194" font-size="9.5" text-anchor="middle">建表 O(m) + 扫描 O(n)</text>
    <text class="fig-green" x="559" y="208" font-size="10" text-anchor="middle">总最坏时间严格线性：O(n + m)</text>
  </svg>
  <figcaption>BF 与 KMP 分水岭：BF 每次失配将主串指针拉回上一趟起点后一位（i = i - j + 2），重复字符多时退化为 O(nm)</figcaption>
</figure>

### KMP 的核心：主串指针不回退

BF 的问题是：失配时前面已经比对成功的字符信息被白白浪费。

KMP 的思路是**主串指针 $i$ 永不回退**，只把子串向右滑动到合适位置继续比较。滑动多少只取决于**子串本身**：

- 已经匹配的部分是 `abab`
- 它的前缀 `ab` 和后缀 `ab` 相同，最长相等前后缀长度为 2
- 直接把子串右滑，让头部的 `ab` 对齐主串刚才尾部的 `ab`
- 主串指针不动，子串从第 3 个字符继续比

KMP 总复杂度为 $O(m+n)$。

<figure class="cpd-figure not-content" id="ds40-fig-s2">
  <svg viewBox="0 0 680 240" role="img" aria-label="KMP 失配滑动：主串指针 i 不回退，模式串按最长相等前后缀对齐">
    <defs>
      <marker id="ds40-arr-s2" viewBox="0 0 10 10" refX="8" refY="5" markerWidth="6" markerHeight="6" markerUnits="userSpaceOnUse" orient="auto-start-reverse">
        <path d="M1.5,1.5 L8.5,5 L1.5,8.5" fill="none" stroke="context-stroke" stroke-width="1.5" stroke-linecap="round" stroke-linejoin="round"/>
      </marker>
    </defs>
    <line class="fig-line" stroke="var(--sl-color-orange)" stroke-width="1.5" x1="351" y1="22" x2="351" y2="40" marker-end="url(#ds40-arr-s2)"/>
    <path class="fig-line-accent" stroke-width="1.5" stroke-dasharray="4 3" d="M 183 127 C 183 146 274 144 279 161" marker-end="url(#ds40-arr-s2)"/>
    <rect class="fig-box" x="136" y="43" width="46" height="32" rx="4"/>
    <rect class="fig-box" x="184" y="43" width="46" height="32" rx="4"/>
    <rect class="fig-box-green" x="232" y="43" width="46" height="32" rx="4"/>
    <rect class="fig-box-green" x="280" y="43" width="46" height="32" rx="4"/>
    <rect class="fig-box-orange" x="328" y="43" width="46" height="32" rx="4"/>
    <rect class="fig-box" x="376" y="43" width="46" height="32" rx="4"/>
    <rect class="fig-box" x="424" y="43" width="46" height="32" rx="4"/>
    <rect class="fig-box" x="472" y="43" width="46" height="32" rx="4"/>
    <rect class="fig-box-blue" x="136" y="93" width="46" height="32" rx="4"/>
    <rect class="fig-box-blue" x="184" y="93" width="46" height="32" rx="4"/>
    <rect class="fig-box-green" x="232" y="93" width="46" height="32" rx="4"/>
    <rect class="fig-box-green" x="280" y="93" width="46" height="32" rx="4"/>
    <rect class="fig-box-red" x="328" y="93" width="46" height="32" rx="4"/>
    <rect class="fig-box" x="376" y="93" width="46" height="32" rx="4"/>
    <rect class="fig-box" x="292" y="134" width="174" height="22" rx="4"/>
    <rect class="fig-box-green" x="232" y="165" width="46" height="32" rx="4"/>
    <rect class="fig-box-green" x="280" y="165" width="46" height="32" rx="4"/>
    <rect class="fig-box-orange" x="328" y="165" width="46" height="32" rx="4"/>
    <rect class="fig-box" x="376" y="165" width="46" height="32" rx="4"/>
    <rect class="fig-box" x="424" y="165" width="46" height="32" rx="4"/>
    <rect class="fig-box" x="472" y="165" width="46" height="32" rx="4"/>
    <text class="fig-orange" x="351" y="18" text-anchor="middle" font-size="12">i = 5（不动）</text>
    <text class="fig-muted" x="120" y="64" text-anchor="end" font-size="12">主串 S</text>
    <g font-size="13" text-anchor="middle">
      <text class="fig-label" x="159" y="64">a</text>
      <text class="fig-label" x="207" y="64">b</text>
      <text class="fig-green" x="255" y="64">a</text>
      <text class="fig-green" x="303" y="64">b</text>
      <text class="fig-orange" x="351" y="64">c</text>
      <text class="fig-label" x="399" y="64">a</text>
      <text class="fig-label" x="447" y="64">b</text>
      <text class="fig-label" x="495" y="64">a</text>
    </g>
    <text class="fig-muted" x="120" y="114" text-anchor="end" font-size="12">失配前 T</text>
    <g font-size="13" text-anchor="middle">
      <text class="fig-blue" x="159" y="114">a</text>
      <text class="fig-blue" x="207" y="114">b</text>
      <text class="fig-green" x="255" y="114">a</text>
      <text class="fig-green" x="303" y="114">b</text>
      <text class="fig-red" x="351" y="114">a</text>
      <text class="fig-label" x="399" y="114">a</text>
    </g>
    <text class="fig-red" x="434" y="114" font-size="11.5">j = 5 失配（c ≠ a）</text>
    <text class="fig-accent" x="379" y="149" text-anchor="middle" font-size="11">前缀 ab 右滑对齐后缀 ab</text>
    <text class="fig-muted" x="120" y="186" text-anchor="end" font-size="12">滑动后 T</text>
    <g font-size="13" text-anchor="middle">
      <text class="fig-green" x="255" y="186">a</text>
      <text class="fig-green" x="303" y="186">b</text>
      <text class="fig-orange" x="351" y="186">a</text>
      <text class="fig-label" x="399" y="186">b</text>
      <text class="fig-label" x="447" y="186">a</text>
      <text class="fig-label" x="495" y="186">a</text>
    </g>
    <text class="fig-orange" x="530" y="186" font-size="11.5">j = next[5] = 3 继续比</text>
    <text class="fig-muted" x="340" y="222" text-anchor="middle" font-size="11.5">已匹配串“abab”最长相等前后缀“ab”长为 2：前 2 位无需重比，i 不动、j 跳至 3</text>
  </svg>
  <figcaption>KMP 核心滑动机制：利用已匹配部分的最长相等前后缀跳过冗余比较</figcaption>
</figure>

### next 数组

考研标准公式（下标从 1 开始）：

$$
\text{next}[j] = \text{前 } j-1 \text{ 个字符的最长相等前后缀长度} + 1
$$

并规定 $\text{next}[1]=0$，$\text{next}[2]=1$。

**前后缀的定义**：以 `"aba"` 为例——
- 前缀必须包含第一个字符、不能包含最后一个字符：`"a"`、`"ab"`
- 后缀必须包含最后一个字符、不能包含第一个字符：`"a"`、`"ba"`
- 相等的只有 `"a"`，最长相等前后缀长度为 1

对模式串 $T = \texttt{“ababaa”}$ 手算：

| $j$ | 前 $j-1$ 个字符 | 最长相等前后缀 | $\text{next}[j]$ |
|:---:|:---------------:|:--------------:|:----------------:|
| 1 | — | 规定 | **0** |
| 2 | `a` | 规定 | **1** |
| 3 | `ab` | 0 | **1** |
| 4 | `aba` | 1（`a`） | **2** |
| 5 | `abab` | 2（`ab`） | **3** |
| 6 | `ababa` | 3（`aba`） | **4** |

结果：$\text{next} = [0,1,1,2,3,4]$。

<figure class="cpd-figure not-content" id="ds40-img3-fig-2">
  <svg viewBox="0 0 700 282" role="img" aria-label="KMP next 数组最长相等真前后缀对齐模型与 ababaa 手算结果">
    <rect class="fig-box-subtle" x="16" y="14" width="668" height="254" rx="8"/>
    <rect class="fig-box" x="28" y="42" width="340" height="162" rx="6"/>
    <rect class="fig-box" x="380" y="42" width="292" height="162" rx="6"/>
    <rect class="fig-box-blue" x="96" y="76" width="38" height="24" rx="3"/>
    <rect class="fig-box-blue" x="136" y="76" width="38" height="24" rx="3"/>
    <rect class="fig-box-green" x="176" y="76" width="38" height="24" rx="3"/>
    <rect class="fig-box-green" x="216" y="76" width="38" height="24" rx="3"/>
    <rect class="fig-box-blue" x="176" y="106" width="38" height="22" rx="3"/>
    <rect class="fig-box-blue" x="216" y="106" width="38" height="22" rx="3"/>
    <rect class="fig-box-purple" x="96" y="148" width="114" height="22" rx="3"/>
    <rect class="fig-box-purple" x="176" y="174" width="114" height="22" rx="3"/>
    <rect class="fig-box-orange" x="392" y="72" width="268" height="36" rx="4"/>
    <rect class="fig-box-accent" x="392" y="114" width="268" height="46" rx="4"/>
    <rect class="fig-box-green" x="392" y="166" width="268" height="28" rx="4"/>
    <rect class="fig-box" x="28" y="212" width="644" height="46" rx="6"/>
    <g class="fig-divider">
      <line x1="118" y1="212" x2="118" y2="258"/>
      <line x1="210" y1="212" x2="210" y2="258"/>
      <line x1="302" y1="212" x2="302" y2="258"/>
      <line x1="394" y1="212" x2="394" y2="258"/>
      <line x1="486" y1="212" x2="486" y2="258"/>
      <line x1="578" y1="212" x2="578" y2="258"/>
    </g>
    <text class="fig-title" x="350" y="32" font-size="12.5" text-anchor="middle">next[j] = 前 j - 1 个字符的最长相等真前后缀长度 + 1（下标从 1 开始）</text>
    <text class="fig-blue" x="198" y="61" font-size="11" text-anchor="middle">① 真前缀与真后缀对齐重叠（以 “abab” 与 “ababa” 为例）</text>
    <text class="fig-muted" x="88" y="92" font-size="10" text-anchor="end">j=5 前串</text>
    <text class="fig-blue" x="115" y="92" font-size="11" text-anchor="middle">a</text>
    <text class="fig-blue" x="155" y="92" font-size="11" text-anchor="middle">b</text>
    <text class="fig-green" x="195" y="92" font-size="11" text-anchor="middle">a</text>
    <text class="fig-green" x="235" y="92" font-size="11" text-anchor="middle">b</text>
    <text class="fig-muted" x="264" y="92" font-size="10">（前缀 ab == 后缀 ab）</text>
    <text class="fig-muted" x="168" y="121" font-size="10" text-anchor="end">右滑对齐</text>
    <text class="fig-blue" x="195" y="121" font-size="11" text-anchor="middle">a</text>
    <text class="fig-blue" x="235" y="121" font-size="11" text-anchor="middle">b</text>
    <text class="fig-green" x="264" y="121" font-size="10">L = 2 ⇒ next[5] = 3</text>
    <text class="fig-muted" x="88" y="163" font-size="10" text-anchor="end">j=6 前缀</text>
    <text class="fig-purple" x="153" y="163" font-size="11" text-anchor="middle">a  b  a（前 3 位）</text>
    <text class="fig-muted" x="218" y="163" font-size="10">重叠于 “ababa”</text>
    <text class="fig-muted" x="168" y="189" font-size="10" text-anchor="end">j=6 后缀</text>
    <text class="fig-purple" x="233" y="189" font-size="11" text-anchor="middle">a  b  a（后 3 位）</text>
    <text class="fig-green" x="298" y="189" font-size="10">L = 3 ⇒ 4</text>
    <text class="fig-title" x="526" y="61" font-size="11" text-anchor="middle">② 考研标准公式与边界</text>
    <text class="fig-orange" x="526" y="94" font-size="11" text-anchor="middle">约定：next[1] = 0，next[2] = 1</text>
    <text class="fig-accent" x="526" y="133" font-size="10.5" text-anchor="middle">j ≥ 3：next[j] = max{ L | T[1..L]==T[j-L..j-1] } + 1</text>
    <text class="fig-muted" x="526" y="150" font-size="9.5" text-anchor="middle">（真前后缀不可取串本身，故 0 ≤ L &lt; j - 1）</text>
    <text class="fig-green" x="526" y="184" font-size="10.5" text-anchor="middle">例 “aba”：前缀 {a, ab} ∩ 后缀 {a, ba} = {a} ⇒ L=1</text>
    <text class="fig-muted" x="73" y="230" font-size="10.5" text-anchor="middle">下标 j / T[j]</text>
    <text class="fig-accent" x="73" y="249" font-size="11" text-anchor="middle">next[j]</text>
    <g class="fig-label" font-size="10.5" text-anchor="middle">
      <text x="164" y="230">1 : a</text><text x="256" y="230">2 : b</text><text x="348" y="230">3 : a</text>
      <text x="440" y="230">4 : b</text><text x="532" y="230">5 : a</text><text x="624" y="230">6 : a</text>
    </g>
    <g class="fig-accent" font-size="12" text-anchor="middle">
      <text x="164" y="249">0</text><text x="256" y="249">1</text><text x="348" y="249">1</text>
      <text x="440" y="249">2</text><text x="532" y="249">3</text><text x="624" y="249">4</text>
    </g>
  </svg>
  <figcaption>图 22：next 数组公式与 ababaa 的手算结果</figcaption>
</figure>

<figure class="cpd-figure not-content" id="ds40-w2-fig-8">
  <svg viewBox="0 0 700 288" role="img" aria-label="KMP 模式串 ababaa 的 next 数组最长相等真前后缀逐行手算推导图">
    <rect class="fig-box-subtle" x="16" y="14" width="668" height="260" rx="8"/>
    <rect class="fig-box-purple" x="28" y="42" width="644" height="28" rx="4"/>
    <rect class="fig-box" x="28" y="74" width="644" height="26" rx="4"/>
    <rect class="fig-box" x="28" y="104" width="644" height="26" rx="4"/>
    <rect class="fig-box" x="28" y="134" width="644" height="26" rx="4"/>
    <rect class="fig-box-blue" x="28" y="164" width="644" height="26" rx="4"/>
    <rect class="fig-box-blue" x="28" y="194" width="644" height="26" rx="4"/>
    <rect class="fig-box-green" x="28" y="224" width="644" height="26" rx="4"/>
    <line class="fig-divider" x1="94" y1="42" x2="94" y2="250"/>
    <line class="fig-divider" x1="236" y1="42" x2="236" y2="250"/>
    <line class="fig-divider" x1="454" y1="42" x2="454" y2="250"/>
    <line class="fig-divider" x1="556" y1="42" x2="556" y2="250"/>
    <text class="fig-title" x="350" y="32" font-size="12.5" text-anchor="middle">KMP 手算口诀：PM（最长相等真前后缀长）整体右移一位再全加 1 ⇒ next[j]</text>
    <text class="fig-purple" x="61" y="60" font-size="10.5" text-anchor="middle">下标 j</text>
    <text class="fig-purple" x="165" y="60" font-size="10.5" text-anchor="middle">前 j-1 子串 T[1..j-1]</text>
    <text class="fig-purple" x="345" y="60" font-size="10.5" text-anchor="middle">真前缀 == 真后缀对齐检验</text>
    <text class="fig-purple" x="505" y="60" font-size="10.5" text-anchor="middle">相等长度 L</text>
    <text class="fig-purple" x="614" y="60" font-size="10.5" text-anchor="middle">next[j] = L+1</text>
    <text class="fig-label" x="61" y="91" font-size="10.5" text-anchor="middle">j = 1 (a)</text>
    <text class="fig-muted" x="165" y="91" font-size="10.5" text-anchor="middle">空串（首字符失配）</text>
    <text class="fig-muted" x="345" y="91" font-size="10" text-anchor="middle">规定特殊标记（i++ 且 j++ 同步后移）</text>
    <text class="fig-muted" x="505" y="91" font-size="10.5" text-anchor="middle">—</text>
    <text class="fig-orange" x="614" y="91" font-size="11" text-anchor="middle">0（死记）</text>
    <text class="fig-label" x="61" y="121" font-size="10.5" text-anchor="middle">j = 2 (b)</text>
    <text class="fig-label" x="165" y="121" font-size="10.5" text-anchor="middle">“a”（单字符）</text>
    <text class="fig-muted" x="345" y="121" font-size="10" text-anchor="middle">单字符无真前缀与真后缀</text>
    <text class="fig-label" x="505" y="121" font-size="10.5" text-anchor="middle">0</text>
    <text class="fig-orange" x="614" y="121" font-size="11" text-anchor="middle">0 + 1 = 1</text>
    <text class="fig-label" x="61" y="151" font-size="10.5" text-anchor="middle">j = 3 (a)</text>
    <text class="fig-label" x="165" y="151" font-size="10.5" text-anchor="middle">“ab”</text>
    <text class="fig-muted" x="345" y="151" font-size="10" text-anchor="middle">前缀 {a} ≠ 后缀 {b}</text>
    <text class="fig-label" x="505" y="151" font-size="10.5" text-anchor="middle">0</text>
    <text class="fig-label" x="614" y="151" font-size="11" text-anchor="middle">0 + 1 = 1</text>
    <text class="fig-blue" x="61" y="181" font-size="10.5" text-anchor="middle">j = 4 (b)</text>
    <text class="fig-blue" x="165" y="181" font-size="10.5" text-anchor="middle">[a] b [a]</text>
    <text class="fig-blue" x="345" y="181" font-size="10" text-anchor="middle">前缀 “a” == 后缀 “a”（ab ≠ ba）</text>
    <text class="fig-blue" x="505" y="181" font-size="10.5" text-anchor="middle">1</text>
    <text class="fig-blue" x="614" y="181" font-size="11" text-anchor="middle">1 + 1 = 2</text>
    <text class="fig-blue" x="61" y="211" font-size="10.5" text-anchor="middle">j = 5 (a)</text>
    <text class="fig-blue" x="165" y="211" font-size="10.5" text-anchor="middle">[ab] [ab]</text>
    <text class="fig-blue" x="345" y="211" font-size="10" text-anchor="middle">前缀 “ab” == 后缀 “ab”</text>
    <text class="fig-blue" x="505" y="211" font-size="10.5" text-anchor="middle">2</text>
    <text class="fig-blue" x="614" y="211" font-size="11" text-anchor="middle">2 + 1 = 3</text>
    <text class="fig-green" x="61" y="241" font-size="10.5" text-anchor="middle">j = 6 (a)</text>
    <text class="fig-green" x="165" y="241" font-size="10.5" text-anchor="middle">[aba] b a → [aba]</text>
    <text class="fig-green" x="345" y="241" font-size="10" text-anchor="middle">“ababa”首尾重叠 3 位 “aba”</text>
    <text class="fig-green" x="505" y="241" font-size="10.5" text-anchor="middle">3</text>
    <text class="fig-green" x="614" y="241" font-size="11" text-anchor="middle">3 + 1 = 4</text>
    <text class="fig-muted" x="350" y="266" font-size="10.5" text-anchor="middle">★ 编号换算：1-based 得 [0,1,1,2,3,4]；若题干从下标 0 编号（next[0]=-1），整体减 1 即 [-1,0,0,1,2,3]</text>
  </svg>
  <figcaption>next 数组逐位手算推导：只看失配位 j 左边的子串 T[1..j-1]，算最长相等真前后缀长度 L 再加 1</figcaption>
</figure>

### nextval 数组

`next` 会做无效比较：若 $j=3$ 的 `a` 失配，`next[3]=1`，但第 1 位也是 `a`，换一个相同的 `a` 去比必然再次失败。

**求法**（从左往右）：
- 若 $T[j] == T[\text{next}[j]]$：继承目标位置的值，$\text{nextval}[j] = \text{nextval}[\text{next}[j]]$
- 若 $T[j] \neq T[\text{next}[j]]$：保留原值，$\text{nextval}[j] = \text{next}[j]$
- $\text{nextval}[1] = 0$

对 `ababaa`：

- $j=2$：`b != a` → $\text{nextval}[2]=1$
- $j=3$：`a == a` → $\text{nextval}[3]=\text{nextval}[1]=0$
- $j=4$：`b == b` → $\text{nextval}[4]=\text{nextval}[2]=1$
- $j=5$：`a == a` → $\text{nextval}[5]=\text{nextval}[3]=0$
- $j=6$：`a != b` → $\text{nextval}[6]=\text{next}[6]=4$

结果：$\text{nextval} = [0,1,0,1,0,4]$。

<figure class="cpd-figure not-content" id="ds40-img3-fig-3">
  <svg viewBox="0 0 700 272" role="img" aria-label="nextval 优化规则判定流与 ababaa 逐位手算推导">
    <defs>
      <marker id="ds40-img3-arr-3" viewBox="0 0 10 10" refX="8.5" refY="5" markerWidth="6.5" markerHeight="6.5" markerUnits="userSpaceOnUse" orient="auto-start-reverse">
        <path d="M 1 1.5 L 8.5 5 L 1 8.5 z" fill="context-stroke" stroke="none"/>
      </marker>
    </defs>
    <rect class="fig-box-subtle" x="16" y="14" width="668" height="244" rx="8"/>
    <rect class="fig-box" x="28" y="42" width="232" height="164" rx="6"/>
    <rect class="fig-box" x="272" y="42" width="400" height="164" rx="6"/>
    <line class="fig-line" x1="144" y1="88" x2="144" y2="104" marker-end="url(#ds40-img3-arr-3)"/>
    <path class="fig-line" stroke="var(--sl-color-green)" d="M 104 142 L 88 142 L 88 158" marker-end="url(#ds40-img3-arr-3)"/>
    <path class="fig-line" stroke="var(--sl-color-blue)" d="M 184 142 L 200 142 L 200 158" marker-end="url(#ds40-img3-arr-3)"/>
    <rect class="fig-box-orange" x="48" y="60" width="192" height="28" rx="4"/>
    <rect class="fig-box-purple" x="48" y="106" width="192" height="30" rx="4"/>
    <rect class="fig-box-green" x="38" y="160" width="100" height="36" rx="4"/>
    <rect class="fig-box-blue" x="150" y="160" width="100" height="36" rx="4"/>
    <rect class="fig-box-blue" x="284" y="64" width="376" height="24" rx="3"/>
    <rect class="fig-box-green" x="284" y="92" width="376" height="24" rx="3"/>
    <rect class="fig-box-green" x="284" y="120" width="376" height="24" rx="3"/>
    <rect class="fig-box-green" x="284" y="148" width="376" height="24" rx="3"/>
    <rect class="fig-box-blue" x="284" y="176" width="376" height="24" rx="3"/>
    <rect class="fig-box-accent" x="28" y="214" width="644" height="32" rx="5"/>
    <text class="fig-title" x="350" y="32" font-size="12.5" text-anchor="middle">nextval 跳转压缩：消除 “同字符重复失配”，从左向右递推一次完成</text>
    <text class="fig-orange" x="144" y="78" font-size="11" text-anchor="middle">起点：nextval[1] = 0</text>
    <text class="fig-purple" x="144" y="125" font-size="10.5" text-anchor="middle">判断 T[j] == T[next[j]] ？</text>
    <text class="fig-green" x="74" y="153" font-size="9.5" text-anchor="middle">相等</text>
    <text class="fig-blue" x="214" y="153" font-size="9.5" text-anchor="middle">不等</text>
    <text class="fig-green" x="88" y="175" font-size="10" text-anchor="middle">继承跳转目标</text>
    <text class="fig-green" x="88" y="190" font-size="9.5" text-anchor="middle">nextval[next[j]]</text>
    <text class="fig-blue" x="200" y="175" font-size="10" text-anchor="middle">保留原跳转</text>
    <text class="fig-blue" x="200" y="190" font-size="9.5" text-anchor="middle">next[j]</text>
    <text class="fig-title" x="472" y="58" font-size="11" text-anchor="middle">对 T = “ababaa”（next = [0, 1, 1, 2, 3, 4]）逐位递推</text>
    <text class="fig-blue" x="296" y="80" font-size="10.5">j = 2：T[2]=b ≠ T[next[2]=1]=a  ⇒  nextval[2] = next[2] = 1</text>
    <text class="fig-green" x="296" y="108" font-size="10.5">j = 3：T[3]=a == T[next[3]=1]=a ⇒  nextval[3] = nextval[1] = 0</text>
    <text class="fig-green" x="296" y="136" font-size="10.5">j = 4：T[4]=b == T[next[4]=2]=b ⇒  nextval[4] = nextval[2] = 1</text>
    <text class="fig-green" x="296" y="164" font-size="10.5">j = 5：T[5]=a == T[next[5]=3]=a ⇒  nextval[5] = nextval[3] = 0</text>
    <text class="fig-blue" x="296" y="192" font-size="10.5">j = 6：T[6]=a ≠ T[next[6]=4]=b  ⇒  nextval[6] = next[6] = 4</text>
    <text class="fig-accent" x="350" y="234" font-size="11.5" text-anchor="middle">最终结果：nextval = [0, 1, 0, 1, 0, 4] ｜ KMP 总复杂度 = 建表 O(m) + 匹配 O(n) = O(m + n)</text>
  </svg>
  <figcaption>图 23：nextval 优化规则与 ababaa 的结果，KMP 总复杂度 $O(m+n)$</figcaption>
</figure>

<figure class="cpd-figure not-content" id="ds40-w2-fig-9">
  <svg viewBox="0 0 700 264" role="img" aria-label="KMP next 数组冗余回跳链与 nextval 路径压缩一步到位对比图">
    <defs>
      <marker id="ds40-w2-arr-9" viewBox="0 0 10 10" refX="8.5" refY="5" markerWidth="6.5" markerHeight="6.5" markerUnits="userSpaceOnUse" orient="auto-start-reverse">
        <path d="M 1 1.5 L 8.5 5 L 1 8.5 z" fill="context-stroke" stroke="none"/>
      </marker>
    </defs>
    <rect class="fig-box-subtle" x="16" y="14" width="668" height="236" rx="8"/>
    <path class="fig-line" stroke="var(--sl-color-red)" stroke-width="1.4" stroke-dasharray="4 3" d="M 484 74 C 460 44 374 44 350 74" marker-end="url(#ds40-w2-arr-9)"/>
    <path class="fig-line" stroke="var(--sl-color-red)" stroke-width="1.4" stroke-dasharray="4 3" d="M 342 74 C 318 44 232 44 208 74" marker-end="url(#ds40-w2-arr-9)"/>
    <path class="fig-line" stroke="var(--sl-color-red)" stroke-width="1.4" stroke-dasharray="4 3" d="M 200 74 C 178 48 136 52 122 74" marker-end="url(#ds40-w2-arr-9)"/>
    <path class="fig-line" stroke="var(--sl-color-green)" stroke-width="1.6" d="M 484 186 C 410 226 196 226 122 186" marker-end="url(#ds40-w2-arr-9)"/>
    <path class="fig-line" stroke="var(--sl-color-blue)" stroke-width="1.5" d="M 555 186 C 530 212 442 212 417 186" marker-end="url(#ds40-w2-arr-9)"/>
    <rect class="fig-box-dashed" x="92" y="78" width="56" height="104" rx="4"/>
    <rect class="fig-box" x="168" y="78" width="62" height="32" rx="4"/>
    <rect class="fig-box" x="239" y="78" width="62" height="32" rx="4"/>
    <rect class="fig-box" x="310" y="78" width="62" height="32" rx="4"/>
    <rect class="fig-box-blue" x="381" y="78" width="62" height="32" rx="4"/>
    <rect class="fig-box-red" x="452" y="78" width="62" height="32" rx="4"/>
    <rect class="fig-box-orange" x="523" y="78" width="62" height="32" rx="4"/>
    <rect class="fig-box-subtle" x="168" y="114" width="62" height="30" rx="4"/>
    <rect class="fig-box-subtle" x="239" y="114" width="62" height="30" rx="4"/>
    <rect class="fig-box-subtle" x="310" y="114" width="62" height="30" rx="4"/>
    <rect class="fig-box-subtle" x="381" y="114" width="62" height="30" rx="4"/>
    <rect class="fig-box-red" x="452" y="114" width="62" height="30" rx="4"/>
    <rect class="fig-box-subtle" x="523" y="114" width="62" height="30" rx="4"/>
    <rect class="fig-box-green" x="168" y="148" width="62" height="34" rx="4"/>
    <rect class="fig-box-green" x="239" y="148" width="62" height="34" rx="4"/>
    <rect class="fig-box-green" x="310" y="148" width="62" height="34" rx="4"/>
    <rect class="fig-box-green" x="381" y="148" width="62" height="34" rx="4"/>
    <rect class="fig-box-green" x="452" y="148" width="62" height="34" rx="4"/>
    <rect class="fig-box-blue" x="523" y="148" width="62" height="34" rx="4"/>
    <rect class="fig-box" x="186" y="42" width="328" height="18" rx="3"/>
    <rect class="fig-box" x="200" y="212" width="248" height="20" rx="3"/>
    <text class="fig-title" x="350" y="32" font-size="12.5" text-anchor="middle">nextval 消解无效回跳原理（模式串 T = “ababaa”，从左向右递推继承）</text>
    <text class="fig-red" x="350" y="55" font-size="10" text-anchor="middle">next 连跳链：j=5(‘a’) → 3(‘a’) → 1(‘a’) → 0（重比相同字符）</text>
    <text class="fig-muted" x="80" y="99" font-size="10.5" text-anchor="end">T[j]</text>
    <text class="fig-muted" x="80" y="133" font-size="10.5" text-anchor="end">next[j]</text>
    <text class="fig-green" x="80" y="169" font-size="10.5" text-anchor="end">nextval[j]</text>
    <text class="fig-muted" x="120" y="122" font-size="11" text-anchor="middle">j = 0</text>
    <text class="fig-muted" x="120" y="142" font-size="10" text-anchor="middle">(i++, j++)</text>
    <text class="fig-label" x="199" y="99" font-size="11.5" text-anchor="middle">1: a</text>
    <text class="fig-label" x="270" y="99" font-size="11.5" text-anchor="middle">2: b</text>
    <text class="fig-label" x="341" y="99" font-size="11.5" text-anchor="middle">3: a</text>
    <text class="fig-blue" x="412" y="99" font-size="11.5" text-anchor="middle">4: b</text>
    <text class="fig-red" x="483" y="99" font-size="11.5" text-anchor="middle">5: a</text>
    <text class="fig-orange" x="554" y="99" font-size="11.5" text-anchor="middle">6: a</text>
    <text class="fig-muted" x="199" y="134" font-size="11" text-anchor="middle">0</text>
    <text class="fig-muted" x="270" y="134" font-size="11" text-anchor="middle">1</text>
    <text class="fig-muted" x="341" y="134" font-size="11" text-anchor="middle">1</text>
    <text class="fig-muted" x="412" y="134" font-size="11" text-anchor="middle">2</text>
    <text class="fig-red" x="483" y="134" font-size="11" text-anchor="middle">3</text>
    <text class="fig-muted" x="554" y="134" font-size="11" text-anchor="middle">4</text>
    <text class="fig-green" x="199" y="170" font-size="11.5" text-anchor="middle">0</text>
    <text class="fig-green" x="270" y="170" font-size="11" text-anchor="middle">1 (b≠a)</text>
    <text class="fig-green" x="341" y="170" font-size="11" text-anchor="middle">0 (a=a)</text>
    <text class="fig-green" x="412" y="170" font-size="11" text-anchor="middle">1 (b=b)</text>
    <text class="fig-green" x="483" y="170" font-size="11" text-anchor="middle">0 (a=a)</text>
    <text class="fig-blue" x="554" y="170" font-size="11" text-anchor="middle">4 (a≠b)</text>
    <text class="fig-green" x="324" y="226" font-size="10" text-anchor="middle">nextval 一步短路：T[5]=T[3]=‘a’ ⇒ 继承 nextval[3]=0</text>
    <text class="fig-blue" x="634" y="132" font-size="9.5" text-anchor="middle">j=6: T[6]=‘a’</text>
    <text class="fig-blue" x="634" y="148" font-size="9.5" text-anchor="middle">≠ T[4]=‘b’</text>
    <text class="fig-blue" x="634" y="164" font-size="9.5" text-anchor="middle">保留原值 4</text>
  </svg>
  <figcaption>nextval 核心直觉：相等则继承（T[j] == T[next[j]] 时抄目标的 nextval），不等则保留（直接抄自己的 next）</figcaption>
</figure>

## 六、数组与特殊矩阵

### 二维数组的存储

数组可以看作线性表的推广。二维数组在内存中依然按一维排开，有两种排法：

- **行优先**：先存第 0 行，再存第 1 行……
- **列优先**：先存第 0 列，再存第 1 列……

行优先、下标从 0 开始：

$$
\text{LOC}(a[i][j]) = \text{LOC}(a[0][0]) + (i \times n + j) \times L
$$

其中 $n$ 为列数，$L$ 为每个元素占用的字节数。

例：$4$ 列数组中的 $a[2][1]$，前面有 $2 \times 4 + 1 = 9$ 个元素。

<figure class="cpd-figure not-content" id="ds40-img4-fig-4">
  <svg viewBox="0 0 700 232" role="img" aria-label="二维数组行优先与列优先内存展平映射及行优先跨过 i 行加 j 个元素的地址计算">
    <defs>
      <marker id="ds40-img4-arr-3" viewBox="0 0 10 10" refX="8.5" refY="5" markerWidth="6.5" markerHeight="6.5" markerUnits="userSpaceOnUse" orient="auto-start-reverse">
        <path d="M 1 1.5 L 8.5 5 L 1 8.5 z" fill="context-stroke" stroke="none"/>
      </marker>
    </defs>
    <rect class="fig-box-subtle" x="16" y="14" width="668" height="204" rx="8"/>
    <rect class="fig-box" x="28" y="44" width="284" height="160" rx="6"/>
    <rect class="fig-box" x="324" y="44" width="348" height="160" rx="6"/>
    <line class="fig-line" stroke="var(--sl-color-blue)" stroke-width="1.5" x1="256" y1="86" x2="294" y2="86" marker-end="url(#ds40-img4-arr-3)"/>
    <line class="fig-line" stroke="var(--sl-color-blue)" stroke-width="1.5" x1="256" y1="114" x2="294" y2="114" marker-end="url(#ds40-img4-arr-3)"/>
    <line class="fig-line" stroke="var(--sl-color-orange)" stroke-width="1.5" x1="256" y1="142" x2="294" y2="142" marker-end="url(#ds40-img4-arr-3)"/>
    <rect class="fig-box-blue" x="48" y="74" width="44" height="24" rx="3"/>
    <rect class="fig-box-blue" x="94" y="74" width="44" height="24" rx="3"/>
    <rect class="fig-box-blue" x="140" y="74" width="44" height="24" rx="3"/>
    <rect class="fig-box-blue" x="186" y="74" width="44" height="24" rx="3"/>
    <rect class="fig-box-blue" x="48" y="102" width="44" height="24" rx="3"/>
    <rect class="fig-box-blue" x="94" y="102" width="44" height="24" rx="3"/>
    <rect class="fig-box-blue" x="140" y="102" width="44" height="24" rx="3"/>
    <rect class="fig-box-blue" x="186" y="102" width="44" height="24" rx="3"/>
    <rect class="fig-box-green" x="48" y="130" width="44" height="24" rx="3"/>
    <rect class="fig-box-orange" x="94" y="130" width="44" height="24" rx="3"/>
    <rect class="fig-box-subtle" x="140" y="130" width="44" height="24" rx="3"/>
    <rect class="fig-box-subtle" x="186" y="130" width="44" height="24" rx="3"/>
    <rect class="fig-box-blue" x="338" y="84" width="76" height="28" rx="3"/>
    <rect class="fig-box-blue" x="416" y="84" width="76" height="28" rx="3"/>
    <rect class="fig-box-green" x="494" y="84" width="38" height="28" rx="3"/>
    <rect class="fig-box-orange" x="534" y="84" width="52" height="28" rx="3"/>
    <rect class="fig-box-subtle" x="588" y="84" width="70" height="28" rx="3"/>
    <rect class="fig-box-accent" x="338" y="144" width="320" height="46" rx="4"/>
    <text class="fig-title" x="350" y="34" font-size="12.5" text-anchor="middle">图 24：二维数组 A[3][4] 行优先内存布局（定位 a[2][1]，0 起始）</text>
    <text class="fig-blue" x="170" y="63" font-size="11" text-anchor="middle">逻辑二维矩阵（m = 3 行，n = 4 列）</text>
    <text class="fig-blue" x="70" y="90" font-size="9.5" text-anchor="middle">a[0][0]</text>
    <text class="fig-blue" x="116" y="90" font-size="9.5" text-anchor="middle">a[0][1]</text>
    <text class="fig-blue" x="162" y="90" font-size="9.5" text-anchor="middle">a[0][2]</text>
    <text class="fig-blue" x="208" y="90" font-size="9.5" text-anchor="middle">a[0][3]</text>
    <text class="fig-blue" x="70" y="118" font-size="9.5" text-anchor="middle">a[1][0]</text>
    <text class="fig-blue" x="116" y="118" font-size="9.5" text-anchor="middle">a[1][1]</text>
    <text class="fig-blue" x="162" y="118" font-size="9.5" text-anchor="middle">a[1][2]</text>
    <text class="fig-blue" x="208" y="118" font-size="9.5" text-anchor="middle">a[1][3]</text>
    <text class="fig-green" x="70" y="146" font-size="9.5" text-anchor="middle">a[2][0]</text>
    <text class="fig-orange" x="116" y="146" font-size="9.5" text-anchor="middle">a[2][1]</text>
    <text class="fig-muted" x="162" y="146" font-size="9.5" text-anchor="middle">a[2][2]</text>
    <text class="fig-muted" x="208" y="146" font-size="9.5" text-anchor="middle">a[2][3]</text>
    <text class="fig-muted" x="170" y="178" font-size="10" text-anchor="middle">先跨过 i = 2 个整行（每行 n = 4 个）</text>
    <text class="fig-orange" x="170" y="194" font-size="10" text-anchor="middle">再数第 2 行前面 j = 1 个（a[2][0]）</text>
    <text class="fig-green" x="498" y="63" font-size="11" text-anchor="middle">内存一维连续展平（按行拼接）</text>
    <text class="fig-blue" x="376" y="102" font-size="10" text-anchor="middle">第 0 行 (4个)</text>
    <text class="fig-blue" x="454" y="102" font-size="10" text-anchor="middle">第 1 行 (4个)</text>
    <text class="fig-green" x="513" y="102" font-size="9.5" text-anchor="middle">2,0</text>
    <text class="fig-orange" x="560" y="102" font-size="10" text-anchor="middle">★2,1</text>
    <text class="fig-muted" x="623" y="102" font-size="9.5" text-anchor="middle">后续…</text>
    <text class="fig-muted" x="454" y="129" font-size="9.5" text-anchor="middle">前面元素总数 = 2 × 4 + 1 = 9 个</text>
    <text class="fig-accent" x="498" y="163" font-size="10.5" text-anchor="middle">行优先：LOC(a[i][j]) = LOC(a[0][0]) + (i × n + j) × L</text>
    <text class="fig-label" x="498" y="180" font-size="10" text-anchor="middle">列优先对比：先跨 j 个整列（每列 m 行），再数 i 个 ⇒ (j × m + i) × L</text>
  </svg>
  <figcaption>图 24：行优先：先跨过 i 个整行，再数本行的 j 个元素</figcaption>
</figure>

下标从 1 开始时：

$$
\text{LOC}(a[i][j]) = \text{LOC}(a[1][1]) + [(i-1) \times n + (j-1)] \times L
$$

> **★考点**：

1. 下标从 0 还是从 1 开始？
2. 行优先还是列优先？
3. 数清它前面有多少个元素。

<figure class="cpd-figure not-content" id="ds40-img4-fig-5">
  <svg viewBox="0 0 700 226" role="img" aria-label="二维数组地址计算三问与 1-based 考题实战推导">
    <rect class="fig-box-subtle" x="16" y="14" width="668" height="198" rx="8"/>
    <rect class="fig-box" x="28" y="44" width="308" height="154" rx="6"/>
    <rect class="fig-box" x="348" y="44" width="324" height="154" rx="6"/>
    <rect class="fig-box-blue" x="40" y="68" width="284" height="36" rx="4"/>
    <rect class="fig-box-green" x="40" y="110" width="284" height="36" rx="4"/>
    <rect class="fig-box-orange" x="40" y="152" width="284" height="36" rx="4"/>
    <rect class="fig-box-purple" x="360" y="68" width="300" height="58" rx="4"/>
    <rect class="fig-box-accent" x="360" y="134" width="300" height="54" rx="4"/>
    <text class="fig-title" x="350" y="34" font-size="12.5" text-anchor="middle">图 25：地址计算审题三问与 1-based 考题秒杀</text>
    <text class="fig-label" x="182" y="60" font-size="11" text-anchor="middle">① 考场审题必问三句话</text>
    <text class="fig-blue" x="52" y="84" font-size="10.5">1. 下标从 0 还是 1 开始？（决定是否减 1）</text>
    <text class="fig-muted" x="52" y="98" font-size="9.5">如 A[1..m][1..n] 则行偏置 i-1，列偏置 j-1</text>
    <text class="fig-green" x="52" y="126" font-size="10.5">2. 行优先还是列优先？（决定乘 n 还是乘 m）</text>
    <text class="fig-muted" x="52" y="140" font-size="9.5">行优先跨整行（乘列数 n）；列优先跨整列（乘行数 m）</text>
    <text class="fig-orange" x="52" y="168" font-size="10.5">3. 目标元素前面有几个元素？（乘单元素字节 L）</text>
    <text class="fig-muted" x="52" y="182" font-size="9.5">切勿漏乘 L，也别把已知地址的起始点看错！</text>
    <text class="fig-purple" x="510" y="60" font-size="11" text-anchor="middle">② 1-based 公式与真题实战</text>
    <text class="fig-purple" x="372" y="86" font-size="10">行优先（1 起始）：</text>
    <text class="fig-label" x="372" y="102" font-size="10">LOC(a[i][j]) = LOC(a[1][1]) + [(i-1)×n + (j-1)]×L</text>
    <text class="fig-muted" x="372" y="118" font-size="9.5">列优先（1 起始）：+ [(j-1)×m + (i-1)]×L</text>
    <text class="fig-accent" x="372" y="152" font-size="10">例：5×6 数组（1 起始），LOC(a[1][1])=1000，L=4</text>
    <text class="fig-label" x="372" y="169" font-size="10">按行优先求 a[3][4]：前面有 (3-1)×6 + (4-1) = 15 个</text>
    <text class="fig-orange" x="372" y="183" font-size="10">LOC(a[3][4]) = 1000 + 15 × 4 = 1060</text>
  </svg>
  <figcaption>图 25：地址计算三问与 1-based 公式</figcaption>
</figure>

<figure class="cpd-figure not-content" id="ds40-w2-fig-10">
  <svg viewBox="0 0 700 274" role="img" aria-label="二维数组行优先与列优先线性内存展平映射及 0-based 与 1-based 四象限地址公式表">
    <rect class="fig-box-subtle" x="16" y="14" width="668" height="246" rx="8"/>
    <rect class="fig-box" x="28" y="42" width="286" height="204" rx="6"/>
    <rect class="fig-box" x="326" y="42" width="346" height="204" rx="6"/>
    <rect class="fig-box-blue" x="52" y="72" width="44" height="26" rx="3"/>
    <rect class="fig-box-blue" x="98" y="72" width="44" height="26" rx="3"/>
    <rect class="fig-box-blue" x="144" y="72" width="44" height="26" rx="3"/>
    <rect class="fig-box-blue" x="190" y="72" width="44" height="26" rx="3"/>
    <rect class="fig-box-blue" x="52" y="100" width="44" height="26" rx="3"/>
    <rect class="fig-box-blue" x="98" y="100" width="44" height="26" rx="3"/>
    <rect class="fig-box-blue" x="144" y="100" width="44" height="26" rx="3"/>
    <rect class="fig-box-blue" x="190" y="100" width="44" height="26" rx="3"/>
    <rect class="fig-box-green" x="52" y="128" width="44" height="26" rx="3"/>
    <rect class="fig-box-orange" x="98" y="128" width="44" height="26" rx="3"/>
    <rect class="fig-box-subtle" x="144" y="128" width="44" height="26" rx="3"/>
    <rect class="fig-box-subtle" x="190" y="128" width="44" height="26" rx="3"/>
    <rect class="fig-box-accent" x="40" y="164" width="262" height="70" rx="4"/>
    <rect class="fig-box-blue" x="338" y="68" width="322" height="54" rx="4"/>
    <rect class="fig-box-green" x="338" y="128" width="322" height="54" rx="4"/>
    <rect class="fig-box-purple" x="338" y="188" width="322" height="46" rx="4"/>
    <text class="fig-title" x="350" y="32" font-size="12.5" text-anchor="middle">二维数组 A[m][n] 线性映射：数清目标元素前面跨过了多少整行（或整列）</text>
    <text class="fig-blue" x="171" y="59" font-size="11" text-anchor="middle">① 3×4 数组定位 a[2][1]（0-based）</text>
    <text class="fig-blue" x="74" y="89" font-size="9.5" text-anchor="middle">0,0</text>
    <text class="fig-blue" x="120" y="89" font-size="9.5" text-anchor="middle">0,1</text>
    <text class="fig-blue" x="166" y="89" font-size="9.5" text-anchor="middle">0,2</text>
    <text class="fig-blue" x="212" y="89" font-size="9.5" text-anchor="middle">0,3</text>
    <text class="fig-blue" x="270" y="89" font-size="9.5" text-anchor="middle">第 0 行(4个)</text>
    <text class="fig-blue" x="74" y="117" font-size="9.5" text-anchor="middle">1,0</text>
    <text class="fig-blue" x="120" y="117" font-size="9.5" text-anchor="middle">1,1</text>
    <text class="fig-blue" x="166" y="117" font-size="9.5" text-anchor="middle">1,2</text>
    <text class="fig-blue" x="212" y="117" font-size="9.5" text-anchor="middle">1,3</text>
    <text class="fig-blue" x="270" y="117" font-size="9.5" text-anchor="middle">第 1 行(4个)</text>
    <text class="fig-green" x="74" y="145" font-size="9.5" text-anchor="middle">2,0</text>
    <text class="fig-orange" x="120" y="145" font-size="10" text-anchor="middle">★2,1</text>
    <text class="fig-muted" x="166" y="145" font-size="9.5" text-anchor="middle">2,2</text>
    <text class="fig-muted" x="212" y="145" font-size="9.5" text-anchor="middle">2,3</text>
    <text class="fig-orange" x="270" y="145" font-size="9.5" text-anchor="middle">本行前 1 个</text>
    <text class="fig-accent" x="171" y="182" font-size="10.5" text-anchor="middle">行优先：前有 2×4 + 1 = 9 个元素 ⇒ + 9L</text>
    <text class="fig-label" x="171" y="201" font-size="10" text-anchor="middle">列优先（m=3 行）：先跨第 0 列 3 个，</text>
    <text class="fig-label" x="171" y="219" font-size="10" text-anchor="middle">再数第 1 列前 2 个 ⇒ 1×3 + 2 = 5 个 ⇒ + 5L</text>
    <text class="fig-blue" x="499" y="59" font-size="11" text-anchor="middle">② 四象限地址速查表（m 行 n 列，元素占 L 字节）</text>
    <text class="fig-blue" x="348" y="86" font-size="10.5">行优先（Row-Major，乘列数 n）：</text>
    <text class="fig-label" x="348" y="102" font-size="10">• 0 起始：LOC(0,0) + (i × n + j) × L</text>
    <text class="fig-label" x="348" y="116" font-size="10">• 1 起始：LOC(1,1) + [(i - 1) × n + (j - 1)] × L</text>
    <text class="fig-green" x="348" y="146" font-size="10.5">列优先（Column-Major，乘行数 m）：</text>
    <text class="fig-label" x="348" y="162" font-size="10">• 0 起始：LOC(0,0) + (j × m + i) × L</text>
    <text class="fig-label" x="348" y="176" font-size="10">• 1 起始：LOC(1,1) + [(j - 1) × m + (i - 1)] × L</text>
    <text class="fig-purple" x="499" y="206" font-size="10" text-anchor="middle">★ 防错铁律：行优先乘“每行元素数 n（列数）”，</text>
    <text class="fig-purple" x="499" y="224" font-size="10" text-anchor="middle">列优先乘“每列元素数 m（行数）”，切勿把 m、n 乘反！</text>
  </svg>
  <figcaption>二维数组地址公式对照：做题先圈出“起始下标（0 或 1）”与“存储次序（行优先乘 n，列优先乘 m）”</figcaption>
</figure>

### 对称矩阵

对称矩阵满足 $a[i][j] = a[j][i]$，只需存下三角部分，共 $n(n+1)/2$ 个元素，几乎省一半空间。

下标从 1 开始，按行存储下三角（$i \ge j$）：

$$
k = \frac{i(i-1)}{2} + j - 1
$$

例：$a[4][2] \rightarrow k = \frac{4 \times 3}{2} + 1 = 7$。

若给的是上三角元素，交换 $i$、$j$ 即可。

<figure class="cpd-figure not-content" id="ds40-img4-fig-6">
  <svg viewBox="0 0 700 232" role="img" aria-label="对称矩阵只存下三角压缩到一维数组的映射与 a[4][2] 下标 k=7 验证">
    <defs>
      <marker id="ds40-img4-arr-4" viewBox="0 0 10 10" refX="8.5" refY="5" markerWidth="6.5" markerHeight="6.5" markerUnits="userSpaceOnUse" orient="auto-start-reverse">
        <path d="M 1 1.5 L 8.5 5 L 1 8.5 z" fill="context-stroke" stroke="none"/>
      </marker>
    </defs>
    <rect class="fig-box-subtle" x="16" y="14" width="668" height="204" rx="8"/>
    <rect class="fig-box" x="28" y="44" width="248" height="160" rx="6"/>
    <rect class="fig-box" x="288" y="44" width="384" height="160" rx="6"/>
    <line class="fig-line" stroke="var(--sl-color-orange)" stroke-width="1.5" x1="154" y1="150" x2="298" y2="108" marker-end="url(#ds40-img4-arr-4)"/>
    <rect class="fig-box-blue" x="56" y="70" width="28" height="22" rx="2"/>
    <rect class="fig-box-dashed" x="86" y="70" width="28" height="22" rx="2"/>
    <rect class="fig-box-dashed" x="116" y="70" width="28" height="22" rx="2"/>
    <rect class="fig-box-dashed" x="146" y="70" width="28" height="22" rx="2"/>
    <rect class="fig-box-blue" x="56" y="94" width="28" height="22" rx="2"/>
    <rect class="fig-box-blue" x="86" y="94" width="28" height="22" rx="2"/>
    <rect class="fig-box-dashed" x="116" y="94" width="28" height="22" rx="2"/>
    <rect class="fig-box-dashed" x="146" y="94" width="28" height="22" rx="2"/>
    <rect class="fig-box-blue" x="56" y="118" width="28" height="22" rx="2"/>
    <rect class="fig-box-blue" x="86" y="118" width="28" height="22" rx="2"/>
    <rect class="fig-box-blue" x="116" y="118" width="28" height="22" rx="2"/>
    <rect class="fig-box-dashed" x="146" y="118" width="28" height="22" rx="2"/>
    <rect class="fig-box-green" x="56" y="142" width="28" height="22" rx="2"/>
    <rect class="fig-box-orange" x="86" y="142" width="28" height="22" rx="2"/>
    <rect class="fig-box-green" x="116" y="142" width="28" height="22" rx="2"/>
    <rect class="fig-box-green" x="146" y="142" width="28" height="22" rx="2"/>
    <rect class="fig-box-blue" x="304" y="84" width="186" height="28" rx="3"/>
    <rect class="fig-box-green" x="492" y="84" width="30" height="28" rx="3"/>
    <rect class="fig-box-orange" x="524" y="84" width="42" height="28" rx="3"/>
    <rect class="fig-box-green" x="568" y="84" width="88" height="28" rx="3"/>
    <rect class="fig-box-purple" x="304" y="138" width="352" height="54" rx="4"/>
    <text class="fig-title" x="350" y="34" font-size="12.5" text-anchor="middle">图 26：对称矩阵下三角按行压缩（1 起始：a[i][j] = a[j][i]）</text>
    <text class="fig-blue" x="152" y="61" font-size="10.5" text-anchor="middle">4×4 对称矩阵（仅存下三角 10 格）</text>
    <text class="fig-blue" x="70" y="85" font-size="9" text-anchor="middle">1,1</text>
    <text class="fig-muted" x="130" y="85" font-size="9" text-anchor="middle">上三角=下三角</text>
    <text class="fig-blue" x="70" y="109" font-size="9" text-anchor="middle">2,1</text>
    <text class="fig-blue" x="100" y="109" font-size="9" text-anchor="middle">2,2</text>
    <text class="fig-blue" x="70" y="133" font-size="9" text-anchor="middle">3,1</text>
    <text class="fig-blue" x="100" y="133" font-size="9" text-anchor="middle">3,2</text>
    <text class="fig-blue" x="130" y="133" font-size="9" text-anchor="middle">3,3</text>
    <text class="fig-green" x="70" y="157" font-size="9" text-anchor="middle">4,1</text>
    <text class="fig-orange" x="100" y="157" font-size="9.5" text-anchor="middle">★4,2</text>
    <text class="fig-green" x="130" y="157" font-size="9" text-anchor="middle">4,3</text>
    <text class="fig-green" x="160" y="157" font-size="9" text-anchor="middle">4,4</text>
    <text class="fig-muted" x="152" y="186" font-size="9.5" text-anchor="middle">省近半空间：n² → n(n+1)/2</text>
    <text class="fig-label" x="480" y="64" font-size="11" text-anchor="middle">一维压缩数组 B[0 .. n(n+1)/2 - 1]</text>
    <text class="fig-blue" x="397" y="102" font-size="10" text-anchor="middle">前 i-1=3 行：1+2+3 = 6 个</text>
    <text class="fig-green" x="507" y="102" font-size="9.5" text-anchor="middle">4,1</text>
    <text class="fig-orange" x="545" y="102" font-size="10" text-anchor="middle">★4,2</text>
    <text class="fig-green" x="612" y="102" font-size="9.5" text-anchor="middle">4,3 | 4,4</text>
    <text class="fig-muted" x="397" y="126" font-size="9.5" text-anchor="middle">k = 0 .. 5</text>
    <text class="fig-muted" x="507" y="126" font-size="9.5" text-anchor="middle">k=6</text>
    <text class="fig-orange" x="545" y="126" font-size="9.5" text-anchor="middle">k=7</text>
    <text class="fig-muted" x="612" y="126" font-size="9.5" text-anchor="middle">k=8, 9</text>
    <text class="fig-purple" x="480" y="156" font-size="10.5" text-anchor="middle">下三角 (i ≥ j)：k = i(i - 1) / 2 + j - 1</text>
    <text class="fig-label" x="480" y="176" font-size="10" text-anchor="middle">例 a[4][2] → k = 4×3/2 + 2 - 1 = 7；若给上三角 (i &lt; j) 互换 i, j</text>
  </svg>
  <figcaption>图 26：只存下三角，k = i(i - 1)/2 + j - 1</figcaption>
</figure>

### 三角矩阵与三对角矩阵

- **下三角矩阵**：上方全是常数 $c$，只存下三角和常数
- **上三角矩阵**：下方全是常数 $c$，只存上三角和常数
- **三对角矩阵**：只有主对角线和左右两条对角线非零，只存这三条对角线

三对角矩阵（下标从 1 开始）：

$$
k = 2i + j - 3
$$

<figure class="cpd-figure not-content" id="ds40-img4-fig-7">
  <svg viewBox="0 0 700 228" role="img" aria-label="下三角矩阵、上三角矩阵与三对角矩阵一维压缩存储对比">
    <rect class="fig-box-subtle" x="16" y="14" width="668" height="200" rx="8"/>
    <rect class="fig-box" x="28" y="44" width="204" height="156" rx="6"/>
    <rect class="fig-box" x="244" y="44" width="204" height="156" rx="6"/>
    <rect class="fig-box" x="460" y="44" width="212" height="156" rx="6"/>
    <rect class="fig-box-blue" x="46" y="72" width="26" height="20" rx="2"/>
    <rect class="fig-box-dashed" x="74" y="72" width="26" height="20" rx="2"/>
    <rect class="fig-box-dashed" x="102" y="72" width="26" height="20" rx="2"/>
    <rect class="fig-box-blue" x="46" y="94" width="26" height="20" rx="2"/>
    <rect class="fig-box-blue" x="74" y="94" width="26" height="20" rx="2"/>
    <rect class="fig-box-dashed" x="102" y="94" width="26" height="20" rx="2"/>
    <rect class="fig-box-blue" x="46" y="116" width="26" height="20" rx="2"/>
    <rect class="fig-box-blue" x="74" y="116" width="26" height="20" rx="2"/>
    <rect class="fig-box-blue" x="102" y="116" width="26" height="20" rx="2"/>
    <rect class="fig-box-orange" x="142" y="94" width="76" height="42" rx="4"/>
    <rect class="fig-box-purple" x="262" y="72" width="26" height="20" rx="2"/>
    <rect class="fig-box-purple" x="290" y="72" width="26" height="20" rx="2"/>
    <rect class="fig-box-purple" x="318" y="72" width="26" height="20" rx="2"/>
    <rect class="fig-box-dashed" x="262" y="94" width="26" height="20" rx="2"/>
    <rect class="fig-box-purple" x="290" y="94" width="26" height="20" rx="2"/>
    <rect class="fig-box-purple" x="318" y="94" width="26" height="20" rx="2"/>
    <rect class="fig-box-dashed" x="262" y="116" width="26" height="20" rx="2"/>
    <rect class="fig-box-dashed" x="290" y="116" width="26" height="20" rx="2"/>
    <rect class="fig-box-purple" x="318" y="116" width="26" height="20" rx="2"/>
    <rect class="fig-box-orange" x="358" y="94" width="76" height="42" rx="4"/>
    <rect class="fig-box-green" x="478" y="70" width="24" height="18" rx="2"/>
    <rect class="fig-box-green" x="504" y="70" width="24" height="18" rx="2"/>
    <rect class="fig-box-subtle" x="530" y="70" width="24" height="18" rx="2"/>
    <rect class="fig-box-subtle" x="556" y="70" width="24" height="18" rx="2"/>
    <rect class="fig-box-green" x="478" y="90" width="24" height="18" rx="2"/>
    <rect class="fig-box-green" x="504" y="90" width="24" height="18" rx="2"/>
    <rect class="fig-box-green" x="530" y="90" width="24" height="18" rx="2"/>
    <rect class="fig-box-subtle" x="556" y="90" width="24" height="18" rx="2"/>
    <rect class="fig-box-subtle" x="478" y="110" width="24" height="18" rx="2"/>
    <rect class="fig-box-green" x="504" y="110" width="24" height="18" rx="2"/>
    <rect class="fig-box-green" x="530" y="110" width="24" height="18" rx="2"/>
    <rect class="fig-box-green" x="556" y="110" width="24" height="18" rx="2"/>
    <rect class="fig-box-subtle" x="478" y="130" width="24" height="18" rx="2"/>
    <rect class="fig-box-subtle" x="504" y="130" width="24" height="18" rx="2"/>
    <rect class="fig-box-green" x="530" y="130" width="24" height="18" rx="2"/>
    <rect class="fig-box-green" x="556" y="130" width="24" height="18" rx="2"/>
    <rect class="fig-box-accent" x="590" y="70" width="70" height="78" rx="4"/>
    <text class="fig-title" x="350" y="34" font-size="12.5" text-anchor="middle">图 27：三角矩阵与三对角矩阵的压缩存储策略（1 起始）</text>
    <text class="fig-blue" x="130" y="61" font-size="11" text-anchor="middle">① 下三角矩阵</text>
    <text class="fig-muted" x="87" y="86" font-size="9.5" text-anchor="middle">c</text>
    <text class="fig-muted" x="115" y="86" font-size="9.5" text-anchor="middle">c</text>
    <text class="fig-muted" x="115" y="108" font-size="9.5" text-anchor="middle">c</text>
    <text class="fig-orange" x="180" y="112" font-size="9.5" text-anchor="middle">末尾多存 1 格</text>
    <text class="fig-orange" x="180" y="126" font-size="9.5" text-anchor="middle">放常数 c</text>
    <text class="fig-label" x="130" y="162" font-size="9.5" text-anchor="middle">共需 n(n+1)/2 + 1 单元</text>
    <text class="fig-muted" x="130" y="180" font-size="9.5" text-anchor="middle">常数 c 放 B[n(n+1)/2]</text>
    <text class="fig-purple" x="346" y="61" font-size="11" text-anchor="middle">② 上三角矩阵</text>
    <text class="fig-muted" x="275" y="108" font-size="9.5" text-anchor="middle">c</text>
    <text class="fig-muted" x="275" y="130" font-size="9.5" text-anchor="middle">c</text>
    <text class="fig-muted" x="303" y="130" font-size="9.5" text-anchor="middle">c</text>
    <text class="fig-orange" x="396" y="112" font-size="9.5" text-anchor="middle">只存上三角</text>
    <text class="fig-orange" x="396" y="126" font-size="9.5" text-anchor="middle">+ 末尾常数 c</text>
    <text class="fig-label" x="346" y="162" font-size="9.5" text-anchor="middle">共需 n(n+1)/2 + 1 单元</text>
    <text class="fig-muted" x="346" y="180" font-size="9.5" text-anchor="middle">按行存：(i-1)(2n-i+2)/2+j-i</text>
    <text class="fig-green" x="566" y="61" font-size="11" text-anchor="middle">③ 三对角矩阵（|i-j| ≤ 1）</text>
    <text class="fig-accent" x="625" y="96" font-size="9.5" text-anchor="middle">三条对角线</text>
    <text class="fig-accent" x="625" y="112" font-size="9.5" text-anchor="middle">共 3n - 2 个</text>
    <text class="fig-accent" x="625" y="128" font-size="9.5" text-anchor="middle">非零元素</text>
    <text class="fig-green" x="566" y="168" font-size="10.5" text-anchor="middle">下标公式：k = 2i + j - 3</text>
    <text class="fig-muted" x="566" y="184" font-size="9.5" text-anchor="middle">其余 |i - j| &gt; 1 处均为 0</text>
  </svg>
  <figcaption>图 27：三角矩阵只存有用部分，三对角矩阵 k = 2i + j - 3</figcaption>
</figure>

<figure class="cpd-figure not-content" id="ds40-w2-fig-11">
  <svg viewBox="0 0 700 290" role="img" aria-label="对称矩阵、三角矩阵与三对角矩阵压缩存储一维下标正逆映射全解">
    <rect class="fig-box-subtle" x="16" y="14" width="668" height="262" rx="8"/>
    <rect class="fig-box" x="28" y="42" width="322" height="222" rx="6"/>
    <rect class="fig-box" x="362" y="42" width="310" height="222" rx="6"/>
    <rect class="fig-box-blue" x="44" y="68" width="24" height="20" rx="2"/>
    <rect class="fig-box-dashed" x="70" y="68" width="24" height="20" rx="2"/>
    <rect class="fig-box-dashed" x="96" y="68" width="24" height="20" rx="2"/>
    <rect class="fig-box-dashed" x="122" y="68" width="24" height="20" rx="2"/>
    <rect class="fig-box-blue" x="44" y="90" width="24" height="20" rx="2"/>
    <rect class="fig-box-blue" x="70" y="90" width="24" height="20" rx="2"/>
    <rect class="fig-box-dashed" x="96" y="90" width="24" height="20" rx="2"/>
    <rect class="fig-box-dashed" x="122" y="90" width="24" height="20" rx="2"/>
    <rect class="fig-box-blue" x="44" y="112" width="24" height="20" rx="2"/>
    <rect class="fig-box-blue" x="70" y="112" width="24" height="20" rx="2"/>
    <rect class="fig-box-blue" x="96" y="112" width="24" height="20" rx="2"/>
    <rect class="fig-box-dashed" x="122" y="112" width="24" height="20" rx="2"/>
    <rect class="fig-box-green" x="44" y="134" width="24" height="20" rx="2"/>
    <rect class="fig-box-orange" x="70" y="134" width="24" height="20" rx="2"/>
    <rect class="fig-box-green" x="96" y="134" width="24" height="20" rx="2"/>
    <rect class="fig-box-green" x="122" y="134" width="24" height="20" rx="2"/>
    <rect class="fig-box-blue" x="158" y="68" width="180" height="86" rx="4"/>
    <rect class="fig-box-purple" x="40" y="162" width="298" height="92" rx="4"/>
    <rect class="fig-box-green" x="378" y="68" width="24" height="20" rx="2"/>
    <rect class="fig-box-green" x="404" y="68" width="24" height="20" rx="2"/>
    <rect class="fig-box-subtle" x="430" y="68" width="24" height="20" rx="2"/>
    <rect class="fig-box-subtle" x="456" y="68" width="24" height="20" rx="2"/>
    <rect class="fig-box-green" x="378" y="90" width="24" height="20" rx="2"/>
    <rect class="fig-box-green" x="404" y="90" width="24" height="20" rx="2"/>
    <rect class="fig-box-green" x="430" y="90" width="24" height="20" rx="2"/>
    <rect class="fig-box-subtle" x="456" y="90" width="24" height="20" rx="2"/>
    <rect class="fig-box-subtle" x="378" y="112" width="24" height="20" rx="2"/>
    <rect class="fig-box-orange" x="404" y="112" width="24" height="20" rx="2"/>
    <rect class="fig-box-green" x="430" y="112" width="24" height="20" rx="2"/>
    <rect class="fig-box-green" x="456" y="112" width="24" height="20" rx="2"/>
    <rect class="fig-box-subtle" x="378" y="134" width="24" height="20" rx="2"/>
    <rect class="fig-box-subtle" x="404" y="134" width="24" height="20" rx="2"/>
    <rect class="fig-box-green" x="430" y="134" width="24" height="20" rx="2"/>
    <rect class="fig-box-green" x="456" y="134" width="24" height="20" rx="2"/>
    <rect class="fig-box-green" x="490" y="68" width="170" height="86" rx="4"/>
    <rect class="fig-box-accent" x="374" y="162" width="286" height="92" rx="4"/>
    <text class="fig-title" x="350" y="32" font-size="12.5" text-anchor="middle">特殊矩阵压缩到一维数组 B[0..]（矩阵行列 i, j 从 1 开始，B 下标 k 从 0 开始）</text>
    <text class="fig-blue" x="189" y="59" font-size="11" text-anchor="middle">① 对称矩阵 &amp; 下三角矩阵（按行存下三角）</text>
    <text class="fig-blue" x="56" y="82" font-size="9" text-anchor="middle">1</text>
    <text class="fig-muted" x="109" y="82" font-size="8.5" text-anchor="middle">上三角区</text>
    <text class="fig-blue" x="56" y="104" font-size="9" text-anchor="middle">2</text>
    <text class="fig-blue" x="82" y="104" font-size="9" text-anchor="middle">3</text>
    <text class="fig-blue" x="56" y="126" font-size="9" text-anchor="middle">4</text>
    <text class="fig-blue" x="82" y="126" font-size="9" text-anchor="middle">5</text>
    <text class="fig-blue" x="108" y="126" font-size="9" text-anchor="middle">6</text>
    <text class="fig-green" x="56" y="148" font-size="9" text-anchor="middle">7</text>
    <text class="fig-orange" x="82" y="148" font-size="9" text-anchor="middle">★</text>
    <text class="fig-green" x="108" y="148" font-size="9" text-anchor="middle">9</text>
    <text class="fig-green" x="134" y="148" font-size="9" text-anchor="middle">10</text>
    <text class="fig-blue" x="248" y="86" font-size="10" text-anchor="middle">前 i-1 行等差求和：</text>
    <text class="fig-label" x="248" y="104" font-size="10" text-anchor="middle">1 + 2 + … + (i-1)</text>
    <text class="fig-label" x="248" y="122" font-size="10.5" text-anchor="middle">= i(i - 1) / 2 个</text>
    <text class="fig-orange" x="248" y="142" font-size="9.5" text-anchor="middle">a[4][2] → k = 6 + 2 - 1 = 7</text>
    <text class="fig-purple" x="50" y="180" font-size="10">• 下三角 i &gt;= j：k = i(i - 1)/2 + j - 1</text>
    <text class="fig-purple" x="50" y="198" font-size="10">• 对称矩阵上三角 i &lt; j：互换得 k = j(j - 1)/2 + i - 1</text>
    <text class="fig-purple" x="50" y="216" font-size="10">• 下三角矩阵上三角 i &lt; j：统一存末尾常量格 k = n(n+1)/2</text>
    <text class="fig-muted" x="50" y="236" font-size="9.5">• 附：上三角矩阵按行存上三角 (i&lt;=j)：k = (i-1)(2n-i+2)/2 + j - i</text>
    <text class="fig-green" x="517" y="59" font-size="11" text-anchor="middle">② 三对角带状矩阵（|i - j| &lt;= 1，共 3n-2 个）</text>
    <text class="fig-green" x="390" y="82" font-size="9" text-anchor="middle">0</text>
    <text class="fig-green" x="416" y="82" font-size="9" text-anchor="middle">1</text>
    <text class="fig-muted" x="442" y="82" font-size="8.5" text-anchor="middle">0</text>
    <text class="fig-muted" x="468" y="82" font-size="8.5" text-anchor="middle">0</text>
    <text class="fig-green" x="390" y="104" font-size="9" text-anchor="middle">2</text>
    <text class="fig-green" x="416" y="104" font-size="9" text-anchor="middle">3</text>
    <text class="fig-green" x="442" y="104" font-size="9" text-anchor="middle">4</text>
    <text class="fig-muted" x="468" y="104" font-size="8.5" text-anchor="middle">0</text>
    <text class="fig-muted" x="390" y="126" font-size="8.5" text-anchor="middle">0</text>
    <text class="fig-orange" x="416" y="126" font-size="9" text-anchor="middle">★5</text>
    <text class="fig-green" x="442" y="126" font-size="9" text-anchor="middle">6</text>
    <text class="fig-green" x="468" y="126" font-size="9" text-anchor="middle">7</text>
    <text class="fig-muted" x="390" y="148" font-size="8.5" text-anchor="middle">0</text>
    <text class="fig-muted" x="416" y="148" font-size="8.5" text-anchor="middle">0</text>
    <text class="fig-green" x="442" y="148" font-size="9" text-anchor="middle">8</text>
    <text class="fig-green" x="468" y="148" font-size="9" text-anchor="middle">9</text>
    <text class="fig-green" x="575" y="86" font-size="10" text-anchor="middle">首尾行 2 个，中间行 3 个</text>
    <text class="fig-label" x="575" y="104" font-size="10" text-anchor="middle">前 i-1 行共 3(i-1) - 1 个</text>
    <text class="fig-label" x="575" y="122" font-size="10" text-anchor="middle">本行第 (j - i + 2) 个</text>
    <text class="fig-orange" x="575" y="142" font-size="9.5" text-anchor="middle">a[3][2] → k = 2×3+2-3 = 5</text>
    <text class="fig-accent" x="384" y="182" font-size="10.5">• 正向求下标 k：k = 2i + j - 3（B 从 0 起）</text>
    <text class="fig-label" x="384" y="201" font-size="10">• 逆向由 k 反求 (i, j)（408 真题）：</text>
    <text class="fig-label" x="394" y="219" font-size="10">i = ⌊(k + 1) / 3⌋ + 1 ， j = k - 2i + 3</text>
    <text class="fig-muted" x="384" y="238" font-size="9.5">• 若题干 B[1..] 从 1 编号，所有 k 公式末尾整体 + 1 即可！</text>
  </svg>
  <figcaption>特殊矩阵一维压缩全景：全部保留 O(1) 随机存取能力，解题务必核对“数组 B 下标从 0 还是从 1 开始”</figcaption>
</figure>

### 稀疏矩阵

矩阵中绝大多数元素为 0 时称为**稀疏矩阵**，只记录非零元素：

- **三元组表**：每行记录 `(行号 i, 列号 j, 值 v)`
- **十字链表**：链式存储非零元素

**★考点**：稀疏矩阵压缩存储后，**失去随机存取特性**——不能再通过下标直接算出地址。

<figure class="cpd-figure not-content" id="ds40-img4-fig-8">
  <svg viewBox="0 0 700 226" role="img" aria-label="稀疏矩阵三元组表与十字链表存储结构示意图">
    <defs>
      <marker id="ds40-img4-arr-5" viewBox="0 0 10 10" refX="8.5" refY="5" markerWidth="6.5" markerHeight="6.5" markerUnits="userSpaceOnUse" orient="auto-start-reverse">
        <path d="M 1 1.5 L 8.5 5 L 1 8.5 z" fill="context-stroke" stroke="none"/>
      </marker>
    </defs>
    <rect class="fig-box-subtle" x="16" y="14" width="668" height="198" rx="8"/>
    <rect class="fig-box" x="28" y="44" width="290" height="154" rx="6"/>
    <rect class="fig-box" x="330" y="44" width="342" height="154" rx="6"/>
    <line class="fig-line" stroke="var(--sl-color-blue)" stroke-width="1.4" x1="386" y1="112" x2="452" y2="112" marker-end="url(#ds40-img4-arr-5)"/>
    <line class="fig-line" stroke="var(--sl-color-blue)" stroke-width="1.4" x1="528" y1="112" x2="564" y2="112" marker-end="url(#ds40-img4-arr-5)"/>
    <line class="fig-line" stroke="var(--sl-color-blue)" stroke-width="1.4" x1="386" y1="152" x2="452" y2="152" marker-end="url(#ds40-img4-arr-5)"/>
    <line class="fig-line" stroke="var(--sl-color-green)" stroke-width="1.4" x1="490" y1="86" x2="490" y2="98" marker-end="url(#ds40-img4-arr-5)"/>
    <line class="fig-line" stroke="var(--sl-color-green)" stroke-width="1.4" x1="490" y1="126" x2="490" y2="138" marker-end="url(#ds40-img4-arr-5)"/>
    <line class="fig-line" stroke="var(--sl-color-green)" stroke-width="1.4" x1="602" y1="86" x2="602" y2="98" marker-end="url(#ds40-img4-arr-5)"/>
    <rect class="fig-box-purple" x="42" y="68" width="262" height="24" rx="3"/>
    <rect class="fig-box-blue" x="42" y="96" width="262" height="22" rx="3"/>
    <rect class="fig-box-blue" x="42" y="121" width="262" height="22" rx="3"/>
    <rect class="fig-box-blue" x="42" y="146" width="262" height="22" rx="3"/>
    <rect class="fig-box-red" x="42" y="172" width="262" height="20" rx="3"/>
    <rect class="fig-box-green" x="462" y="64" width="56" height="22" rx="3"/>
    <rect class="fig-box-green" x="574" y="64" width="56" height="22" rx="3"/>
    <rect class="fig-box-blue" x="344" y="100" width="42" height="24" rx="3"/>
    <rect class="fig-box-blue" x="344" y="140" width="42" height="24" rx="3"/>
    <rect class="fig-box-orange" x="454" y="100" width="74" height="24" rx="4"/>
    <rect class="fig-box-orange" x="566" y="100" width="74" height="24" rx="4"/>
    <rect class="fig-box-orange" x="454" y="140" width="74" height="24" rx="4"/>
    <rect class="fig-box-red" x="344" y="172" width="314" height="20" rx="3"/>
    <text class="fig-title" x="350" y="34" font-size="12.5" text-anchor="middle">图 28：稀疏矩阵压缩存储（三元组表 / 十字链表）</text>
    <text class="fig-blue" x="173" y="60" font-size="11" text-anchor="middle">① 三元组顺序表 (i, j, v)</text>
    <text class="fig-purple" x="173" y="84" font-size="9.5" text-anchor="middle">表头：(行=3, 列=4, 非零元=3)</text>
    <text class="fig-label" x="173" y="111" font-size="10" text-anchor="middle">( 1, 2, 5 ) ｜ 第 1 行第 2 列 = 5</text>
    <text class="fig-label" x="173" y="136" font-size="10" text-anchor="middle">( 1, 4, 8 ) ｜ 第 1 行第 4 列 = 8</text>
    <text class="fig-label" x="173" y="161" font-size="10" text-anchor="middle">( 2, 2, -3 ) ｜ 第 2 行第 2 列 = -3</text>
    <text class="fig-red" x="173" y="186" font-size="9.5" text-anchor="middle">✗ 失去 O(1) 随机存取（需顺序查找）</text>
    <text class="fig-green" x="501" y="60" font-size="11" text-anchor="middle">② 十字链表（行链 + 列链正交）</text>
    <text class="fig-green" x="490" y="79" font-size="9.5" text-anchor="middle">列 2 ↓</text>
    <text class="fig-green" x="602" y="79" font-size="9.5" text-anchor="middle">列 4 ↓</text>
    <text class="fig-blue" x="365" y="116" font-size="9.5" text-anchor="middle">行 1 →</text>
    <text class="fig-blue" x="365" y="156" font-size="9.5" text-anchor="middle">行 2 →</text>
    <text class="fig-orange" x="491" y="116" font-size="9.5" text-anchor="middle">1,2 | 5</text>
    <text class="fig-orange" x="603" y="116" font-size="9.5" text-anchor="middle">1,4 | 8</text>
    <text class="fig-orange" x="491" y="156" font-size="9.5" text-anchor="middle">2,2 | -3</text>
    <text class="fig-red" x="501" y="186" font-size="9.5" text-anchor="middle">结点的 5 域：[i | j | v | down | right]，同样无随机存取</text>
  </svg>
  <figcaption>图 28：三元组表 / 十字链表；压缩后失去随机存取</figcaption>
</figure>

<figure class="cpd-figure not-content" id="ds40-w2-fig-12">
  <svg viewBox="0 0 700 282" role="img" aria-label="稀疏矩阵三元组顺序表与十字链表存储结构对比及丧失随机存取原因">
    <defs>
      <marker id="ds40-w2-arr-12" viewBox="0 0 10 10" refX="8.5" refY="5" markerWidth="6.5" markerHeight="6.5" markerUnits="userSpaceOnUse" orient="auto-start-reverse">
        <path d="M 1 1.5 L 8.5 5 L 1 8.5 z" fill="context-stroke" stroke="none"/>
      </marker>
    </defs>
    <rect class="fig-box-subtle" x="16" y="14" width="668" height="254" rx="8"/>
    <rect class="fig-box" x="28" y="42" width="302" height="214" rx="6"/>
    <rect class="fig-box" x="342" y="42" width="330" height="214" rx="6"/>
    <line class="fig-line" stroke="var(--sl-color-blue)" stroke-width="1.4" x1="402" y1="128" x2="500" y2="128" marker-end="url(#ds40-w2-arr-12)"/>
    <line class="fig-line" stroke="var(--sl-color-blue)" stroke-width="1.4" x1="566" y1="128" x2="584" y2="128" marker-end="url(#ds40-w2-arr-12)"/>
    <line class="fig-line" stroke="var(--sl-color-blue)" stroke-width="1.4" x1="402" y1="166" x2="500" y2="166" marker-end="url(#ds40-w2-arr-12)"/>
    <line class="fig-line" stroke="var(--sl-color-blue)" stroke-width="1.4" x1="402" y1="204" x2="422" y2="204" marker-end="url(#ds40-w2-arr-12)"/>
    <line class="fig-line" stroke="var(--sl-color-green)" stroke-width="1.4" x1="456" y1="96" x2="456" y2="190" marker-end="url(#ds40-w2-arr-12)"/>
    <line class="fig-line" stroke="var(--sl-color-green)" stroke-width="1.4" x1="534" y1="96" x2="534" y2="114" marker-end="url(#ds40-w2-arr-12)"/>
    <line class="fig-line" stroke="var(--sl-color-green)" stroke-width="1.4" x1="534" y1="140" x2="534" y2="152" marker-end="url(#ds40-w2-arr-12)"/>
    <line class="fig-line" stroke="var(--sl-color-green)" stroke-width="1.4" x1="618" y1="96" x2="618" y2="114" marker-end="url(#ds40-w2-arr-12)"/>
    <rect class="fig-box-purple" x="40" y="68" width="278" height="24" rx="3"/>
    <rect class="fig-box-blue" x="40" y="96" width="278" height="22" rx="3"/>
    <rect class="fig-box-blue" x="40" y="121" width="278" height="22" rx="3"/>
    <rect class="fig-box-blue" x="40" y="146" width="278" height="22" rx="3"/>
    <rect class="fig-box-blue" x="40" y="171" width="278" height="22" rx="3"/>
    <rect class="fig-box-red" x="40" y="201" width="278" height="44" rx="4"/>
    <rect class="fig-box-green" x="430" y="74" width="52" height="22" rx="3"/>
    <rect class="fig-box-green" x="508" y="74" width="52" height="22" rx="3"/>
    <rect class="fig-box-green" x="592" y="74" width="52" height="22" rx="3"/>
    <rect class="fig-box-blue" x="356" y="116" width="46" height="24" rx="3"/>
    <rect class="fig-box-blue" x="356" y="154" width="46" height="24" rx="3"/>
    <rect class="fig-box-blue" x="356" y="192" width="46" height="24" rx="3"/>
    <rect class="fig-box-orange" x="502" y="116" width="64" height="24" rx="4"/>
    <rect class="fig-box-orange" x="586" y="116" width="64" height="24" rx="4"/>
    <rect class="fig-box-orange" x="502" y="154" width="64" height="24" rx="4"/>
    <rect class="fig-box-orange" x="424" y="192" width="64" height="24" rx="4"/>
    <rect class="fig-box-purple" x="356" y="224" width="302" height="24" rx="4"/>
    <text class="fig-title" x="350" y="32" font-size="12.5" text-anchor="middle">稀疏矩阵 M（3×4，含 4 个非零元）的两种压缩方案对比</text>
    <text class="fig-blue" x="179" y="59" font-size="11" text-anchor="middle">① 三元组顺序表（i, j, v）</text>
    <text class="fig-purple" x="179" y="84" font-size="10" text-anchor="middle">[0] 表头元数据：(行数=3, 列数=4, 非零元数=4)</text>
    <text class="fig-label" x="179" y="111" font-size="10" text-anchor="middle">[1]  ( i = 1 ,  j = 2 ,  v =  5 )</text>
    <text class="fig-label" x="179" y="136" font-size="10" text-anchor="middle">[2]  ( i = 1 ,  j = 4 ,  v =  8 )</text>
    <text class="fig-label" x="179" y="161" font-size="10" text-anchor="middle">[3]  ( i = 2 ,  j = 2 ,  v = -3 )</text>
    <text class="fig-label" x="179" y="186" font-size="10" text-anchor="middle">[4]  ( i = 3 ,  j = 1 ,  v =  6 )</text>
    <text class="fig-red" x="179" y="218" font-size="10" text-anchor="middle">★ 失去 O(1) 随机存取！查 M[i][j] 需顺序扫描</text>
    <text class="fig-muted" x="179" y="236" font-size="9.5" text-anchor="middle">插入/删除非零元会改变三元组排布，需搬移元素</text>
    <text class="fig-green" x="507" y="59" font-size="11" text-anchor="middle">② 十字链表（行 right 链 + 列 down 链正交）</text>
    <text class="fig-green" x="456" y="89" font-size="9.5" text-anchor="middle">C1↓</text>
    <text class="fig-green" x="534" y="89" font-size="9.5" text-anchor="middle">C2↓</text>
    <text class="fig-green" x="618" y="89" font-size="9.5" text-anchor="middle">C4↓</text>
    <text class="fig-blue" x="379" y="132" font-size="9.5" text-anchor="middle">R1→</text>
    <text class="fig-blue" x="379" y="170" font-size="9.5" text-anchor="middle">R2→</text>
    <text class="fig-blue" x="379" y="208" font-size="9.5" text-anchor="middle">R3→</text>
    <text class="fig-orange" x="534" y="132" font-size="9.5" text-anchor="middle">1,2 | 5</text>
    <text class="fig-orange" x="618" y="132" font-size="9.5" text-anchor="middle">1,4 | 8</text>
    <text class="fig-orange" x="534" y="170" font-size="9.5" text-anchor="middle">2,2 | -3</text>
    <text class="fig-orange" x="456" y="208" font-size="9.5" text-anchor="middle">3,1 | 6</text>
    <text class="fig-purple" x="507" y="240" font-size="9.5" text-anchor="middle">五域结点 [i|j|v|down|right]：易增删非零元，同样无随机存取</text>
  </svg>
  <figcaption>稀疏矩阵压缩本质：非零元素分布无规律，无论三元组表还是十字链表均破坏下标线性映射，丧失随机存取特性</figcaption>
</figure>

## 七、树与二叉树

### 树的定义与术语

- **树**：$n$ 个结点的有限集合。$n=0$ 为空树；$n>0$ 时有唯一的**根**，其余结点分成若干互不相交的子树。
- **叶子**：没有孩子的结点。
- **结点的度**：结点拥有的孩子数；**树的度**：所有结点度的最大值。
- **层次**：根在第 1 层，逐层加 1；**高度/深度**：最大层数。
- 基本性质：**结点数 = 所有结点度数之和 + 1**（除根外，每个结点都恰好有一条边连向父亲）。

<figure class="cpd-figure not-content" id="ds40-w3-fig-1">
  <svg viewBox="0 0 700 288" role="img" aria-label="树的基本术语、层次高度与握手定理及m叉树叶结点公式推导">
    <rect class="fig-box-subtle" x="16" y="16" width="334" height="256" rx="8"/>
    <rect class="fig-box" x="364" y="16" width="320" height="256" rx="8"/>
    <g class="fig-line-dashed">
      <line x1="58" y1="68" x2="338" y2="68"/>
      <line x1="58" y1="122" x2="338" y2="122"/>
      <line x1="58" y1="176" x2="338" y2="176"/>
      <line x1="58" y1="230" x2="338" y2="230"/>
    </g>
    <line class="fig-divider" x1="380" y1="142" x2="668" y2="142"/>
    <g class="fig-line">
      <line x1="190" y1="68" x2="106" y2="122"/>
      <line x1="190" y1="68" x2="190" y2="122"/>
      <line x1="190" y1="68" x2="274" y2="122"/>
      <line x1="106" y1="122" x2="78" y2="176"/>
      <line x1="106" y1="122" x2="134" y2="176"/>
      <line x1="274" y1="122" x2="226" y2="176"/>
      <line x1="274" y1="122" x2="274" y2="176"/>
      <line x1="274" y1="122" x2="320" y2="176"/>
      <line x1="134" y1="176" x2="134" y2="230"/>
    </g>
    <circle class="fig-box-orange" cx="190" cy="68" r="16"/>
    <circle class="fig-box-blue" cx="106" cy="122" r="16"/>
    <circle class="fig-box-green" cx="190" cy="122" r="16"/>
    <circle class="fig-box-blue" cx="274" cy="122" r="16"/>
    <circle class="fig-box-green" cx="78" cy="176" r="15"/>
    <circle class="fig-box-blue" cx="134" cy="176" r="15"/>
    <circle class="fig-box-green" cx="226" cy="176" r="15"/>
    <circle class="fig-box-green" cx="274" cy="176" r="15"/>
    <circle class="fig-box-green" cx="320" cy="176" r="15"/>
    <circle class="fig-box-green" cx="134" cy="230" r="15"/>
    <rect class="fig-box-accent" x="380" y="106" width="288" height="26" rx="5"/>
    <rect class="fig-box-orange" x="380" y="210" width="288" height="26" rx="5"/>
    <text class="fig-title" x="183" y="38" text-anchor="middle" font-size="12.5">树的术语示例（树的度 m=3，高度 h=4）</text>
    <g class="fig-muted" font-size="11" text-anchor="middle">
      <text x="38" y="72">L1</text>
      <text x="38" y="126">L2</text>
      <text x="38" y="180">L3</text>
      <text x="38" y="234">L4</text>
    </g>
    <g class="fig-label" font-size="12" text-anchor="middle">
      <text x="190" y="72">A</text>
      <text x="106" y="126">B</text>
      <text x="190" y="126">C</text>
      <text x="274" y="126">D</text>
      <text x="78" y="180">E</text>
      <text x="134" y="180">F</text>
      <text x="226" y="180">G</text>
      <text x="274" y="180">H</text>
      <text x="320" y="180">I</text>
      <text x="134" y="234">J</text>
    </g>
    <text class="fig-orange" x="214" y="62" font-size="10.5">根 d=3</text>
    <text class="fig-blue" x="76" y="114" font-size="10.5">d=2</text>
    <text class="fig-blue" x="296" y="114" font-size="10.5">d=3</text>
    <text class="fig-blue" x="155" y="172" font-size="10.5">d=1</text>
    <text class="fig-green" x="226" y="258" text-anchor="middle" font-size="11">绿色为叶结点（d=0，共 6 个：C, E, G, H, I, J）</text>
    <text class="fig-title" x="380" y="38" font-size="12.5">① 结点数与度数守恒（边数双向计数）</text>
    <text class="fig-label" x="380" y="62" font-size="10.5">向上看父边：除根外每结点 1 条边 ⇒ B = n - 1 = 9</text>
    <text class="fig-label" x="380" y="82" font-size="10">向下看度数：各结点孩子数之和 ⇒ B = Σdᵢ = 3+2+3+1 = 9</text>
    <text class="fig-accent" x="524" y="123" text-anchor="middle" font-size="11">核心性质：总结点数 n = 所有结点度数之和 + 1</text>
    <text class="fig-title" x="380" y="164" font-size="12">② m 叉树与二叉树叶结点通式</text>
    <text class="fig-muted" x="380" y="184" font-size="10">联立 n = n₀+n₁+…+nₘ 与 n-1 = 1·n₁+2·n₂+…+m·nₘ：</text>
    <text class="fig-label" x="380" y="202" font-size="10.5">消去 n₁ 得：n₀ = 1 + n₂ + 2n₃ + … + (m - 1)nₘ</text>
    <text class="fig-orange" x="524" y="227" text-anchor="middle" font-size="11">m=2 特例（二叉树）：n₀ = n₂ + 1（与 n₁ 无关）</text>
    <text class="fig-muted" x="380" y="256" font-size="10">左图验证 (m=3)：n₀ = 1 + n₂(1) + 2×n₃(2) = 6，完全吻合</text>
  </svg>
  <figcaption>树的基本术语与度数守恒：通过“向上数父边”与“向下数孩子度”双向计数推导叶结点通式</figcaption>
</figure>

### 二叉树

每个结点最多两个孩子，且左右孩子有次序，不能颠倒。

- **满二叉树**：每一层都满。
- **完全二叉树**：除最后一层外都满，最后一层的结点连续靠左排列。

> **★考点**：以下二叉树性质必须记熟。

1. 第 $i$ 层最多有 $2^{i-1}$ 个结点；高度为 $h$ 的二叉树最多有 $2^h-1$ 个结点。
2. **叶子数 = 度为 2 的结点数 + 1**，即 $n_0=n_2+1$。
3. $n$ 个结点的完全二叉树高度为 $\lceil \log_2(n+1) \rceil$。
4. 完全二叉树按层编号（从 1 开始，0 号单元不用）：结点 $i$ 的左孩子是 $2i$，右孩子是 $2i+1$，父结点是 $\lfloor i/2 \rfloor$。

**$n_0=n_2+1$ 的证明**：

$$
n=n_0+n_1+n_2
$$

总边数从“孩子向上看”数：除根外每个结点一条边，所以

$$
B=n-1
$$

从“父亲向下看”数：度为 1 的结点贡献 1 条边，度为 2 的结点贡献 2 条边，所以

$$
B=n_1+2n_2
$$

联立：

$$
n_0+n_1+n_2-1=n_1+2n_2 \Rightarrow n_0=n_2+1
$$

> **★考点**：$n_0=n_2+1$ 与完全二叉树高度公式必须记熟。视频中的例子：$n_2=2$（A、B），$n_1=2$（C、E），$n_0=3$（D、F、G），$3=2+1$ 成立。

<figure class="cpd-figure not-content" id="ds40-w3-fig-2">
  <svg viewBox="0 0 700 328" role="img" aria-label="满二叉树、完全二叉树与右单支树对比及顺序存储下标定位公式">
    <defs>
      <marker id="ds40-w3-arr-2" viewBox="0 0 10 10" refX="8.5" refY="5" markerWidth="6" markerHeight="6" markerUnits="userSpaceOnUse" orient="auto-start-reverse">
        <path d="M 1 1.5 L 8.5 5 L 1 8.5 z" fill="context-stroke" stroke="none"/>
      </marker>
    </defs>
    <rect class="fig-box" x="16" y="16" width="212" height="158" rx="8"/>
    <rect class="fig-box" x="242" y="16" width="216" height="158" rx="8"/>
    <rect class="fig-box" x="472" y="16" width="212" height="158" rx="8"/>
    <rect class="fig-box-subtle" x="16" y="186" width="668" height="126" rx="8"/>
    <g class="fig-line">
      <line x1="122" y1="58" x2="78" y2="98"/>
      <line x1="122" y1="58" x2="166" y2="98"/>
      <line x1="78" y1="98" x2="54" y2="138"/>
      <line x1="78" y1="98" x2="100" y2="138"/>
      <line x1="166" y1="98" x2="144" y2="138"/>
      <line x1="166" y1="98" x2="190" y2="138"/>
      <line x1="350" y1="58" x2="306" y2="98"/>
      <line x1="350" y1="58" x2="394" y2="98"/>
      <line x1="306" y1="98" x2="282" y2="138"/>
      <line x1="306" y1="98" x2="328" y2="138"/>
      <line x1="394" y1="98" x2="372" y2="138"/>
      <line x1="544" y1="58" x2="588" y2="98"/>
      <line x1="588" y1="98" x2="632" y2="138"/>
    </g>
    <g class="fig-line-dashed">
      <line x1="394" y1="98" x2="418" y2="138"/>
      <line x1="544" y1="58" x2="508" y2="98"/>
      <line x1="588" y1="98" x2="562" y2="138"/>
    </g>
    <path class="fig-line" stroke="var(--sl-color-blue)" d="M 145 238 Q 191 210 233 236" marker-end="url(#ds40-w3-arr-2)"/>
    <path class="fig-line" stroke="var(--sl-color-green)" d="M 151 238 Q 217 202 279 236" marker-end="url(#ds40-w3-arr-2)"/>
    <g class="fig-box-blue">
      <circle cx="122" cy="58" r="12"/><circle cx="78" cy="98" r="12"/><circle cx="166" cy="98" r="12"/>
      <circle cx="350" cy="58" r="12"/><circle cx="306" cy="98" r="12"/><circle cx="394" cy="98" r="12"/>
    </g>
    <g class="fig-box-green">
      <circle cx="54" cy="138" r="12"/><circle cx="100" cy="138" r="12"/><circle cx="144" cy="138" r="12"/><circle cx="190" cy="138" r="12"/>
      <circle cx="282" cy="138" r="12"/><circle cx="328" cy="138" r="12"/><circle cx="372" cy="138" r="12"/>
    </g>
    <g class="fig-box-red">
      <circle cx="544" cy="58" r="12"/><circle cx="588" cy="98" r="12"/><circle cx="632" cy="138" r="12"/>
    </g>
    <g class="fig-box-dashed">
      <circle cx="418" cy="138" r="12"/>
      <circle cx="508" cy="98" r="12"/><circle cx="562" cy="138" r="12"/>
    </g>
    <rect class="fig-box-dashed" x="32" y="238" width="44" height="28" rx="4"/>
    <rect class="fig-box-blue" x="78" y="238" width="44" height="28" rx="4"/>
    <rect class="fig-box-orange" x="124" y="238" width="44" height="28" rx="4"/>
    <rect class="fig-box-blue" x="170" y="238" width="44" height="28" rx="4"/>
    <rect class="fig-box-green" x="216" y="238" width="44" height="28" rx="4"/>
    <rect class="fig-box-green" x="262" y="238" width="44" height="28" rx="4"/>
    <rect class="fig-box-green" x="308" y="238" width="44" height="28" rx="4"/>
    <rect class="fig-box-dashed" x="354" y="238" width="44" height="28" rx="4"/>
    <rect class="fig-box" x="412" y="212" width="258" height="88" rx="6"/>
    <text class="fig-title" x="122" y="35" text-anchor="middle" font-size="12">① 满二叉树 (h=3, n=2³-1=7)</text>
    <text class="fig-title" x="350" y="35" text-anchor="middle" font-size="12">② 完全二叉树 (n=6, 靠左连续)</text>
    <text class="fig-title" x="578" y="35" text-anchor="middle" font-size="12">③ 右单支树 (非完全, 浪费槽位)</text>
    <g class="fig-label" font-size="11" text-anchor="middle">
      <text x="122" y="62">1</text><text x="78" y="102">2</text><text x="166" y="102">3</text>
      <text x="54" y="142">4</text><text x="100" y="142">5</text><text x="144" y="142">6</text><text x="190" y="142">7</text>
      <text x="350" y="62">1</text><text x="306" y="102">2</text><text x="394" y="102">3</text>
      <text x="282" y="142">4</text><text x="328" y="142">5</text><text x="372" y="142">6</text>
      <text x="544" y="62">1</text><text x="588" y="102">3</text><text x="632" y="142">7</text>
    </g>
    <g class="fig-muted" font-size="10.5" text-anchor="middle">
      <text x="418" y="142">空</text>
      <text x="508" y="102">2空</text><text x="562" y="142">6空</text>
      <text x="122" y="164">每层全满，无空缺</text>
      <text x="350" y="164">1..6 与满二叉树编号一一对应</text>
      <text x="578" y="164">仅 3 结点却需占到数组下标 7</text>
    </g>
    <text class="fig-title" x="32" y="205" font-size="12">完全二叉树顺序存储（图② n=6）：按层存数组，算术下标直接还原父子关系</text>
    <text class="fig-blue" x="184" y="222" font-size="10.5">2i=4</text>
    <text class="fig-green" x="234" y="216" font-size="10.5">2i+1=5</text>
    <g class="fig-label" font-size="11.5" text-anchor="middle">
      <text x="54" y="256">不用</text>
      <text x="100" y="256">A</text><text x="146" y="256">B</text><text x="192" y="256">C</text>
      <text x="238" y="256">D</text><text x="284" y="256">E</text><text x="330" y="256">F</text>
      <text x="376" y="256">∅</text>
    </g>
    <g class="fig-muted" font-size="10.5" text-anchor="middle">
      <text x="54" y="280">[0]</text><text x="100" y="280">[1]</text><text x="146" y="280">[2]</text><text x="192" y="280">[3]</text>
      <text x="238" y="280">[4]</text><text x="284" y="280">[5]</text><text x="330" y="280">[6]</text><text x="376" y="280">[7]</text>
    </g>
    <text class="fig-muted" x="215" y="298" text-anchor="middle" font-size="11">分支结点：i ≤ ⌊n/2⌋ = 3（蓝/橙） ｜ 叶结点：i &gt; ⌊n/2⌋（绿）</text>
    <text class="fig-label" x="426" y="233" font-size="11.5">• 左孩子 = 2i ，右孩子 = 2i + 1</text>
    <text class="fig-label" x="426" y="253" font-size="11.5">• 父结点 = ⌊i / 2⌋（如 ⌊5/2⌋ = 2）</text>
    <text class="fig-orange" x="426" y="273" font-size="11.5">• 完全二叉树高度 h = ⌈log₂(n + 1)⌉</text>
    <text class="fig-red" x="426" y="291" font-size="11">• 易错：若从 0 编号，孩子变为 2i+1, 2i+2</text>
  </svg>
  <figcaption>满二叉树、完全二叉树与单支树对比：完全二叉树紧凑无空洞，可用 1 起始数组实现 O(1) 父子寻址</figcaption>
</figure>

### 二叉链表

每个结点存 `data`、`lchild`、`rchild` 三个域。

> **★考点**：$n$ 个结点的二叉链表共有 $2n$ 个指针域，其中 $n-1$ 个用于连接孩子，**空指针域有 $n+1$ 个**。

<figure class="cpd-figure not-content" id="ds40-img4-fig-9">
  <svg viewBox="0 0 700 242" role="img" aria-label="二叉链表结点结构与 n 个结点共 n+1 个空指针域示意图">
    <defs>
      <marker id="ds40-img4-arr-6" viewBox="0 0 10 10" refX="8.5" refY="5" markerWidth="6.5" markerHeight="6.5" markerUnits="userSpaceOnUse" orient="auto-start-reverse">
        <path d="M 1 1.5 L 8.5 5 L 1 8.5 z" fill="context-stroke" stroke="none"/>
      </marker>
    </defs>
    <rect class="fig-box-subtle" x="16" y="14" width="668" height="214" rx="8"/>
    <rect class="fig-box" x="28" y="44" width="356" height="170" rx="6"/>
    <rect class="fig-box" x="396" y="44" width="276" height="170" rx="6"/>
    <path class="fig-line" stroke="var(--sl-color-blue)" d="M 164 88 L 164 102 L 106 102 L 106 116" marker-end="url(#ds40-img4-arr-6)"/>
    <path class="fig-line" stroke="var(--sl-color-blue)" d="M 248 88 L 248 102 L 306 102 L 306 116" marker-end="url(#ds40-img4-arr-6)"/>
    <path class="fig-line" stroke="var(--sl-color-blue)" d="M 140 144 L 140 158 L 206 158 L 206 172" marker-end="url(#ds40-img4-arr-6)"/>
    <rect class="fig-box-blue" x="148" y="60" width="32" height="28" rx="3"/>
    <rect class="fig-box" x="180" y="60" width="52" height="28"/>
    <rect class="fig-box-blue" x="232" y="60" width="32" height="28" rx="3"/>
    <rect class="fig-box-orange" x="56" y="116" width="32" height="28" rx="3"/>
    <rect class="fig-box" x="88" y="116" width="36" height="28"/>
    <rect class="fig-box-blue" x="124" y="116" width="32" height="28" rx="3"/>
    <rect class="fig-box-orange" x="256" y="116" width="32" height="28" rx="3"/>
    <rect class="fig-box" x="288" y="116" width="36" height="28"/>
    <rect class="fig-box-orange" x="324" y="116" width="32" height="28" rx="3"/>
    <rect class="fig-box-orange" x="156" y="172" width="32" height="28" rx="3"/>
    <rect class="fig-box" x="188" y="172" width="36" height="28"/>
    <rect class="fig-box-orange" x="224" y="172" width="32" height="28" rx="3"/>
    <rect class="fig-box-blue" x="410" y="72" width="248" height="30" rx="4"/>
    <rect class="fig-box-green" x="410" y="108" width="248" height="30" rx="4"/>
    <rect class="fig-box-accent" x="410" y="146" width="248" height="54" rx="4"/>
    <text class="fig-title" x="350" y="34" font-size="12.5" text-anchor="middle">图 29：二叉链表结构与 n + 1 个空指针域（以 n = 4 为例）</text>
    <text class="fig-blue" x="164" y="78" font-size="10.5" text-anchor="middle">●</text>
    <text class="fig-label" x="206" y="78" font-size="11.5" text-anchor="middle">A</text>
    <text class="fig-blue" x="248" y="78" font-size="10.5" text-anchor="middle">●</text>
    <text class="fig-orange" x="72" y="134" font-size="10" text-anchor="middle">∧</text>
    <text class="fig-label" x="106" y="134" font-size="11.5" text-anchor="middle">B</text>
    <text class="fig-blue" x="140" y="134" font-size="10.5" text-anchor="middle">●</text>
    <text class="fig-orange" x="272" y="134" font-size="10" text-anchor="middle">∧</text>
    <text class="fig-label" x="306" y="134" font-size="11.5" text-anchor="middle">C</text>
    <text class="fig-orange" x="340" y="134" font-size="10" text-anchor="middle">∧</text>
    <text class="fig-orange" x="172" y="190" font-size="10" text-anchor="middle">∧</text>
    <text class="fig-label" x="206" y="190" font-size="11.5" text-anchor="middle">D</text>
    <text class="fig-orange" x="240" y="190" font-size="10" text-anchor="middle">∧</text>
    <text class="fig-muted" x="206" y="210" font-size="9.5" text-anchor="middle">每结点 3 域：[ lchild | data | rchild ]</text>
    <text class="fig-label" x="534" y="61" font-size="11" text-anchor="middle">空指针数量推导</text>
    <text class="fig-blue" x="422" y="91" font-size="10.5">总指针域 = 2n（n=4 时共 8 个）</text>
    <text class="fig-green" x="422" y="127" font-size="10.5">非空指针 = 边数 = n - 1（3 条）</text>
    <text class="fig-accent" x="534" y="168" font-size="11.5" text-anchor="middle">空指针 = 2n - (n - 1) = n + 1</text>
    <text class="fig-muted" x="534" y="188" font-size="10" text-anchor="middle">本例共 4 + 1 = 5 个橙色 ∧ 空指针</text>
  </svg>
  <figcaption>图 29：二叉链表结构；2n - (n - 1) = n + 1 个空指针</figcaption>
</figure>

<figure class="cpd-figure not-content" id="ds40-w3-fig-3">
  <svg viewBox="0 0 700 264" role="img" aria-label="二叉链表三域结点结构与n+1个空指针域及k叉链表推广公式">
    <defs>
      <marker id="ds40-w3-arr-3" viewBox="0 0 10 10" refX="8.5" refY="5" markerWidth="6" markerHeight="6" markerUnits="userSpaceOnUse" orient="auto-start-reverse">
        <path d="M 1 1.5 L 8.5 5 L 1 8.5 z" fill="context-stroke" stroke="none"/>
      </marker>
    </defs>
    <rect class="fig-box-subtle" x="16" y="16" width="372" height="232" rx="8"/>
    <rect class="fig-box" x="402" y="16" width="282" height="232" rx="8"/>
    <line class="fig-divider" x1="418" y1="150" x2="668" y2="150"/>
    <path class="fig-line" stroke="var(--sl-color-blue)" d="M 161 78 L 161 94 L 96 94 L 96 114" marker-end="url(#ds40-w3-arr-3)"/>
    <path class="fig-line" stroke="var(--sl-color-blue)" d="M 239 78 L 239 94 L 304 94 L 304 114" marker-end="url(#ds40-w3-arr-3)"/>
    <path class="fig-line" stroke="var(--sl-color-blue)" d="M 135 144 L 135 162 L 184 162 L 184 182" marker-end="url(#ds40-w3-arr-3)"/>
    <rect class="fig-box-blue" x="144" y="48" width="34" height="30" rx="3"/>
    <rect class="fig-box" x="178" y="48" width="44" height="30"/>
    <rect class="fig-box-blue" x="222" y="48" width="34" height="30" rx="3"/>
    <rect class="fig-box-orange" x="42" y="114" width="34" height="30" rx="3"/>
    <rect class="fig-box" x="76" y="114" width="40" height="30"/>
    <rect class="fig-box-blue" x="116" y="114" width="34" height="30" rx="3"/>
    <rect class="fig-box-orange" x="250" y="114" width="34" height="30" rx="3"/>
    <rect class="fig-box" x="284" y="114" width="40" height="30"/>
    <rect class="fig-box-orange" x="324" y="114" width="34" height="30" rx="3"/>
    <rect class="fig-box-orange" x="130" y="182" width="34" height="30" rx="3"/>
    <rect class="fig-box" x="164" y="182" width="40" height="30"/>
    <rect class="fig-box-orange" x="204" y="182" width="34" height="30" rx="3"/>
    <rect class="fig-box-accent" x="418" y="108" width="250" height="28" rx="5"/>
    <rect class="fig-box-purple" x="418" y="204" width="250" height="28" rx="5"/>
    <text class="fig-title" x="202" y="36" text-anchor="middle" font-size="12">二叉链表实例（n=4 结点：lchild | data | rchild）</text>
    <g class="fig-blue" font-size="11" text-anchor="middle">
      <text x="161" y="67">●</text>
      <text x="239" y="67">●</text>
      <text x="133" y="133">●</text>
    </g>
    <g class="fig-label" font-size="12.5" text-anchor="middle">
      <text x="200" y="68">A</text>
      <text x="96" y="134">B</text>
      <text x="304" y="134">C</text>
      <text x="184" y="202">D</text>
    </g>
    <g class="fig-orange" font-size="11" text-anchor="middle">
      <text x="59" y="133">∧①</text>
      <text x="267" y="133">∧④</text>
      <text x="341" y="133">∧⑤</text>
      <text x="147" y="201">∧②</text>
      <text x="221" y="201">∧③</text>
    </g>
    <text class="fig-muted" x="202" y="234" text-anchor="middle" font-size="11">蓝格 ● 为非空指针（3 个）｜ 橙格 ∧ 为空指针 NULL（5 个）</text>
    <text class="fig-title" x="418" y="38" font-size="12.5">空指针域计数推导</text>
    <text class="fig-label" x="418" y="60" font-size="11.5">• 总指针域数：每个结点 2 个 ⇒ 2n</text>
    <text class="fig-label" x="418" y="80" font-size="11.5">• 非空指针数：树总边数（除根各 1 入边）⇒ n - 1</text>
    <text class="fig-muted" x="418" y="98" font-size="11">• 空指针域数 = 总指针域 - 非空指针：</text>
    <text class="fig-accent" x="543" y="126" text-anchor="middle" font-size="12">2n - (n - 1) = n + 1 个空指针</text>
    <text class="fig-title" x="418" y="170" font-size="12">408 高频推广：k 叉链表空指针数</text>
    <text class="fig-label" x="418" y="192" font-size="11.5">每结点 k 个孩子指针，总域 kn，非空仍为 n - 1：</text>
    <text class="fig-purple" x="543" y="222" text-anchor="middle" font-size="12">空指针数 = kn - (n - 1) = (k - 1)n + 1</text>
  </svg>
  <figcaption>二叉链表存储结构与空指针计数：n=4 时共 8 个指针域、3 条边、5 个空域（恰好用于构造线索二叉树）</figcaption>
</figure>

## 八、二叉树的遍历

### 四种遍历

| 遍历 | 顺序 | 实现 |
|:----:|:-----|:-----|
| 先序 | 根 → 左 → 右 | 递归/栈 |
| 中序 | 左 → 根 → 右 | 递归/栈 |
| 后序 | 左 → 右 → 根 | 递归/栈 |
| 层序 | 一层一层从左到右 | 队列 |

**手算技巧**：从根出发沿树的外轮廓逆时针绕一圈——经过结点**左侧**记入先序，经过**下方**记入中序，经过**右侧**记入后序。

> **★考点**：先序 + 中序、后序 + 中序、层序 + 中序都能唯一确定一棵二叉树；只给先序 + 后序**不能**唯一确定。

**还原方法**：先序的第一个结点是根；在中序中找到根，左边是左子树、右边是右子树；对左右两部分递归重复。

<figure class="cpd-figure not-content" id="ds40-fig-s3">
  <svg viewBox="0 0 680 262" role="img" aria-label="二叉树逆时针绕外轮廓手算三序、层序队列遍历与先序加中序唯一还原">
    <defs>
      <marker id="ds40-arr-s3" viewBox="0 0 10 10" refX="8.5" refY="5" markerWidth="6.5" markerHeight="6.5" markerUnits="userSpaceOnUse" orient="auto-start-reverse">
        <path d="M 1 1.5 L 8.5 5 L 1 8.5 z" fill="context-stroke" stroke="none"/>
      </marker>
    </defs>
    <rect class="fig-box-subtle" x="16" y="16" width="312" height="230" rx="8"/>
    <rect class="fig-box" x="342" y="16" width="322" height="114" rx="8"/>
    <rect class="fig-box" x="342" y="142" width="322" height="104" rx="8"/>
    <path class="fig-line-dashed" d="M 166 28 C 118 38 74 88 48 162 C 34 202 72 224 96 196 C 112 176 118 152 136 196 C 154 224 194 200 178 158 C 164 124 152 106 184 82 C 214 106 226 156 246 196 C 270 224 312 200 292 156 C 270 106 242 44 204 28" marker-end="url(#ds40-arr-s3)"/>
    <g class="fig-line">
      <line x1="184" y1="54" x2="114" y2="116"/>
      <line x1="184" y1="54" x2="254" y2="116"/>
      <line x1="114" y1="116" x2="74" y2="180"/>
      <line x1="114" y1="116" x2="154" y2="180"/>
      <line x1="254" y1="116" x2="274" y2="180"/>
    </g>
    <g class="fig-box">
      <circle cx="184" cy="54" r="18"/>
      <circle cx="114" cy="116" r="18"/>
      <circle cx="254" cy="116" r="18"/>
      <circle cx="74" cy="180" r="18"/>
      <circle cx="154" cy="180" r="18"/>
      <circle cx="274" cy="180" r="18"/>
    </g>
    <g class="fig-box-blue">
      <circle cx="163" cy="54" r="3.5"/><circle cx="93" cy="116" r="3.5"/><circle cx="53" cy="180" r="3.5"/>
      <circle cx="133" cy="180" r="3.5"/><circle cx="233" cy="116" r="3.5"/><circle cx="253" cy="180" r="3.5"/>
      <circle cx="362" cy="60" r="4"/>
    </g>
    <g class="fig-box-orange">
      <circle cx="184" cy="75" r="3.5"/><circle cx="114" cy="137" r="3.5"/><circle cx="254" cy="137" r="3.5"/>
      <circle cx="74" cy="201" r="3.5"/><circle cx="154" cy="201" r="3.5"/><circle cx="274" cy="201" r="3.5"/>
      <circle cx="362" cy="80" r="4"/>
      <rect x="398" y="172" width="26" height="20" rx="3"/>
      <rect x="480" y="200" width="26" height="20" rx="3"/>
    </g>
    <g class="fig-box-green">
      <circle cx="205" cy="54" r="3.5"/><circle cx="135" cy="116" r="3.5"/><circle cx="275" cy="116" r="3.5"/>
      <circle cx="95" cy="180" r="3.5"/><circle cx="175" cy="180" r="3.5"/><circle cx="295" cy="180" r="3.5"/>
      <circle cx="362" cy="100" r="4"/>
      <rect x="510" y="172" width="56" height="20" rx="3"/>
      <rect x="510" y="200" width="56" height="20" rx="3"/>
    </g>
    <g class="fig-box-purple">
      <circle cx="362" cy="119" r="4"/>
    </g>
    <rect class="fig-box-blue" x="428" y="172" width="78" height="20" rx="3"/>
    <rect class="fig-box-blue" x="398" y="200" width="78" height="20" rx="3"/>
    <g class="fig-label" font-size="13" text-anchor="middle">
      <text x="184" y="59">A</text>
      <text x="114" y="121">B</text>
      <text x="254" y="121">C</text>
      <text x="74" y="185">D</text>
      <text x="154" y="185">E</text>
      <text x="274" y="185">F</text>
    </g>
    <text class="fig-muted" x="172" y="234" text-anchor="middle" font-size="11">沿虚线轮廓逆时针绕行一周</text>
    <text class="fig-title" x="358" y="38" font-size="12.5">四种遍历序列对比（栈递归 vs 队列层序）</text>
    <text class="fig-blue" x="374" y="64" font-size="11.5">左侧触点（先序）：A  B  D  E  C  F</text>
    <text class="fig-orange" x="374" y="84" font-size="11.5">下方触点（中序）：D  B  E  A  C  F</text>
    <text class="fig-green" x="374" y="104" font-size="11.5">右侧触点（后序）：D  E  B  F  C  A</text>
    <text class="fig-purple" x="374" y="123" font-size="11.5">逐层出队（层序）：A  B  C  D  E  F</text>
    <text class="fig-title" x="358" y="162" font-size="12.5">先序定根 + 中序切分左右子树</text>
    <text class="fig-muted" x="358" y="186" font-size="11.5">先序：</text>
    <text class="fig-orange" x="411" y="186" text-anchor="middle" font-size="11.5">A</text>
    <text class="fig-blue" x="467" y="186" text-anchor="middle" font-size="11.5">B  D  E</text>
    <text class="fig-green" x="538" y="186" text-anchor="middle" font-size="11.5">C  F</text>
    <text class="fig-muted" x="358" y="214" font-size="11.5">中序：</text>
    <text class="fig-blue" x="437" y="214" text-anchor="middle" font-size="11.5">D  B  E</text>
    <text class="fig-orange" x="493" y="214" text-anchor="middle" font-size="11.5">A</text>
    <text class="fig-green" x="538" y="214" text-anchor="middle" font-size="11.5">C  F</text>
    <text class="fig-muted" x="576" y="198" font-size="11">递归还原</text>
    <text class="fig-muted" x="358" y="236" font-size="11">后序末位定根、层序首位定根，均需配合中序切分</text>
  </svg>
  <figcaption>三序轮廓手算法（左先、下中、右后）、队列层序遍历与“先序定根 + 中序切分”唯一还原过程</figcaption>
</figure>

<figure class="cpd-figure not-content" id="ds40-w3-fig-4">
  <svg viewBox="0 0 700 278" role="img" aria-label="表达式树三序对应波兰式逆波兰式与先序加后序无法唯一确定二叉树的反例">
    <rect class="fig-box-subtle" x="16" y="16" width="334" height="246" rx="8"/>
    <rect class="fig-box" x="364" y="16" width="320" height="246" rx="8"/>
    <g class="fig-line">
      <line x1="183" y1="62" x2="116" y2="110"/>
      <line x1="183" y1="62" x2="250" y2="110"/>
      <line x1="116" y1="110" x2="80" y2="158"/>
      <line x1="116" y1="110" x2="152" y2="158"/>
      <line x1="250" y1="110" x2="214" y2="158"/>
      <line x1="250" y1="110" x2="286" y2="158"/>
      <line x1="454" y1="126" x2="418" y2="174"/>
      <line x1="594" y1="126" x2="630" y2="174"/>
    </g>
    <g class="fig-line-dashed">
      <line x1="454" y1="126" x2="490" y2="174"/>
      <line x1="594" y1="126" x2="558" y2="174"/>
    </g>
    <circle class="fig-box-orange" cx="183" cy="62" r="15"/>
    <circle class="fig-box-blue" cx="116" cy="110" r="15"/>
    <circle class="fig-box-blue" cx="250" cy="110" r="15"/>
    <g class="fig-box-green">
      <circle cx="80" cy="158" r="14"/><circle cx="152" cy="158" r="14"/>
      <circle cx="214" cy="158" r="14"/><circle cx="286" cy="158" r="14"/>
    </g>
    <rect class="fig-box" x="28" y="184" width="310" height="68" rx="6"/>
    <g class="fig-box-red">
      <circle cx="454" cy="126" r="15"/><circle cx="418" cy="174" r="15"/>
      <circle cx="594" cy="126" r="15"/><circle cx="630" cy="174" r="15"/>
    </g>
    <g class="fig-box-dashed">
      <circle cx="490" cy="174" r="14"/><circle cx="558" cy="174" r="14"/>
    </g>
    <rect class="fig-box-accent" x="380" y="206" width="288" height="46" rx="5"/>
    <text class="fig-title" x="183" y="38" text-anchor="middle" font-size="12.5">表达式树与波兰/中缀/逆波兰式</text>
    <g class="fig-label" font-size="13" text-anchor="middle">
      <text x="183" y="67">*</text>
      <text x="116" y="115">+</text><text x="250" y="115">-</text>
      <text x="80" y="163">a</text><text x="152" y="163">b</text>
      <text x="214" y="163">c</text><text x="286" y="163">d</text>
      <text x="454" y="131">A</text><text x="418" y="179">B</text>
      <text x="594" y="131">A</text><text x="630" y="179">B</text>
    </g>
    <text class="fig-blue" x="38" y="202" font-size="11.5">先序（前缀波兰式）：* + a b - c d</text>
    <text class="fig-orange" x="38" y="222" font-size="11.5">中序（中缀需括号）：(a + b) * (c - d)</text>
    <text class="fig-green" x="38" y="242" font-size="11.5">后序（逆波兰 RPN）：a b + c d - *（栈求值）</text>
    <text class="fig-title" x="524" y="38" text-anchor="middle" font-size="12.5">为何“先序 + 后序”不能唯一确定二叉树？</text>
    <text class="fig-label" x="524" y="62" text-anchor="middle" font-size="11.5">当存在度为 1 的结点时，无法区分左右孩子：</text>
    <text class="fig-red" x="524" y="84" text-anchor="middle" font-size="11.5">已知 先序 = A B ，后序 = B A（同属以下两树）</text>
    <g class="fig-muted" font-size="10.5" text-anchor="middle">
      <text x="490" y="178">空</text><text x="558" y="178">空</text>
      <text x="454" y="198">形态①：B 为左孩子</text>
      <text x="594" y="198">形态②：B 为右孩子</text>
    </g>
    <text class="fig-accent" x="524" y="224" text-anchor="middle" font-size="11.5">唯一还原必含中序：先+中 ✓ ｜ 后+中 ✓ ｜ 层+中 ✓</text>
    <text class="fig-muted" x="524" y="243" text-anchor="middle" font-size="10.5">注：若限定“每结点度均为 0 或 2”，则先+后可唯一确定</text>
  </svg>
  <figcaption>表达式树三序物理意义，以及度为 1 结点导致“先序 + 后序”丢失左右方位信息的经典反例</figcaption>
</figure>

### 线索二叉树

利用二叉链表中的 $n+1$ 个空指针，让它们指向该结点在某种遍历序列中的**前驱**和**后继**，遍历时就不需要栈。

- 左指针为空 → 指向遍历前驱
- 右指针为空 → 指向遍历后继
- 用 `ltag`、`rtag` 区分“孩子指针”和“线索”

<figure class="cpd-figure not-content" id="ds40-img4-fig-10">
  <svg viewBox="0 0 700 246" role="img" aria-label="线索二叉树利用空链域指向遍历前驱和后继及 ltag rtag 标志位含义">
    <defs>
      <marker id="ds40-img4-arr-7" viewBox="0 0 10 10" refX="8.5" refY="5" markerWidth="6.5" markerHeight="6.5" markerUnits="userSpaceOnUse" orient="auto-start-reverse">
        <path d="M 1 1.5 L 8.5 5 L 1 8.5 z" fill="context-stroke" stroke="none"/>
      </marker>
    </defs>
    <rect class="fig-box-subtle" x="16" y="14" width="668" height="218" rx="8"/>
    <rect class="fig-box" x="28" y="44" width="374" height="174" rx="6"/>
    <rect class="fig-box" x="414" y="44" width="258" height="174" rx="6"/>
    <line class="fig-line" stroke="var(--sl-color-blue)" stroke-width="1.5" x1="186" y1="90" x2="144" y2="122" marker-end="url(#ds40-img4-arr-7)"/>
    <line class="fig-line" stroke="var(--sl-color-blue)" stroke-width="1.5" x1="244" y1="90" x2="290" y2="122" marker-end="url(#ds40-img4-arr-7)"/>
    <line class="fig-line" stroke="var(--sl-color-blue)" stroke-width="1.5" x1="112" y1="148" x2="88" y2="180" marker-end="url(#ds40-img4-arr-7)"/>
    <path class="fig-line-dashed" stroke="var(--sl-color-green)" d="M 118 180 Q 126 164 128 148" marker-end="url(#ds40-img4-arr-7)"/>
    <path class="fig-line-dashed" stroke="var(--sl-color-orange)" d="M 276 122 Q 254 108 232 90" marker-end="url(#ds40-img4-arr-7)"/>
    <rect class="fig-box" x="174" y="64" width="82" height="26" rx="3"/>
    <rect class="fig-box" x="98" y="122" width="82" height="26" rx="3"/>
    <rect class="fig-box" x="256" y="122" width="82" height="26" rx="3"/>
    <rect class="fig-box" x="46" y="180" width="82" height="26" rx="3"/>
    <rect class="fig-box-orange" x="256" y="122" width="18" height="26" rx="2"/>
    <rect class="fig-box-green" x="110" y="180" width="18" height="26" rx="2"/>
    <rect class="fig-box-purple" x="46" y="180" width="18" height="26" rx="2"/>
    <rect class="fig-box-purple" x="320" y="122" width="18" height="26" rx="2"/>
    <rect class="fig-box-blue" x="426" y="70" width="234" height="44" rx="4"/>
    <rect class="fig-box-orange" x="426" y="120" width="234" height="42" rx="4"/>
    <rect class="fig-box-green" x="426" y="168" width="234" height="42" rx="4"/>
    <text class="fig-title" x="350" y="34" font-size="12.5" text-anchor="middle">图 30：中序线索二叉树（中序序列：D → B → A → C）</text>
    <text class="fig-muted" x="215" y="56" font-size="9.5" text-anchor="middle">结点的 5 域：[ lchild | ltag | data | rtag | rchild ]</text>
    <text class="fig-label" x="215" y="81" font-size="10.5" text-anchor="middle">● | 0 | A | 0 | ●</text>
    <text class="fig-label" x="139" y="139" font-size="10.5" text-anchor="middle">● | 0 | B | 1 | ∧</text>
    <text class="fig-label" x="297" y="139" font-size="10.5" text-anchor="middle">● | 1 | C | 1 | ∧</text>
    <text class="fig-label" x="87" y="197" font-size="10.5" text-anchor="middle">∧ | 1 | D | 1 | ●</text>
    <text class="fig-muted" x="242" y="190" font-size="9.5" text-anchor="middle">橙虚线：指向前驱</text>
    <text class="fig-muted" x="242" y="206" font-size="9.5" text-anchor="middle">绿虚线：指向后继</text>
    <text class="fig-label" x="543" y="60" font-size="11" text-anchor="middle">ltag / rtag 标志位约定</text>
    <text class="fig-blue" x="438" y="88" font-size="10.5">tag = 0：指针指向左/右孩子</text>
    <text class="fig-muted" x="438" y="104" font-size="9.5">（如 A 的 ltag=0, rtag=0）</text>
    <text class="fig-orange" x="438" y="138" font-size="10.5">ltag = 1：lchild 指向遍历前驱</text>
    <text class="fig-muted" x="438" y="154" font-size="9.5">（如 C 的左线索指向前驱 A）</text>
    <text class="fig-green" x="438" y="186" font-size="10.5">rtag = 1：rchild 指向遍历后继</text>
    <text class="fig-muted" x="438" y="202" font-size="9.5">（如 D 的右线索指向后继 B）</text>
  </svg>
  <figcaption>图 30：空链域改为指向前驱/后继的线索</figcaption>
</figure>

<figure class="cpd-figure not-content" id="ds40-w3-fig-5">
  <svg viewBox="0 0 700 294" role="img" aria-label="中序线索二叉树结点标志位结构、前驱后继虚线线索与查找规则及遍历坑点">
    <defs>
      <marker id="ds40-w3-arr-5" viewBox="0 0 10 10" refX="8.5" refY="5" markerWidth="6" markerHeight="6" markerUnits="userSpaceOnUse" orient="auto-start-reverse">
        <path d="M 1 1.5 L 8.5 5 L 1 8.5 z" fill="context-stroke" stroke="none"/>
      </marker>
    </defs>
    <rect class="fig-box-subtle" x="16" y="16" width="388" height="262" rx="8"/>
    <rect class="fig-box" x="416" y="16" width="268" height="262" rx="8"/>
    <line class="fig-divider" x1="430" y1="126" x2="670" y2="126"/>
    <line class="fig-divider" x1="430" y1="212" x2="670" y2="212"/>
    <g class="fig-line" stroke="var(--sl-color-blue)">
      <line x1="190" y1="90" x2="143" y2="128" marker-end="url(#ds40-w3-arr-5)"/>
      <line x1="262" y1="90" x2="326" y2="128" marker-end="url(#ds40-w3-arr-5)"/>
      <line x1="107" y1="154" x2="82" y2="198" marker-end="url(#ds40-w3-arr-5)"/>
      <line x1="179" y1="154" x2="204" y2="198" marker-end="url(#ds40-w3-arr-5)"/>
    </g>
    <path class="fig-line-dashed" stroke="var(--sl-color-green)" d="M 118 198 Q 124 176 126 155" marker-end="url(#ds40-w3-arr-5)"/>
    <path class="fig-line-dashed" stroke="var(--sl-color-orange)" d="M 168 198 Q 162 176 160 155" marker-end="url(#ds40-w3-arr-5)"/>
    <path class="fig-line-dashed" stroke="var(--sl-color-green)" d="M 240 198 Q 258 148 232 91" marker-end="url(#ds40-w3-arr-5)"/>
    <path class="fig-line-dashed" stroke="var(--sl-color-orange)" d="M 290 128 Q 268 112 245 91" marker-end="url(#ds40-w3-arr-5)"/>
    <g class="fig-box">
      <rect x="181" y="64" width="90" height="26" rx="3"/>
      <rect x="98" y="128" width="90" height="26" rx="3"/>
      <rect x="281" y="128" width="90" height="26" rx="3"/>
      <rect x="37" y="198" width="90" height="26" rx="3"/>
      <rect x="159" y="198" width="90" height="26" rx="3"/>
    </g>
    <g class="fig-box-blue">
      <rect x="181" y="64" width="18" height="26" rx="2"/><rect x="253" y="64" width="18" height="26" rx="2"/>
      <rect x="98" y="128" width="18" height="26" rx="2"/><rect x="170" y="128" width="18" height="26" rx="2"/>
    </g>
    <g class="fig-box-orange">
      <rect x="281" y="128" width="18" height="26" rx="2"/>
      <rect x="159" y="198" width="18" height="26" rx="2"/>
    </g>
    <g class="fig-box-green">
      <rect x="109" y="198" width="18" height="26" rx="2"/>
      <rect x="231" y="198" width="18" height="26" rx="2"/>
    </g>
    <g class="fig-box-purple">
      <rect x="353" y="128" width="18" height="26" rx="2"/>
      <rect x="37" y="198" width="18" height="26" rx="2"/>
    </g>
    <g class="fig-divider">
      <line x1="215" y1="64" x2="215" y2="90"/><line x1="237" y1="64" x2="237" y2="90"/>
      <line x1="132" y1="128" x2="132" y2="154"/><line x1="154" y1="128" x2="154" y2="154"/>
      <line x1="315" y1="128" x2="315" y2="154"/><line x1="337" y1="128" x2="337" y2="154"/>
      <line x1="71" y1="198" x2="71" y2="224"/><line x1="93" y1="198" x2="93" y2="224"/>
      <line x1="193" y1="198" x2="193" y2="224"/><line x1="215" y1="198" x2="215" y2="224"/>
    </g>
    <rect class="fig-box-red" x="428" y="222" width="244" height="44" rx="5"/>
    <text class="fig-title" x="210" y="36" text-anchor="middle" font-size="12">中序线索二叉树（中序：D → B → E → A → C）</text>
    <text class="fig-muted" x="210" y="53" text-anchor="middle" font-size="10.5">结点域：[ lchild | ltag | data | rtag | rchild ]（0=孩子, 1=线索）</text>
    <g class="fig-label" font-size="11" text-anchor="middle">
      <text x="190" y="81">●</text><text x="207" y="81">0</text><text x="226" y="81">A</text><text x="245" y="81">0</text><text x="262" y="81">●</text>
      <text x="107" y="145">●</text><text x="124" y="145">0</text><text x="143" y="145">B</text><text x="162" y="145">0</text><text x="179" y="145">●</text>
      <text x="290" y="145">●</text><text x="307" y="145">1</text><text x="326" y="145">C</text><text x="345" y="145">1</text><text x="362" y="145">∧</text>
      <text x="46" y="215">∧</text><text x="63" y="215">1</text><text x="82" y="215">D</text><text x="101" y="215">1</text><text x="118" y="215">●</text>
      <text x="168" y="215">●</text><text x="185" y="215">1</text><text x="204" y="215">E</text><text x="223" y="215">1</text><text x="240" y="215">●</text>
    </g>
    <text class="fig-orange" x="210" y="246" text-anchor="middle" font-size="11">橙虚线：前驱线索（ltag=1） ｜ 绿虚线：后继线索（rtag=1）</text>
    <text class="fig-purple" x="210" y="264" text-anchor="middle" font-size="11">紫格 ∧：首结点 D 左空、尾结点 C 右空（仍剩 2 个 NULL）</text>
    <text class="fig-title" x="430" y="38" font-size="12">① 中序线索树找后继 Next(p)</text>
    <text class="fig-green" x="430" y="58" font-size="11">• rtag == 1：直接返回 p-&gt;rchild</text>
    <text class="fig-label" x="430" y="78" font-size="11">• rtag == 0：进入 p 右子树，一路向</text>
    <text class="fig-label" x="440" y="95" font-size="11">左到底（右子树最左下结点）</text>
    <text class="fig-muted" x="430" y="114" font-size="10.5">例：B 的后继是右子树最左下 E</text>
    <text class="fig-title" x="430" y="146" font-size="12">② 中序线索树找前驱 Pre(p)</text>
    <text class="fig-orange" x="430" y="166" font-size="11">• ltag == 1：直接返回 p-&gt;lchild</text>
    <text class="fig-label" x="430" y="186" font-size="11">• ltag == 0：左子树最右下结点</text>
    <text class="fig-muted" x="430" y="203" font-size="10.5">例：A 的前驱是左子树最右下 E</text>
    <text class="fig-red" x="550" y="240" text-anchor="middle" font-size="11">408 必考坑：先序线索找前驱、</text>
    <text class="fig-red" x="550" y="257" text-anchor="middle" font-size="11">后序线索找后继需借助双亲指针！</text>
  </svg>
  <figcaption>中序线索二叉树结构与 O(1)/O(h) 无栈前驱后继查找规则（n+1 个空域化为 n-1 条线索与首尾 2 个 NULL）</figcaption>
</figure>

## 九、树、森林与哈夫曼树

### 孩子兄弟表示法

每个结点两个指针：

- `firstchild`：指向第一个孩子
- `nextsibling`：指向右边的兄弟

口诀：**左孩子右兄弟**。任意一棵树都能转成二叉树。

森林转二叉树：先把每棵树转成二叉树，再把后一棵树的根作为前一棵树根的右孩子连起来。

**遍历对应关系**：

| 树/森林 | 对应二叉树 |
|:-------:|:----------:|
| 树的先根遍历 | 先序遍历 |
| 树的后根遍历 | 中序遍历 |
| 森林的先序遍历 | 先序遍历 |
| 森林的中序遍历 | 中序遍历 |

<figure class="cpd-figure not-content" id="ds40-w3-fig-6">
  <svg viewBox="0 0 700 266" role="img" aria-label="树的三种存储结构对比：双亲表示法、孩子表示法与孩子兄弟表示法">
    <defs>
      <marker id="ds40-w3-arr-6" viewBox="0 0 10 10" refX="8.5" refY="5" markerWidth="6" markerHeight="6" markerUnits="userSpaceOnUse" orient="auto-start-reverse">
        <path d="M 1 1.5 L 8.5 5 L 1 8.5 z" fill="context-stroke" stroke="none"/>
      </marker>
    </defs>
    <rect class="fig-box" x="16" y="16" width="206" height="234" rx="8"/>
    <rect class="fig-box" x="236" y="16" width="232" height="234" rx="8"/>
    <rect class="fig-box-subtle" x="482" y="16" width="202" height="234" rx="8"/>
    <g class="fig-line" stroke="var(--sl-color-blue)">
      <line x1="306" y1="73" x2="328" y2="73" marker-end="url(#ds40-w3-arr-6)"/>
      <line x1="372" y1="73" x2="394" y2="73" marker-end="url(#ds40-w3-arr-6)"/>
      <line x1="306" y1="103" x2="328" y2="103" marker-end="url(#ds40-w3-arr-6)"/>
      <line x1="372" y1="103" x2="394" y2="103" marker-end="url(#ds40-w3-arr-6)"/>
      <line x1="566" y1="78" x2="534" y2="112" marker-end="url(#ds40-w3-arr-6)"/>
      <line x1="534" y1="138" x2="512" y2="174" marker-end="url(#ds40-w3-arr-6)"/>
    </g>
    <g class="fig-line" stroke="var(--sl-color-green)">
      <line x1="556" y1="125" x2="604" y2="125" marker-end="url(#ds40-w3-arr-6)"/>
      <line x1="534" y1="187" x2="582" y2="187" marker-end="url(#ds40-w3-arr-6)"/>
    </g>
    <g class="fig-box-subtle">
      <rect x="44" y="60" width="150" height="26"/><rect x="44" y="86" width="150" height="26"/>
      <rect x="44" y="112" width="150" height="26"/><rect x="44" y="138" width="150" height="26"/>
      <rect x="44" y="164" width="150" height="26"/>
    </g>
    <rect class="fig-box-orange" x="134" y="60" width="60" height="26"/>
    <g class="fig-divider">
      <line x1="84" y1="60" x2="84" y2="190"/>
      <line x1="134" y1="60" x2="134" y2="190"/>
    </g>
    <g class="fig-box">
      <rect x="252" y="60" width="54" height="26" rx="2"/><rect x="252" y="90" width="54" height="26" rx="2"/>
      <rect x="252" y="120" width="54" height="26" rx="2"/><rect x="252" y="150" width="54" height="26" rx="2"/>
      <rect x="252" y="180" width="54" height="26" rx="2"/>
    </g>
    <g class="fig-divider">
      <line x1="280" y1="60" x2="280" y2="86"/><line x1="280" y1="90" x2="280" y2="116"/>
      <line x1="280" y1="120" x2="280" y2="146"/><line x1="280" y1="150" x2="280" y2="176"/>
      <line x1="280" y1="180" x2="280" y2="206"/>
    </g>
    <g class="fig-box-blue">
      <rect x="330" y="61" width="42" height="24" rx="3"/><rect x="396" y="61" width="46" height="24" rx="3"/>
      <rect x="330" y="91" width="42" height="24" rx="3"/><rect x="396" y="91" width="46" height="24" rx="3"/>
    </g>
    <g class="fig-box">
      <rect x="544" y="54" width="44" height="24" rx="4"/>
      <rect x="512" y="113" width="44" height="24" rx="4"/>
      <rect x="606" y="113" width="44" height="24" rx="4"/>
      <rect x="490" y="175" width="44" height="24" rx="4"/>
      <rect x="584" y="175" width="44" height="24" rx="4"/>
    </g>
    <text class="fig-title" x="119" y="36" text-anchor="middle" font-size="12">① 双亲表示法（顺序数组）</text>
    <text class="fig-muted" x="64" y="54" text-anchor="middle" font-size="10.5">下标</text>
    <text class="fig-muted" x="109" y="54" text-anchor="middle" font-size="10.5">data</text>
    <text class="fig-muted" x="164" y="54" text-anchor="middle" font-size="10.5">parent</text>
    <g class="fig-label" font-size="11" text-anchor="middle">
      <text x="64" y="77">0</text><text x="109" y="77">A</text><text x="164" y="77">-1</text>
      <text x="64" y="103">1</text><text x="109" y="103">B</text><text x="164" y="103">0</text>
      <text x="64" y="129">2</text><text x="109" y="129">C</text><text x="164" y="129">0</text>
      <text x="64" y="155">3</text><text x="109" y="155">D</text><text x="164" y="155">1</text>
      <text x="64" y="181">4</text><text x="109" y="181">E</text><text x="164" y="181">1</text>
    </g>
    <text class="fig-orange" x="119" y="214" text-anchor="middle" font-size="10.5">找父 O(1)，找孩子需遍历 O(n)</text>
    <text class="fig-muted" x="119" y="232" text-anchor="middle" font-size="10.5">典型应用：并查集底层存储</text>
    <text class="fig-title" x="352" y="36" text-anchor="middle" font-size="12">② 孩子表示法（数组+孩子链表）</text>
    <g class="fig-label" font-size="11" text-anchor="middle">
      <text x="266" y="77">A</text><text x="293" y="77">●</text>
      <text x="266" y="107">B</text><text x="293" y="107">●</text>
      <text x="266" y="137">C</text><text x="293" y="137">∧</text>
      <text x="266" y="167">D</text><text x="293" y="167">∧</text>
      <text x="266" y="197">E</text><text x="293" y="197">∧</text>
      <text x="351" y="77">1(B)</text><text x="419" y="77">2(C)∧</text>
      <text x="351" y="107">3(D)</text><text x="419" y="107">4(E)∧</text>
    </g>
    <text class="fig-blue" x="352" y="224" text-anchor="middle" font-size="10.5">找孩子快，找父慢（可合为带双亲域）</text>
    <text class="fig-title" x="583" y="36" text-anchor="middle" font-size="12">③ 孩子兄弟法（二叉链表）</text>
    <g class="fig-label" font-size="11.5" text-anchor="middle">
      <text x="566" y="70">A</text>
      <text x="534" y="129">B</text><text x="628" y="129">C</text>
      <text x="512" y="191">D</text><text x="606" y="191">E</text>
    </g>
    <text class="fig-blue" x="583" y="216" text-anchor="middle" font-size="10.5">蓝：firstchild ｜ 绿：nextsibling</text>
    <text class="fig-accent" x="583" y="234" text-anchor="middle" font-size="10.5">统一转化为二叉树处理（最常用）</text>
  </svg>
  <figcaption>同一棵树（A 含子 B、C，B 含子 D、E）在双亲表示法、孩子表示法与孩子兄弟表示法下的存储形态</figcaption>
</figure>

<figure class="cpd-figure not-content" id="ds40-fig-s4">
  <svg viewBox="0 0 700 284" role="img" aria-label="孩子兄弟表示法：一般树保留长子连线与兄弟串接顺时针旋转45度转换为二叉树">
    <defs>
      <marker id="ds40-arr-s4" viewBox="0 0 10 10" refX="8.5" refY="5" markerWidth="6.5" markerHeight="6.5" markerUnits="userSpaceOnUse" orient="auto-start-reverse">
        <path d="M 1 1.5 L 8.5 5 L 1 8.5 z" fill="context-stroke" stroke="none"/>
      </marker>
    </defs>
    <rect class="fig-box-subtle" x="16" y="16" width="268" height="252" rx="8"/>
    <rect class="fig-box-subtle" x="366" y="16" width="318" height="252" rx="8"/>
    <g class="fig-line-dashed">
      <line x1="150" y1="62" x2="150" y2="122"/>
      <line x1="150" y1="62" x2="230" y2="122"/>
      <line x1="70" y1="122" x2="116" y2="186"/>
    </g>
    <g class="fig-line" stroke="var(--sl-color-blue)">
      <line x1="150" y1="62" x2="70" y2="122"/>
      <line x1="70" y1="122" x2="54" y2="186"/>
      <line x1="230" y1="122" x2="230" y2="186"/>
      <line x1="484" y1="56" x2="432" y2="100"/>
      <line x1="432" y1="100" x2="388" y2="146"/>
      <line x1="572" y1="192" x2="528" y2="236"/>
    </g>
    <g class="fig-line-dashed" stroke="var(--sl-color-green)">
      <line x1="86" y1="122" x2="134" y2="122"/>
      <line x1="166" y1="122" x2="214" y2="122"/>
      <line x1="70" y1="186" x2="100" y2="186"/>
    </g>
    <g class="fig-line" stroke="var(--sl-color-green)">
      <line x1="432" y1="100" x2="502" y2="146"/>
      <line x1="388" y1="146" x2="444" y2="192"/>
      <line x1="502" y1="146" x2="572" y2="192"/>
    </g>
    <line class="fig-line-accent" x1="294" y1="142" x2="356" y2="142" marker-end="url(#ds40-arr-s4)"/>
    <g class="fig-box">
      <circle cx="150" cy="62" r="16"/>
      <circle cx="70" cy="122" r="16"/><circle cx="150" cy="122" r="16"/><circle cx="230" cy="122" r="16"/>
      <circle cx="54" cy="186" r="16"/><circle cx="116" cy="186" r="16"/><circle cx="230" cy="186" r="16"/>
      <circle cx="484" cy="56" r="15"/>
      <circle cx="432" cy="100" r="15"/>
      <circle cx="388" cy="146" r="15"/><circle cx="502" cy="146" r="15"/>
      <circle cx="444" cy="192" r="15"/><circle cx="572" cy="192" r="15"/>
      <circle cx="528" cy="236" r="15"/>
    </g>
    <text class="fig-title" x="150" y="36" text-anchor="middle" font-size="12.5">原一般树（留长子边 + 连兄弟边）</text>
    <text class="fig-title" x="525" y="36" text-anchor="middle" font-size="12.5">转换后的二叉树（左孩子 · 右兄弟）</text>
    <g class="fig-label" font-size="12" text-anchor="middle">
      <text x="150" y="67">A</text>
      <text x="70" y="127">B</text><text x="150" y="127">C</text><text x="230" y="127">D</text>
      <text x="54" y="191">E</text><text x="116" y="191">F</text><text x="230" y="191">G</text>
      <text x="484" y="61">A</text>
      <text x="432" y="105">B</text>
      <text x="388" y="151">E</text><text x="502" y="151">C</text>
      <text x="444" y="197">F</text><text x="572" y="197">D</text>
      <text x="528" y="241">G</text>
    </g>
    <text class="fig-accent" x="325" y="131" text-anchor="middle" font-size="11">顺时针转 45°</text>
    <text class="fig-muted" x="150" y="228" text-anchor="middle" font-size="11">灰虚线：断开非长子与父结点的连边</text>
    <text class="fig-muted" x="150" y="248" text-anchor="middle" font-size="11">重要推论：单棵树转换后根 A 无右子树</text>
    <text class="fig-blue" x="438" y="258" text-anchor="middle" font-size="11">蓝实线：firstchild（左）</text>
    <text class="fig-green" x="614" y="258" text-anchor="middle" font-size="11">绿实线：nextsibling（右）</text>
  </svg>
  <figcaption>孩子兄弟表示法转换几何意义：保留长子连线（左指针），同层兄弟顺次串接并顺时针旋转 45°（右指针）</figcaption>
</figure>

<figure class="cpd-figure not-content" id="ds40-w3-fig-7">
  <svg viewBox="0 0 700 304" role="img" aria-label="森林与二叉树互转规则、根结点右链串接及先序中序遍历序列一一对应关系">
    <defs>
      <marker id="ds40-w3-arr-7" viewBox="0 0 10 10" refX="8.5" refY="5" markerWidth="6" markerHeight="6" markerUnits="userSpaceOnUse" orient="auto-start-reverse">
        <path d="M 1 1.5 L 8.5 5 L 1 8.5 z" fill="context-stroke" stroke="none"/>
      </marker>
    </defs>
    <rect class="fig-box-subtle" x="16" y="16" width="292" height="198" rx="8"/>
    <rect class="fig-box-subtle" x="372" y="16" width="312" height="198" rx="8"/>
    <rect class="fig-box" x="16" y="224" width="668" height="64" rx="8"/>
    <g class="fig-line" stroke="var(--sl-color-blue)">
      <line x1="68" y1="70" x2="44" y2="128"/>
      <line x1="162" y1="70" x2="162" y2="128"/>
      <line x1="250" y1="70" x2="226" y2="128"/>
      <line x1="432" y1="52" x2="398" y2="88"/>
      <line x1="516" y1="84" x2="482" y2="122"/>
      <line x1="600" y1="116" x2="568" y2="154"/>
    </g>
    <g class="fig-line-dashed">
      <line x1="68" y1="70" x2="92" y2="128"/>
      <line x1="250" y1="70" x2="274" y2="128"/>
    </g>
    <g class="fig-line-dashed" stroke="var(--sl-color-green)">
      <line x1="82" y1="70" x2="146" y2="70" marker-end="url(#ds40-w3-arr-7)"/>
      <line x1="176" y1="70" x2="234" y2="70" marker-end="url(#ds40-w3-arr-7)"/>
      <line x1="57" y1="128" x2="79" y2="128"/>
      <line x1="239" y1="128" x2="261" y2="128"/>
    </g>
    <g class="fig-line" stroke="var(--sl-color-green)">
      <line x1="432" y1="52" x2="516" y2="84"/>
      <line x1="516" y1="84" x2="600" y2="116"/>
      <line x1="398" y1="88" x2="432" y2="126"/>
      <line x1="568" y1="154" x2="604" y2="190"/>
    </g>
    <line class="fig-line-accent" x1="316" y1="108" x2="364" y2="108" marker-end="url(#ds40-w3-arr-7)"/>
    <line class="fig-line-accent" x1="364" y1="124" x2="316" y2="124" marker-end="url(#ds40-w3-arr-7)"/>
    <g class="fig-box-orange">
      <circle cx="68" cy="70" r="13"/><circle cx="162" cy="70" r="13"/><circle cx="250" cy="70" r="13"/>
      <circle cx="432" cy="52" r="12"/><circle cx="516" cy="84" r="12"/><circle cx="600" cy="116" r="12"/>
    </g>
    <g class="fig-box">
      <circle cx="44" cy="128" r="13"/><circle cx="92" cy="128" r="13"/>
      <circle cx="162" cy="128" r="13"/>
      <circle cx="226" cy="128" r="13"/><circle cx="274" cy="128" r="13"/>
      <circle cx="398" cy="88" r="12"/><circle cx="432" cy="126" r="12"/>
      <circle cx="482" cy="122" r="12"/>
      <circle cx="568" cy="154" r="12"/><circle cx="604" cy="190" r="12"/>
    </g>
    <rect class="fig-box-accent" x="394" y="234" width="278" height="44" rx="5"/>
    <text class="fig-title" x="162" y="36" text-anchor="middle" font-size="12">森林 F = (T₁, T₂, T₃)：树根视作同层兄弟</text>
    <text class="fig-title" x="528" y="34" text-anchor="middle" font-size="12">对应二叉树 B(F)：后树根挂前树根右链</text>
    <g class="fig-label" font-size="11.5" text-anchor="middle">
      <text x="68" y="74">A</text><text x="162" y="74">D</text><text x="250" y="74">F</text>
      <text x="44" y="132">B</text><text x="92" y="132">C</text>
      <text x="162" y="132">E</text>
      <text x="226" y="132">G</text><text x="274" y="132">H</text>
      <text x="432" y="56">A</text><text x="516" y="88">D</text><text x="600" y="120">F</text>
      <text x="398" y="92">B</text><text x="432" y="130">C</text>
      <text x="482" y="126">E</text>
      <text x="568" y="158">G</text><text x="604" y="194">H</text>
    </g>
    <g class="fig-muted" font-size="10.5" text-anchor="middle">
      <text x="68" y="160">T₁</text><text x="162" y="160">T₂</text><text x="250" y="160">T₃</text>
      <text x="162" y="184">逆转换：沿二叉树根右链拆开即还原各棵树</text>
      <text x="162" y="202">（右链上有 k 个结点 ⇔ 森林含 k 棵树）</text>
      <text x="340" y="98">互转</text>
      <text x="458" y="198">橙色 A→D→F 为根右链</text>
    </g>
    <text class="fig-blue" x="30" y="248" font-size="11.5">森林先序 = 二叉先序：A B C D E F G H</text>
    <text class="fig-green" x="30" y="272" font-size="11.5">森林中序(各树后根) = 二叉中序：B C A E D G H F</text>
    <text class="fig-accent" x="533" y="252" text-anchor="middle" font-size="11">★ 408 必考：森林叶子结点数（B,C,E,G,H 共 5 个）</text>
    <text class="fig-accent" x="533" y="270" text-anchor="middle" font-size="11">= 对应二叉树中“左孩子指针为空”的结点总数！</text>
  </svg>
  <figcaption>森林与二叉树可逆转换：树根沿右孩子指针串接，森林先序/中序与二叉树先序/中序严格等价</figcaption>
</figure>

### 并查集

每个集合是一棵树，用根代表整个集合。

- **Find**：顺着父指针一路向上找到根
- **Union**：把一棵树的根挂到另一棵树的根下面

常用来判断两个元素是否属于同一集合，也是 Kruskal 算法判断回路的基础。

<figure class="cpd-figure not-content" id="ds40-w3-fig-8">
  <svg viewBox="0 0 700 278" role="img" aria-label="并查集双亲数组负数表示法、小树挂大树按规模合并与Find路径压缩优化">
    <defs>
      <marker id="ds40-w3-arr-8" viewBox="0 0 10 10" refX="8.5" refY="5" markerWidth="6" markerHeight="6" markerUnits="userSpaceOnUse" orient="auto-start-reverse">
        <path d="M 1 1.5 L 8.5 5 L 1 8.5 z" fill="context-stroke" stroke="none"/>
      </marker>
    </defs>
    <rect class="fig-box" x="16" y="16" width="332" height="246" rx="8"/>
    <rect class="fig-box-subtle" x="362" y="16" width="322" height="246" rx="8"/>
    <g class="fig-line">
      <line x1="62" y1="116" x2="90" y2="80" marker-end="url(#ds40-w3-arr-8)"/>
      <line x1="100" y1="116" x2="100" y2="83" marker-end="url(#ds40-w3-arr-8)"/>
      <line x1="62" y1="160" x2="62" y2="142" marker-end="url(#ds40-w3-arr-8)"/>
      <line x1="250" y1="120" x2="220" y2="82" marker-end="url(#ds40-w3-arr-8)"/>
      <line x1="426" y1="106" x2="426" y2="79" marker-end="url(#ds40-w3-arr-8)"/>
      <line x1="426" y1="150" x2="426" y2="133" marker-end="url(#ds40-w3-arr-8)"/>
      <line x1="426" y1="194" x2="426" y2="177" marker-end="url(#ds40-w3-arr-8)"/>
    </g>
    <path class="fig-line" stroke="var(--sl-color-orange)" d="M 196 68 Q 156 54 116 66" marker-end="url(#ds40-w3-arr-8)"/>
    <g class="fig-line" stroke="var(--sl-color-green)">
      <line x1="546" y1="138" x2="584" y2="82" marker-end="url(#ds40-w3-arr-8)"/>
      <line x1="594" y1="138" x2="594" y2="84" marker-end="url(#ds40-w3-arr-8)"/>
      <line x1="642" y1="138" x2="604" y2="82" marker-end="url(#ds40-w3-arr-8)"/>
    </g>
    <line class="fig-line-accent" x1="464" y1="132" x2="512" y2="132" marker-end="url(#ds40-w3-arr-8)"/>
    <circle class="fig-box-orange" cx="100" cy="68" r="14"/>
    <circle class="fig-box-blue" cx="210" cy="68" r="13"/>
    <g class="fig-box">
      <circle cx="62" cy="128" r="12"/><circle cx="100" cy="128" r="12"/>
      <circle cx="62" cy="172" r="12"/><circle cx="250" cy="128" r="12"/>
      <circle cx="426" cy="118" r="12"/><circle cx="426" cy="162" r="12"/>
    </g>
    <circle class="fig-box-orange" cx="426" cy="64" r="13"/>
    <circle class="fig-box-red" cx="426" cy="206" r="12"/>
    <circle class="fig-box-orange" cx="594" cy="68" r="14"/>
    <g class="fig-box-green">
      <circle cx="546" cy="150" r="12"/><circle cx="594" cy="150" r="12"/><circle cx="642" cy="150" r="12"/>
    </g>
    <g class="fig-box-subtle">
      <rect x="44" y="210" width="42" height="24"/><rect x="86" y="210" width="42" height="24"/>
      <rect x="128" y="210" width="42" height="24"/><rect x="170" y="210" width="42" height="24"/>
      <rect x="212" y="210" width="42" height="24"/><rect x="254" y="210" width="42" height="24"/>
    </g>
    <rect class="fig-box-orange" x="44" y="210" width="42" height="24"/>
    <rect class="fig-box-blue" x="212" y="210" width="42" height="24"/>
    <rect class="fig-box-accent" x="478" y="188" width="192" height="58" rx="6"/>
    <text class="fig-title" x="182" y="36" text-anchor="middle" font-size="12">① Union 按规模合并（小树挂入大树根）</text>
    <text class="fig-orange" x="156" y="54" text-anchor="middle" font-size="10.5">小树根4 挂向 大树根0</text>
    <text class="fig-muted" x="292" y="72" text-anchor="middle" font-size="10.5">原 S[4]=-2</text>
    <text class="fig-muted" x="292" y="90" text-anchor="middle" font-size="10.5">现 S[4]=0</text>
    <text class="fig-muted" x="292" y="108" text-anchor="middle" font-size="10.5">S[0]=-6</text>
    <g class="fig-label" font-size="11" text-anchor="middle">
      <text x="100" y="72">0</text><text x="62" y="132">1</text><text x="100" y="132">2</text><text x="62" y="176">3</text>
      <text x="210" y="72">4</text><text x="250" y="132">5</text>
      <text x="426" y="68">0</text><text x="426" y="122">1</text><text x="426" y="166">3</text><text x="426" y="210">6</text>
      <text x="594" y="72">0</text><text x="546" y="154">1</text><text x="594" y="154">3</text><text x="642" y="154">6</text>
      <text x="65" y="226">-6</text><text x="107" y="226">0</text><text x="149" y="226">0</text>
      <text x="191" y="226">1</text><text x="233" y="226">0</text><text x="275" y="226">4</text>
    </g>
    <g class="fig-muted" font-size="10" text-anchor="middle">
      <text x="65" y="248">S[0]</text><text x="107" y="248">S[1]</text><text x="149" y="248">S[2]</text>
      <text x="191" y="248">S[3]</text><text x="233" y="248">S[4]</text><text x="275" y="248">S[5]</text>
    </g>
    <text class="fig-title" x="523" y="36" text-anchor="middle" font-size="12">② Find(6) 路径压缩（沿途全改挂根）</text>
    <text class="fig-accent" x="488" y="122" text-anchor="middle" font-size="10.5">压缩</text>
    <text class="fig-muted" x="426" y="236" text-anchor="middle" font-size="10.5">退化链 O(n)</text>
    <text class="fig-accent" x="574" y="208" text-anchor="middle" font-size="11">树高控制在 O(log n) 以内</text>
    <text class="fig-accent" x="574" y="226" text-anchor="middle" font-size="11">两者结合均摊复杂度 O(α(n))</text>
    <text class="fig-muted" x="574" y="241" text-anchor="middle" font-size="10">根存负规模：|S[root]| = 集合元素数</text>
  </svg>
  <figcaption>并查集双亲数组表示法（根存负结点数）及“按规模合并 + Find 路径压缩”双重复杂度优化</figcaption>
</figure>

### 哈夫曼树

**带权路径长度 WPL**：每个叶子的权值乘以它到根的路径长度，再全部相加。

$$
\text{WPL}=\sum_{i=1}^{n} w_i \times l_i
$$

WPL 最小的二叉树叫**哈夫曼树**（最优二叉树）。

**构造**：每次选出权值最小的两个结点合并成新树，新树权值为两者之和，重复直到只剩一棵树。

> **★考点**：WPL 的快速算法 = **所有合并出来的新结点权值之和**。例：叶子权值 $2,3,6,7,9$，合并出 $5,11,16,27$，所以
> $$
> \text{WPL}=5+11+16+27=59
> $$

**哈夫曼编码**：左分支记 0，右分支记 1，从根到叶子的路径就是该字符的编码。

- 出现次数越多的字符编码越短
- 任何一个编码都不是另一个编码的前缀（**前缀编码**），解码不会产生歧义
- 例：`1100001` 依次译为 `A E C`

> **★考点**：$n$ 个叶子的哈夫曼树共有 $2n-1$ 个结点，**不存在度为 1 的结点**；哈夫曼树可能不唯一，但最小 WPL 相同。

<figure class="cpd-figure not-content" id="ds40-img4-fig-11">
  <svg viewBox="0 0 700 248" role="img" aria-label="权值 2、3、6、7、9 的哈夫曼树构造过程、WPL=59 与前缀编码">
    <rect class="fig-box-subtle" x="16" y="14" width="668" height="220" rx="8"/>
    <rect class="fig-box" x="28" y="44" width="332" height="176" rx="6"/>
    <rect class="fig-box" x="372" y="44" width="300" height="176" rx="6"/>
    <g class="fig-line" stroke="var(--sl-color-blue)">
      <line x1="196" y1="68" x2="128" y2="108"/>
      <line x1="128" y1="108" x2="88" y2="150"/>
      <line x1="264" y1="108" x2="226" y2="150"/>
      <line x1="88" y1="150" x2="58" y2="192"/>
    </g>
    <g class="fig-line" stroke="var(--sl-color-purple)">
      <line x1="196" y1="68" x2="264" y2="108"/>
      <line x1="128" y1="108" x2="166" y2="150"/>
      <line x1="264" y1="108" x2="302" y2="150"/>
      <line x1="88" y1="150" x2="118" y2="192"/>
    </g>
    <g class="fig-box-orange">
      <circle cx="196" cy="68" r="14"/>
      <circle cx="128" cy="108" r="14"/>
      <circle cx="264" cy="108" r="14"/>
      <circle cx="88" cy="150" r="14"/>
    </g>
    <g class="fig-box-green">
      <circle cx="166" cy="150" r="14"/>
      <circle cx="226" cy="150" r="14"/>
      <circle cx="302" cy="150" r="14"/>
      <circle cx="58" cy="192" r="14"/>
      <circle cx="118" cy="192" r="14"/>
    </g>
    <rect class="fig-box-orange" x="384" y="68" width="276" height="58" rx="4"/>
    <rect class="fig-box-blue" x="384" y="132" width="276" height="76" rx="4"/>
    <text class="fig-title" x="350" y="34" font-size="12.5" text-anchor="middle">图 31：哈夫曼树构造与前缀编码（叶子权值：2, 3, 6, 7, 9）</text>
    <g class="fig-label" font-size="11" text-anchor="middle">
      <text x="196" y="72">27</text>
      <text x="128" y="112">11</text>
      <text x="264" y="112">16</text>
      <text x="88" y="154">5</text>
      <text x="166" y="154">6</text>
      <text x="226" y="154">7</text>
      <text x="302" y="154">9</text>
      <text x="58" y="196">2</text>
      <text x="118" y="196">3</text>
    </g>
    <g class="fig-blue" font-size="9.5" text-anchor="middle">
      <text x="154" y="84">0</text>
      <text x="102" y="126">0</text>
      <text x="238" y="126">0</text>
      <text x="66" y="168">0</text>
    </g>
    <g class="fig-purple" font-size="9.5" text-anchor="middle">
      <text x="238" y="84">1</text>
      <text x="154" y="126">1</text>
      <text x="290" y="126">1</text>
      <text x="110" y="168">1</text>
    </g>
    <text class="fig-muted" x="194" y="213" font-size="9.5" text-anchor="middle">左分支 = 0，右分支 = 1；合并 4 次得 4 个橙色非叶结点</text>
    <text class="fig-orange" x="522" y="60" font-size="11" text-anchor="middle">① WPL 快速计算 = 非叶结点之和</text>
    <text class="fig-label" x="396" y="85" font-size="10">合并：2+3→5，5+6→11，7+9→16，11+16→27</text>
    <text class="fig-orange" x="396" y="103" font-size="10.5">WPL = 5 + 11 + 16 + 27 = 59</text>
    <text class="fig-muted" x="396" y="119" font-size="9.5">验算：(2+3)×3 + (6+7+9)×2 = 15 + 44 = 59</text>
    <text class="fig-blue" x="396" y="149" font-size="10.5">② 前缀编码（左 0 右 1，任一编码非他码前缀）</text>
    <text class="fig-label" x="396" y="167" font-size="10">9 → 11 ， 7 → 10 ， 6 → 01（高频短码）</text>
    <text class="fig-label" x="396" y="183" font-size="10">3 → 001 ， 2 → 000（低频长码）</text>
    <text class="fig-muted" x="396" y="199" font-size="9.5">例：比特流 1100001 → 11 | 000 | 01 → A E C</text>
  </svg>
  <figcaption>图 31：权值 2、3、6、7、9 的哈夫曼树；WPL = 59，左侧为前缀编码</figcaption>
</figure>

<figure class="cpd-figure not-content" id="ds40-w3-fig-9">
  <svg viewBox="0 0 700 324" role="img" aria-label="哈夫曼树四步贪心合并过程、WPL两种求法对比、左0右1前缀编码与1100001无歧义译码">
    <rect class="fig-box-subtle" x="16" y="16" width="356" height="292" rx="8"/>
    <rect class="fig-box" x="386" y="16" width="298" height="292" rx="8"/>
    <line class="fig-divider" x1="402" y1="120" x2="668" y2="120"/>
    <line class="fig-divider" x1="402" y1="226" x2="668" y2="226"/>
    <g class="fig-line" stroke="var(--sl-color-blue)">
      <line x1="206" y1="64" x2="128" y2="122"/>
      <line x1="128" y1="122" x2="84" y2="182"/>
      <line x1="284" y1="122" x2="244" y2="182"/>
      <line x1="84" y1="182" x2="52" y2="244"/>
    </g>
    <g class="fig-line" stroke="var(--sl-color-purple)">
      <line x1="206" y1="64" x2="284" y2="122"/>
      <line x1="128" y1="122" x2="168" y2="182"/>
      <line x1="284" y1="122" x2="324" y2="182"/>
      <line x1="84" y1="182" x2="116" y2="244"/>
    </g>
    <g class="fig-box" rx="3">
      <rect x="157" y="82" width="16" height="16" rx="3"/><rect x="239" y="82" width="16" height="16" rx="3"/>
      <rect x="96" y="142" width="16" height="16" rx="3"/><rect x="142" y="142" width="16" height="16" rx="3"/>
      <rect x="254" y="142" width="16" height="16" rx="3"/><rect x="298" y="142" width="16" height="16" rx="3"/>
      <rect x="58" y="204" width="16" height="16" rx="3"/><rect x="94" y="204" width="16" height="16" rx="3"/>
    </g>
    <g class="fig-box-orange">
      <circle cx="206" cy="64" r="16"/>
      <circle cx="128" cy="122" r="16"/><circle cx="284" cy="122" r="16"/>
      <circle cx="84" cy="182" r="15"/>
      <rect x="402" y="86" width="266" height="24" rx="4"/>
    </g>
    <g class="fig-box-green">
      <circle cx="168" cy="182" r="16"/><circle cx="244" cy="182" r="16"/><circle cx="324" cy="182" r="16"/>
      <circle cx="52" cy="244" r="16"/><circle cx="116" cy="244" r="16"/>
    </g>
    <rect class="fig-box-blue" x="458" y="188" width="60" height="24" rx="4"/>
    <rect class="fig-box-green" x="524" y="188" width="68" height="24" rx="4"/>
    <rect class="fig-box-purple" x="598" y="188" width="60" height="24" rx="4"/>
    <rect class="fig-box-accent" x="402" y="238" width="266" height="56" rx="5"/>
    <text class="fig-title" x="194" y="36" text-anchor="middle" font-size="12">哈夫曼树构造与左0右1编码（n=5 叶）</text>
    <g class="fig-blue" font-size="10.5" text-anchor="middle">
      <text x="165" y="94">0</text><text x="104" y="154">0</text><text x="262" y="154">0</text><text x="66" y="216">0</text>
    </g>
    <g class="fig-purple" font-size="10.5" text-anchor="middle">
      <text x="247" y="94">1</text><text x="150" y="154">1</text><text x="306" y="154">1</text><text x="102" y="216">1</text>
    </g>
    <g class="fig-label" font-size="11" text-anchor="middle">
      <text x="206" y="68">27</text>
      <text x="128" y="126">11</text><text x="284" y="126">16</text>
      <text x="84" y="186">5</text>
      <text x="168" y="186">C:6</text><text x="244" y="186">B:7</text><text x="324" y="186">A:9</text>
      <text x="52" y="248">E:2</text><text x="116" y="248">D:3</text>
    </g>
    <g class="fig-orange" font-size="10">
      <text x="226" y="58">④合</text><text x="95" y="114">②合</text><text x="304" y="114">③合</text><text x="53" y="176">①合</text>
    </g>
    <g class="fig-muted" font-size="10.5" text-anchor="middle">
      <text x="168" y="210">01</text><text x="244" y="210">10</text><text x="324" y="210">11</text>
      <text x="52" y="272">000</text><text x="116" y="272">001</text>
      <text x="236" y="292">绿：初始叶子 ｜ 橙：合并出的 4 个新结点</text>
    </g>
    <text class="fig-title" x="402" y="38" font-size="12">① 带权路径长度 WPL 两种求法</text>
    <text class="fig-label" x="402" y="58" font-size="11">• 按叶累加：(9+7+6)×2 + (2+3)×3 = 59</text>
    <text class="fig-muted" x="402" y="76" font-size="10.5">• 按非叶结点权值累加（考场秒杀法）：</text>
    <text class="fig-orange" x="535" y="102" text-anchor="middle" font-size="11.5">WPL = 5 + 11 + 16 + 27 = 59</text>
    <text class="fig-title" x="402" y="140" font-size="12">② 前缀编码与比特流无歧义译码</text>
    <text class="fig-label" x="402" y="160" font-size="11">高频短码：A=11, B=10, C=01；低频长码：D=001, E=000</text>
    <text class="fig-muted" x="402" y="178" font-size="10.5">任一字符均位于叶结点 ⇒ 绝非另一编码前缀</text>
    <text class="fig-muted" x="402" y="204" font-size="11">译码串：</text>
    <text class="fig-blue" x="488" y="204" text-anchor="middle" font-size="11">11 → A</text>
    <text class="fig-green" x="558" y="204" text-anchor="middle" font-size="11">000 → E</text>
    <text class="fig-purple" x="628" y="204" text-anchor="middle" font-size="11">01 → C</text>
    <text class="fig-accent" x="535" y="258" text-anchor="middle" font-size="11">★ n 个叶子必合并 n-1 次，总结点 = 2n - 1 (9个)</text>
    <text class="fig-accent" x="535" y="278" text-anchor="middle" font-size="11">★ 度为 1 结点数 n₁ = 0；最小 WPL 唯一确定</text>
  </svg>
  <figcaption>权值 {2, 3, 6, 7, 9} 的哈夫曼树四步合并、WPL 双算法验证与“1100001 → A E C”前缀译码</figcaption>
</figure>

## 十、图的存储与遍历

### 基本概念

图 $G=(V,E)$：顶点集 $V$ + 边集 $E$。

- 边无方向 → 无向图；边有方向 → 有向图，有向边也叫**弧**
- 无向图顶点的度 = 相连的边数；有向图分**入度**和**出度**

> **★考点**：所有顶点的度数之和 = 边数的 2 倍，因为每条边贡献两个度。

- $n$ 个顶点的无向完全图最多 $n(n-1)/2$ 条边
- $n$ 个顶点的有向完全图最多 $n(n-1)$ 条弧
- 任意两点都有路径：无向图叫**连通图**，有向图叫**强连通图**

> **★考点**：$n$ 个顶点的连通图至少 $n-1$ 条边（此时是一棵树）；强连通图至少 $n$ 条边（$n \ge 2$，此时构成一个环）。$n=1$ 时强连通图需要 0 条边，是公式的边界例外。

<figure class="cpd-figure not-content" id="ds40-w4-fig-1">
  <svg viewBox="0 0 700 316" role="img" aria-label="无向图度数握手定理、有向图出入度守恒与强连通分量SCC、以及n顶点图极值边数汇总">
    <defs>
      <marker id="ds40-w4-arr-1" viewBox="0 0 10 10" refX="8.5" refY="5" markerWidth="6.5" markerHeight="6.5" markerUnits="userSpaceOnUse" orient="auto-start-reverse">
        <path d="M 1 1.5 L 8.5 5 L 1 8.5 z" fill="context-stroke" stroke="none"/>
      </marker>
    </defs>
    <rect class="fig-box-subtle" x="16" y="16" width="330" height="150" rx="8"/>
    <rect class="fig-box-subtle" x="356" y="16" width="328" height="150" rx="8"/>
    <rect class="fig-box" x="16" y="178" width="668" height="122" rx="8"/>
    <rect class="fig-box-dashed" x="370" y="48" width="114" height="106" rx="8"/>
    <rect class="fig-box-dashed" x="494" y="76" width="52" height="58" rx="8"/>
    <g class="fig-line" stroke="var(--sl-color-blue)" stroke-width="1.6">
      <line x1="58" y1="74" x2="128" y2="74"/>
      <line x1="58" y1="74" x2="58" y2="132"/>
      <line x1="58" y1="74" x2="128" y2="132"/>
      <line x1="128" y1="74" x2="58" y2="132"/>
    </g>
    <g class="fig-line" stroke="var(--sl-color-green)" stroke-width="1.6" marker-end="url(#ds40-w4-arr-1)">
      <line x1="412" y1="72" x2="448" y2="72"/>
      <line x1="455" y1="84" x2="436" y2="120"/>
      <line x1="421" y1="120" x2="403" y2="85"/>
    </g>
    <line class="fig-line" stroke="var(--sl-color-orange)" stroke-width="1.6" x1="476" y1="77" x2="507" y2="96" marker-end="url(#ds40-w4-arr-1)"/>
    <g class="fig-box-blue">
      <circle cx="58" cy="74" r="14"/><circle cx="128" cy="74" r="14"/>
      <circle cx="58" cy="132" r="14"/><circle cx="128" cy="132" r="14"/>
    </g>
    <g class="fig-box-green">
      <circle cx="398" cy="72" r="14"/><circle cx="462" cy="72" r="14"/><circle cx="428" cy="132" r="14"/>
    </g>
    <circle class="fig-box-orange" cx="520" cy="105" r="14"/>
    <rect class="fig-box-green" x="28" y="212" width="154" height="74" rx="6"/>
    <rect class="fig-box-orange" x="190" y="212" width="164" height="74" rx="6"/>
    <rect class="fig-box-blue" x="362" y="212" width="154" height="74" rx="6"/>
    <rect class="fig-box-purple" x="524" y="212" width="148" height="74" rx="6"/>
    <text class="fig-title" x="181" y="36" text-anchor="middle" font-size="12">无向图握手定理与连通性</text>
    <text class="fig-title" x="520" y="36" text-anchor="middle" font-size="12">有向图出入度与强连通分量（SCC）</text>
    <g class="fig-blue" font-size="11" text-anchor="middle">
      <text x="58" y="78">A</text><text x="128" y="78">B</text>
      <text x="58" y="136">C</text><text x="128" y="136">D</text>
    </g>
    <g class="fig-muted" font-size="9.5" text-anchor="middle">
      <text x="34" y="64">d=3</text><text x="152" y="64">d=2</text>
      <text x="34" y="148">d=2</text><text x="152" y="148">d=1</text>
    </g>
    <text class="fig-label" x="174" y="70" font-size="11">∑TD(v) = 3+2+2+1 = 8</text>
    <text class="fig-accent" x="174" y="90" font-size="11">= 2|E|（|E|=4，每边献2度）</text>
    <text class="fig-muted" x="174" y="114" font-size="10.5">• 连通分量 = 极大连通子图</text>
    <text class="fig-muted" x="174" y="132" font-size="10.5">• 连通图只有 1 个连通分量</text>
    <text class="fig-muted" x="174" y="150" font-size="10.5">• 生成树 = 极小连通子图(n-1边)</text>
    <g class="fig-green" font-size="10.5" text-anchor="middle">
      <text x="398" y="76">V1</text><text x="462" y="76">V2</text><text x="428" y="136">V3</text>
    </g>
    <text class="fig-orange" x="520" y="109" text-anchor="middle" font-size="10.5">V4</text>
    <text class="fig-green" x="427" y="60" text-anchor="middle" font-size="9.5">SCC₁(互达)</text>
    <text class="fig-orange" x="520" y="146" text-anchor="middle" font-size="9.5">SCC₂</text>
    <text class="fig-label" x="554" y="68" font-size="11">∑ID = ∑OD = |E| = 4</text>
    <text class="fig-green" x="554" y="90" font-size="10.5">{V1,V2,V3} 双向可达</text>
    <text class="fig-orange" x="554" y="110" font-size="10.5">V2→V4 单向无回边</text>
    <text class="fig-muted" x="554" y="132" font-size="10.5">共 2 个强连通分量</text>
    <text class="fig-muted" x="554" y="150" font-size="10.5">(极大强连通子图)</text>
    <text class="fig-title" x="350" y="198" text-anchor="middle" font-size="12">408 高频秒杀：n 个顶点的简单图边数临界值汇总</text>
    <text class="fig-green" x="105" y="232" text-anchor="middle" font-size="11">无向连通最少边</text>
    <text class="fig-label" x="105" y="254" text-anchor="middle" font-size="12">n - 1 条（树）</text>
    <text class="fig-muted" x="105" y="274" text-anchor="middle" font-size="10">&lt; n-1 必非连通</text>
    <text class="fig-orange" x="272" y="232" text-anchor="middle" font-size="11">确保无向连通最少边</text>
    <text class="fig-label" x="272" y="254" text-anchor="middle" font-size="11.5">(n-1)(n-2)/2 + 1</text>
    <text class="fig-muted" x="272" y="274" text-anchor="middle" font-size="10">n-1点完全图再连第n点</text>
    <text class="fig-blue" x="439" y="232" text-anchor="middle" font-size="11">有向强连通最少弧</text>
    <text class="fig-label" x="439" y="254" text-anchor="middle" font-size="12">n 条（单向环）</text>
    <text class="fig-muted" x="439" y="274" text-anchor="middle" font-size="10">n≥2；n=1 例外需 0 条</text>
    <text class="fig-purple" x="598" y="232" text-anchor="middle" font-size="11">完全图最大边/弧</text>
    <text class="fig-label" x="598" y="253" text-anchor="middle" font-size="11">无向 n(n-1)/2</text>
    <text class="fig-muted" x="598" y="274" text-anchor="middle" font-size="10.5">有向 n(n-1) 条弧</text>
  </svg>
  <figcaption>图的基本性质：握手定理、极大连通（强连通）子图判定与四类极值边数公式</figcaption>
</figure>

### 邻接矩阵与邻接表

| 存储方式 | 做法 | 空间 | 适用 |
|:--------:|:-----|:----:|:----:|
| 邻接矩阵 | $n \times n$ 二维数组，`a[i][j]=1` 表示有边 | $O(n^2)$ | 稠密图 |
| 邻接表 | 每个顶点挂一个链表，存所有邻居 | $O(n+e)$ | 稀疏图 |

无向图的邻接矩阵对称；邻接表的表示不唯一，所以遍历序列也可能不唯一。

<figure class="cpd-figure not-content" id="ds40-w4-fig-2">
  <svg viewBox="0 0 700 250" role="img" aria-label="同一张4顶点无向图的邻接矩阵对称表示与邻接表链表表示映射对照">
    <defs>
      <marker id="ds40-w4-arr-2" viewBox="0 0 10 10" refX="8.5" refY="5" markerWidth="6" markerHeight="6" markerUnits="userSpaceOnUse" orient="auto-start-reverse">
        <path d="M 1 1.5 L 8.5 5 L 1 8.5 z" fill="context-stroke" stroke="none"/>
      </marker>
    </defs>
    <rect class="fig-box-subtle" x="16" y="16" width="186" height="218" rx="8"/>
    <rect class="fig-box-subtle" x="212" y="16" width="206" height="218" rx="8"/>
    <rect class="fig-box-subtle" x="428" y="16" width="256" height="218" rx="8"/>
    <g class="fig-line" stroke="var(--sl-color-blue)" stroke-width="1.8">
      <line x1="70" y1="84" x2="148" y2="84"/>
      <line x1="70" y1="84" x2="148" y2="158"/>
      <line x1="70" y1="84" x2="70" y2="158"/>
      <line x1="148" y1="84" x2="148" y2="158"/>
    </g>
    <g class="fig-box-blue">
      <circle cx="70" cy="84" r="15"/><circle cx="148" cy="84" r="15"/>
      <circle cx="148" cy="158" r="15"/><circle cx="70" cy="158" r="15"/>
    </g>
    <rect class="fig-box" x="256" y="72" width="120" height="104" rx="4"/>
    <rect class="fig-box-accent" x="256" y="72" width="30" height="26"/>
    <rect class="fig-box-accent" x="286" y="98" width="30" height="26"/>
    <rect class="fig-box-accent" x="316" y="124" width="30" height="26"/>
    <rect class="fig-box-accent" x="346" y="150" width="30" height="26"/>
    <g class="fig-divider">
      <line x1="286" y1="72" x2="286" y2="176"/><line x1="316" y1="72" x2="316" y2="176"/><line x1="346" y1="72" x2="346" y2="176"/>
      <line x1="256" y1="98" x2="376" y2="98"/><line x1="256" y1="124" x2="376" y2="124"/><line x1="256" y1="150" x2="376" y2="150"/>
    </g>
    <g class="fig-line" stroke="var(--sl-color-blue)" marker-end="url(#ds40-w4-arr-2)">
      <line x1="484" y1="69" x2="500" y2="69"/><line x1="540" y1="69" x2="556" y2="69"/><line x1="596" y1="69" x2="612" y2="69"/>
      <line x1="484" y1="101" x2="500" y2="101"/><line x1="540" y1="101" x2="556" y2="101"/>
      <line x1="484" y1="133" x2="500" y2="133"/><line x1="540" y1="133" x2="556" y2="133"/>
      <line x1="484" y1="165" x2="500" y2="165"/>
    </g>
    <g class="fig-box-blue">
      <rect x="440" y="58" width="44" height="22" rx="3"/>
      <rect x="440" y="90" width="44" height="22" rx="3"/>
      <rect x="440" y="122" width="44" height="22" rx="3"/>
      <rect x="440" y="154" width="44" height="22" rx="3"/>
    </g>
    <g class="fig-box">
      <rect x="502" y="58" width="38" height="22" rx="3"/><rect x="558" y="58" width="38" height="22" rx="3"/><rect x="614" y="58" width="44" height="22" rx="3"/>
      <rect x="502" y="90" width="38" height="22" rx="3"/><rect x="558" y="90" width="44" height="22" rx="3"/>
      <rect x="502" y="122" width="38" height="22" rx="3"/><rect x="558" y="122" width="44" height="22" rx="3"/>
      <rect x="502" y="154" width="44" height="22" rx="3"/>
    </g>
    <text class="fig-title" x="109" y="38" text-anchor="middle" font-size="12">无向图 G（4 点 4 边）</text>
    <text class="fig-title" x="315" y="38" text-anchor="middle" font-size="12">邻接矩阵（对称）</text>
    <text class="fig-title" x="556" y="38" text-anchor="middle" font-size="12">邻接表（头结点 + 边链表）</text>
    <g class="fig-blue" font-size="11" text-anchor="middle">
      <text x="70" y="88">V0</text><text x="148" y="88">V1</text>
      <text x="148" y="162">V2</text><text x="70" y="162">V3</text>
    </g>
    <text class="fig-label" x="109" y="202" text-anchor="middle" font-size="10.5">|V| = 4，|E| = 4（d(V0)=3）</text>
    <text class="fig-muted" x="109" y="220" text-anchor="middle" font-size="10">图确定则矩阵唯一，表不唯一</text>
    <g class="fig-muted" font-size="10" text-anchor="middle">
      <text x="271" y="64">V0</text><text x="301" y="64">V1</text><text x="331" y="64">V2</text><text x="361" y="64">V3</text>
      <text x="241" y="89">V0</text><text x="241" y="115">V1</text><text x="241" y="141">V2</text><text x="241" y="167">V3</text>
    </g>
    <g class="fig-muted" font-size="11" text-anchor="middle">
      <text x="271" y="89">0</text><text x="301" y="115">0</text><text x="331" y="141">0</text><text x="361" y="167">0</text>
      <text x="361" y="115">0</text><text x="361" y="141">0</text>
      <text x="301" y="167">0</text><text x="331" y="167">0</text>
    </g>
    <g class="fig-accent" font-size="11" text-anchor="middle">
      <text x="301" y="89">1</text><text x="331" y="89">1</text><text x="361" y="89">1</text>
      <text x="271" y="115">1</text><text x="331" y="115">1</text>
      <text x="271" y="141">1</text><text x="301" y="141">1</text>
      <text x="271" y="167">1</text>
    </g>
    <text class="fig-label" x="315" y="202" text-anchor="middle" font-size="10.5">行 i 和 = 度 d(Vi)，空间 O(n²)</text>
    <text class="fig-muted" x="315" y="220" text-anchor="middle" font-size="10">对称矩阵可只存上/下三角</text>
    <g class="fig-blue" font-size="10.5" text-anchor="middle">
      <text x="462" y="73">V0 | •</text><text x="462" y="105">V1 | •</text>
      <text x="462" y="137">V2 | •</text><text x="462" y="169">V3 | •</text>
    </g>
    <g class="fig-label" font-size="10.5" text-anchor="middle">
      <text x="521" y="73">1 | •</text><text x="577" y="73">2 | •</text><text x="636" y="73">3 | ∧</text>
      <text x="521" y="105">0 | •</text><text x="580" y="105">2 | ∧</text>
      <text x="521" y="137">0 | •</text><text x="580" y="137">1 | ∧</text>
      <text x="524" y="169">0 | ∧</text>
    </g>
    <text class="fig-label" x="556" y="202" text-anchor="middle" font-size="10.5">每条无向边存 2 次 ⇒ 2|E| = 8 个边结点</text>
    <text class="fig-muted" x="556" y="220" text-anchor="middle" font-size="10">单链内邻居次序任意 ⇒ 遍历序列不唯一</text>
  </svg>
  <figcaption>同一张无向图的邻接矩阵（对称二维数组）与邻接表（2|E| 个边结点）映射对照</figcaption>
</figure>

<figure class="cpd-figure not-content" id="ds40-w4-fig-3">
  <svg viewBox="0 0 700 344" role="img" aria-label="图的四种存储结构对比：邻接矩阵、邻接表、有向图十字链表与无向图邻接多重表">
    <defs>
      <marker id="ds40-w4-arr-3" viewBox="0 0 10 10" refX="8.5" refY="5" markerWidth="6" markerHeight="6" markerUnits="userSpaceOnUse" orient="auto-start-reverse">
        <path d="M 1 1.5 L 8.5 5 L 1 8.5 z" fill="context-stroke" stroke="none"/>
      </marker>
    </defs>
    <rect class="fig-box-subtle" x="16" y="16" width="330" height="152" rx="8"/>
    <rect class="fig-box-subtle" x="356" y="16" width="328" height="152" rx="8"/>
    <rect class="fig-box-subtle" x="16" y="180" width="330" height="148" rx="8"/>
    <rect class="fig-box-subtle" x="356" y="180" width="328" height="148" rx="8"/>
    <rect class="fig-box" x="42" y="58" width="104" height="88" rx="4"/>
    <g class="fig-divider">
      <line x1="68" y1="58" x2="68" y2="146"/><line x1="94" y1="58" x2="94" y2="146"/><line x1="120" y1="58" x2="120" y2="146"/>
      <line x1="42" y1="80" x2="146" y2="80"/><line x1="42" y1="102" x2="146" y2="102"/><line x1="42" y1="124" x2="146" y2="124"/>
    </g>
    <g class="fig-line" stroke="var(--sl-color-blue)" marker-end="url(#ds40-w4-arr-3)">
      <line x1="420" y1="62" x2="442" y2="62"/><line x1="490" y1="62" x2="512" y2="62"/>
      <line x1="420" y1="88" x2="442" y2="88"/>
      <line x1="420" y1="114" x2="442" y2="114"/><line x1="490" y1="114" x2="512" y2="114"/>
    </g>
    <g class="fig-box-blue">
      <rect x="370" y="51" width="50" height="22" rx="3"/><rect x="370" y="77" width="50" height="22" rx="3"/>
      <rect x="370" y="103" width="50" height="22" rx="3"/><rect x="370" y="129" width="50" height="22" rx="3"/>
    </g>
    <g class="fig-box">
      <rect x="444" y="51" width="46" height="22" rx="3"/><rect x="514" y="51" width="46" height="22" rx="3"/>
      <rect x="444" y="77" width="46" height="22" rx="3"/>
      <rect x="444" y="103" width="46" height="22" rx="3"/><rect x="514" y="103" width="46" height="22" rx="3"/>
    </g>
    <rect class="fig-box-green" x="28" y="212" width="186" height="28" rx="4"/>
    <rect class="fig-box-accent" x="28" y="250" width="248" height="28" rx="4"/>
    <g class="fig-divider">
      <line x1="76" y1="212" x2="76" y2="240"/><line x1="142" y1="212" x2="142" y2="240"/>
      <line x1="90" y1="250" x2="90" y2="278"/><line x1="156" y1="250" x2="156" y2="278"/><line x1="216" y1="250" x2="216" y2="278"/>
    </g>
    <rect class="fig-box-orange" x="368" y="212" width="144" height="28" rx="4"/>
    <rect class="fig-box-purple" x="368" y="250" width="244" height="28" rx="4"/>
    <g class="fig-divider">
      <line x1="424" y1="212" x2="424" y2="240"/>
      <line x1="428" y1="250" x2="428" y2="278"/><line x1="490" y1="250" x2="490" y2="278"/><line x1="550" y1="250" x2="550" y2="278"/>
    </g>
    <text class="fig-title" x="181" y="36" text-anchor="middle" font-size="12">① 邻接矩阵 O(|V|²) · 稠密图 · 唯一</text>
    <text class="fig-title" x="520" y="36" text-anchor="middle" font-size="12">② 邻接表 O(|V|+|E|) · 稀疏图 · 不唯一</text>
    <g class="fig-muted" font-size="10" text-anchor="middle">
      <text x="55" y="53">0</text><text x="81" y="53">1</text><text x="107" y="53">2</text><text x="133" y="53">3</text>
      <text x="32" y="73">0</text><text x="32" y="95">1</text><text x="32" y="117">2</text><text x="32" y="139">3</text>
    </g>
    <g class="fig-muted" font-size="11" text-anchor="middle">
      <text x="55" y="73">0</text><text x="133" y="73">0</text>
      <text x="55" y="95">0</text><text x="81" y="95">0</text><text x="133" y="95">0</text>
      <text x="81" y="117">0</text><text x="107" y="117">0</text>
      <text x="55" y="139">0</text><text x="81" y="139">0</text><text x="107" y="139">0</text><text x="133" y="139">0</text>
    </g>
    <g class="fig-accent" font-size="11" text-anchor="middle">
      <text x="81" y="73">1</text><text x="107" y="73">1</text>
      <text x="107" y="95">1</text>
      <text x="55" y="117">1</text><text x="133" y="117">1</text>
    </g>
    <text class="fig-label" x="158" y="68" font-size="10.5">• 无向图矩阵对称，可上三角压缩</text>
    <text class="fig-label" x="158" y="88" font-size="10.5">• 有向图：行 i 和=出度，列 j 和=入度</text>
    <text class="fig-label" x="158" y="108" font-size="10.5">• 查任意两点是否有边：O(1)</text>
    <text class="fig-accent" x="158" y="130" font-size="10.5">★ Aᵏ[i][j] = i 到 j 长度为 k 的路径数</text>
    <g class="fig-blue" font-size="10.5" text-anchor="middle">
      <text x="395" y="66">V0 | •</text><text x="395" y="92">V1 | •</text>
      <text x="395" y="118">V2 | •</text><text x="395" y="144">V3 | ∧</text>
    </g>
    <g class="fig-label" font-size="10.5" text-anchor="middle">
      <text x="467" y="66">1 | •</text><text x="537" y="66">2 | ∧</text>
      <text x="467" y="92">2 | ∧</text>
      <text x="467" y="118">0 | •</text><text x="537" y="118">3 | ∧</text>
    </g>
    <text class="fig-muted" x="568" y="68" font-size="10">边结点总数：</text>
    <text class="fig-label" x="568" y="85" font-size="10">无向 = 2|E|</text>
    <text class="fig-label" x="568" y="102" font-size="10">有向 = |E|</text>
    <text class="fig-orange" x="568" y="124" font-size="10">有向求入度慢</text>
    <text class="fig-muted" x="568" y="141" font-size="9.5">需扫全表或逆邻接表</text>
    <text class="fig-title" x="181" y="200" text-anchor="middle" font-size="12">③ 十字链表（仅存有向图 · O(|V|+|E|)）</text>
    <text class="fig-title" x="520" y="200" text-anchor="middle" font-size="12">④ 邻接多重表（仅存无向图 · O(|V|+|E|)）</text>
    <g class="fig-green" font-size="10.5" text-anchor="middle">
      <text x="52" y="230">data</text><text x="109" y="230">firstin</text><text x="178" y="230">firstout</text>
    </g>
    <text class="fig-muted" x="224" y="230" font-size="10">← 顶点结点</text>
    <g class="fig-accent" font-size="10.5" text-anchor="middle">
      <text x="59" y="268">tailvex</text><text x="123" y="268">headvex</text><text x="186" y="268">hlink</text><text x="246" y="268">tlink</text>
    </g>
    <text class="fig-muted" x="282" y="268" font-size="10">← 弧结点</text>
    <text class="fig-label" x="28" y="298" font-size="10.5">• hlink 串同弧头（入边），tlink 串同弧尾（出边）</text>
    <text class="fig-green" x="28" y="316" font-size="10.5">★ 求出度与入度均只需顺链扫描，无需逆邻接表</text>
    <g class="fig-orange" font-size="10.5" text-anchor="middle">
      <text x="396" y="230">data</text><text x="468" y="230">firstedge</text>
    </g>
    <text class="fig-muted" x="520" y="230" font-size="10">← 顶点结点</text>
    <g class="fig-purple" font-size="10.5" text-anchor="middle">
      <text x="398" y="268">ivex</text><text x="459" y="268">ilink</text><text x="520" y="268">jvex</text><text x="581" y="268">jlink</text>
    </g>
    <text class="fig-muted" x="618" y="268" font-size="10">← 边结点</text>
    <text class="fig-label" x="368" y="298" font-size="10.5">• 每条无向边只存 1 个边结点（邻接表需存 2 个）</text>
    <text class="fig-purple" x="368" y="316" font-size="10.5">★ 删边/标记访问边极快，不必寻找另一侧镜像结点</text>
  </svg>
  <figcaption>图的四种存储结构对比：矩阵适合稠密图，邻接表/十字链表（有向）/邻接多重表（无向）空间均为 O(|V|+|E|)</figcaption>
</figure>

### DFS 与 BFS

- **DFS（深度优先）**：一条路走到底，走不通再回退；类似树的先序遍历；用递归或栈
- **BFS（广度优先）**：先访问所有邻居，再访问邻居的邻居；类似层序遍历；用队列

图中可能有环，必须用 `visited[]` 标记已访问顶点，避免死循环。

> **★考点**：邻接矩阵实现：时间 $O(n^2)$、空间 $O(n)$；邻接表实现：时间 $O(n+e)$、空间 $O(n)$。邻接表表示不唯一，DFS/BFS 序列也可能不唯一。

<figure class="cpd-figure not-content" id="ds40-w4-fig-4">
  <svg viewBox="0 0 700 308" role="img" aria-label="BFS队列逐层入队出队快照与DFS递归调用栈深搜回溯轨迹">
    <rect class="fig-box-subtle" x="16" y="16" width="330" height="276" rx="8"/>
    <rect class="fig-box-subtle" x="356" y="16" width="328" height="276" rx="8"/>
    <g class="fig-box">
      <rect x="28" y="52" width="306" height="28" rx="4"/>
      <rect x="28" y="86" width="306" height="28" rx="4"/>
      <rect x="28" y="120" width="306" height="28" rx="4"/>
      <rect x="28" y="154" width="306" height="28" rx="4"/>
      <rect x="28" y="188" width="306" height="28" rx="4"/>
    </g>
    <rect class="fig-box-blue" x="28" y="222" width="306" height="28" rx="4"/>
    <rect class="fig-box-green" x="368" y="52" width="124" height="24" rx="4"/>
    <rect class="fig-box-orange" x="384" y="82" width="124" height="24" rx="4"/>
    <rect class="fig-box-orange" x="400" y="112" width="124" height="24" rx="4"/>
    <rect class="fig-box-orange" x="416" y="142" width="124" height="24" rx="4"/>
    <rect class="fig-box-orange" x="432" y="172" width="124" height="24" rx="4"/>
    <rect class="fig-box-orange" x="448" y="202" width="124" height="24" rx="4"/>
    <rect class="fig-box-purple" x="368" y="242" width="304" height="36" rx="5"/>
    <text class="fig-title" x="181" y="38" text-anchor="middle" font-size="12">BFS 队列状态演化（FIFO 逐层扩散）</text>
    <text class="fig-title" x="520" y="38" text-anchor="middle" font-size="12">DFS 调用栈压栈与回溯轨迹（LIFO 一路到底）</text>
    <g class="fig-label" font-size="10.5">
      <text x="38" y="70">① 出队 A → 访邻居 B, C</text>
      <text x="38" y="104">② 出队 B → 访未标邻居 D</text>
      <text x="38" y="138">③ 出队 C → 访未标邻居 E</text>
      <text x="38" y="172">④ 出队 D → 访未标邻居 F</text>
      <text x="38" y="206">⑤ 出队 E, F → 邻居均已 visited</text>
    </g>
    <g class="fig-blue" font-size="10.5" text-anchor="end">
      <text x="324" y="70">队: [B, C]</text>
      <text x="324" y="104">队: [C, D]</text>
      <text x="324" y="138">队: [D, E]</text>
      <text x="324" y="172">队: [E, F]</text>
      <text x="324" y="206">队: [F] → []</text>
    </g>
    <text class="fig-blue" x="181" y="240" text-anchor="middle" font-size="11">BFS 序列：A → B → C → D → E → F</text>
    <text class="fig-muted" x="181" y="268" text-anchor="middle" font-size="10">入队前立刻置 visited[w]=true，防止同一顶点重复入队</text>
    <text class="fig-muted" x="181" y="284" text-anchor="middle" font-size="10">树边数 = |V|-1，恰好构成无权单源最短路径树</text>
    <text class="fig-green" x="430" y="68" text-anchor="middle" font-size="10.5">① dfs(A) 压栈</text>
    <g class="fig-orange" font-size="10.5" text-anchor="middle">
      <text x="446" y="98">② dfs(B) 压栈</text>
      <text x="462" y="128">③ dfs(D) 压栈</text>
      <text x="478" y="158">④ dfs(F) 压栈</text>
      <text x="494" y="188">⑤ dfs(E) 压栈</text>
      <text x="510" y="218">⑥ dfs(C) 触底</text>
    </g>
    <g class="fig-muted" font-size="10">
      <text x="500" y="68">标记 A</text>
      <text x="516" y="98">取邻接 B</text>
      <text x="532" y="128">取邻接 D</text>
      <text x="548" y="158">取邻接 F</text>
      <text x="564" y="188">取邻接 E</text>
      <text x="580" y="218">无新邻接 → 逐层弹栈</text>
    </g>
    <text class="fig-purple" x="520" y="257" text-anchor="middle" font-size="10.5">DFS 序列：A → B → D → F → E → C</text>
    <text class="fig-muted" x="520" y="272" text-anchor="middle" font-size="10">回溯至 C→E→F→D→B→A 弹空；非连通图外层再调一次</text>
  </svg>
  <figcaption>同一张图上 BFS 辅助队列逐层演化与 DFS 递归调用栈深搜回溯轨迹</figcaption>
</figure>

<figure class="cpd-figure not-content" id="ds40-w4-fig-5">
  <svg viewBox="0 0 700 326" role="img" aria-label="同一张无向图上BFS队列层序扩散生成无权最短路径树与DFS递归栈深搜回溯生成深度树对比">
    <defs>
      <marker id="ds40-w4-arr-5" viewBox="0 0 10 10" refX="8.5" refY="5" markerWidth="6.5" markerHeight="6.5" markerUnits="userSpaceOnUse" orient="auto-start-reverse">
        <path d="M 1 1.5 L 8.5 5 L 1 8.5 z" fill="context-stroke" stroke="none"/>
      </marker>
    </defs>
    <rect class="fig-box-subtle" x="16" y="16" width="330" height="238" rx="8"/>
    <rect class="fig-box-subtle" x="356" y="16" width="328" height="238" rx="8"/>
    <rect class="fig-box" x="16" y="264" width="668" height="46" rx="8"/>
    <g class="fig-box-dashed">
      <rect x="110" y="54" width="44" height="122" rx="6"/>
      <rect x="188" y="54" width="44" height="122" rx="6"/>
    </g>
    <g class="fig-line-dashed">
      <line x1="132" y1="78" x2="132" y2="152"/>
      <line x1="210" y1="78" x2="210" y2="152"/>
      <line x1="210" y1="152" x2="286" y2="115"/>
      <line x1="396" y1="115" x2="472" y2="152"/>
      <line x1="472" y1="78" x2="472" y2="152"/>
      <line x1="550" y1="78" x2="550" y2="152"/>
    </g>
    <g class="fig-line" stroke="var(--sl-color-blue)" stroke-width="1.8" marker-end="url(#ds40-w4-arr-5)">
      <line x1="69" y1="108" x2="118" y2="85"/>
      <line x1="69" y1="122" x2="118" y2="145"/>
      <line x1="147" y1="78" x2="195" y2="78"/>
      <line x1="147" y1="152" x2="195" y2="152"/>
      <line x1="224" y1="85" x2="272" y2="108"/>
    </g>
    <g class="fig-line" stroke="var(--sl-color-orange)" stroke-width="1.8" marker-end="url(#ds40-w4-arr-5)">
      <line x1="409" y1="108" x2="458" y2="85"/>
      <line x1="487" y1="78" x2="535" y2="78"/>
      <line x1="564" y1="85" x2="612" y2="108"/>
      <line x1="612" y1="122" x2="564" y2="145"/>
      <line x1="535" y1="152" x2="487" y2="152"/>
    </g>
    <path class="fig-line-dashed" stroke="var(--sl-color-purple)" d="M 472 167 Q 430 184 404 130" marker-end="url(#ds40-w4-arr-5)"/>
    <circle class="fig-box-green" cx="56" cy="115" r="14"/>
    <g class="fig-box-blue"><circle cx="132" cy="78" r="14"/><circle cx="132" cy="152" r="14"/></g>
    <g class="fig-box-orange"><circle cx="210" cy="78" r="14"/><circle cx="210" cy="152" r="14"/></g>
    <circle class="fig-box-purple" cx="286" cy="115" r="14"/>
    <circle class="fig-box-green" cx="396" cy="115" r="14"/>
    <g class="fig-box-orange">
      <circle cx="472" cy="78" r="14"/><circle cx="550" cy="78" r="14"/>
      <circle cx="626" cy="115" r="14"/><circle cx="550" cy="152" r="14"/><circle cx="472" cy="152" r="14"/>
    </g>
    <rect class="fig-box-blue" x="28" y="190" width="306" height="26" rx="4"/>
    <rect class="fig-box-purple" x="368" y="190" width="304" height="26" rx="4"/>
    <text class="fig-title" x="181" y="36" text-anchor="middle" font-size="12">BFS 广度优先：队列层序水波扩散</text>
    <text class="fig-title" x="520" y="36" text-anchor="middle" font-size="12">DFS 深度优先：递归栈深搜与回溯</text>
    <g class="fig-muted" font-size="9.5" text-anchor="middle">
      <text x="56" y="94">d=0</text><text x="132" y="50">d=1</text><text x="210" y="50">d=2</text><text x="286" y="94">d=3</text>
    </g>
    <text class="fig-green" x="56" y="119" text-anchor="middle" font-size="11">A</text>
    <g class="fig-blue" font-size="11" text-anchor="middle"><text x="132" y="82">B</text><text x="132" y="156">C</text></g>
    <g class="fig-orange" font-size="11" text-anchor="middle"><text x="210" y="82">D</text><text x="210" y="156">E</text></g>
    <text class="fig-purple" x="286" y="119" text-anchor="middle" font-size="11">F</text>
    <text class="fig-green" x="396" y="119" text-anchor="middle" font-size="11">A</text>
    <g class="fig-orange" font-size="11" text-anchor="middle">
      <text x="472" y="82">B</text><text x="550" y="82">D</text><text x="626" y="119">F</text>
      <text x="550" y="156">E</text><text x="472" y="156">C</text>
    </g>
    <g class="fig-muted" font-size="9.5" text-anchor="middle">
      <text x="434" y="88">①</text><text x="511" y="72">②</text><text x="592" y="88">③</text>
      <text x="592" y="144">④</text><text x="511" y="146">⑤</text><text x="434" y="182">⑥回退</text>
    </g>
    <text class="fig-blue" x="181" y="207" text-anchor="middle" font-size="10.5">队列出队序：A → B, C → D, E → F（树高 3）</text>
    <text class="fig-green" x="181" y="236" text-anchor="middle" font-size="10.5">★ BFS 生成树高最小，即无权图单源最短路径树！</text>
    <text class="fig-purple" x="520" y="207" text-anchor="middle" font-size="10.5">入栈深入序：A → B → D → F → E → C（树高 5）</text>
    <text class="fig-orange" x="520" y="236" text-anchor="middle" font-size="10.5">★ 非连通图形成生成森林；连通分量数 = 顶层调用次数</text>
    <text class="fig-label" x="350" y="283" text-anchor="middle" font-size="11">复杂度对比：邻接矩阵 O(|V|²)（遍历序列唯一） ｜ 邻接表 O(|V|+|E|)（表结点次序不唯一 → 序列不唯一）</text>
    <text class="fig-muted" x="350" y="301" text-anchor="middle" font-size="10.5">空间复杂度均为 O(|V|)：来自 visited[|V|] 数组 + 辅助队列（BFS 最坏 O(|V|)）或递归栈（DFS 最坏单链 O(|V|)）</text>
  </svg>
  <figcaption>同一张图上 BFS（队列层序最短路树）与 DFS（栈回溯深度生成树）的树形态与复杂度对比</figcaption>
</figure>

## 十一、最小生成树与最短路径

### 最小生成树

生成树包含全部 $n$ 个顶点、只有 $n-1$ 条边、没有回路。权值之和最小的生成树叫**最小生成树（MST）**。

**Prim 算法**：从任意顶点出发，每次选一条连接“已选集合”和“未选集合”的最小边，把新顶点拉进来，直到所有顶点都加入。

- 复杂度 $O(n^2)$，只和顶点数有关，适合**稠密图**

<figure class="cpd-figure not-content" id="ds40-img1-fig-1">
  <svg viewBox="0 0 700 280" role="img" aria-label="Prim 算法逐步演示：lowcost 与 adjvex 数组更新及割集扩张">
    <rect class="fig-box-subtle" x="16" y="16" width="256" height="248" rx="8"/>
    <rect class="fig-box-subtle" x="284" y="16" width="400" height="248" rx="8"/>
    <text class="fig-title" x="144" y="36" text-anchor="middle" font-size="12">加点过程与最小生成树结果</text>
    <text class="fig-title" x="484" y="36" text-anchor="middle" font-size="12">lowcost[] 与 adjvex[] 逐轮更新表（从 A 出发）</text>
    <g class="fig-line-dashed">
      <line x1="60" y1="88" x2="152" y2="66"/>
      <line x1="152" y1="66" x2="226" y2="162"/>
      <line x1="236" y1="80" x2="226" y2="162"/>
    </g>
    <g class="fig-line-green">
      <line x1="60" y1="88" x2="116" y2="162"/>
      <line x1="116" y1="162" x2="152" y2="66"/>
      <line x1="116" y1="162" x2="226" y2="162"/>
      <line x1="152" y1="66" x2="236" y2="80"/>
    </g>
    <g class="fig-box">
      <rect x="97" y="68" width="18" height="16" rx="3"/>
      <rect x="180" y="106" width="18" height="16" rx="3"/>
      <rect x="223" y="114" width="18" height="16" rx="3"/>
    </g>
    <g class="fig-box-green">
      <rect x="77" y="117" width="18" height="16" rx="3"/>
      <rect x="125" y="106" width="18" height="16" rx="3"/>
      <rect x="162" y="154" width="18" height="16" rx="3"/>
      <rect x="185" y="64" width="18" height="16" rx="3"/>
    </g>
    <g font-size="10.5" text-anchor="middle">
      <text class="fig-muted" x="106" y="80">3</text>
      <text class="fig-muted" x="189" y="118">6</text>
      <text class="fig-muted" x="232" y="126">5</text>
      <text class="fig-green" x="86" y="129">1</text>
      <text class="fig-green" x="134" y="118">2</text>
      <text class="fig-green" x="171" y="166">3</text>
      <text class="fig-green" x="194" y="76">4</text>
    </g>
    <g class="fig-box-green">
      <circle cx="60" cy="88" r="14"/>
      <circle cx="152" cy="66" r="14"/>
      <circle cx="116" cy="162" r="14"/>
      <circle cx="236" cy="80" r="14"/>
      <circle cx="226" cy="162" r="14"/>
    </g>
    <g class="fig-green" font-size="12" text-anchor="middle">
      <text x="60" y="92">A</text>
      <text x="152" y="70">B</text>
      <text x="116" y="166">C</text>
      <text x="236" y="84">D</text>
      <text x="226" y="166">E</text>
    </g>
    <rect class="fig-box" x="28" y="194" width="232" height="58" rx="6"/>
    <text class="fig-label" x="144" y="213" text-anchor="middle" font-size="10.5">拉入顺序：A → C → B → E → D</text>
    <text class="fig-green" x="144" y="230" text-anchor="middle" font-size="10.5">MST 边权和 = 1 + 2 + 3 + 4 = 10</text>
    <text class="fig-muted" x="144" y="245" text-anchor="middle" font-size="9.5">每次只更新新并入点相邻的 lowcost[]</text>
    <rect class="fig-box" x="294" y="48" width="380" height="24" rx="4"/>
    <g class="fig-label" font-size="10.5" text-anchor="middle">
      <text x="314" y="64">轮</text>
      <text x="364" y="64">已选集 U</text>
      <text x="480" y="64">lowcost [A, B, C, D, E]</text>
      <text x="622" y="64">选中最小边</text>
    </g>
    <rect class="fig-box" x="294" y="76" width="380" height="32" rx="4"/>
    <rect class="fig-box-accent" x="488" y="80" width="26" height="24" rx="3"/>
    <g font-size="10.5" text-anchor="middle">
      <text class="fig-muted" x="314" y="96">初</text>
      <text class="fig-label" x="364" y="96">{A}</text>
      <text class="fig-muted" x="430" y="96">0</text>
      <text class="fig-label" x="466" y="96">3</text>
      <text class="fig-accent" x="501" y="96">1</text>
      <text class="fig-muted" x="534" y="96">∞</text>
      <text class="fig-muted" x="562" y="96">∞</text>
      <text class="fig-green" x="622" y="96">(A, C) = 1</text>
    </g>
    <rect class="fig-box" x="294" y="112" width="380" height="32" rx="4"/>
    <rect class="fig-box-accent" x="453" y="116" width="26" height="24" rx="3"/>
    <g font-size="10.5" text-anchor="middle">
      <text class="fig-muted" x="314" y="132">①</text>
      <text class="fig-label" x="364" y="132">{A, C}</text>
      <text class="fig-muted" x="430" y="132">0</text>
      <text class="fig-accent" x="466" y="132">2↓</text>
      <text class="fig-muted" x="501" y="132">0</text>
      <text class="fig-muted" x="534" y="132">∞</text>
      <text class="fig-blue" x="562" y="132">3↓</text>
      <text class="fig-green" x="622" y="132">(C, B) = 2</text>
    </g>
    <rect class="fig-box" x="294" y="148" width="380" height="32" rx="4"/>
    <rect class="fig-box-accent" x="549" y="152" width="26" height="24" rx="3"/>
    <g font-size="10.5" text-anchor="middle">
      <text class="fig-muted" x="314" y="168">②</text>
      <text class="fig-label" x="364" y="168">{A, C, B}</text>
      <text class="fig-muted" x="430" y="168">0</text>
      <text class="fig-muted" x="466" y="168">0</text>
      <text class="fig-muted" x="501" y="168">0</text>
      <text class="fig-blue" x="534" y="168">4↓</text>
      <text class="fig-accent" x="562" y="168">3</text>
      <text class="fig-green" x="622" y="168">(C, E) = 3</text>
    </g>
    <rect class="fig-box" x="294" y="184" width="380" height="32" rx="4"/>
    <rect class="fig-box-accent" x="521" y="188" width="26" height="24" rx="3"/>
    <g font-size="10.5" text-anchor="middle">
      <text class="fig-muted" x="314" y="204">③</text>
      <text class="fig-label" x="364" y="204">{A, C, B, E}</text>
      <text class="fig-muted" x="430" y="204">0</text>
      <text class="fig-muted" x="466" y="204">0</text>
      <text class="fig-muted" x="501" y="204">0</text>
      <text class="fig-accent" x="534" y="204">4</text>
      <text class="fig-muted" x="562" y="204">0</text>
      <text class="fig-green" x="622" y="204">(B, D) = 4</text>
    </g>
    <rect class="fig-box-green" x="294" y="222" width="380" height="30" rx="4"/>
    <text class="fig-green" x="484" y="241" text-anchor="middle" font-size="10.5">④ U = {A, B, C, D, E} 满 n 个顶点停止（↓ 表示比原值更小并更新 adjvex）</text>
  </svg>
  <figcaption>图 34：Prim 算法从已选集合向外挑最小边，借助 lowcost[] 实现 O(n²) 加点扩张</figcaption>
</figure>

**Kruskal 算法**：把所有边按权值从小到大排序，依次考察；只要不形成回路就选上，直到选够 $n-1$ 条边。

- 复杂度 $O(e \log e)$，主要和边数有关，适合**稀疏图**
- 用并查集判断是否成环

<figure class="cpd-figure not-content" id="ds40-img1-fig-2">
  <svg viewBox="0 0 700 286" role="img" aria-label="Kruskal 算法按边权排序并结合并查集判环逐步演示">
    <rect class="fig-box-subtle" x="16" y="16" width="406" height="254" rx="8"/>
    <rect class="fig-box-subtle" x="434" y="16" width="250" height="254" rx="8"/>
    <text class="fig-title" x="219" y="36" text-anchor="middle" font-size="12">按边权升序扫描 + 并查集判环</text>
    <text class="fig-title" x="559" y="36" text-anchor="middle" font-size="12">第 ③ 步成环拒收 vs 第 ④ 步合并</text>
    <rect class="fig-box" x="26" y="48" width="386" height="22" rx="4"/>
    <g class="fig-label" font-size="10.5" text-anchor="middle">
      <text x="48" y="63">次序</text>
      <text x="102" y="63">边 (u, v)</text>
      <text x="150" y="63">权</text>
      <text x="236" y="63">并查集 Find 判定</text>
      <text x="358" y="63">决策与边数</text>
    </g>
    <rect class="fig-box-green" x="26" y="74" width="386" height="24" rx="4"/>
    <g class="fig-green" font-size="10.5" text-anchor="middle">
      <text x="48" y="90">①</text><text x="102" y="90">(A, C)</text><text x="150" y="90">1</text>
      <text x="236" y="90">Find(A)≠Find(C)</text><text x="358" y="90">✓ 选入（1/4）</text>
    </g>
    <rect class="fig-box-green" x="26" y="102" width="386" height="24" rx="4"/>
    <g class="fig-green" font-size="10.5" text-anchor="middle">
      <text x="48" y="118">②</text><text x="102" y="118">(C, B)</text><text x="150" y="118">2</text>
      <text x="236" y="118">Find(C)≠Find(B)</text><text x="358" y="118">✓ 选入（2/4）</text>
    </g>
    <rect class="fig-box-red" x="26" y="130" width="386" height="24" rx="4"/>
    <g class="fig-red" font-size="10.5" text-anchor="middle">
      <text x="48" y="146">③</text><text x="102" y="146">(A, B)</text><text x="150" y="146">3</text>
      <text x="236" y="146">Find(A)=Find(B)=A</text><text x="358" y="146">✗ 成环跳过</text>
    </g>
    <rect class="fig-box-green" x="26" y="158" width="386" height="24" rx="4"/>
    <g class="fig-green" font-size="10.5" text-anchor="middle">
      <text x="48" y="174">④</text><text x="102" y="174">(C, E)</text><text x="150" y="174">3</text>
      <text x="236" y="174">Find(C)≠Find(E)</text><text x="358" y="174">✓ 选入（3/4）</text>
    </g>
    <rect class="fig-box-green" x="26" y="186" width="386" height="24" rx="4"/>
    <g class="fig-green" font-size="10.5" text-anchor="middle">
      <text x="48" y="202">⑤</text><text x="102" y="202">(B, D)</text><text x="150" y="202">4</text>
      <text x="236" y="202">Find(B)≠Find(D)</text><text x="358" y="202">✓ 选入（4/4 满）</text>
    </g>
    <rect class="fig-box" x="26" y="214" width="386" height="22" rx="4"/>
    <g class="fig-muted" font-size="10" text-anchor="middle">
      <text x="48" y="229">⑥</text><text x="102" y="229">(D, E)</text><text x="150" y="229">5</text>
      <text x="236" y="229">已有 n−1 = 4 条边</text><text x="358" y="229">— 提前终止</text>
    </g>
    <rect class="fig-box" x="26" y="239" width="386" height="22" rx="4"/>
    <g class="fig-muted" font-size="10" text-anchor="middle">
      <text x="48" y="254">⑦</text><text x="102" y="254">(B, E)</text><text x="150" y="254">6</text>
      <text x="236" y="254">不再扫描</text><text x="358" y="254">— 总权值 = 10</text>
    </g>
    <rect class="fig-box-dashed" x="448" y="50" width="144" height="120" rx="10"/>
    <text class="fig-muted" x="520" y="65" text-anchor="middle" font-size="9.5">连通分量 {A, B, C}</text>
    <line x1="478" y1="88" x2="562" y2="88" stroke="var(--sl-color-red)" stroke-width="1.6" stroke-dasharray="4 3"/>
    <line class="fig-line-green" x1="478" y1="88" x2="520" y2="146"/>
    <line class="fig-line-green" x1="520" y1="146" x2="562" y2="88"/>
    <line class="fig-line-green" x1="520" y1="146" x2="640" y2="146"/>
    <rect class="fig-box-red" x="502" y="79" width="36" height="16" rx="3"/>
    <text class="fig-red" x="520" y="91" text-anchor="middle" font-size="9.5">3 ✗</text>
    <rect class="fig-box-green" x="482" y="110" width="18" height="15" rx="3"/>
    <text class="fig-green" x="491" y="121" text-anchor="middle" font-size="9.5">1</text>
    <rect class="fig-box-green" x="540" y="110" width="18" height="15" rx="3"/>
    <text class="fig-green" x="549" y="121" text-anchor="middle" font-size="9.5">2</text>
    <rect class="fig-box-green" x="576" y="138" width="24" height="16" rx="3"/>
    <text class="fig-green" x="588" y="150" text-anchor="middle" font-size="9.5">3 ✓</text>
    <g class="fig-box-green">
      <circle cx="478" cy="88" r="13"/>
      <circle cx="562" cy="88" r="13"/>
      <circle cx="520" cy="146" r="13"/>
      <circle cx="640" cy="146" r="13"/>
    </g>
    <g class="fig-green" font-size="11" text-anchor="middle">
      <text x="478" y="92">A</text><text x="562" y="92">B</text>
      <text x="520" y="150">C</text><text x="640" y="150">E</text>
    </g>
    <rect class="fig-box" x="448" y="182" width="222" height="76" rx="6"/>
    <text class="fig-red" x="559" y="201" text-anchor="middle" font-size="10.5">③ (A,B)：A、B 已同根 → 必成环</text>
    <text class="fig-green" x="559" y="220" text-anchor="middle" font-size="10.5">④ (C,E)：跨分量连接 → 安全合并</text>
    <text class="fig-muted" x="559" y="241" text-anchor="middle" font-size="9.5">排序主导 O(e log e)，适合稀疏图</text>
  </svg>
  <figcaption>图 35：Kruskal 按边权排序，并用并查集跳过会成环的边</figcaption>
</figure>

> **★考点**：最小生成树可能不唯一，但**最小权值之和唯一**；Prim $O(n^2)$ 适合稠密图，Kruskal $O(e\log e)$ 适合稀疏图。

<figure class="cpd-figure not-content" id="ds40-w4-fig-4">
  <svg viewBox="0 0 700 332" role="img" aria-label="最小生成树Prim割集加点法lowcost数组更新与Kruskal边权排序结合并查集判环逐步对比">
    <rect class="fig-box-subtle" x="16" y="16" width="330" height="246" rx="8"/>
    <rect class="fig-box-subtle" x="356" y="16" width="328" height="246" rx="8"/>
    <rect class="fig-box-accent" x="16" y="272" width="668" height="44" rx="8"/>
    <rect class="fig-box-dashed" x="32" y="48" width="136" height="110" rx="10"/>
    <g class="fig-line-dashed">
      <line x1="60" y1="80" x2="138" y2="68"/>
      <line x1="138" y1="68" x2="226" y2="136"/>
      <line x1="234" y1="68" x2="226" y2="136"/>
    </g>
    <g class="fig-line" stroke="var(--sl-color-green)" stroke-width="2.2">
      <line x1="60" y1="80" x2="124" y2="136"/>
      <line x1="124" y1="136" x2="138" y2="68"/>
      <line x1="124" y1="136" x2="226" y2="136"/>
      <line x1="138" y1="68" x2="234" y2="68"/>
    </g>
    <g class="fig-box" rx="3">
      <rect x="82" y="100" width="18" height="15" rx="3"/><rect x="122" y="94" width="18" height="15" rx="3"/>
      <rect x="90" y="64" width="18" height="15" rx="3"/><rect x="168" y="128" width="18" height="15" rx="3"/>
      <rect x="178" y="58" width="18" height="15" rx="3"/><rect x="222" y="95" width="18" height="15" rx="3"/>
      <rect x="176" y="94" width="18" height="15" rx="3"/>
    </g>
    <g class="fig-box-green">
      <circle cx="60" cy="80" r="13"/><circle cx="124" cy="136" r="13"/><circle cx="138" cy="68" r="13"/>
    </g>
    <g class="fig-box-blue">
      <circle cx="234" cy="68" r="13"/><circle cx="226" cy="136" r="13"/>
    </g>
    <rect class="fig-box" x="28" y="168" width="306" height="82" rx="5"/>
    <rect class="fig-box-green" x="368" y="50" width="304" height="26" rx="4"/>
    <rect class="fig-box-green" x="368" y="82" width="304" height="26" rx="4"/>
    <rect class="fig-box-red" x="368" y="114" width="304" height="26" rx="4"/>
    <rect class="fig-box-green" x="368" y="146" width="304" height="26" rx="4"/>
    <rect class="fig-box-green" x="368" y="178" width="304" height="26" rx="4"/>
    <rect class="fig-box" x="368" y="214" width="304" height="36" rx="4"/>
    <text class="fig-title" x="181" y="36" text-anchor="middle" font-size="12">Prim 算法（按点扩张割集 · O(|V|²)）</text>
    <text class="fig-title" x="520" y="36" text-anchor="middle" font-size="12">Kruskal 算法（按边排序 + 并查集 · O(|E|log|E|)）</text>
    <text class="fig-green" x="76" y="60" font-size="9.5">已选集 U</text>
    <text class="fig-blue" x="262" y="60" font-size="9.5">未选集 V-U</text>
    <g class="fig-green" font-size="10.5" text-anchor="middle">
      <text x="60" y="84">A</text><text x="124" y="140">C</text><text x="138" y="72">B</text>
      <text x="91" y="111">1</text><text x="131" y="105">2</text><text x="177" y="139">3</text><text x="187" y="69">4</text>
    </g>
    <g class="fig-blue" font-size="10.5" text-anchor="middle">
      <text x="234" y="72">D</text><text x="226" y="140">E</text>
    </g>
    <g class="fig-muted" font-size="10" text-anchor="middle">
      <text x="99" y="75">3</text><text x="231" y="106">5</text><text x="185" y="105">6</text>
    </g>
    <text class="fig-label" x="38" y="185" font-size="10.5">① U={A}：lowcost=[0,3,1,∞,∞] → 挑最小边(A,C)=1</text>
    <text class="fig-label" x="38" y="203" font-size="10.5">② U={A,C}：松弛 B:3→2, E:∞→3 → 挑(C,B)=2</text>
    <text class="fig-label" x="38" y="221" font-size="10.5">③ U={A,C,B}：跨割集最小为(C,E)=3 → 拉入 E</text>
    <text class="fig-green" x="38" y="239" font-size="10.5">④ 挑(B,D)=4 拉入 D，总权值 1+2+3+4 = 10</text>
    <text class="fig-green" x="378" y="67" font-size="10.5">① (A,C) w=1 │ Find(A)≠Find(C) → 选入 ✓ 合并{A,C}</text>
    <text class="fig-green" x="378" y="99" font-size="10.5">② (C,B) w=2 │ Find(C)≠Find(B) → 选入 ✓ 合并{A,B,C}</text>
    <text class="fig-red" x="378" y="131" font-size="10.5">③ (A,B) w=3 │ Find(A)=Find(B) 同集合 → 成环跳过 ✗</text>
    <text class="fig-green" x="378" y="163" font-size="10.5">④ (C,E) w=3 │ Find(C)≠Find(E) → 选入 ✓ 合并{A,B,C,E}</text>
    <text class="fig-green" x="378" y="195" font-size="10.5">⑤ (B,D) w=4 │ Find(B)≠Find(D) → 选入 ✓ 满 n-1=4 边停</text>
    <text class="fig-muted" x="378" y="229" font-size="10">后续边 (D,E)=5, (B,E)=6 无需考察；并查集单次判环近似 O(1)</text>
    <text class="fig-muted" x="378" y="244" font-size="10">主导开销在边排序 O(|E|log|E|)（或建小根堆按权取边）</text>
    <text class="fig-accent" x="350" y="291" text-anchor="middle" font-size="11">★ MST 唯一性铁律：各边权值互不相等 ⇒ MST 必唯一；存在相等权值边 ⇒ MST 可能不唯一，但最小权值和必唯一</text>
    <text class="fig-muted" x="350" y="308" text-anchor="middle" font-size="10.5">适用场景对比：稠密图（|E| ≈ |V|²）选 Prim O(|V|²)；稀疏图（|E| ≪ |V|²）选 Kruskal O(|E|log|E|)</text>
  </svg>
  <figcaption>最小生成树双算法同图对比：Prim 维护 lowcost[] 向外扩点，Kruskal 排序边并用并查集跳过成环边</figcaption>
</figure>

### Dijkstra 单源最短路径

从起点出发，每次在未确定的顶点中选出 `dist` 最小的一个，把它**敲定**，再用它更新邻居的距离。

> **★考点**：Dijkstra 采用贪心，**不能处理负权边**；邻接矩阵实现复杂度 $O(n^2)$。

以 $A$ 为起点，最终：

$$
\text{dist}=\{A:0,\ B:3,\ C:2,\ D:8,\ E:10,\ F:13\}
$$

<figure class="cpd-figure not-content" id="ds40-img1-fig-3">
  <svg viewBox="0 0 700 322" role="img" aria-label="Dijkstra 单源最短路径逐轮敲定过程与红色最短路径树">
    <defs>
      <marker id="ds40-img1-arr-1" viewBox="0 0 10 10" refX="8" refY="5" markerWidth="6" markerHeight="6" markerUnits="userSpaceOnUse" orient="auto-start-reverse">
        <path d="M 1 1.5 L 8.5 5 L 1 8.5 z" fill="context-stroke" stroke="none"/>
      </marker>
    </defs>
    <rect class="fig-box-subtle" x="16" y="16" width="328" height="244" rx="8"/>
    <rect class="fig-box-subtle" x="356" y="16" width="328" height="244" rx="8"/>
    <rect class="fig-box-accent" x="16" y="270" width="668" height="36" rx="6"/>
    <text class="fig-title" x="180" y="36" text-anchor="middle" font-size="12">加权有向图与最短路径树（红边）</text>
    <text class="fig-title" x="520" y="36" text-anchor="middle" font-size="12">逐轮敲定最近点并松弛邻接点</text>
    <g stroke="var(--sl-color-gray-4)" stroke-width="1.3" stroke-dasharray="4 3" fill="none" marker-end="url(#ds40-img1-arr-1)">
      <line x1="65" y1="125" x2="117" y2="85"/>
      <line x1="145" y1="184" x2="215" y2="92"/>
      <line x1="149" y1="194" x2="207" y2="194"/>
      <line x1="224" y1="92" x2="224" y2="177"/>
      <line x1="238" y1="84" x2="292" y2="127"/>
    </g>
    <g stroke="var(--sl-color-red)" stroke-width="2.2" fill="none" marker-end="url(#ds40-img1-arr-1)">
      <line x1="65" y1="147" x2="117" y2="185"/>
      <line x1="132" y1="178" x2="132" y2="94"/>
      <line x1="149" y1="76" x2="207" y2="76"/>
      <line x1="145" y1="88" x2="212" y2="180"/>
      <line x1="238" y1="184" x2="292" y2="145"/>
    </g>
    <g class="fig-box">
      <rect x="78" y="96" width="16" height="15" rx="3"/>
      <rect x="164" y="136" width="16" height="15" rx="3"/>
      <rect x="172" y="187" width="16" height="15" rx="3"/>
      <rect x="216" y="128" width="16" height="15" rx="3"/>
      <rect x="260" y="96" width="16" height="15" rx="3"/>
    </g>
    <g class="fig-box-red">
      <rect x="78" y="160" width="16" height="15" rx="3"/>
      <rect x="124" y="128" width="16" height="15" rx="3"/>
      <rect x="172" y="68" width="16" height="15" rx="3"/>
      <rect x="182" y="114" width="16" height="15" rx="3"/>
      <rect x="260" y="158" width="16" height="15" rx="3"/>
    </g>
    <g font-size="10" text-anchor="middle">
      <text class="fig-muted" x="86" y="107">4</text>
      <text class="fig-muted" x="172" y="147">8</text>
      <text class="fig-muted" x="180" y="198">10</text>
      <text class="fig-muted" x="224" y="139">2</text>
      <text class="fig-muted" x="268" y="107">6</text>
      <text class="fig-red" x="86" y="171">2</text>
      <text class="fig-red" x="132" y="139">1</text>
      <text class="fig-red" x="180" y="79">5</text>
      <text class="fig-red" x="190" y="125">7</text>
      <text class="fig-red" x="268" y="169">3</text>
    </g>
    <circle class="fig-box-accent" cx="52" cy="136" r="15"/>
    <g class="fig-box-red">
      <circle cx="132" cy="76" r="15"/>
      <circle cx="132" cy="194" r="15"/>
      <circle cx="224" cy="76" r="15"/>
      <circle cx="224" cy="194" r="15"/>
      <circle cx="306" cy="136" r="15"/>
    </g>
    <g font-size="12" text-anchor="middle">
      <text class="fig-accent" x="52" y="140">A</text>
      <text class="fig-red" x="132" y="80">B</text>
      <text class="fig-red" x="132" y="198">C</text>
      <text class="fig-red" x="224" y="80">D</text>
      <text class="fig-red" x="224" y="198">E</text>
      <text class="fig-red" x="306" y="140">F</text>
    </g>
    <g class="fig-label" font-size="10" text-anchor="middle">
      <text x="52" y="164">0</text>
      <text x="132" y="54">dist=3</text>
      <text x="132" y="222">dist=2</text>
      <text x="224" y="54">dist=8</text>
      <text x="224" y="222">dist=10</text>
      <text x="306" y="164">13</text>
    </g>
    <text class="fig-muted" x="180" y="248" text-anchor="middle" font-size="10">松弛示例：C→B 使 B 从 4 降为 3；B→D 使 D 从 10 降为 8</text>
    <rect class="fig-box" x="366" y="48" width="308" height="24" rx="4"/>
    <g class="fig-label" font-size="10.5" text-anchor="middle">
      <text x="384" y="64">轮</text>
      <text x="424" y="64">敲定</text>
      <text x="466" y="64">B</text>
      <text x="506" y="64">C</text>
      <text x="546" y="64">D</text>
      <text x="586" y="64">E</text>
      <text x="634" y="64">F</text>
    </g>
    <rect class="fig-box" x="366" y="76" width="308" height="28" rx="4"/>
    <rect class="fig-box-red" x="492" y="79" width="28" height="22" rx="3"/>
    <g font-size="10.5" text-anchor="middle">
      <text class="fig-muted" x="384" y="94">初</text>
      <text class="fig-accent" x="424" y="94">A(0)</text>
      <text class="fig-label" x="466" y="94">4</text>
      <text class="fig-red" x="506" y="94">2</text>
      <text class="fig-muted" x="546" y="94">∞</text>
      <text class="fig-muted" x="586" y="94">∞</text>
      <text class="fig-muted" x="634" y="94">∞</text>
    </g>
    <rect class="fig-box" x="366" y="108" width="308" height="28" rx="4"/>
    <rect class="fig-box-red" x="452" y="111" width="28" height="22" rx="3"/>
    <g font-size="10.5" text-anchor="middle">
      <text class="fig-muted" x="384" y="126">①</text>
      <text class="fig-red" x="424" y="126">C(2)</text>
      <text class="fig-red" x="466" y="126">3↓</text>
      <text class="fig-muted" x="506" y="126">—</text>
      <text class="fig-blue" x="546" y="126">10↓</text>
      <text class="fig-blue" x="586" y="126">12↓</text>
      <text class="fig-muted" x="634" y="126">∞</text>
    </g>
    <rect class="fig-box" x="366" y="140" width="308" height="28" rx="4"/>
    <rect class="fig-box-red" x="532" y="143" width="28" height="22" rx="3"/>
    <g font-size="10.5" text-anchor="middle">
      <text class="fig-muted" x="384" y="158">②</text>
      <text class="fig-red" x="424" y="158">B(3)</text>
      <text class="fig-muted" x="466" y="158">—</text>
      <text class="fig-muted" x="506" y="158">—</text>
      <text class="fig-red" x="546" y="158">8↓</text>
      <text class="fig-blue" x="586" y="158">10↓</text>
      <text class="fig-muted" x="634" y="158">∞</text>
    </g>
    <rect class="fig-box" x="366" y="172" width="308" height="28" rx="4"/>
    <rect class="fig-box-red" x="572" y="175" width="28" height="22" rx="3"/>
    <g font-size="10.5" text-anchor="middle">
      <text class="fig-muted" x="384" y="190">③</text>
      <text class="fig-red" x="424" y="190">D(8)</text>
      <text class="fig-muted" x="466" y="190">—</text>
      <text class="fig-muted" x="506" y="190">—</text>
      <text class="fig-muted" x="546" y="190">—</text>
      <text class="fig-red" x="586" y="190">10</text>
      <text class="fig-blue" x="634" y="190">14↓</text>
    </g>
    <rect class="fig-box" x="366" y="204" width="308" height="28" rx="4"/>
    <rect class="fig-box-red" x="620" y="207" width="28" height="22" rx="3"/>
    <g font-size="10.5" text-anchor="middle">
      <text class="fig-muted" x="384" y="222">④</text>
      <text class="fig-red" x="424" y="222">E(10)</text>
      <text class="fig-muted" x="466" y="222">—</text>
      <text class="fig-muted" x="506" y="222">—</text>
      <text class="fig-muted" x="546" y="222">—</text>
      <text class="fig-muted" x="586" y="222">—</text>
      <text class="fig-red" x="634" y="222">13↓</text>
    </g>
    <text class="fig-green" x="520" y="248" text-anchor="middle" font-size="10.5">⑤ 敲定 F(13)，得 dist = {A:0, B:3, C:2, D:8, E:10, F:13}</text>
    <text class="fig-accent" x="350" y="292" text-anchor="middle" font-size="11">★ 敲定顺序 A → C → B → D → E → F；贪心前提是无负权边，否则已敲定点可能后续变短</text>
  </svg>
  <figcaption>图 36：Dijkstra 逐轮敲定最近顶点；红色为最短路径树</figcaption>
</figure>

### Floyd 多源最短路径

求任意两点之间的最短路径。逐个让每个顶点 $k$ 当中转站：

$$
A[i][j]=\min\left(A[i][j],\ A[i][k]+A[k][j]\right)
$$

三重循环，**中转站 $k$ 必须在最外层**：

```c
for (int k = 0; k < n; k++)
    for (int i = 0; i < n; i++)
        for (int j = 0; j < n; j++)
            A[i][j] = min(A[i][j], A[i][k] + A[k][j]);
```

> **★考点**：Floyd 复杂度 $O(n^3)$；**允许负权边，不允许负权回路**（否则最短路径可以无限小，不存在）。

<figure class="cpd-figure not-content" id="ds40-img1-fig-4">
  <svg viewBox="0 0 700 302" role="img" aria-label="Floyd 多源最短路径算法矩阵递推与负权边松弛示例">
    <defs>
      <marker id="ds40-img1-arr-2" viewBox="0 0 10 10" refX="8" refY="5" markerWidth="6" markerHeight="6" markerUnits="userSpaceOnUse" orient="auto-start-reverse">
        <path d="M 1 1.5 L 8.5 5 L 1 8.5 z" fill="context-stroke" stroke="none"/>
      </marker>
    </defs>
    <rect class="fig-box-subtle" x="16" y="16" width="194" height="226" rx="8"/>
    <rect class="fig-box-subtle" x="222" y="16" width="462" height="226" rx="8"/>
    <rect class="fig-box-accent" x="16" y="252" width="668" height="36" rx="6"/>
    <text class="fig-title" x="113" y="36" text-anchor="middle" font-size="12">带负权边有向图（无负环）</text>
    <text class="fig-title" x="453" y="36" text-anchor="middle" font-size="12">距离矩阵 A 递推：A[i][j] = min(A[i][j], A[i][k] + A[k][j])</text>
    <g stroke="var(--sl-color-gray-4)" stroke-width="1.5" fill="none" marker-end="url(#ds40-img1-arr-2)">
      <line x1="102" y1="80" x2="66" y2="146"/>
      <line x1="124" y1="80" x2="160" y2="146"/>
      <path d="M 164 176 Q 113 206 106 86"/>
    </g>
    <line x1="74" y1="162" x2="152" y2="162" stroke="var(--sl-color-blue)" stroke-width="1.8" marker-end="url(#ds40-img1-arr-2)"/>
    <g class="fig-box">
      <rect x="70" y="106" width="18" height="16" rx="3"/>
      <rect x="138" y="106" width="18" height="16" rx="3"/>
      <rect x="122" y="182" width="18" height="16" rx="3"/>
    </g>
    <rect class="fig-box-blue" x="100" y="153" width="24" height="16" rx="3"/>
    <g font-size="10.5" text-anchor="middle">
      <text class="fig-label" x="79" y="118">5</text>
      <text class="fig-label" x="147" y="118">3</text>
      <text class="fig-muted" x="131" y="194">4</text>
      <text class="fig-blue" x="112" y="165">−3</text>
    </g>
    <g class="fig-box-accent">
      <circle cx="113" cy="68" r="15"/>
      <circle cx="58" cy="162" r="15"/>
      <circle cx="168" cy="162" r="15"/>
    </g>
    <g class="fig-accent" font-size="12" text-anchor="middle">
      <text x="113" y="72">0</text>
      <text x="58" y="166">1</text>
      <text x="168" y="166">2</text>
    </g>
    <text class="fig-muted" x="113" y="216" text-anchor="middle" font-size="10">0→1→2 权和 = 5 + (−3) = 2</text>
    <text class="fig-muted" x="113" y="232" text-anchor="middle" font-size="10">负权边 −3 允许，但不可有负环</text>
    <g font-size="10.5" text-anchor="middle">
      <text class="fig-muted" x="288" y="58">A⁽⁻¹⁾ 初始直达矩阵</text>
      <text class="fig-blue" x="404" y="58">k = 0（经 0 中转）</text>
      <text class="fig-green" x="520" y="58">k = 1（经 1 中转）</text>
      <text class="fig-accent" x="630" y="58">k = 2（经 2 中转）</text>
    </g>
    <g class="fig-box">
      <rect x="236" y="68" width="96" height="84" rx="5"/>
      <rect x="352" y="68" width="96" height="84" rx="5"/>
      <rect x="468" y="68" width="96" height="84" rx="5"/>
      <rect x="582" y="68" width="92" height="84" rx="5"/>
    </g>
    <g class="fig-divider">
      <line x1="236" y1="96" x2="332" y2="96"/><line x1="236" y1="124" x2="332" y2="124"/>
      <line x1="268" y1="68" x2="268" y2="152"/><line x1="300" y1="68" x2="300" y2="152"/>
      <line x1="352" y1="96" x2="448" y2="96"/><line x1="352" y1="124" x2="448" y2="124"/>
      <line x1="384" y1="68" x2="384" y2="152"/><line x1="416" y1="68" x2="416" y2="152"/>
      <line x1="468" y1="96" x2="564" y2="96"/><line x1="468" y1="124" x2="564" y2="124"/>
      <line x1="500" y1="68" x2="500" y2="152"/><line x1="532" y1="68" x2="532" y2="152"/>
      <line x1="582" y1="96" x2="674" y2="96"/><line x1="582" y1="124" x2="674" y2="124"/>
      <line x1="612" y1="68" x2="612" y2="152"/><line x1="643" y1="68" x2="643" y2="152"/>
    </g>
    <rect class="fig-box-blue" x="385" y="125" width="30" height="26" rx="2"/>
    <rect class="fig-box-green" x="533" y="69" width="30" height="26" rx="2"/>
    <rect class="fig-box-accent" x="583" y="97" width="28" height="26" rx="2"/>
    <g class="fig-label" font-size="11" text-anchor="middle">
      <text x="252" y="86">0</text><text x="284" y="86">5</text><text x="316" y="86">3</text>
      <text class="fig-muted" x="252" y="114">∞</text><text x="284" y="114">0</text><text class="fig-blue" x="316" y="114">−3</text>
      <text x="252" y="142">4</text><text class="fig-muted" x="284" y="142">∞</text><text x="316" y="142">0</text>
      <text x="368" y="86">0</text><text x="400" y="86">5</text><text x="432" y="86">3</text>
      <text class="fig-muted" x="368" y="114">∞</text><text x="400" y="114">0</text><text x="432" y="114">−3</text>
      <text x="368" y="142">4</text><text class="fig-blue" x="400" y="142">9</text><text x="432" y="142">0</text>
      <text x="484" y="86">0</text><text x="516" y="86">5</text><text class="fig-green" x="548" y="86">2</text>
      <text class="fig-muted" x="484" y="114">∞</text><text x="516" y="114">0</text><text x="548" y="114">−3</text>
      <text x="484" y="142">4</text><text x="516" y="142">9</text><text x="548" y="142">0</text>
      <text x="597" y="86">0</text><text x="627" y="86">5</text><text x="658" y="86">2</text>
      <text class="fig-accent" x="597" y="114">1</text><text x="627" y="114">0</text><text x="658" y="114">−3</text>
      <text x="597" y="142">4</text><text x="627" y="142">9</text><text x="658" y="142">0</text>
    </g>
    <rect class="fig-box" x="236" y="164" width="438" height="66" rx="5"/>
    <text class="fig-blue" x="248" y="182" font-size="10.5">• k=0：A[2][1] = min(∞, A[2][0] + A[0][1]) = 4 + 5 = 9</text>
    <text class="fig-green" x="248" y="200" font-size="10.5">• k=1：A[0][2] = min(3, A[0][1] + A[1][2]) = 5 + (−3) = 2（借助负权边缩短）</text>
    <text class="fig-accent" x="248" y="218" font-size="10.5">• k=2：A[1][0] = min(∞, A[1][2] + A[2][0]) = −3 + 4 = 1</text>
    <text class="fig-accent" x="350" y="274" text-anchor="middle" font-size="9.5">★ k 必须在最外层：算 A⁽ᵏ⁾[i][j] 时需前 k−1 个点中转已完成；若对角线 A[i][i] &lt; 0 则含负环</text>
  </svg>
  <figcaption>图 37：Floyd 以 k 为中转站逐层松弛距离矩阵；k 必须在最外层</figcaption>
</figure>

## 十二、拓扑排序与关键路径

### AOV 网与拓扑排序

用顶点表示活动、有向边表示先后关系，这样的有向无环图叫 **AOV 网**（DAG）。

**拓扑排序**：每次选一个入度为 0 的顶点输出，删除它和它发出的所有边，重复到图为空。

<figure class="cpd-figure not-content">
  <svg viewBox="0 0 680 198" role="img" aria-labelledby="ds40-fig-s5">
    <title id="ds40-fig-s5">AOV 网课程依赖图与入度消去拓扑排序</title>
    <defs>
      <marker id="ds40-arr-s5" viewBox="0 0 10 10" refX="8" refY="5" markerWidth="6" markerHeight="6" markerUnits="userSpaceOnUse" orient="auto-start-reverse">
        <path d="M1.5,1.5 L8.5,5 L1.5,8.5" fill="none" stroke="context-stroke" stroke-width="1.5" stroke-linecap="round" stroke-linejoin="round"/>
      </marker>
    </defs>
    <g stroke="var(--sl-color-gray-4)" stroke-width="1.5" fill="none" marker-end="url(#ds40-arr-s5)">
      <line x1="126" y1="54" x2="196" y2="54"/>
      <line x1="296" y1="54" x2="366" y2="88"/>
      <line x1="126" y1="132" x2="366" y2="104"/>
      <line x1="478" y1="88" x2="546" y2="54"/>
      <line x1="478" y1="104" x2="546" y2="132"/>
    </g>
    <rect class="fig-box-green" x="30" y="34" width="96" height="40" rx="6"/>
    <text class="fig-green" x="78" y="54" text-anchor="middle" font-size="13">高数</text>
    <text class="fig-muted" x="78" y="69" text-anchor="middle" font-size="10.5">入度 0（起点）</text>
    <rect class="fig-box-green" x="30" y="112" width="96" height="40" rx="6"/>
    <text class="fig-green" x="78" y="132" text-anchor="middle" font-size="13">C 语言</text>
    <text class="fig-muted" x="78" y="147" text-anchor="middle" font-size="10.5">入度 0（起点）</text>
    <rect class="fig-box" x="200" y="34" width="96" height="40" rx="6"/>
    <text class="fig-label" x="248" y="54" text-anchor="middle" font-size="13">离散</text>
    <text class="fig-muted" x="248" y="69" text-anchor="middle" font-size="10.5">入度 1</text>
    <rect class="fig-box-accent" x="370" y="76" width="108" height="40" rx="6"/>
    <text class="fig-accent" x="424" y="96" text-anchor="middle" font-size="13">数据结构</text>
    <text class="fig-muted" x="424" y="111" text-anchor="middle" font-size="10.5">入度 2（汇合）</text>
    <rect class="fig-box-blue" x="550" y="34" width="100" height="40" rx="6"/>
    <text class="fig-blue" x="600" y="54" text-anchor="middle" font-size="13">算法</text>
    <text class="fig-muted" x="600" y="69" text-anchor="middle" font-size="10.5">入度 1（可互换）</text>
    <rect class="fig-box-blue" x="550" y="112" width="100" height="40" rx="6"/>
    <text class="fig-blue" x="600" y="132" text-anchor="middle" font-size="13">操作系统</text>
    <text class="fig-muted" x="600" y="147" text-anchor="middle" font-size="10.5">入度 1（可互换）</text>
    <text class="fig-muted" x="340" y="182" text-anchor="middle" font-size="11.5">高数与 C 语言初始入度均为 0、算法与操作系统输出次序亦可互换 → 拓扑序列不唯一</text>
  </svg>
  <figcaption>AOV 网示例：按“选入度 0 顶点 → 删出边减邻居入度”反复消去</figcaption>
</figure>

例如下面两个序列都合法：

- 高数 → C 语言 → 离散 → 数据结构 → 算法 → 操作系统
- C 语言 → 高数 → 离散 → 数据结构 → 操作系统 → 算法

> **★考点**：拓扑序列**不一定唯一**；邻接表实现 $O(n+e)$；若还有顶点却找不到入度为 0 的顶点，说明图中有环，因此拓扑排序可用来判环。

**判断一个序列是否合法**：对原图每条边 $u \to v$，序列中 $u$ 必须排在 $v$ 前面。不要凭课程名称猜依赖。

> **纠错**：视频里那张 X/Y/Z 配图与“没有入度为 0 的顶点 → 有环”的结论对不上——图中 Z 的入度为 0，它其实是 DAG。判环的正确说法是：**在剩余未输出的子图中**找不到入度为 0 的顶点，才说明有环。

> **纠错**：如果课程依赖图被画成一条“高数 → C 语言 → 离散 → 数据结构 → 算法 → 操作系统”的单链，那么拓扑序列其实是唯一的，和“拓扑序列不一定唯一”矛盾。只有存在可互换的分支（如高数与 C 语言都可先学，或算法与操作系统都只依赖数据结构）时，序列才不唯一；判断合法性只看边，不看课程名称。

### AOE 网与关键路径

**AOE 网**：边表示活动（权值为持续时间），顶点表示事件。

- $ve[v]$：事件最早发生时间，从前往后取**最大值**
- $vl[v]$：事件最迟发生时间，从后往前取**最小值**
- 活动 $a$（$u \to v$，权值 $w$）：$e[a]=ve[u]$，$l[a]=vl[v]-w$
- $l-e=0$ 的活动是**关键活动**，连起来就是**关键路径**

<figure class="cpd-figure not-content" id="ds40-img1-fig-5">
  <svg viewBox="0 0 700 296" role="img" aria-label="AOE 网关键路径与事件 ve、vl 及活动 e、l 计算图示">
    <defs>
      <marker id="ds40-img1-arr-3" viewBox="0 0 10 10" refX="8" refY="5" markerWidth="6" markerHeight="6" markerUnits="userSpaceOnUse" orient="auto-start-reverse">
        <path d="M 1 1.5 L 8.5 5 L 1 8.5 z" fill="context-stroke" stroke="none"/>
      </marker>
    </defs>
    <rect class="fig-box-subtle" x="16" y="16" width="668" height="226" rx="8"/>
    <rect class="fig-box-red" x="16" y="252" width="668" height="32" rx="6"/>
    <g stroke="var(--sl-color-gray-4)" stroke-width="1.5" fill="none" marker-end="url(#ds40-img1-arr-3)">
      <line x1="76" y1="122" x2="203" y2="70"/>
      <line x1="238" y1="70" x2="412" y2="138"/>
      <line x1="239" y1="62" x2="411" y2="56"/>
      <path d="M 239 200 Q 430 236 616 142"/>
      <line x1="448" y1="63" x2="614" y2="123"/>
    </g>
    <g stroke="var(--sl-color-red)" stroke-width="2.4" fill="none" marker-end="url(#ds40-img1-arr-3)">
      <line x1="76" y1="138" x2="203" y2="190"/>
      <line x1="238" y1="192" x2="411" y2="150"/>
      <line x1="448" y1="145" x2="611" y2="132"/>
    </g>
    <g class="fig-box">
      <rect x="115" y="84" width="38" height="17" rx="3"/>
      <rect x="308" y="96" width="38" height="17" rx="3"/>
      <rect x="306" y="48" width="38" height="17" rx="3"/>
      <rect x="411" y="205" width="38" height="17" rx="3"/>
      <rect x="515" y="82" width="38" height="17" rx="3"/>
    </g>
    <g class="fig-box-red">
      <rect x="112" y="156" width="44" height="18" rx="3"/>
      <rect x="302" y="162" width="44" height="18" rx="3"/>
      <rect x="512" y="130" width="44" height="18" rx="3"/>
    </g>
    <g font-size="10.5" text-anchor="middle">
      <text class="fig-muted" x="134" y="96">a₁=3</text>
      <text class="fig-muted" x="327" y="108">a₃=2</text>
      <text class="fig-muted" x="325" y="60">a₄=3</text>
      <text class="fig-muted" x="430" y="217">a₆=3</text>
      <text class="fig-muted" x="534" y="94">a₈=1</text>
      <text class="fig-red" x="134" y="169">a₂=2 ★</text>
      <text class="fig-red" x="324" y="175">a₅=4 ★</text>
      <text class="fig-red" x="534" y="143">a₇=2 ★</text>
    </g>
    <g class="fig-box-red">
      <circle cx="60" cy="130" r="18"/>
      <circle cx="220" cy="198" r="18"/>
      <circle cx="430" cy="146" r="18"/>
      <circle cx="630" cy="130" r="18"/>
    </g>
    <g class="fig-box">
      <circle cx="220" cy="62" r="18"/>
      <circle cx="430" cy="56" r="18"/>
    </g>
    <g font-size="12" text-anchor="middle">
      <text class="fig-red" x="60" y="134">V₁</text>
      <text class="fig-label" x="220" y="66">V₂</text>
      <text class="fig-red" x="220" y="202">V₃</text>
      <text class="fig-red" x="430" y="150">V₄</text>
      <text class="fig-label" x="430" y="60">V₅</text>
      <text class="fig-red" x="630" y="134">V₆</text>
    </g>
    <g class="fig-box">
      <rect x="26" y="154" width="68" height="20" rx="4"/>
      <rect x="186" y="22" width="68" height="20" rx="4"/>
      <rect x="142" y="218" width="68" height="20" rx="4"/>
      <rect x="396" y="168" width="68" height="20" rx="4"/>
      <rect x="396" y="18" width="68" height="20" rx="4"/>
      <rect x="596" y="154" width="68" height="20" rx="4"/>
    </g>
    <g font-size="10" text-anchor="middle">
      <text class="fig-red" x="60" y="168">ve=0 vl=0</text>
      <text class="fig-muted" x="220" y="36">ve=3 vl=4</text>
      <text class="fig-red" x="176" y="232">ve=2 vl=2</text>
      <text class="fig-red" x="430" y="182">ve=6 vl=6</text>
      <text class="fig-muted" x="430" y="32">ve=6 vl=7</text>
      <text class="fig-red" x="630" y="168">ve=8 vl=8</text>
    </g>
    <text class="fig-red" x="350" y="272" text-anchor="middle" font-size="11">★ 关键路径：V₁ → V₃ → V₄ → V₆（关键活动 a₂, a₅, a₇ 均满足 l − e = 0，总工期 = 2 + 4 + 2 = 8）</text>
  </svg>
  <figcaption>图 38：AOE 网示例；红色为关键活动，节点旁标出事件最早/最迟发生时间 ve / vl</figcaption>
</figure>

**事件表**：

| 事件 | $V_1$ | $V_2$ | $V_3$ | $V_4$ | $V_5$ | $V_6$ |
|:----:|:-----:|:-----:|:-----:|:-----:|:-----:|:-----:|
| $ve$ | 0 | 3 | 2 | 6 | 6 | 8 |
| $vl$ | 0 | 4 | 2 | 6 | 7 | 8 |

**活动表**：

| 活动 | $a_1$ | $a_2$ | $a_3$ | $a_4$ | $a_5$ | $a_6$ | $a_7$ | $a_8$ |
|:----:|:-----:|:-----:|:-----:|:-----:|:-----:|:-----:|:-----:|:-----:|
| $e$ | 0 | 0 | 3 | 3 | 2 | 2 | 6 | 6 |
| $l$ | 1 | 0 | 4 | 4 | 2 | 5 | 6 | 7 |
| $l-e$ | 1 | 0 | 1 | 1 | 0 | 3 | 0 | 1 |

关键活动：$a_2,a_5,a_7$；关键路径：$V_1 \to V_3 \to V_4 \to V_6$；工期 $2+4+2=8$。

> **★考点**：
> 1. 缩短关键活动可以缩短工期；缩短非关键活动没有用。
> 2. 关键活动不能无限缩短；缩短过多，关键路径可能转移到原本的次长路径。
> 3. 有多条关键路径时，必须**同时缩短每条关键路径**上的活动，只缩短其中一条不能缩短总工期。

**验算**：把所有 $l-e=0$ 的活动连起来后，路径上的权值之和应等于终点的 $ve$（本例 $2+4+2=8$）；若对不上，说明连线或计算有误。

## 十三、查找

### 平均查找长度 ASL

$$
ASL=\sum_{i=1}^{n} p_i c_i
$$

$p_i$ 是查找第 $i$ 个元素的概率，$c_i$ 是找到它需要的比较次数。等概率时就是平均比较次数。

### 顺序查找

从头到尾逐个比较。

- 查找成功：$ASL=\dfrac{n+1}{2}$
- 时间复杂度 $O(n)$
- 有序表可以提前结束：遇到比目标大的元素就说明不存在

### 折半查找

只适用于**有序的顺序表**，不能用于链表。

每次和中间元素比较，目标更小去左半边，更大去右半边，范围每次缩小一半，复杂度 $O(\log n)$。

比较过程可以画成一棵**判定树**：最多比较次数不超过树高，即 $\lceil \log_2(n+1) \rceil$。

> **★考点**：折半查找必须“有序 + 顺序存储”；判定树的比较次数与 ASL 计算是高频题。

<figure class="cpd-figure not-content" id="ds40-img2-fig-39">
  <svg viewBox="0 0 700 250" role="img" aria-label="11 个元素的折半查找判定树">
    <defs>
      <marker id="ds40-img2-arr-39" viewBox="0 0 10 10" refX="8" refY="5" markerWidth="6" markerHeight="6" markerUnits="userSpaceOnUse" orient="auto">
        <path d="M 1 1.5 L 8.5 5 L 1 8.5 z" fill="context-stroke" stroke="none"/>
      </marker>
    </defs>
    <text x="350" y="22" text-anchor="middle" class="fig-title">折半查找判定树（n = 11，mid = ⌊(low + high) / 2⌋）</text>
    <!-- Level 1 -> Level 2 -->
    <line x1="336" y1="62" x2="188" y2="98" class="fig-line"/>
    <line x1="364" y1="62" x2="512" y2="98" class="fig-line"/>
    <!-- Level 2 -> Level 3 -->
    <line x1="166" y1="118" x2="98" y2="152" class="fig-line"/>
    <line x1="194" y1="118" x2="262" y2="152" class="fig-line"/>
    <line x1="506" y1="118" x2="438" y2="152" class="fig-line"/>
    <line x1="534" y1="118" x2="602" y2="152" class="fig-line"/>
    <!-- Level 3 -> Level 4 -->
    <line x1="80" y1="170" x2="52" y2="204" class="fig-line"/>
    <line x1="100" y1="170" x2="128" y2="204" class="fig-line"/>
    <line x1="278" y1="170" x2="306" y2="204" class="fig-line"/>
    <line x1="422" y1="170" x2="394" y2="204" class="fig-line"/>
    <line x1="618" y1="170" x2="646" y2="204" class="fig-line"/>
    <!-- Nodes -->
    <circle cx="350" cy="54" r="16" class="fig-box-accent"/>
    <text x="350" y="59" text-anchor="middle" class="fig-accent">6</text>
    <circle cx="180" cy="108" r="15" class="fig-box-blue"/>
    <text x="180" y="113" text-anchor="middle" class="fig-blue">3</text>
    <circle cx="520" cy="108" r="15" class="fig-box-blue"/>
    <text x="520" y="113" text-anchor="middle" class="fig-blue">9</text>
    <circle cx="90" cy="162" r="14" class="fig-box"/>
    <text x="90" y="167" text-anchor="middle" class="fig-label">1</text>
    <circle cx="270" cy="162" r="14" class="fig-box"/>
    <text x="270" y="167" text-anchor="middle" class="fig-label">4</text>
    <circle cx="430" cy="162" r="14" class="fig-box"/>
    <text x="430" y="167" text-anchor="middle" class="fig-label">7</text>
    <circle cx="610" cy="162" r="14" class="fig-box"/>
    <text x="610" y="167" text-anchor="middle" class="fig-label">10</text>
    <circle cx="46" cy="214" r="13" class="fig-box-subtle"/>
    <text x="46" y="219" text-anchor="middle" class="fig-muted">…</text>
    <circle cx="134" cy="214" r="13" class="fig-box-green"/>
    <text x="134" y="219" text-anchor="middle" class="fig-green">2</text>
    <circle cx="312" cy="214" r="13" class="fig-box-green"/>
    <text x="312" y="219" text-anchor="middle" class="fig-green">5</text>
    <circle cx="388" cy="214" r="13" class="fig-box-green"/>
    <text x="388" y="219" text-anchor="middle" class="fig-green">8</text>
    <circle cx="652" cy="214" r="13" class="fig-box-green"/>
    <text x="652" y="219" text-anchor="middle" class="fig-green">11</text>
    <!-- Level annotations -->
    <text x="680" y="58" text-anchor="end" class="fig-muted">第 1 层：1×1 = 1</text>
    <text x="680" y="112" text-anchor="end" class="fig-muted">第 2 层：2×2 = 4</text>
    <text x="680" y="166" text-anchor="end" class="fig-muted">第 3 层：3×4 = 12</text>
    <text x="680" y="218" text-anchor="end" class="fig-muted">第 4 层：4×4 = 16</text>
  </svg>
  <figcaption>图 39：11 个元素的判定树；$ASL=(1+4+12+16)/11=3$</figcaption>
</figure>

### 分块查找

块内可以无序，但**块与块之间有序**（后一块的所有关键字都大于前一块）。

先查索引表确定目标在哪一块，再在块内顺序查找。

- 索引表存每块的最大关键字
- 平均性能介于顺序查找和折半查找之间，是一种“折中”

<figure class="cpd-figure not-content" id="ds40-img2-fig-40">
  <svg viewBox="0 0 700 220" role="img" aria-label="分块查找索引表与数据块结构">
    <defs>
      <marker id="ds40-img2-arr-40" viewBox="0 0 10 10" refX="8" refY="5" markerWidth="6" markerHeight="6" markerUnits="userSpaceOnUse" orient="auto">
        <path d="M 1 1.5 L 8.5 5 L 1 8.5 z" fill="context-stroke" stroke="none"/>
      </marker>
    </defs>
    <text x="350" y="22" text-anchor="middle" class="fig-title">分块查找：块间有序（索引表），块内无序</text>
    <!-- Index table -->
    <text x="115" y="48" text-anchor="middle" class="fig-blue">索引表（按最大关键字有序）</text>
    <rect x="40" y="58" width="150" height="36" rx="4" class="fig-box-blue"/>
    <text x="78" y="81" text-anchor="middle" class="fig-blue">max: 24</text>
    <line x1="115" y1="58" x2="115" y2="94" class="fig-divider"/>
    <text x="152" y="81" text-anchor="middle" class="fig-muted">addr: 0</text>
    <rect x="40" y="102" width="150" height="36" rx="4" class="fig-box-green"/>
    <text x="78" y="125" text-anchor="middle" class="fig-green">max: 53</text>
    <line x1="115" y1="102" x2="115" y2="138" class="fig-divider"/>
    <text x="152" y="125" text-anchor="middle" class="fig-muted">addr: 4</text>
    <rect x="40" y="146" width="150" height="36" rx="4" class="fig-box-orange"/>
    <text x="78" y="169" text-anchor="middle" class="fig-orange">max: 88</text>
    <line x1="115" y1="146" x2="115" y2="182" class="fig-divider"/>
    <text x="152" y="169" text-anchor="middle" class="fig-muted">addr: 8</text>
    <!-- Pointers -->
    <line x1="190" y1="76" x2="262" y2="76" class="fig-line-accent" marker-end="url(#ds40-img2-arr-40)"/>
    <line x1="190" y1="120" x2="262" y2="120" class="fig-line-accent" marker-end="url(#ds40-img2-arr-40)"/>
    <line x1="190" y1="164" x2="262" y2="164" class="fig-line-accent" marker-end="url(#ds40-img2-arr-40)"/>
    <!-- Data blocks -->
    <text x="455" y="48" text-anchor="middle" class="fig-label">主表数据块（块内顺序查找）</text>
    <!-- Block 1 -->
    <rect x="270" y="58" width="370" height="36" rx="4" class="fig-box"/>
    <text x="316" y="81" text-anchor="middle" class="fig-label">24</text>
    <line x1="362" y1="58" x2="362" y2="94" class="fig-divider"/>
    <text x="408" y="81" text-anchor="middle" class="fig-label">10</text>
    <line x1="455" y1="58" x2="455" y2="94" class="fig-divider"/>
    <text x="501" y="81" text-anchor="middle" class="fig-label">18</text>
    <line x1="548" y1="58" x2="548" y2="94" class="fig-divider"/>
    <text x="594" y="81" text-anchor="middle" class="fig-label">7</text>
    <!-- Block 2 -->
    <rect x="270" y="102" width="370" height="36" rx="4" class="fig-box"/>
    <text x="316" y="125" text-anchor="middle" class="fig-label">46</text>
    <line x1="362" y1="102" x2="362" y2="138" class="fig-divider"/>
    <text x="408" y="125" text-anchor="middle" class="fig-label">30</text>
    <line x1="455" y1="102" x2="455" y2="138" class="fig-divider"/>
    <text x="501" y="125" text-anchor="middle" class="fig-green">53</text>
    <line x1="548" y1="102" x2="548" y2="138" class="fig-divider"/>
    <text x="594" y="125" text-anchor="middle" class="fig-label">38</text>
    <!-- Block 3 -->
    <rect x="270" y="146" width="370" height="36" rx="4" class="fig-box"/>
    <text x="316" y="169" text-anchor="middle" class="fig-label">72</text>
    <line x1="362" y1="146" x2="362" y2="182" class="fig-divider"/>
    <text x="408" y="169" text-anchor="middle" class="fig-label">60</text>
    <line x1="455" y1="146" x2="455" y2="182" class="fig-divider"/>
    <text x="501" y="169" text-anchor="middle" class="fig-label">88</text>
    <line x1="548" y1="146" x2="548" y2="182" class="fig-divider"/>
    <text x="594" y="169" text-anchor="middle" class="fig-label">65</text>
    <text x="350" y="206" text-anchor="middle" class="fig-muted">① 折半/顺序查索引表定位块 → ② 块内顺序查找</text>
  </svg>
  <figcaption>图 40：先查索引表定位块，再在块内顺序查找</figcaption>
</figure>

### 二叉排序树（BST）

规则：左子树上所有结点都小于根，右子树上所有结点都大于根。

- **中序遍历**得到递增序列
- 查找：比根小往左走，比根大往右走
- 插入：一路比较，走到空位放下
- 删除：
  - 叶子：直接删
  - 只有一个孩子：让孩子顶替
  - 有两个孩子：用**中序后继**（右子树最小结点）或中序前驱替代，再删除替代结点

> **★考点**：按有序顺序插入会退化成一条链，查找从 $O(\log n)$ 退化为 $O(n)$，这正是引入平衡树的原因。

<figure class="cpd-figure not-content">
  <svg viewBox="0 0 680 226" role="img" aria-labelledby="ds40-fig-s6">
    <title id="ds40-fig-s6">二叉排序树双孩子删除（中序后继替换）与有序插入退化</title>
    <defs>
      <marker id="ds40-arr-s6" viewBox="0 0 10 10" refX="8" refY="5" markerWidth="6" markerHeight="6" markerUnits="userSpaceOnUse" orient="auto-start-reverse">
        <path d="M1.5,1.5 L8.5,5 L1.5,8.5" fill="none" stroke="context-stroke" stroke-width="1.5" stroke-linecap="round" stroke-linejoin="round"/>
      </marker>
    </defs>
    <text class="fig-title" x="215" y="20" text-anchor="middle" font-size="12.5">删除双孩子结点 50：用右子树最小结点（中序后继 60）顶替</text>
    <text class="fig-title" x="565" y="20" text-anchor="middle" font-size="12.5">有序插入退化 O(n)</text>
    <g class="fig-line">
      <line x1="110" y1="54" x2="62" y2="106"/><line x1="110" y1="54" x2="158" y2="106"/>
      <line x1="158" y1="106" x2="132" y2="162"/><line x1="158" y1="106" x2="184" y2="162"/>
      <line x1="335" y1="54" x2="287" y2="106"/><line x1="335" y1="54" x2="383" y2="106"/>
      <line x1="383" y1="106" x2="409" y2="162"/>
    </g>
    <line class="fig-line-dashed" x1="383" y1="106" x2="357" y2="162"/>
    <g class="fig-box">
      <circle cx="62" cy="106" r="16"/><circle cx="158" cy="106" r="16"/><circle cx="184" cy="162" r="16"/>
      <circle cx="287" cy="106" r="16"/><circle cx="383" cy="106" r="16"/><circle cx="409" cy="162" r="16"/>
    </g>
    <circle class="fig-box-red" cx="110" cy="54" r="16"/>
    <circle class="fig-box-green" cx="132" cy="162" r="16"/>
    <circle class="fig-box-green" cx="335" cy="54" r="16"/>
    <circle class="fig-box-subtle" cx="357" cy="162" r="15"/>
    <g font-size="12" text-anchor="middle">
      <text class="fig-red" x="110" y="59">50</text>
      <text class="fig-label" x="62" y="111">30</text><text class="fig-label" x="158" y="111">70</text>
      <text class="fig-green" x="132" y="167">60</text><text class="fig-label" x="184" y="167">80</text>
      <text class="fig-green" x="335" y="59">60</text>
      <text class="fig-label" x="287" y="111">30</text><text class="fig-label" x="383" y="111">70</text>
      <text class="fig-muted" x="357" y="167">删</text><text class="fig-label" x="409" y="167">80</text>
    </g>
    <path d="M124,148 C104,120 100,90 106,72" fill="none" stroke="var(--sl-color-green)" stroke-width="1.4" stroke-dasharray="3 2" marker-end="url(#ds40-arr-s6)"/>
    <line stroke="var(--sl-color-accent)" stroke-width="1.6" x1="214" y1="106" x2="254" y2="106" marker-end="url(#ds40-arr-s6)"/>
    <line class="fig-divider" x1="454" y1="16" x2="454" y2="205"/>
    <g stroke="var(--sl-color-orange)" stroke-width="1.5" fill="none">
      <line x1="505" y1="48" x2="545" y2="88"/>
      <line x1="545" y1="88" x2="585" y2="128"/>
      <line x1="585" y1="128" x2="625" y2="168"/>
    </g>
    <g class="fig-box-orange">
      <circle cx="505" cy="48" r="15"/><circle cx="545" cy="88" r="15"/>
      <circle cx="585" cy="128" r="15"/><circle cx="625" cy="168" r="15"/>
    </g>
    <g class="fig-orange" font-size="12" text-anchor="middle">
      <text x="505" y="53">10</text><text x="545" y="93">20</text>
      <text x="585" y="133">30</text><text x="625" y="173">40</text>
    </g>
    <text class="fig-muted" x="225" y="208" text-anchor="middle" font-size="11.5">中序序列 30, 50, 60, 70, 80 → 替换后 30, 60, 70, 80 仍严格递增</text>
    <text class="fig-muted" x="565" y="208" text-anchor="middle" font-size="11.5">依次插入 10,20,30,40 成单支树</text>
  </svg>
  <figcaption>BST 核心操作与退化：双孩子结点转化为叶/单孩子删除；递增插入导致树高退化为 n</figcaption>
</figure>

### 平衡二叉树（AVL）

每个结点的**平衡因子** = 左子树高度 − 右子树高度，只能是 $-1,0,1$。

插入后若某结点 $|BF| \ge 2$，从插入点向上找到**第一个**失衡结点，以它为根的子树叫**最小不平衡子树**，只调整这棵子树。

| 类型 | 走向 | 旋转 |
|:----:|:----:|:-----|
| LL | 左 → 左 | 右单旋 |
| RR | 右 → 右 | 左单旋 |
| LR | 左 → 右 | 先左旋，再右旋 |
| RL | 右 → 左 | 先右旋，再左旋 |

**秒杀口诀**：参与调整的三个结点中，**数值居中的提起来当根，小的放左边，大的放右边**。

> **★考点**：只调整“最小不平衡子树”，不要动更上面的结点；四类旋转的方向不能记反。

<figure class="cpd-figure not-content" id="ds40-img2-fig-41">
  <svg viewBox="0 0 700 230" role="img" aria-label="AVL 树四种失衡与对应旋转">
    <defs>
      <marker id="ds40-img2-arr-41" viewBox="0 0 10 10" refX="8" refY="5" markerWidth="6" markerHeight="6" markerUnits="userSpaceOnUse" orient="auto">
        <path d="M 1 1.5 L 8.5 5 L 1 8.5 z" fill="context-stroke" stroke="none"/>
      </marker>
    </defs>
    <text x="350" y="22" text-anchor="middle" class="fig-title">AVL 树四种失衡类型与旋转方向</text>
    <!-- LL -->
    <rect x="20" y="38" width="155" height="174" rx="6" class="fig-box-subtle"/>
    <text x="97" y="58" text-anchor="middle" class="fig-blue">LL 型 → 右单旋</text>
    <line x1="115" y1="84" x2="85" y2="112" class="fig-line"/>
    <line x1="85" y1="112" x2="55" y2="140" class="fig-line"/>
    <circle cx="115" cy="84" r="12" class="fig-box-red"/>
    <text x="115" y="88" text-anchor="middle" class="fig-red">A</text>
    <circle cx="85" cy="112" r="12" class="fig-box-blue"/>
    <text x="85" y="116" text-anchor="middle" class="fig-blue">B</text>
    <circle cx="55" cy="140" r="12" class="fig-box"/>
    <text x="55" y="144" text-anchor="middle" class="fig-label">C</text>
    <text x="97" y="176" text-anchor="middle" class="fig-muted">B 上提，A 右下</text>
    <text x="97" y="196" text-anchor="middle" class="fig-muted">B(C, A)</text>
    <!-- RR -->
    <rect x="188" y="38" width="155" height="174" rx="6" class="fig-box-subtle"/>
    <text x="265" y="58" text-anchor="middle" class="fig-green">RR 型 → 左单旋</text>
    <line x1="235" y1="84" x2="265" y2="112" class="fig-line"/>
    <line x1="265" y1="112" x2="295" y2="140" class="fig-line"/>
    <circle cx="235" cy="84" r="12" class="fig-box-red"/>
    <text x="235" y="88" text-anchor="middle" class="fig-red">A</text>
    <circle cx="265" cy="112" r="12" class="fig-box-green"/>
    <text x="265" y="116" text-anchor="middle" class="fig-green">B</text>
    <circle cx="295" cy="140" r="12" class="fig-box"/>
    <text x="295" y="144" text-anchor="middle" class="fig-label">C</text>
    <text x="265" y="176" text-anchor="middle" class="fig-muted">B 上提，A 左下</text>
    <text x="265" y="196" text-anchor="middle" class="fig-muted">B(A, C)</text>
    <!-- LR -->
    <rect x="356" y="38" width="155" height="174" rx="6" class="fig-box-subtle"/>
    <text x="433" y="58" text-anchor="middle" class="fig-orange">LR 型 → 先左后右</text>
    <line x1="450" y1="84" x2="415" y2="112" class="fig-line"/>
    <line x1="415" y1="112" x2="445" y2="140" class="fig-line"/>
    <circle cx="450" cy="84" r="12" class="fig-box-red"/>
    <text x="450" y="88" text-anchor="middle" class="fig-red">A</text>
    <circle cx="415" cy="112" r="12" class="fig-box"/>
    <text x="415" y="116" text-anchor="middle" class="fig-label">B</text>
    <circle cx="445" cy="140" r="12" class="fig-box-orange"/>
    <text x="445" y="144" text-anchor="middle" class="fig-orange">C</text>
    <text x="433" y="176" text-anchor="middle" class="fig-muted">C 最终上位为根</text>
    <text x="433" y="196" text-anchor="middle" class="fig-muted">C(B, A)</text>
    <!-- RL -->
    <rect x="524" y="38" width="155" height="174" rx="6" class="fig-box-subtle"/>
    <text x="601" y="58" text-anchor="middle" class="fig-purple">RL 型 → 先右后左</text>
    <line x1="584" y1="84" x2="619" y2="112" class="fig-line"/>
    <line x1="619" y1="112" x2="589" y2="140" class="fig-line"/>
    <circle cx="584" cy="84" r="12" class="fig-box-red"/>
    <text x="584" y="88" text-anchor="middle" class="fig-red">A</text>
    <circle cx="619" cy="112" r="12" class="fig-box"/>
    <text x="619" y="116" text-anchor="middle" class="fig-label">B</text>
    <circle cx="589" cy="140" r="12" class="fig-box-purple"/>
    <text x="589" y="144" text-anchor="middle" class="fig-purple">C</text>
    <text x="601" y="176" text-anchor="middle" class="fig-muted">C 最终上位为根</text>
    <text x="601" y="196" text-anchor="middle" class="fig-muted">C(A, B)</text>
  </svg>
  <figcaption>图 41：LL/RR/LR/RL 四种失衡与对应旋转</figcaption>
</figure>

<figure class="cpd-figure not-content" id="ds40-img2-fig-42">
  <svg viewBox="0 0 700 200" role="img" aria-label="最小不平衡子树与 RR 左旋示例">
    <defs>
      <marker id="ds40-img2-arr-42" viewBox="0 0 10 10" refX="8" refY="5" markerWidth="6" markerHeight="6" markerUnits="userSpaceOnUse" orient="auto">
        <path d="M 1 1.5 L 8.5 5 L 1 8.5 z" fill="context-stroke" stroke="none"/>
      </marker>
    </defs>
    <text x="350" y="22" text-anchor="middle" class="fig-title">最小不平衡子树定位与 RR 左旋（插入 99）</text>
    <!-- Before -->
    <text x="165" y="46" text-anchor="middle" class="fig-muted">失衡时（90 为最小不平衡子树根）</text>
    <line x1="120" y1="68" x2="165" y2="104" class="fig-line"/>
    <line x1="165" y1="104" x2="210" y2="140" class="fig-line"/>
    <circle cx="120" cy="68" r="15" class="fig-box-red"/>
    <text x="120" y="73" text-anchor="middle" class="fig-red">90</text>
    <text x="92" y="73" text-anchor="end" class="fig-red">BF=-2</text>
    <circle cx="165" cy="104" r="15" class="fig-box-blue"/>
    <text x="165" y="109" text-anchor="middle" class="fig-blue">95</text>
    <circle cx="210" cy="140" r="15" class="fig-box-accent"/>
    <text x="210" y="145" text-anchor="middle" class="fig-accent">99</text>
    <!-- Arrow -->
    <line x1="275" y1="105" x2="405" y2="105" class="fig-line-accent" marker-end="url(#ds40-img2-arr-42)"/>
    <text x="340" y="95" text-anchor="middle" class="fig-accent">RR 左旋</text>
    <!-- After -->
    <text x="530" y="46" text-anchor="middle" class="fig-green">旋转后（95 上位，恢复平衡）</text>
    <line x1="530" y1="74" x2="480" y2="124" class="fig-line"/>
    <line x1="530" y1="74" x2="580" y2="124" class="fig-line"/>
    <circle cx="530" cy="74" r="15" class="fig-box-green"/>
    <text x="530" y="79" text-anchor="middle" class="fig-green">95</text>
    <circle cx="480" cy="124" r="15" class="fig-box"/>
    <text x="480" y="129" text-anchor="middle" class="fig-label">90</text>
    <circle cx="580" cy="124" r="15" class="fig-box"/>
    <text x="580" y="129" text-anchor="middle" class="fig-label">99</text>
    <text x="350" y="182" text-anchor="middle" class="fig-muted">只需调整最小不平衡子树，其祖先平衡因子自动恢复</text>
  </svg>
  <figcaption>图 42：插入 99 后，90 是最小不平衡子树的根，做 RR 左旋，95 上位</figcaption>
</figure>

> **★考点**：高度为 $h$ 的 AVL 树最少结点数递推：
> $$
> N(h)=N(h-1)+N(h-2)+1
> $$
> $N(1)=1,\ N(2)=2,\ N(3)=4,\ N(4)=7,\ N(5)=12$。

### 红黑树

- 结点非红即黑，根结点是黑色
- 红结点的孩子必须是黑色（红结点不能相邻）
- 从任一结点到所有叶子的路径上，黑结点个数相同

要求比 AVL 宽松，插入删除时旋转更少，因此被广泛用于各种语言的标准库。

### B 树与 B+ 树

**B 树**：多路平衡查找树，一个结点存多个关键字、有多个分支，树很矮，适合磁盘存储，减少读盘次数。

$M$ 阶 B 树：

- 每个结点最多 $M$ 棵子树（$M-1$ 个关键字）
- 除根以外的非叶结点至少有 $\lceil M/2 \rceil$ 棵子树
- 所有叶子在同一层

**B+ 树**：B 树的变体。

- 所有数据都存放在叶子层
- 叶子之间用指针串成链表，范围查询很方便
- 数据库索引大多使用 B+ 树

<figure class="cpd-figure not-content" id="ds40-img2-fig-43">
  <svg viewBox="0 0 700 215" role="img" aria-label="4 阶 B 树结构示意图">
    <text x="350" y="22" text-anchor="middle" class="fig-title">4 阶 B 树（m = 4）：最多 3 个关键字、4 棵子树，所有叶结点在同一层</text>
    <!-- Edges -->
    <line x1="305" y1="76" x2="120" y2="112" class="fig-line"/>
    <line x1="350" y1="76" x2="350" y2="112" class="fig-line"/>
    <line x1="395" y1="76" x2="580" y2="112" class="fig-line"/>
    <!-- Root -->
    <rect x="285" y="42" width="130" height="34" rx="4" class="fig-box-accent"/>
    <text x="320" y="64" text-anchor="middle" class="fig-accent">25</text>
    <line x1="350" y1="42" x2="350" y2="76" class="fig-divider"/>
    <text x="380" y="64" text-anchor="middle" class="fig-accent">50</text>
    <!-- Level 2 Left -->
    <rect x="45" y="112" width="150" height="34" rx="4" class="fig-box-blue"/>
    <text x="82" y="134" text-anchor="middle" class="fig-blue">10</text>
    <line x1="120" y1="112" x2="120" y2="146" class="fig-divider"/>
    <text x="157" y="134" text-anchor="middle" class="fig-blue">18</text>
    <!-- Level 2 Mid -->
    <rect x="260" y="112" width="180" height="34" rx="4" class="fig-box-blue"/>
    <text x="290" y="134" text-anchor="middle" class="fig-blue">30</text>
    <line x1="320" y1="112" x2="320" y2="146" class="fig-divider"/>
    <text x="350" y="134" text-anchor="middle" class="fig-blue">38</text>
    <line x1="380" y1="112" x2="380" y2="146" class="fig-divider"/>
    <text x="410" y="134" text-anchor="middle" class="fig-blue">45</text>
    <!-- Level 2 Right -->
    <rect x="505" y="112" width="150" height="34" rx="4" class="fig-box-blue"/>
    <text x="542" y="134" text-anchor="middle" class="fig-blue">65</text>
    <line x1="580" y1="112" x2="580" y2="146" class="fig-divider"/>
    <text x="617" y="134" text-anchor="middle" class="fig-blue">80</text>
    <!-- Failure / leaf pointers -->
    <text x="350" y="176" text-anchor="middle" class="fig-muted">↓ 下层为外部失败结点（NULL 指针），共 n + 1 个</text>
    <text x="350" y="198" text-anchor="middle" class="fig-muted">非根结点关键字数：⌈m/2⌉ − 1 ≤ k ≤ m − 1（即 1 ≤ k ≤ 3）</text>
  </svg>
  <figcaption>图 43：4 阶 B 树；结点多关键字、多分支，树高很低</figcaption>
</figure>

<figure class="cpd-figure not-content" id="ds40-img2-fig-44">
  <svg viewBox="0 0 700 220" role="img" aria-label="B+ 树结构与叶子链表示意图">
    <defs>
      <marker id="ds40-img2-arr-44" viewBox="0 0 10 10" refX="8" refY="5" markerWidth="6" markerHeight="6" markerUnits="userSpaceOnUse" orient="auto">
        <path d="M 1 1.5 L 8.5 5 L 1 8.5 z" fill="context-stroke" stroke="none"/>
      </marker>
    </defs>
    <text x="350" y="22" text-anchor="middle" class="fig-title">B+ 树：内部结点仅作索引，全部关键字与记录指针都在叶子结点</text>
    <!-- Edges -->
    <line x1="315" y1="74" x2="130" y2="120" class="fig-line"/>
    <line x1="350" y1="74" x2="350" y2="120" class="fig-line"/>
    <line x1="385" y1="74" x2="570" y2="120" class="fig-line"/>
    <!-- Root -->
    <rect x="260" y="40" width="180" height="34" rx="4" class="fig-box-accent"/>
    <text x="305" y="62" text-anchor="middle" class="fig-accent">25</text>
    <line x1="350" y1="40" x2="350" y2="74" class="fig-divider"/>
    <text x="395" y="62" text-anchor="middle" class="fig-accent">50</text>
    <!-- Leaf 1 -->
    <rect x="40" y="120" width="175" height="38" rx="4" class="fig-box-green"/>
    <text x="84" y="144" text-anchor="middle" class="fig-green">10</text>
    <line x1="128" y1="120" x2="128" y2="158" class="fig-divider"/>
    <text x="171" y="144" text-anchor="middle" class="fig-green">25</text>
    <!-- Leaf 2 -->
    <rect x="262" y="120" width="175" height="38" rx="4" class="fig-box-green"/>
    <text x="306" y="144" text-anchor="middle" class="fig-green">35</text>
    <line x1="350" y1="120" x2="350" y2="158" class="fig-divider"/>
    <text x="393" y="144" text-anchor="middle" class="fig-green">50</text>
    <!-- Leaf 3 -->
    <rect x="485" y="120" width="175" height="38" rx="4" class="fig-box-green"/>
    <text x="529" y="144" text-anchor="middle" class="fig-green">65</text>
    <line x1="572" y1="120" x2="572" y2="158" class="fig-divider"/>
    <text x="616" y="144" text-anchor="middle" class="fig-green">80</text>
    <!-- Leaf linked list arrows -->
    <line x1="215" y1="139" x2="256" y2="139" class="fig-line-accent" marker-end="url(#ds40-img2-arr-44)"/>
    <line x1="437" y1="139" x2="479" y2="139" class="fig-line-accent" marker-end="url(#ds40-img2-arr-44)"/>
    <text x="350" y="184" text-anchor="middle" class="fig-muted">叶子结点通过有序链表串联，支持范围查询与全表顺序扫描</text>
    <text x="350" y="204" text-anchor="middle" class="fig-muted">内部结点无数据指针，单块可存更多索引项，扇出更大、树更矮</text>
  </svg>
  <figcaption>图 44：B+ 树数据全在叶子，叶子链表支持范围查询</figcaption>
</figure>

> **★考点**：B 树与 B+ 树的区别（数据位置、叶子链表、范围查询）；$M$ 阶 B 树的子树数上下界。

### 散列表

用散列函数根据关键字直接算出存储地址。

**除留余数法**：

$$
H(key)=key \bmod p
$$

$p$ 一般取不大于表长 $m$ 的最大质数，地址分布更均匀。

不同关键字算出同一地址叫**冲突**，无法完全避免，只能处理。

**开放定址法**：地址被占了就找别的空位。

- 线性探测：$d_i=1,2,3,\dots$，往后一格一格找
- 平方探测：$d_i=1^2,-1^2,2^2,-2^2,\dots$
- 双散列：$d_i=i \times H_2(key)$

线性探测会产生**聚集/堆积**：冲突元素扎堆，又引发更多冲突。

> **★考点**：开放定址法不能直接删除元素，只能做**删除标记**，否则会切断其他元素的查找路径。

**链地址法（拉链法）**：把散列到同一地址的元素串成链表，不会产生聚集。

**装填因子**：

$$
\alpha=\frac{n}{m}
$$

$n$ 是元素个数，$m$ 是表长。

> **★考点**：散列表的 ASL 不直接依赖元素个数 $n$，而取决于装填因子 $\alpha$；$\alpha$ 越大，冲突越多，查找越慢。

**ASL 计算例**：$m=7$，$H(key)=key \bmod 7$，依次插入 $15,22,8,30,4$，比较次数分别为 $1,2,3,3,2$：

$$
ASL_{\text{成功}}=\frac{1+2+3+3+2}{5}=\frac{11}{5}=2.2
$$

<figure class="cpd-figure not-content" id="ds40-img2-fig-45">
  <svg viewBox="0 0 700 205" role="img" aria-label="线性探测法散列表与一次聚集现象">
    <defs>
      <marker id="ds40-img2-arr-45" viewBox="0 0 10 10" refX="8" refY="5" markerWidth="6" markerHeight="6" markerUnits="userSpaceOnUse" orient="auto">
        <path d="M 1 1.5 L 8.5 5 L 1 8.5 z" fill="context-stroke" stroke="none"/>
      </marker>
    </defs>
    <text x="350" y="22" text-anchor="middle" class="fig-title">线性探测法（H(k) = k mod 7）：冲突后顺序向后探测，易产生聚集</text>
    <!-- Hash table slots 0..6 -->
    <rect x="65" y="64" width="80" height="44" rx="4" class="fig-box-subtle"/>
    <rect x="145" y="64" width="80" height="44" rx="4" class="fig-box-blue"/>
    <rect x="225" y="64" width="80" height="44" rx="4" class="fig-box-blue"/>
    <rect x="305" y="64" width="80" height="44" rx="4" class="fig-box-orange"/>
    <rect x="385" y="64" width="80" height="44" rx="4" class="fig-box-orange"/>
    <rect x="465" y="64" width="80" height="44" rx="4" class="fig-box-green"/>
    <rect x="545" y="64" width="80" height="44" rx="4" class="fig-box-subtle"/>
    <!-- Indices -->
    <text x="105" y="54" text-anchor="middle" class="fig-muted">0</text>
    <text x="185" y="54" text-anchor="middle" class="fig-muted">1</text>
    <text x="265" y="54" text-anchor="middle" class="fig-muted">2</text>
    <text x="345" y="54" text-anchor="middle" class="fig-muted">3</text>
    <text x="425" y="54" text-anchor="middle" class="fig-muted">4</text>
    <text x="505" y="54" text-anchor="middle" class="fig-muted">5</text>
    <text x="585" y="54" text-anchor="middle" class="fig-muted">6</text>
    <!-- Keys -->
    <text x="105" y="91" text-anchor="middle" class="fig-muted">—</text>
    <text x="185" y="91" text-anchor="middle" class="fig-blue">15</text>
    <text x="265" y="91" text-anchor="middle" class="fig-blue">22</text>
    <text x="345" y="91" text-anchor="middle" class="fig-orange">8</text>
    <text x="425" y="91" text-anchor="middle" class="fig-orange">9</text>
    <text x="505" y="91" text-anchor="middle" class="fig-green">12</text>
    <text x="585" y="91" text-anchor="middle" class="fig-muted">—</text>
    <!-- Probe counts -->
    <text x="185" y="130" text-anchor="middle" class="fig-muted">1 次</text>
    <text x="265" y="130" text-anchor="middle" class="fig-muted">2 次</text>
    <text x="345" y="130" text-anchor="middle" class="fig-orange">3 次</text>
    <text x="425" y="130" text-anchor="middle" class="fig-orange">3 次</text>
    <text x="505" y="130" text-anchor="middle" class="fig-muted">1 次</text>
    <!-- Clustering bracket -->
    <path d="M 150 144 L 150 154 L 540 154 L 540 144" fill="none" class="fig-line-accent"/>
    <text x="345" y="176" text-anchor="middle" class="fig-accent">连续占用的“聚集区”：非同义词（如 8、9、12）也会互相阻塞</text>
  </svg>
  <figcaption>图 45：线性探测产生聚集；比较次数 1、2、3、3、2</figcaption>
</figure>

<figure class="cpd-figure not-content" id="ds40-img2-fig-46">
  <svg viewBox="0 0 700 215" role="img" aria-label="链地址法散列表结构">
    <defs>
      <marker id="ds40-img2-arr-46" viewBox="0 0 10 10" refX="8" refY="5" markerWidth="6" markerHeight="6" markerUnits="userSpaceOnUse" orient="auto">
        <path d="M 1 1.5 L 8.5 5 L 1 8.5 z" fill="context-stroke" stroke="none"/>
      </marker>
    </defs>
    <text x="350" y="22" text-anchor="middle" class="fig-title">链地址法（拉链法）：同义词串成单链表，无聚集现象</text>
    <!-- Buckets -->
    <rect x="70" y="46" width="70" height="32" rx="4" class="fig-box-subtle"/>
    <text x="105" y="67" text-anchor="middle" class="fig-muted">0</text>
    <rect x="70" y="84" width="70" height="32" rx="4" class="fig-box-blue"/>
    <text x="105" y="105" text-anchor="middle" class="fig-blue">1</text>
    <rect x="70" y="122" width="70" height="32" rx="4" class="fig-box-green"/>
    <text x="105" y="143" text-anchor="middle" class="fig-green">2</text>
    <rect x="70" y="160" width="70" height="32" rx="4" class="fig-box-orange"/>
    <text x="105" y="181" text-anchor="middle" class="fig-orange">5</text>
    <!-- Chain 0 -->
    <text x="165" y="67" class="fig-muted">^ (NULL)</text>
    <!-- Chain 1: 15 -> 22 -> 8 -->
    <line x1="140" y1="100" x2="188" y2="100" class="fig-line-accent" marker-end="url(#ds40-img2-arr-46)"/>
    <rect x="195" y="84" width="60" height="32" rx="4" class="fig-box-blue"/>
    <text x="225" y="105" text-anchor="middle" class="fig-blue">15</text>
    <line x1="255" y1="100" x2="303" y2="100" class="fig-line-accent" marker-end="url(#ds40-img2-arr-46)"/>
    <rect x="310" y="84" width="60" height="32" rx="4" class="fig-box-blue"/>
    <text x="340" y="105" text-anchor="middle" class="fig-blue">22</text>
    <line x1="370" y1="100" x2="418" y2="100" class="fig-line-accent" marker-end="url(#ds40-img2-arr-46)"/>
    <rect x="425" y="84" width="60" height="32" rx="4" class="fig-box-blue"/>
    <text x="455" y="105" text-anchor="middle" class="fig-blue">8</text>
    <text x="500" y="105" class="fig-muted">^ (NULL)</text>
    <!-- Chain 2: 9 -->
    <line x1="140" y1="138" x2="188" y2="138" class="fig-line-accent" marker-end="url(#ds40-img2-arr-46)"/>
    <rect x="195" y="122" width="60" height="32" rx="4" class="fig-box-green"/>
    <text x="225" y="143" text-anchor="middle" class="fig-green">9</text>
    <text x="270" y="143" class="fig-muted">^ (NULL)</text>
    <!-- Chain 5: 12 -->
    <line x1="140" y1="176" x2="188" y2="176" class="fig-line-accent" marker-end="url(#ds40-img2-arr-46)"/>
    <rect x="195" y="160" width="60" height="32" rx="4" class="fig-box-orange"/>
    <text x="225" y="181" text-anchor="middle" class="fig-orange">12</text>
    <text x="270" y="181" class="fig-muted">^ (NULL)</text>
  </svg>
  <figcaption>图 46：同义词串成链表；装填因子 $\alpha=n/m$</figcaption>
</figure>

## 十四、排序

### 稳定性

若两个相等元素排序后相对次序不变，这个排序算法就是**稳定的**；否则不稳定。

### 插入排序

**直接插入排序**：像打扑克摸牌，每次把新牌插到前面已经排好的牌中合适的位置。

- 平均/最坏 $O(n^2)$，最好 $O(n)$（原本基本有序）
- 空间 $O(1)$，**稳定**

**折半插入排序**：用二分查找确定插入位置，减少比较次数，但移动次数不变，仍然是 $O(n^2)$，稳定。

**希尔排序**：按某个增量分组，组内做插入排序，再逐步缩小增量，最后增量为 1 时做一次整体插入排序。

- 前面几轮让序列“基本有序”，最后一轮很快
- 平均约 $O(n^{1.3})$（取决于增量序列），最坏 $O(n^2)$
- 空间 $O(1)$，**不稳定**

<figure class="cpd-figure not-content">
  <svg viewBox="0 0 680 284" role="img" aria-labelledby="ds40-fig-s7">
    <title id="ds40-fig-s7">直接插入排序与希尔排序过程演示</title>
    <defs>
      <marker id="ds40-arr-s7" viewBox="0 0 10 10" refX="8" refY="5" markerWidth="6" markerHeight="6" markerUnits="userSpaceOnUse" orient="auto-start-reverse">
        <path d="M1.5,1.5 L8.5,5 L1.5,8.5" fill="none" stroke="context-stroke" stroke-width="1.5" stroke-linecap="round" stroke-linejoin="round"/>
      </marker>
    </defs>
    <text class="fig-title" x="16" y="22" font-size="13">一、直接插入排序过程：[49, 38, 65, 97, 27]</text>
    <text class="fig-muted" x="28" y="54" font-size="11.5">初始</text>
    <rect class="fig-box-green" x="76" y="34" width="44" height="28" rx="4"/>
    <rect class="fig-box" x="124" y="34" width="44" height="28" rx="4"/>
    <rect class="fig-box" x="172" y="34" width="44" height="28" rx="4"/>
    <rect class="fig-box" x="220" y="34" width="44" height="28" rx="4"/>
    <rect class="fig-box" x="268" y="34" width="44" height="28" rx="4"/>
    <g font-size="12" text-anchor="middle">
      <text class="fig-green" x="98" y="52">49</text><text class="fig-label" x="146" y="52">38</text>
      <text class="fig-label" x="194" y="52">65</text><text class="fig-label" x="242" y="52">97</text><text class="fig-label" x="290" y="52">27</text>
    </g>
    <text class="fig-muted" x="326" y="52" font-size="11">有序区 [49]</text>
    <text class="fig-muted" x="28" y="92" font-size="11.5">i=1</text>
    <rect class="fig-box-blue" x="76" y="72" width="44" height="28" rx="4"/>
    <rect class="fig-box-green" x="124" y="72" width="44" height="28" rx="4"/>
    <rect class="fig-box" x="172" y="72" width="44" height="28" rx="4"/>
    <rect class="fig-box" x="220" y="72" width="44" height="28" rx="4"/>
    <rect class="fig-box" x="268" y="72" width="44" height="28" rx="4"/>
    <g font-size="12" text-anchor="middle">
      <text class="fig-blue" x="98" y="90">38</text><text class="fig-green" x="146" y="90">49</text>
      <text class="fig-label" x="194" y="90">65</text><text class="fig-label" x="242" y="90">97</text><text class="fig-label" x="290" y="90">27</text>
    </g>
    <text class="fig-blue" x="326" y="90" font-size="11">38 插入 49 前，49 后移</text>
    <text class="fig-muted" x="28" y="130" font-size="11.5">i=2,3</text>
    <rect class="fig-box-green" x="76" y="110" width="44" height="28" rx="4"/>
    <rect class="fig-box-green" x="124" y="110" width="44" height="28" rx="4"/>
    <rect class="fig-box-green" x="172" y="110" width="44" height="28" rx="4"/>
    <rect class="fig-box-green" x="220" y="110" width="44" height="28" rx="4"/>
    <rect class="fig-box-orange" x="268" y="110" width="44" height="28" rx="4"/>
    <g font-size="12" text-anchor="middle">
      <text class="fig-green" x="98" y="128">38</text><text class="fig-green" x="146" y="128">49</text>
      <text class="fig-green" x="194" y="128">65</text><text class="fig-green" x="242" y="128">97</text><text class="fig-orange" x="290" y="128">27</text>
    </g>
    <text class="fig-muted" x="326" y="128" font-size="11">65、97 直接留在尾部</text>
    <text class="fig-muted" x="28" y="168" font-size="11.5">i=4</text>
    <rect class="fig-box-orange" x="76" y="148" width="44" height="28" rx="4"/>
    <rect class="fig-box-green" x="124" y="148" width="44" height="28" rx="4"/>
    <rect class="fig-box-green" x="172" y="148" width="44" height="28" rx="4"/>
    <rect class="fig-box-green" x="220" y="148" width="44" height="28" rx="4"/>
    <rect class="fig-box-green" x="268" y="148" width="44" height="28" rx="4"/>
    <g font-size="12" text-anchor="middle">
      <text class="fig-orange" x="98" y="166">27</text><text class="fig-green" x="146" y="166">38</text>
      <text class="fig-green" x="194" y="166">49</text><text class="fig-green" x="242" y="166">65</text><text class="fig-green" x="290" y="166">97</text>
    </g>
    <text class="fig-orange" x="326" y="166" font-size="11">27 插入队首，38..97 全后移</text>
    <line class="fig-divider" x1="16" y1="190" x2="664" y2="190"/>
    <text class="fig-title" x="16" y="212" font-size="13">二、希尔排序增量分组（d = 3）：跳跃式组内插排</text>
    <rect class="fig-box-blue" x="76" y="226" width="54" height="28" rx="4"/>
    <rect class="fig-box-orange" x="136" y="226" width="54" height="28" rx="4"/>
    <rect class="fig-box-purple" x="196" y="226" width="54" height="28" rx="4"/>
    <rect class="fig-box-blue" x="256" y="226" width="54" height="28" rx="4"/>
    <rect class="fig-box-orange" x="316" y="226" width="54" height="28" rx="4"/>
    <rect class="fig-box-purple" x="376" y="226" width="54" height="28" rx="4"/>
    <g font-size="12" text-anchor="middle">
      <text class="fig-blue" x="103" y="244">49</text><text class="fig-orange" x="163" y="244">38</text>
      <text class="fig-purple" x="223" y="244">65</text><text class="fig-blue" x="283" y="244">97</text>
      <text class="fig-orange" x="343" y="244">76</text><text class="fig-purple" x="403" y="244">13</text>
    </g>
    <path d="M103,256 C103,270 283,270 283,256" fill="none" stroke="var(--sl-color-blue)" stroke-width="1.2"/>
    <path d="M163,256 C163,276 343,276 343,256" fill="none" stroke="var(--sl-color-orange)" stroke-width="1.2"/>
    <path d="M223,256 C223,280 403,280 403,256" fill="none" stroke="var(--sl-color-purple)" stroke-width="1.2"/>
    <text class="fig-muted" x="452" y="240" font-size="11.5">三色同组各自插排：</text>
    <text class="fig-purple" x="452" y="258" font-size="11.5">65 与 13 换 → [49,38,13,97,76,65]</text>
  </svg>
  <figcaption>插入排序对比：直接插入逐格挪动，希尔排序先以大增量 d=3 跨越分组使其基本有序</figcaption>
</figure>

### 交换排序

**冒泡排序**：相邻两个元素比较，逆序就交换；每一趟把当前最大值冒到最后。

- 平均/最坏 $O(n^2)$，最好 $O(n)$（某一趟没有发生交换即可提前结束）
- 空间 $O(1)$，**稳定**

<figure class="cpd-figure not-content">
  <svg viewBox="0 0 680 218" role="img" aria-labelledby="ds40-fig-s8">
    <title id="ds40-fig-s8">冒泡排序逐趟冒泡与 flag 提前结束</title>
    <text class="fig-title" x="16" y="22" font-size="13">冒泡排序第 1 趟（相邻逆序交换，最大值 97 沉底）</text>
    <text class="fig-muted" x="18" y="54" font-size="11.5">初态</text>
    <rect class="fig-box-blue" x="76" y="34" width="44" height="28" rx="4"/>
    <rect class="fig-box-blue" x="124" y="34" width="44" height="28" rx="4"/>
    <rect class="fig-box" x="172" y="34" width="44" height="28" rx="4"/>
    <rect class="fig-box" x="220" y="34" width="44" height="28" rx="4"/>
    <rect class="fig-box" x="268" y="34" width="44" height="28" rx="4"/>
    <g font-size="12" text-anchor="middle">
      <text class="fig-blue" x="98" y="52">49</text><text class="fig-blue" x="146" y="52">38</text>
      <text class="fig-label" x="194" y="52">97</text><text class="fig-label" x="242" y="52">65</text><text class="fig-label" x="290" y="52">27</text>
    </g>
    <text class="fig-blue" x="332" y="52" font-size="11.5">49 &gt; 38 交换（flag=1）</text>
    <text class="fig-muted" x="18" y="92" font-size="11.5">途中</text>
    <rect class="fig-box" x="76" y="72" width="44" height="28" rx="4"/>
    <rect class="fig-box" x="124" y="72" width="44" height="28" rx="4"/>
    <rect class="fig-box-orange" x="172" y="72" width="44" height="28" rx="4"/>
    <rect class="fig-box-orange" x="220" y="72" width="44" height="28" rx="4"/>
    <rect class="fig-box" x="268" y="72" width="44" height="28" rx="4"/>
    <g font-size="12" text-anchor="middle">
      <text class="fig-label" x="98" y="90">38</text><text class="fig-label" x="146" y="90">49</text>
      <text class="fig-orange" x="194" y="90">97</text><text class="fig-orange" x="242" y="90">65</text><text class="fig-label" x="290" y="90">27</text>
    </g>
    <text class="fig-orange" x="332" y="90" font-size="11.5">49 &lt; 97 不换；97 &gt; 65 交换</text>
    <text class="fig-muted" x="18" y="130" font-size="11.5">1 趟完</text>
    <rect class="fig-box" x="76" y="110" width="44" height="28" rx="4"/>
    <rect class="fig-box" x="124" y="110" width="44" height="28" rx="4"/>
    <rect class="fig-box" x="172" y="110" width="44" height="28" rx="4"/>
    <rect class="fig-box" x="220" y="110" width="44" height="28" rx="4"/>
    <rect class="fig-box-green" x="268" y="110" width="44" height="28" rx="4"/>
    <g font-size="12" text-anchor="middle">
      <text class="fig-label" x="98" y="128">38</text><text class="fig-label" x="146" y="128">49</text>
      <text class="fig-label" x="194" y="128">65</text><text class="fig-label" x="242" y="128">27</text><text class="fig-green" x="290" y="128">97</text>
    </g>
    <text class="fig-green" x="332" y="128" font-size="11.5">97 落至最终位置，后续不再参与比较</text>
    <text class="fig-muted" x="18" y="168" font-size="11.5">第 4 趟</text>
    <rect class="fig-box-green" x="76" y="148" width="44" height="28" rx="4"/>
    <rect class="fig-box-green" x="124" y="148" width="44" height="28" rx="4"/>
    <rect class="fig-box-green" x="172" y="148" width="44" height="28" rx="4"/>
    <rect class="fig-box-green" x="220" y="148" width="44" height="28" rx="4"/>
    <rect class="fig-box-green" x="268" y="148" width="44" height="28" rx="4"/>
    <g font-size="12" text-anchor="middle">
      <text class="fig-green" x="98" y="166">27</text><text class="fig-green" x="146" y="166">38</text>
      <text class="fig-green" x="194" y="166">49</text><text class="fig-green" x="242" y="166">65</text><text class="fig-green" x="290" y="166">97</text>
    </g>
    <text class="fig-green" x="332" y="166" font-size="11.5">若某趟全程 flag = 0（无交换），提前结束</text>
    <text class="fig-muted" x="340" y="202" text-anchor="middle" font-size="11.5">第 m 趟比较 n - m 次，相等元素不触发交换 → 排序稳定</text>
  </svg>
  <figcaption>冒泡排序：每趟将未排区最大值推至末尾；引入 flag 标志使有序输入最好时间达 O(n)</figcaption>
</figure>

**快速排序**：选一个枢轴，一趟划分把比它小的放左边、大的放右边，枢轴落到最终位置；再对左右两部分递归。

- 平均 $O(n\log n)$，最好 $O(n\log n)$
- 最坏 $O(n^2)$：序列本来有序或逆序时，每次划分都极不平衡
- 空间：递归栈平均 $O(\log n)$，最坏 $O(n)$
- **不稳定**

<figure class="cpd-figure not-content" id="ds40-img2-fig-47">
  <svg viewBox="0 0 700 220" role="img" aria-label="快速排序一趟划分过程">
    <defs>
      <marker id="ds40-img2-arr-47" viewBox="0 0 10 10" refX="8" refY="5" markerWidth="6" markerHeight="6" markerUnits="userSpaceOnUse" orient="auto">
        <path d="M 1 1.5 L 8.5 5 L 1 8.5 z" fill="context-stroke" stroke="none"/>
      </marker>
    </defs>
    <text x="350" y="22" text-anchor="middle" class="fig-title">快速排序一趟划分（pivot = 49，low / high 交替向中间扫描）</text>
    <!-- Initial array -->
    <text x="45" y="66" text-anchor="start" class="fig-muted">初始</text>
    <rect x="95" y="44" width="66" height="36" rx="4" class="fig-box-accent"/>
    <text x="128" y="67" text-anchor="middle" class="fig-accent">49</text>
    <rect x="165" y="44" width="66" height="36" rx="4" class="fig-box"/>
    <text x="198" y="67" text-anchor="middle" class="fig-label">38</text>
    <rect x="235" y="44" width="66" height="36" rx="4" class="fig-box"/>
    <text x="268" y="67" text-anchor="middle" class="fig-label">65</text>
    <rect x="305" y="44" width="66" height="36" rx="4" class="fig-box"/>
    <text x="338" y="67" text-anchor="middle" class="fig-label">97</text>
    <rect x="375" y="44" width="66" height="36" rx="4" class="fig-box"/>
    <text x="408" y="67" text-anchor="middle" class="fig-label">76</text>
    <rect x="445" y="44" width="66" height="36" rx="4" class="fig-box"/>
    <text x="478" y="67" text-anchor="middle" class="fig-label">13</text>
    <rect x="515" y="44" width="66" height="36" rx="4" class="fig-box"/>
    <text x="548" y="67" text-anchor="middle" class="fig-label">27</text>
    <rect x="585" y="44" width="66" height="36" rx="4" class="fig-box"/>
    <text x="618" y="67" text-anchor="middle" class="fig-label">49</text>
    <!-- Pointers -->
    <text x="128" y="98" text-anchor="middle" class="fig-blue">↑ low</text>
    <text x="618" y="98" text-anchor="middle" class="fig-orange">↑ high</text>
    <!-- Arrow -->
    <text x="350" y="122" text-anchor="middle" class="fig-muted">↓ high 找 &lt;49 填 low，low 找 &gt;49 填 high，直到 low == high</text>
    <!-- Final array -->
    <text x="45" y="162" text-anchor="start" class="fig-muted">结果</text>
    <rect x="95" y="140" width="66" height="36" rx="4" class="fig-box-blue"/>
    <text x="128" y="163" text-anchor="middle" class="fig-blue">27</text>
    <rect x="165" y="140" width="66" height="36" rx="4" class="fig-box-blue"/>
    <text x="198" y="163" text-anchor="middle" class="fig-blue">38</text>
    <rect x="235" y="140" width="66" height="36" rx="4" class="fig-box-blue"/>
    <text x="268" y="163" text-anchor="middle" class="fig-blue">13</text>
    <rect x="305" y="140" width="66" height="36" rx="4" class="fig-box-accent"/>
    <text x="338" y="163" text-anchor="middle" class="fig-accent">49</text>
    <rect x="375" y="140" width="66" height="36" rx="4" class="fig-box-green"/>
    <text x="408" y="163" text-anchor="middle" class="fig-green">76</text>
    <rect x="445" y="140" width="66" height="36" rx="4" class="fig-box-green"/>
    <text x="478" y="163" text-anchor="middle" class="fig-green">97</text>
    <rect x="515" y="140" width="66" height="36" rx="4" class="fig-box-green"/>
    <text x="548" y="163" text-anchor="middle" class="fig-green">65</text>
    <rect x="585" y="140" width="66" height="36" rx="4" class="fig-box-green"/>
    <text x="618" y="163" text-anchor="middle" class="fig-green">49</text>
    <text x="198" y="198" text-anchor="middle" class="fig-blue">均 ≤ 49</text>
    <text x="338" y="198" text-anchor="middle" class="fig-accent">最终位置</text>
    <text x="513" y="198" text-anchor="middle" class="fig-green">均 ≥ 49</text>
  </svg>
  <figcaption>图 47：以 49 为枢轴，low/high 交替扫描，相遇后 49 落到最终位置</figcaption>
</figure>

<figure class="cpd-figure not-content">
  <svg viewBox="0 0 680 282" role="img" aria-labelledby="ds40-fig-s10">
    <title id="ds40-fig-s10">快速排序一趟划分（pivot = 49，low/high 交替挖坑搬运）</title>
    <text class="fig-title" x="16" y="22" font-size="13">一趟划分：[49, 38, 65, 97, 76, 13, 27]（枢轴 49 暂存）</text>
    <g font-size="11.5">
      <text class="fig-muted" x="22" y="54">初始</text>
      <text class="fig-muted" x="22" y="92">① high↓</text>
      <text class="fig-muted" x="22" y="130">② low↑</text>
      <text class="fig-muted" x="22" y="168">③ high↓</text>
      <text class="fig-muted" x="22" y="206">④ low↑</text>
      <text class="fig-green" x="22" y="244">⑤ 归位</text>
    </g>
    <g font-size="12" text-anchor="middle">
      <rect class="fig-box-orange" x="88" y="34" width="44" height="28" rx="4"/><text class="fig-orange" x="110" y="52">[空]</text>
      <rect class="fig-box" x="136" y="34" width="44" height="28" rx="4"/><text class="fig-label" x="158" y="52">38</text>
      <rect class="fig-box" x="184" y="34" width="44" height="28" rx="4"/><text class="fig-label" x="206" y="52">65</text>
      <rect class="fig-box" x="232" y="34" width="44" height="28" rx="4"/><text class="fig-label" x="254" y="52">97</text>
      <rect class="fig-box" x="280" y="34" width="44" height="28" rx="4"/><text class="fig-label" x="302" y="52">76</text>
      <rect class="fig-box" x="328" y="34" width="44" height="28" rx="4"/><text class="fig-label" x="350" y="52">13</text>
      <rect class="fig-box-blue" x="376" y="34" width="44" height="28" rx="4"/><text class="fig-blue" x="398" y="52">27</text>
      <rect class="fig-box-blue" x="88" y="72" width="44" height="28" rx="4"/><text class="fig-blue" x="110" y="90">27</text>
      <rect class="fig-box" x="136" y="72" width="44" height="28" rx="4"/><text class="fig-label" x="158" y="90">38</text>
      <rect class="fig-box-blue" x="184" y="72" width="44" height="28" rx="4"/><text class="fig-blue" x="206" y="90">65</text>
      <rect class="fig-box" x="232" y="72" width="44" height="28" rx="4"/><text class="fig-label" x="254" y="90">97</text>
      <rect class="fig-box" x="280" y="72" width="44" height="28" rx="4"/><text class="fig-label" x="302" y="90">76</text>
      <rect class="fig-box" x="328" y="72" width="44" height="28" rx="4"/><text class="fig-label" x="350" y="90">13</text>
      <rect class="fig-box-orange" x="376" y="72" width="44" height="28" rx="4"/><text class="fig-orange" x="398" y="90">[空]</text>
      <rect class="fig-box" x="88" y="110" width="44" height="28" rx="4"/><text class="fig-label" x="110" y="128">27</text>
      <rect class="fig-box" x="136" y="110" width="44" height="28" rx="4"/><text class="fig-label" x="158" y="128">38</text>
      <rect class="fig-box-orange" x="184" y="110" width="44" height="28" rx="4"/><text class="fig-orange" x="206" y="128">[空]</text>
      <rect class="fig-box" x="232" y="110" width="44" height="28" rx="4"/><text class="fig-label" x="254" y="128">97</text>
      <rect class="fig-box" x="280" y="110" width="44" height="28" rx="4"/><text class="fig-label" x="302" y="128">76</text>
      <rect class="fig-box-blue" x="328" y="110" width="44" height="28" rx="4"/><text class="fig-blue" x="350" y="128">13</text>
      <rect class="fig-box-blue" x="376" y="110" width="44" height="28" rx="4"/><text class="fig-blue" x="398" y="128">65</text>
      <rect class="fig-box" x="88" y="148" width="44" height="28" rx="4"/><text class="fig-label" x="110" y="166">27</text>
      <rect class="fig-box" x="136" y="148" width="44" height="28" rx="4"/><text class="fig-label" x="158" y="166">38</text>
      <rect class="fig-box-blue" x="184" y="148" width="44" height="28" rx="4"/><text class="fig-blue" x="206" y="166">13</text>
      <rect class="fig-box-blue" x="232" y="148" width="44" height="28" rx="4"/><text class="fig-blue" x="254" y="166">97</text>
      <rect class="fig-box" x="280" y="148" width="44" height="28" rx="4"/><text class="fig-label" x="302" y="166">76</text>
      <rect class="fig-box-orange" x="328" y="148" width="44" height="28" rx="4"/><text class="fig-orange" x="350" y="166">[空]</text>
      <rect class="fig-box" x="376" y="148" width="44" height="28" rx="4"/><text class="fig-label" x="398" y="166">65</text>
      <rect class="fig-box" x="88" y="186" width="44" height="28" rx="4"/><text class="fig-label" x="110" y="204">27</text>
      <rect class="fig-box" x="136" y="186" width="44" height="28" rx="4"/><text class="fig-label" x="158" y="204">38</text>
      <rect class="fig-box" x="184" y="186" width="44" height="28" rx="4"/><text class="fig-label" x="206" y="204">13</text>
      <rect class="fig-box-orange" x="232" y="186" width="44" height="28" rx="4"/><text class="fig-orange" x="254" y="204">[空]</text>
      <rect class="fig-box" x="280" y="186" width="44" height="28" rx="4"/><text class="fig-label" x="302" y="204">76</text>
      <rect class="fig-box-blue" x="328" y="186" width="44" height="28" rx="4"/><text class="fig-blue" x="350" y="204">97</text>
      <rect class="fig-box" x="376" y="186" width="44" height="28" rx="4"/><text class="fig-label" x="398" y="204">65</text>
      <rect class="fig-box-green" x="88" y="224" width="44" height="28" rx="4"/><text class="fig-green" x="110" y="242">27</text>
      <rect class="fig-box-green" x="136" y="224" width="44" height="28" rx="4"/><text class="fig-green" x="158" y="242">38</text>
      <rect class="fig-box-green" x="184" y="224" width="44" height="28" rx="4"/><text class="fig-green" x="206" y="242">13</text>
      <rect class="fig-box-accent" x="232" y="224" width="44" height="28" rx="4"/><text class="fig-accent" x="254" y="242">49</text>
      <rect class="fig-box-purple" x="280" y="224" width="44" height="28" rx="4"/><text class="fig-purple" x="302" y="242">76</text>
      <rect class="fig-box-purple" x="328" y="224" width="44" height="28" rx="4"/><text class="fig-purple" x="350" y="242">97</text>
      <rect class="fig-box-purple" x="376" y="224" width="44" height="28" rx="4"/><text class="fig-purple" x="398" y="242">65</text>
    </g>
    <g font-size="11.5">
      <text class="fig-muted" x="436" y="52">low=0 留坑，high=6 指向 27</text>
      <text class="fig-blue" x="430" y="90">27 &lt; 49 填左坑；low 停在 65</text>
      <text class="fig-blue" x="430" y="128">65 &gt; 49 填右坑；high 搬 13</text>
      <text class="fig-blue" x="430" y="166">13 &lt; 49 填左坑；low 停在 97</text>
      <text class="fig-orange" x="430" y="204">97 &gt; 49 填右坑；high 与 low 相遇</text>
      <text class="fig-green" x="430" y="242">low==high=3，49 归位（左&lt;49，右≥49）</text>
    </g>
    <text class="fig-muted" x="340" y="272" text-anchor="middle" font-size="11.5">一趟划分后 49 确定最终位置；再对左段 [27,38,13] 与右段 [76,97,65] 递归</text>
  </svg>
  <figcaption>快速排序一趟划分全流程：high 找小填左坑、low 找大填右坑，相遇即枢轴最终位置</figcaption>
</figure>

### 选择排序

**简单选择排序**：每一趟从未排序部分选出最小元素，和未排序部分的第一个元素交换。

- 无论原序列如何，比较次数都一样，始终 $O(n^2)$
- 空间 $O(1)$，**不稳定**

<figure class="cpd-figure not-content">
  <svg viewBox="0 0 680 212" role="img" aria-labelledby="ds40-fig-s9">
    <title id="ds40-fig-s9">简单选择排序选最小与远距离交换导致不稳定</title>
    <defs>
      <marker id="ds40-arr-s9" viewBox="0 0 10 10" refX="8" refY="5" markerWidth="6" markerHeight="6" markerUnits="userSpaceOnUse" orient="auto-start-reverse">
        <path d="M1.5,1.5 L8.5,5 L1.5,8.5" fill="none" stroke="context-stroke" stroke-width="1.5" stroke-linecap="round" stroke-linejoin="round"/>
      </marker>
    </defs>
    <text class="fig-title" x="16" y="22" font-size="13">简单选择排序不稳定反例：[5₁, 5₂, 2]</text>
    <text class="fig-muted" x="26" y="92" font-size="11.5">第 1 趟前</text>
    <rect class="fig-box-orange" x="96" y="72" width="52" height="30" rx="4"/>
    <rect class="fig-box-blue" x="156" y="72" width="52" height="30" rx="4"/>
    <rect class="fig-box-green" x="216" y="72" width="52" height="30" rx="4"/>
    <g font-size="12.5" text-anchor="middle">
      <text class="fig-orange" x="122" y="92">5₁</text>
      <text class="fig-blue" x="182" y="92">5₂</text>
      <text class="fig-green" x="242" y="92">2</text>
    </g>
    <path d="M122,68 C122,46 242,46 242,68" fill="none" stroke="var(--sl-color-red)" stroke-width="1.4" marker-end="url(#ds40-arr-s9)" marker-start="url(#ds40-arr-s9)"/>
    <text class="fig-red" x="182" y="42" text-anchor="middle" font-size="11">远距离交换 5₁ ↔ 2</text>
    <line stroke="var(--sl-color-accent)" stroke-width="1.5" x1="292" y1="87" x2="340" y2="87" marker-end="url(#ds40-arr-s9)"/>
    <text class="fig-muted" x="358" y="92" font-size="11.5">第 1 趟后</text>
    <rect class="fig-box-green" x="428" y="72" width="52" height="30" rx="4"/>
    <rect class="fig-box-blue" x="488" y="72" width="52" height="30" rx="4"/>
    <rect class="fig-box-orange" x="548" y="72" width="52" height="30" rx="4"/>
    <g font-size="12.5" text-anchor="middle">
      <text class="fig-green" x="454" y="92">2</text>
      <text class="fig-blue" x="514" y="92">5₂</text>
      <text class="fig-orange" x="574" y="92">5₁</text>
    </g>
    <rect class="fig-box-subtle" x="26" y="128" width="628" height="66" rx="6"/>
    <text class="fig-label" x="42" y="152" font-size="12">结论：原本 5₁ 在 5₂ 前，一趟交换后 5₁ 被换到 5₂ 后面 → 相对次序颠倒（不稳定）</text>
    <text class="fig-muted" x="42" y="176" font-size="11.5">比较次数恒为 n(n−1)/2，与初始是否有序无关；但交换移动次数极少（最多 n − 1 次）</text>
  </svg>
  <figcaption>简单选择排序：每趟选出无序区最小元与区首交换，远距离交换破坏稳定性</figcaption>
</figure>

**堆排序**：堆是一棵完全二叉树，用数组存储，靠编号关系找父亲和孩子。

- 大根堆：每个结点都不小于孩子，堆顶是最大值
- 建堆从最后一个非叶结点 $\lfloor n/2 \rfloor$ 开始，从后往前向下调整，建堆 $O(n)$
- 排序：堆顶与最后一个元素交换，堆规模减一，再把新堆顶向下调整，重复 $n-1$ 次
- 任何情况下都是 $O(n\log n)$，空间 $O(1)$，**不稳定**

> **★考点**：堆排序特别适合 **Top-K**：求最大的 $K$ 个元素时，维护一个大小为 $K$ 的**小根堆**，而不是把全部数据建成大根堆。视频里“从海量数据中选最大的前 K 个”要按这个思路理解。

<figure class="cpd-figure not-content" id="ds40-img2-fig-48">
  <svg viewBox="0 0 700 220" role="img" aria-label="大根堆建堆与向下调整示意图">
    <defs>
      <marker id="ds40-img2-arr-48" viewBox="0 0 10 10" refX="8" refY="5" markerWidth="6" markerHeight="6" markerUnits="userSpaceOnUse" orient="auto">
        <path d="M 1 1.5 L 8.5 5 L 1 8.5 z" fill="context-stroke" stroke="none"/>
      </marker>
    </defs>
    <text x="350" y="22" text-anchor="middle" class="fig-title">大根堆：堆顶与末尾交换后，对新堆顶执行向下调整（sift-down）</text>
    <!-- Left tree: Max Heap -->
    <text x="165" y="46" text-anchor="middle" class="fig-green">① 大根堆（堆顶最大）</text>
    <line x1="165" y1="74" x2="105" y2="118" class="fig-line"/>
    <line x1="165" y1="74" x2="225" y2="118" class="fig-line"/>
    <line x1="105" y1="118" x2="70" y2="164" class="fig-line"/>
    <line x1="105" y1="118" x2="140" y2="164" class="fig-line"/>
    <line x1="225" y1="118" x2="195" y2="164" class="fig-line"/>
    <circle cx="165" cy="74" r="15" class="fig-box-accent"/>
    <text x="165" y="79" text-anchor="middle" class="fig-accent">87</text>
    <circle cx="105" cy="118" r="15" class="fig-box-blue"/>
    <text x="105" y="123" text-anchor="middle" class="fig-blue">78</text>
    <circle cx="225" cy="118" r="15" class="fig-box-blue"/>
    <text x="225" y="123" text-anchor="middle" class="fig-blue">53</text>
    <circle cx="70" cy="164" r="14" class="fig-box"/>
    <text x="70" y="169" text-anchor="middle" class="fig-label">45</text>
    <circle cx="140" cy="164" r="14" class="fig-box"/>
    <text x="140" y="169" text-anchor="middle" class="fig-label">65</text>
    <circle cx="195" cy="164" r="14" class="fig-box"/>
    <text x="195" y="169" text-anchor="middle" class="fig-label">09</text>
    <!-- Arrow -->
    <line x1="275" y1="118" x2="405" y2="118" class="fig-line-accent" marker-end="url(#ds40-img2-arr-48)"/>
    <text x="340" y="106" text-anchor="middle" class="fig-accent">87 ↔ 09 交换</text>
    <text x="340" y="138" text-anchor="middle" class="fig-muted">09 向下调整</text>
    <!-- Right tree: After swap & sift-down -->
    <text x="530" y="46" text-anchor="middle" class="fig-blue">② 调整后（87 已归位）</text>
    <line x1="530" y1="74" x2="470" y2="118" class="fig-line"/>
    <line x1="530" y1="74" x2="590" y2="118" class="fig-line"/>
    <line x1="470" y1="118" x2="435" y2="164" class="fig-line"/>
    <line x1="470" y1="118" x2="505" y2="164" class="fig-line"/>
    <circle cx="530" cy="74" r="15" class="fig-box-green"/>
    <text x="530" y="79" text-anchor="middle" class="fig-green">78</text>
    <circle cx="470" cy="118" r="15" class="fig-box-blue"/>
    <text x="470" y="123" text-anchor="middle" class="fig-blue">65</text>
    <circle cx="590" cy="118" r="15" class="fig-box-blue"/>
    <text x="590" y="123" text-anchor="middle" class="fig-blue">53</text>
    <circle cx="435" cy="164" r="14" class="fig-box"/>
    <text x="435" y="169" text-anchor="middle" class="fig-label">45</text>
    <circle cx="505" cy="164" r="14" class="fig-box"/>
    <text x="505" y="169" text-anchor="middle" class="fig-label">09</text>
    <circle cx="565" cy="164" r="14" class="fig-box-subtle"/>
    <text x="565" y="169" text-anchor="middle" class="fig-muted">87</text>
    <text x="350" y="204" text-anchor="middle" class="fig-muted">建堆时间 O(n)，每趟向下调整 O(log n)，总时间 O(n log n)</text>
  </svg>
  <figcaption>图 48：大根堆；建堆 $O(n)$，每次取堆顶后向下调整 $O(\log n)$</figcaption>
</figure>

<figure class="cpd-figure not-content">
  <svg viewBox="0 0 680 232" role="img" aria-labelledby="ds40-fig-s11">
    <title id="ds40-fig-s11">堆排序流程：建立大根堆、交换堆顶至末尾、堆顶下沉调整</title>
    <defs>
      <marker id="ds40-arr-s11" viewBox="0 0 10 10" refX="8" refY="5" markerWidth="6" markerHeight="6" markerUnits="userSpaceOnUse" orient="auto-start-reverse">
        <path d="M1.5,1.5 L8.5,5 L1.5,8.5" fill="none" stroke="context-stroke" stroke-width="1.5" stroke-linecap="round" stroke-linejoin="round"/>
      </marker>
    </defs>
    <text class="fig-title" x="112" y="20" text-anchor="middle" font-size="12.5">① 建成初始大根堆</text>
    <text class="fig-title" x="340" y="20" text-anchor="middle" font-size="12.5">② 堆顶 97 ↔ 末尾 13</text>
    <text class="fig-title" x="568" y="20" text-anchor="middle" font-size="12.5">③ 前 4 个元素重新下调</text>
    <g class="fig-line">
      <line x1="112" y1="54" x2="66" y2="104"/><line x1="112" y1="54" x2="158" y2="104"/>
      <line x1="66" y1="104" x2="40" y2="156"/><line x1="66" y1="104" x2="92" y2="156"/>
      <line x1="340" y1="54" x2="294" y2="104"/><line x1="340" y1="54" x2="386" y2="104"/>
      <line x1="294" y1="104" x2="268" y2="156"/>
      <line x1="568" y1="54" x2="522" y2="104"/><line x1="568" y1="54" x2="614" y2="104"/>
      <line x1="522" y1="104" x2="496" y2="156"/>
    </g>
    <line class="fig-line-dashed" x1="294" y1="104" x2="320" y2="156"/>
    <line class="fig-line-dashed" x1="522" y1="104" x2="548" y2="156"/>
    <circle class="fig-box-accent" cx="112" cy="54" r="16"/>
    <circle class="fig-box" cx="66" cy="104" r="16"/><circle class="fig-box" cx="158" cy="104" r="16"/>
    <circle class="fig-box" cx="40" cy="156" r="16"/><circle class="fig-box-orange" cx="92" cy="156" r="16"/>
    <g font-size="12" text-anchor="middle">
      <text class="fig-accent" x="112" y="59">97</text>
      <text class="fig-label" x="66" y="109">65</text><text class="fig-label" x="158" y="109">49</text>
      <text class="fig-label" x="40" y="161">38</text><text class="fig-orange" x="92" y="161">13</text>
    </g>
    <line stroke="var(--sl-color-accent)" stroke-width="1.5" x1="192" y1="104" x2="234" y2="104" marker-end="url(#ds40-arr-s11)"/>
    <circle class="fig-box-orange" cx="340" cy="54" r="16"/>
    <circle class="fig-box" cx="294" cy="104" r="16"/><circle class="fig-box" cx="386" cy="104" r="16"/>
    <circle class="fig-box" cx="268" cy="156" r="16"/><circle class="fig-box-green" cx="320" cy="156" r="16"/>
    <g font-size="12" text-anchor="middle">
      <text class="fig-orange" x="340" y="59">13</text>
      <text class="fig-label" x="294" y="109">65</text><text class="fig-label" x="386" y="109">49</text>
      <text class="fig-label" x="268" y="161">38</text><text class="fig-green" x="320" y="161">97</text>
    </g>
    <path d="M356,58 C374,90 364,132 338,152" fill="none" stroke="var(--sl-color-red)" stroke-width="1.3" stroke-dasharray="3 2" marker-end="url(#ds40-arr-s11)"/>
    <line stroke="var(--sl-color-accent)" stroke-width="1.5" x1="420" y1="104" x2="462" y2="104" marker-end="url(#ds40-arr-s11)"/>
    <circle class="fig-box-accent" cx="568" cy="54" r="16"/>
    <circle class="fig-box" cx="522" cy="104" r="16"/><circle class="fig-box" cx="614" cy="104" r="16"/>
    <circle class="fig-box-orange" cx="496" cy="156" r="16"/><circle class="fig-box-green" cx="548" cy="156" r="16"/>
    <g font-size="12" text-anchor="middle">
      <text class="fig-accent" x="568" y="59">65</text>
      <text class="fig-label" x="522" y="109">38</text><text class="fig-label" x="614" y="109">49</text>
      <text class="fig-orange" x="496" y="161">13</text><text class="fig-green" x="548" y="161">97</text>
    </g>
    <text class="fig-muted" x="112" y="198" text-anchor="middle" font-size="11.5">数组 [97,65,49,38,13]</text>
    <text class="fig-muted" x="340" y="198" text-anchor="middle" font-size="11.5">97 锁定末尾，规模 n-1=4</text>
    <text class="fig-muted" x="568" y="198" text-anchor="middle" font-size="11.5">13 下沉 → [65,38,49,13 | 97]</text>
    <text class="fig-muted" x="340" y="218" text-anchor="middle" font-size="11.5">重复“取顶换尾 + 下沉调整” n − 1 轮即得升序；升序用大根堆、降序用小根堆</text>
  </svg>
  <figcaption>堆排序单轮演进：交换堆顶与末尾元素后缩小堆规模，再自顶向下筛选恢复堆序</figcaption>
</figure>

### 归并排序

分而治之：把序列一分为二，分别排好序，再把两个有序序列合并成一个。合并两个有序序列只需要线性时间。

- 始终 $O(n\log n)$
- **稳定**
- 需要 $O(n)$ 辅助空间

<figure class="cpd-figure not-content" id="ds40-img2-fig-49">
  <svg viewBox="0 0 700 225" role="img" aria-label="2 路归并排序分治与合并过程">
    <defs>
      <marker id="ds40-img2-arr-49" viewBox="0 0 10 10" refX="8" refY="5" markerWidth="6" markerHeight="6" markerUnits="userSpaceOnUse" orient="auto">
        <path d="M 1 1.5 L 8.5 5 L 1 8.5 z" fill="context-stroke" stroke="none"/>
      </marker>
    </defs>
    <text x="350" y="22" text-anchor="middle" class="fig-title">2 路归并排序：自底向上两两合并有序子序列</text>
    <!-- Level 0: singletons -->
    <rect x="65" y="42" width="60" height="30" rx="4" class="fig-box"/>
    <text x="95" y="62" text-anchor="middle" class="fig-label">49</text>
    <rect x="135" y="42" width="60" height="30" rx="4" class="fig-box"/>
    <text x="165" y="62" text-anchor="middle" class="fig-label">38</text>
    <rect x="215" y="42" width="60" height="30" rx="4" class="fig-box"/>
    <text x="245" y="62" text-anchor="middle" class="fig-label">65</text>
    <rect x="285" y="42" width="60" height="30" rx="4" class="fig-box"/>
    <text x="315" y="62" text-anchor="middle" class="fig-label">97</text>
    <rect x="365" y="42" width="60" height="30" rx="4" class="fig-box"/>
    <text x="395" y="62" text-anchor="middle" class="fig-label">76</text>
    <rect x="435" y="42" width="60" height="30" rx="4" class="fig-box"/>
    <text x="465" y="62" text-anchor="middle" class="fig-label">13</text>
    <rect x="515" y="42" width="60" height="30" rx="4" class="fig-box"/>
    <text x="545" y="62" text-anchor="middle" class="fig-label">27</text>
    <rect x="585" y="42" width="60" height="30" rx="4" class="fig-box"/>
    <text x="615" y="62" text-anchor="middle" class="fig-label">49</text>
    <!-- Level 1: pairs -->
    <rect x="65" y="96" width="130" height="30" rx="4" class="fig-box-blue"/>
    <text x="130" y="116" text-anchor="middle" class="fig-blue">38, 49</text>
    <rect x="215" y="96" width="130" height="30" rx="4" class="fig-box-blue"/>
    <text x="280" y="116" text-anchor="middle" class="fig-blue">65, 97</text>
    <rect x="365" y="96" width="130" height="30" rx="4" class="fig-box-blue"/>
    <text x="430" y="116" text-anchor="middle" class="fig-blue">13, 76</text>
    <rect x="515" y="96" width="130" height="30" rx="4" class="fig-box-blue"/>
    <text x="580" y="116" text-anchor="middle" class="fig-blue">27, 49</text>
    <!-- Level 2: quads -->
    <rect x="65" y="144" width="280" height="30" rx="4" class="fig-box-green"/>
    <text x="205" y="164" text-anchor="middle" class="fig-green">38, 49, 65, 97</text>
    <rect x="365" y="144" width="280" height="30" rx="4" class="fig-box-green"/>
    <text x="505" y="164" text-anchor="middle" class="fig-green">13, 27, 49, 76</text>
    <!-- Level 3: sorted -->
    <rect x="65" y="188" width="580" height="30" rx="4" class="fig-box-accent"/>
    <text x="355" y="208" text-anchor="middle" class="fig-accent">13, 27, 38, 49, 49, 65, 76, 97</text>
  </svg>
  <figcaption>图 49：2 路归并；每层合并 $O(n)$，共 $\log n$ 层</figcaption>
</figure>

<figure class="cpd-figure not-content">
  <svg viewBox="0 0 680 218" role="img" aria-labelledby="ds40-fig-s12">
    <title id="ds40-fig-s12">二路归并排序：自底向上两两合并有序段</title>
    <defs>
      <marker id="ds40-arr-s12" viewBox="0 0 10 10" refX="8" refY="5" markerWidth="6" markerHeight="6" markerUnits="userSpaceOnUse" orient="auto-start-reverse">
        <path d="M1.5,1.5 L8.5,5 L1.5,8.5" fill="none" stroke="context-stroke" stroke-width="1.5" stroke-linecap="round" stroke-linejoin="round"/>
      </marker>
    </defs>
    <text class="fig-muted" x="16" y="42" font-size="11.5">初始（长度1）</text>
    <rect class="fig-box" x="116" y="24" width="44" height="26" rx="4"/><text class="fig-label" x="138" y="42" text-anchor="middle" font-size="12">49</text>
    <rect class="fig-box" x="168" y="24" width="44" height="26" rx="4"/><text class="fig-label" x="190" y="42" text-anchor="middle" font-size="12">38</text>
    <rect class="fig-box" x="236" y="24" width="44" height="26" rx="4"/><text class="fig-label" x="258" y="42" text-anchor="middle" font-size="12">65</text>
    <rect class="fig-box" x="288" y="24" width="44" height="26" rx="4"/><text class="fig-label" x="310" y="42" text-anchor="middle" font-size="12">97</text>
    <rect class="fig-box" x="364" y="24" width="44" height="26" rx="4"/><text class="fig-label" x="386" y="42" text-anchor="middle" font-size="12">76</text>
    <rect class="fig-box" x="416" y="24" width="44" height="26" rx="4"/><text class="fig-label" x="438" y="42" text-anchor="middle" font-size="12">13</text>
    <rect class="fig-box" x="484" y="24" width="44" height="26" rx="4"/><text class="fig-label" x="506" y="42" text-anchor="middle" font-size="12">27</text>
    <rect class="fig-box" x="536" y="24" width="44" height="26" rx="4"/><text class="fig-label" x="558" y="42" text-anchor="middle" font-size="12">49'</text>
    <g stroke="var(--sl-color-gray-4)" stroke-width="1.2" fill="none">
      <line x1="138" y1="50" x2="164" y2="76"/><line x1="190" y1="50" x2="164" y2="76"/>
      <line x1="258" y1="50" x2="284" y2="76"/><line x1="310" y1="50" x2="284" y2="76"/>
      <line x1="386" y1="50" x2="412" y2="76"/><line x1="438" y1="50" x2="412" y2="76"/>
      <line x1="506" y1="50" x2="532" y2="76"/><line x1="558" y1="50" x2="532" y2="76"/>
    </g>
    <text class="fig-muted" x="16" y="96" font-size="11.5">第 1 趟（长度2）</text>
    <rect class="fig-box-blue" x="120" y="78" width="88" height="26" rx="4"/><text class="fig-blue" x="164" y="96" text-anchor="middle" font-size="12">[38, 49]</text>
    <rect class="fig-box-blue" x="240" y="78" width="88" height="26" rx="4"/><text class="fig-blue" x="284" y="96" text-anchor="middle" font-size="12">[65, 97]</text>
    <rect class="fig-box-orange" x="368" y="78" width="88" height="26" rx="4"/><text class="fig-orange" x="412" y="96" text-anchor="middle" font-size="12">[13, 76]</text>
    <rect class="fig-box-orange" x="488" y="78" width="88" height="26" rx="4"/><text class="fig-orange" x="532" y="96" text-anchor="middle" font-size="12">[27, 49']</text>
    <g stroke="var(--sl-color-gray-4)" stroke-width="1.2" fill="none">
      <line x1="164" y1="104" x2="224" y2="130"/><line x1="284" y1="104" x2="224" y2="130"/>
      <line x1="412" y1="104" x2="472" y2="130"/><line x1="532" y1="104" x2="472" y2="130"/>
    </g>
    <text class="fig-muted" x="16" y="150" font-size="11.5">第 2 趟（长度4）</text>
    <rect class="fig-box-accent" x="128" y="132" width="192" height="26" rx="4"/><text class="fig-accent" x="224" y="150" text-anchor="middle" font-size="12">[38, 49, 65, 97]</text>
    <rect class="fig-box-accent" x="376" y="132" width="192" height="26" rx="4"/><text class="fig-accent" x="472" y="150" text-anchor="middle" font-size="12">[13, 27, 49', 76]</text>
    <g stroke="var(--sl-color-gray-4)" stroke-width="1.2" fill="none">
      <line x1="224" y1="158" x2="348" y2="180"/><line x1="472" y1="158" x2="348" y2="180"/>
    </g>
    <text class="fig-green" x="16" y="198" font-size="11.5">第 3 趟（有序）</text>
    <rect class="fig-box-green" x="130" y="180" width="440" height="28" rx="4"/><text class="fig-green" x="350" y="199" text-anchor="middle" font-size="12">[13, 27, 38, 49, 49', 65, 76, 97]（49 仍在 49' 前，稳定）</text>
  </svg>
  <figcaption>二路归并树（8 元素共 ⌈log₂8⌉ = 3 趟）：相等时优先取左段保证稳定性，需 O(n) 辅助数组</figcaption>
</figure>

### 基数排序

不比较大小，按位分配和收集：先按个位放进 $0 \sim r-1$ 号桶，再依次收集；然后按十位、百位……重复。

- 复杂度 $O(d(n+r))$，$d$ 是位数，$r$ 是基数（十进制 $r=10$）
- **稳定**（收集时保持原顺序）
- 空间 $O(r)$ 的桶/队列；若另开输出数组则为 $O(n+r)$

<figure class="cpd-figure not-content" id="ds40-img2-fig-50">
  <svg viewBox="0 0 700 205" role="img" aria-label="LSD 基数排序按个位分配与收集">
    <defs>
      <marker id="ds40-img2-arr-50" viewBox="0 0 10 10" refX="8" refY="5" markerWidth="6" markerHeight="6" markerUnits="userSpaceOnUse" orient="auto">
        <path d="M 1 1.5 L 8.5 5 L 1 8.5 z" fill="context-stroke" stroke="none"/>
      </marker>
    </defs>
    <text x="350" y="22" text-anchor="middle" class="fig-title">基数排序（LSD）：按个位分配到 0~9 桶，再按桶序收集</text>
    <!-- Input -->
    <text x="350" y="46" text-anchor="middle" class="fig-label">待排序列：278, 109, 063, 930, 589, 184, 505, 269</text>
    <!-- Buckets -->
    <rect x="35" y="64" width="60" height="90" rx="4" class="fig-box-subtle"/>
    <text x="65" y="82" text-anchor="middle" class="fig-muted">桶 0</text>
    <text x="65" y="108" text-anchor="middle" class="fig-blue">930</text>
    <rect x="105" y="64" width="60" height="90" rx="4" class="fig-box-subtle"/>
    <text x="135" y="82" text-anchor="middle" class="fig-muted">桶 3</text>
    <text x="135" y="108" text-anchor="middle" class="fig-blue">063</text>
    <rect x="175" y="64" width="60" height="90" rx="4" class="fig-box-subtle"/>
    <text x="205" y="82" text-anchor="middle" class="fig-muted">桶 4</text>
    <text x="205" y="108" text-anchor="middle" class="fig-blue">184</text>
    <rect x="245" y="64" width="60" height="90" rx="4" class="fig-box-subtle"/>
    <text x="275" y="82" text-anchor="middle" class="fig-muted">桶 5</text>
    <text x="275" y="108" text-anchor="middle" class="fig-blue">505</text>
    <rect x="315" y="64" width="60" height="90" rx="4" class="fig-box-subtle"/>
    <text x="345" y="82" text-anchor="middle" class="fig-muted">桶 8</text>
    <text x="345" y="108" text-anchor="middle" class="fig-blue">278</text>
    <rect x="385" y="64" width="80" height="90" rx="4" class="fig-box-accent"/>
    <text x="425" y="82" text-anchor="middle" class="fig-accent">桶 9</text>
    <text x="425" y="106" text-anchor="middle" class="fig-accent">109</text>
    <text x="425" y="124" text-anchor="middle" class="fig-accent">589</text>
    <text x="425" y="142" text-anchor="middle" class="fig-accent">269</text>
    <!-- Collect arrow -->
    <line x1="485" y1="109" x2="535" y2="109" class="fig-line-accent" marker-end="url(#ds40-img2-arr-50)"/>
    <rect x="545" y="74" width="125" height="70" rx="6" class="fig-box-green"/>
    <text x="607" y="98" text-anchor="middle" class="fig-green">第 1 趟收集</text>
    <text x="607" y="118" text-anchor="middle" class="fig-muted">930, 063, 184,</text>
    <text x="607" y="135" text-anchor="middle" class="fig-muted">505, 278, 109...</text>
    <text x="350" y="184" text-anchor="middle" class="fig-muted">重复 d 趟（个位 → 十位 → 百位），时间 O(d(n+r))，稳定排序</text>
  </svg>
  <figcaption>图 50：按个位分配后再按十位、百位收集；稳定，不比较大小</figcaption>
</figure>

<figure class="cpd-figure not-content">
  <svg viewBox="0 0 680 210" role="img" aria-labelledby="ds40-fig-s13">
    <title id="ds40-fig-s13">基数排序（LSD 低位优先）：按个位分配与按桶号收集</title>
    <defs>
      <marker id="ds40-arr-s13" viewBox="0 0 10 10" refX="8" refY="5" markerWidth="6" markerHeight="6" markerUnits="userSpaceOnUse" orient="auto-start-reverse">
        <path d="M1.5,1.5 L8.5,5 L1.5,8.5" fill="none" stroke="context-stroke" stroke-width="1.5" stroke-linecap="round" stroke-linejoin="round"/>
      </marker>
    </defs>
    <text class="fig-title" x="16" y="20" font-size="12.5">原序列：53, 03, 54, 14, 21, 10</text>
    <text class="fig-muted" x="280" y="20" font-size="12">第 1 趟：按个位分配入 0..4 号桶（FIFO 队列）</text>
    <g class="fig-box">
      <rect x="30" y="42" width="110" height="82" rx="6"/>
      <rect x="158" y="42" width="110" height="82" rx="6"/>
      <rect x="286" y="42" width="110" height="82" rx="6"/>
      <rect x="414" y="42" width="110" height="82" rx="6"/>
      <rect x="542" y="42" width="110" height="82" rx="6"/>
    </g>
    <g class="fig-muted" font-size="11.5" text-anchor="middle">
      <text x="85" y="60">桶 0</text><text x="213" y="60">桶 1</text><text x="341" y="60">桶 2</text><text x="469" y="60">桶 3</text><text x="597" y="60">桶 4</text>
    </g>
    <rect class="fig-box-blue" x="45" y="70" width="80" height="22" rx="4"/><text class="fig-blue" x="85" y="85" text-anchor="middle" font-size="12">10</text>
    <rect class="fig-box-blue" x="173" y="70" width="80" height="22" rx="4"/><text class="fig-blue" x="213" y="85" text-anchor="middle" font-size="12">21</text>
    <text class="fig-muted" x="341" y="92" text-anchor="middle" font-size="11.5">（空）</text>
    <rect class="fig-box-green" x="429" y="68" width="80" height="22" rx="4"/><text class="fig-green" x="469" y="83" text-anchor="middle" font-size="12">53</text>
    <rect class="fig-box-green" x="429" y="94" width="80" height="22" rx="4"/><text class="fig-green" x="469" y="109" text-anchor="middle" font-size="12">03</text>
    <rect class="fig-box-orange" x="557" y="68" width="80" height="22" rx="4"/><text class="fig-orange" x="597" y="83" text-anchor="middle" font-size="12">54</text>
    <rect class="fig-box-orange" x="557" y="94" width="80" height="22" rx="4"/><text class="fig-orange" x="597" y="109" text-anchor="middle" font-size="12">14</text>
    <line stroke="var(--sl-color-accent)" stroke-width="1.5" x1="340" y1="128" x2="340" y2="152" marker-end="url(#ds40-arr-s13)"/>
    <text class="fig-accent" x="352" y="144" font-size="11.5">按桶 0 → 4 依次收集（同桶先入先出）</text>
    <rect class="fig-box-accent" x="40" y="158" width="600" height="32" rx="6"/>
    <text class="fig-accent" x="340" y="179" text-anchor="middle" font-size="12.5">第 1 趟结果：10, 21, 53, 03, 54, 14（再按十位入桶收集 → 03, 10, 14, 21, 53, 54）</text>
  </svg>
  <figcaption>基数排序（LSD）：不比较关键字大小，靠“分配 + 收集”实现 O(d(n+r)) 稳定排序</figcaption>
</figure>

### 排序算法对比

| 算法 | 平均 / 最好 / 最坏 | 空间 | 稳定 |
|:----:|:-------------------|:----:|:----:|
| 插入 | O(n²) / O(n) / O(n²) | O(1) | ✓ |
| 希尔 | 约 O(n^1.3) / — / O(n²) | O(1) | ✗ |
| 冒泡 | O(n²) / O(n) / O(n²) | O(1) | ✓ |
| 快速 | O(nlogn) / O(nlogn) / O(n²) | O(logn) | ✗ |
| 选择 | O(n²) / O(n²) / O(n²) | O(1) | ✗ |
| 堆 | O(nlogn) / O(nlogn) / O(nlogn) | O(1) | ✗ |
| 归并 | O(nlogn) / O(nlogn) / O(nlogn) | O(n) | ✓ |
| 基数 | O(d(n+r)) / 同左 / 同左 | O(r) | ✓ |

> 表中「插入」指直接插入，「选择」指简单选择；`O(nlogn)` 即 $O(n\log n)$。

> **★考点**：
> 1. **不稳定**的口诀：**快、希、选、堆**（快速、希尔、简单选择、堆），其余稳定。
> 2. 平均 $O(n\log n)$ 的有快速、堆、归并；其中**只有归并稳定**。
> 3. 每一趟都能确定一个元素最终位置的有：冒泡、简单选择、堆排序、快速排序。
> 4. 直接插入、冒泡在基本有序时可达 $O(n)$；简单选择、堆排序不因输入而改变复杂度。

### 比较排序的下界

基于比较的排序，最坏情况下至少需要

$$
\lceil \log_2(n!) \rceil \approx n\log_2 n
$$

次比较。原因：判定树有 $n!$ 个叶子，树高至少是 $\lceil \log_2(n!) \rceil$，所以比较排序不可能比 $O(n\log n)$ 更快。

> **★考点**：这是“基于比较”的下界；基数排序不比较大小，所以不受这个限制。

### 外部排序

数据大到内存放不下时，用外部排序：

1. 分段读入内存，排好序后写回外存，生成若干**有序归并段（run）**
2. 再多路归并这些归并段
3. 用**败者树**减少多路归并时的比较次数

<figure class="cpd-figure not-content" id="ds40-img2-fig-51">
  <svg viewBox="0 0 700 220" role="img" aria-label="k 路归并败者树结构">
    <defs>
      <marker id="ds40-img2-arr-51" viewBox="0 0 10 10" refX="8" refY="5" markerWidth="6" markerHeight="6" markerUnits="userSpaceOnUse" orient="auto">
        <path d="M 1 1.5 L 8.5 5 L 1 8.5 z" fill="context-stroke" stroke="none"/>
      </marker>
    </defs>
    <text x="350" y="22" text-anchor="middle" class="fig-title">败者树（k = 4 路归并）：内部结点存“败者”，胜者继续向上比</text>
    <!-- Edges -->
    <line x1="350" y1="54" x2="350" y2="86" class="fig-line-accent"/>
    <line x1="350" y1="86" x2="210" y2="128" class="fig-line"/>
    <line x1="350" y1="86" x2="490" y2="128" class="fig-line"/>
    <line x1="210" y1="128" x2="140" y2="174" class="fig-line"/>
    <line x1="210" y1="128" x2="280" y2="174" class="fig-line"/>
    <line x1="490" y1="128" x2="420" y2="174" class="fig-line"/>
    <line x1="490" y1="128" x2="560" y2="174" class="fig-line"/>
    <!-- Champion -->
    <rect x="295" y="34" width="110" height="28" rx="4" class="fig-box-green"/>
    <text x="350" y="53" text-anchor="middle" class="fig-green">胜者：b2 (6)</text>
    <!-- Internal nodes (losers) -->
    <circle cx="350" cy="86" r="16" class="fig-box-accent"/>
    <text x="350" y="91" text-anchor="middle" class="fig-accent">b3</text>
    <circle cx="210" cy="128" r="16" class="fig-box-blue"/>
    <text x="210" y="133" text-anchor="middle" class="fig-blue">b1</text>
    <circle cx="490" cy="128" r="16" class="fig-box-blue"/>
    <text x="490" y="133" text-anchor="middle" class="fig-blue">b4</text>
    <!-- Leaves (4 runs) -->
    <rect x="105" y="174" width="70" height="28" rx="4" class="fig-box"/>
    <text x="140" y="193" text-anchor="middle" class="fig-label">b1: 10</text>
    <rect x="245" y="174" width="70" height="28" rx="4" class="fig-box-green"/>
    <text x="280" y="193" text-anchor="middle" class="fig-green">b2: 6</text>
    <rect x="385" y="174" width="70" height="28" rx="4" class="fig-box"/>
    <text x="420" y="193" text-anchor="middle" class="fig-label">b3: 9</text>
    <rect x="525" y="174" width="70" height="28" rx="4" class="fig-box"/>
    <text x="560" y="193" text-anchor="middle" class="fig-label">b4: 15</text>
  </svg>
  <figcaption>图 51：$k$ 路归并的败者树；每个内部结点记录“败者”，胜者继续向上比较</figcaption>
</figure>
