mirror of
https://github.com/krahets/hello-algo.git
synced 2026-11-26 22:42:41 +08:00
Release reviewed gh-pages candidate
This commit is contained in:
@@ -47,7 +47,7 @@
|
||||
set(key, value, storage, key === "__palette" ? shared : scope);
|
||||
})();
|
||||
</script>
|
||||
<link href="/assets/figure-svg/css/ca08fd1c3a121ac517cf0998.css" rel="stylesheet"/><link href="/assets/figure-svg/css/9f909651eeceaf5c7dda140d.css" rel="stylesheet"/></head>
|
||||
<link href="/assets/figure-svg/css/ca08fd1c3a121ac517cf0998.css" rel="stylesheet"/><link href="/assets/figure-svg/css/f41655ef506ff5ce3cf0fd42.css" rel="stylesheet"/></head>
|
||||
<body data-md-color-accent="teal" data-md-color-primary="white" data-md-color-scheme="default" dir="ltr">
|
||||
<input autocomplete="off" class="md-toggle" data-md-toggle="drawer" id="__drawer" type="checkbox"/>
|
||||
<input autocomplete="off" class="md-toggle" data-md-toggle="search" id="__search" type="checkbox"/>
|
||||
@@ -1953,21 +1953,21 @@
|
||||
<li><strong>树形结构</strong>:树、堆、哈希表,元素之间是一对多的关系。</li>
|
||||
<li><strong>网状结构</strong>:图,元素之间是多对多的关系。</li>
|
||||
</ul>
|
||||
<p><ha-svg-figure class="animation-figure" data-book-key="chapter_data_structure/classification_of_data_structure.assets/classification_logic_structure.png" data-dark-src="/assets/figure-svg/figures/8194f515246488dedd038f12.json" data-src="/assets/figure-svg/figures/c457af7f9f7850955dbf6e11.json" style=";--figure-ratio:960.0/540.0"><object data="/chapter_data_structure/classification_of_data_structure.assets/classification_logic_structure.svg" data-theme="light" tabindex="-1" type="image/svg+xml">线性数据结构与非线性数据结构</object><object data="/chapter_data_structure/classification_of_data_structure.assets/classification_logic_structure.dark.svg" data-theme="dark" tabindex="-1" type="image/svg+xml">线性数据结构与非线性数据结构</object></ha-svg-figure></p>
|
||||
<p align="center"> 图 3-1 线性数据结构与非线性数据结构 </p>
|
||||
<p><ha-svg-figure aria-label="数据结构的逻辑关系" class="animation-figure" data-book-key="chapter_data_structure/classification_of_data_structure.assets/classification_logic_structure.png" data-dark-src="/assets/figure-svg/figures/8194f515246488dedd038f12.json" data-figure-accessible="" data-src="/assets/figure-svg/figures/c457af7f9f7850955dbf6e11.json" role="img" style=";--figure-ratio:960.0/540.0"><object aria-hidden="true" data="/chapter_data_structure/classification_of_data_structure.assets/classification_logic_structure.svg" data-theme="light" inert="" tabindex="-1" type="image/svg+xml">线性数据结构与非线性数据结构</object><object aria-hidden="true" data="/chapter_data_structure/classification_of_data_structure.assets/classification_logic_structure.dark.svg" data-theme="dark" inert="" tabindex="-1" type="image/svg+xml">线性数据结构与非线性数据结构</object></ha-svg-figure></p>
|
||||
<p align="center"> 图 3-1 线性数据结构与非线性数据结构 </p><div class="ha-sr-only figure-description"><h3>图解 3-1:数据结构的逻辑关系</h3><p>比较顺序、父子层级、网络连接和键值映射四种逻辑关系。</p><p>图左栏为线性数据结构,右栏为非线性数据结构;哈希表位于两栏下方,单独展示键值映射。</p><table><caption>各结构的示例与连接</caption><thead><tr><th scope="col">结构</th><th scope="col">图中分栏</th><th scope="col">图中元素与关系</th></tr></thead><tbody><tr><th scope="row">数组array</th><td>线性</td><td>按1、3、2、5、4排列</td></tr><tr><th scope="row">链表linked list</th><td>线性</td><td>1→3→2→5→4</td></tr><tr><th scope="row">栈stack</th><td>线性</td><td>从开口端到封闭端为1、3、2、5、4;push入栈与pop出栈同在开口端</td></tr><tr><th scope="row">队列queue</th><td>线性</td><td>从出队端到入队端为1、3、2、5、4;push入队、pop出队在不同端</td></tr><tr><th scope="row">树tree</th><td>非线性</td><td>根1左2右3;2左4右5;3左6右7;7节点三层</td></tr><tr><th scope="row">图graph</th><td>非线性</td><td>5顶点1至5;无向边1—2、1—3、2—3、2—4、2—5、3—5、4—5,共7条</td></tr><tr><th scope="row">堆heap</th><td>非线性</td><td>根1左3右2;3左5右4;5节点三层</td></tr><tr><th scope="row">哈希表hash table</th><td>跨两栏单列</td><td>键a、b、c分别映射值1、2、3</td></tr></tbody></table><p>线性结构具有先后次序,但对操作位置的限制不同;树与堆组织父子层级,图允许一个顶点有多个邻居。这里比较的是逻辑关系,不等同于底层存储:同类关系可以有不同实现,哈希表也可组合数组、链表或树。</p></div>
|
||||
<h2 id="312">3.1.2 物理结构:连续与分散<a class="headerlink" href="#312" title="Permanent link">¶</a></h2>
|
||||
<p><strong>当算法程序运行时,正在处理的数据主要存储在内存中</strong>。图 3-2 展示了一个计算机内存条,其中每个黑色方块都包含一块内存空间。我们可以将内存想象成一个巨大的 Excel 表格,其中每个单元格都可以存储一定大小的数据。</p>
|
||||
<p><strong>系统通过内存地址来访问目标位置的数据</strong>。如图 3-2 所示,计算机根据特定规则为表格中的每个单元格分配编号,确保每个内存空间都有唯一的内存地址。有了这些地址,程序便可以访问内存中的数据。</p>
|
||||
<p><ha-svg-figure class="animation-figure" data-book-key="chapter_data_structure/classification_of_data_structure.assets/computer_memory_location.png" data-dark-src="/assets/figure-svg/figures/48add42e25bb56bee3382468.json" data-src="/assets/figure-svg/figures/4f3083d943d718f0b1f4291e.json" style=";--figure-ratio:960.0/540.0"><object data="/chapter_data_structure/classification_of_data_structure.assets/computer_memory_location.svg" data-theme="light" tabindex="-1" type="image/svg+xml">内存条、内存空间、内存地址</object><object data="/chapter_data_structure/classification_of_data_structure.assets/computer_memory_location.dark.svg" data-theme="dark" tabindex="-1" type="image/svg+xml">内存条、内存空间、内存地址</object></ha-svg-figure></p>
|
||||
<p align="center"> 图 3-2 内存条、内存空间、内存地址 </p>
|
||||
<p><ha-svg-figure aria-label="用地址访问内存" class="animation-figure" data-book-key="chapter_data_structure/classification_of_data_structure.assets/computer_memory_location.png" data-dark-src="/assets/figure-svg/figures/48add42e25bb56bee3382468.json" data-figure-accessible="" data-src="/assets/figure-svg/figures/4f3083d943d718f0b1f4291e.json" role="img" style=";--figure-ratio:960.0/540.0"><object aria-hidden="true" data="/chapter_data_structure/classification_of_data_structure.assets/computer_memory_location.svg" data-theme="light" inert="" tabindex="-1" type="image/svg+xml">内存条、内存空间、内存地址</object><object aria-hidden="true" data="/chapter_data_structure/classification_of_data_structure.assets/computer_memory_location.dark.svg" data-theme="dark" inert="" tabindex="-1" type="image/svg+xml">内存条、内存空间、内存地址</object></ha-svg-figure></p>
|
||||
<p align="center"> 图 3-2 内存条、内存空间、内存地址 </p><div class="ha-sr-only figure-description"><h3>图解 3-2:用地址访问内存</h3><p>由行起始地址加列偏移定位内存单元,读取其中的数据。</p><p>左侧是一条竖直的内存条,其黑色芯片区域通过两条灰线引出右侧的内存空间网格。这些芯片与网格内表示“已用”的深色单元不是同一层级。</p><p>网格画出 5 行 8 列:行起始地址为 0x0000、0x0010、0x0020、0x0030、0x0040,列偏移为 00 至 07;右侧和底部的省略号表示还有未画出的单元。浅色是可用空间,深色是已用空间;可用不表示里面存有数值 0。</p><table><caption>图中两个已用内存单元</caption><thead><tr><th scope="col">完整地址</th><th scope="col">行起始地址</th><th scope="col">列偏移</th><th scope="col">存储数据</th></tr></thead><tbody><tr><th scope="row">0x0012</th><td>0x0010</td><td>02</td><td>1</td></tr><tr><th scope="row">0x0024</th><td>0x0020</td><td>04</td><td>2</td></tr></tbody></table><p>把行起始地址与列偏移相加即可定位。例如 0x0020 加偏移 04,得到 0x0024,读出数据 2。网格下方的两个读取示例与表中两行对应。地址标识位置,数据是该位置存放的内容;网格只作寻址示意,不表示芯片的实际电路布局。</p></div>
|
||||
<div class="admonition tip">
|
||||
<p class="admonition-title">Tip</p>
|
||||
<p>值得说明的是,将内存比作 Excel 表格是一个简化的类比,实际内存的工作机制比较复杂,涉及地址空间、内存管理、缓存机制、虚拟内存和物理内存等概念。</p>
|
||||
</div>
|
||||
<p>内存是所有程序的共享资源,当某块内存被某个程序占用时,则通常无法被其他程序同时使用了。<strong>因此在数据结构与算法的设计中,内存资源是一个重要的考虑因素</strong>。比如,算法所占用的内存峰值不应超过系统剩余空闲内存;如果缺少连续大块的内存空间,那么所选用的数据结构必须能够存储在分散的内存空间内。</p>
|
||||
<p>如图 3-3 所示,<strong>物理结构反映了数据在计算机内存中的存储方式</strong>,可分为连续空间存储(数组)和分散空间存储(链表)。物理结构从底层决定了数据的访问、更新、增删等操作方法,两种物理结构在时间效率和空间效率方面呈现出互补的特点。</p>
|
||||
<p><ha-svg-figure class="animation-figure" data-book-key="chapter_data_structure/classification_of_data_structure.assets/classification_phisical_structure.png" data-dark-src="/assets/figure-svg/figures/97a60753fa503cf88a10109a.json" data-src="/assets/figure-svg/figures/a565fa860a9a2e6d69e9f97c.json" style=";--figure-ratio:960.0/540.0"><object data="/chapter_data_structure/classification_of_data_structure.assets/classification_phisical_structure.svg" data-theme="light" tabindex="-1" type="image/svg+xml">连续空间存储与分散空间存储</object><object data="/chapter_data_structure/classification_of_data_structure.assets/classification_phisical_structure.dark.svg" data-theme="dark" tabindex="-1" type="image/svg+xml">连续空间存储与分散空间存储</object></ha-svg-figure></p>
|
||||
<p align="center"> 图 3-3 连续空间存储与分散空间存储 </p>
|
||||
<p><ha-svg-figure aria-label="数组与链表的存储布局" class="animation-figure" data-book-key="chapter_data_structure/classification_of_data_structure.assets/classification_phisical_structure.png" data-dark-src="/assets/figure-svg/figures/97a60753fa503cf88a10109a.json" data-figure-accessible="" data-src="/assets/figure-svg/figures/a565fa860a9a2e6d69e9f97c.json" role="img" style=";--figure-ratio:960.0/540.0"><object aria-hidden="true" data="/chapter_data_structure/classification_of_data_structure.assets/classification_phisical_structure.svg" data-theme="light" inert="" tabindex="-1" type="image/svg+xml">连续空间存储与分散空间存储</object><object aria-hidden="true" data="/chapter_data_structure/classification_of_data_structure.assets/classification_phisical_structure.dark.svg" data-theme="dark" inert="" tabindex="-1" type="image/svg+xml">连续空间存储与分散空间存储</object></ha-svg-figure></p>
|
||||
<p align="center"> 图 3-3 连续空间存储与分散空间存储 </p><div class="ha-sr-only figure-description"><h3>图解 3-3:数组与链表的存储布局</h3><p>相同的逻辑序列,既可连续存储,也可由分散节点的指针连接。</p><p>左右两幅都是 5 行 6 列示意网格,行从上到下、列从左到右,均从 1 起计。左侧数组在第 2 行前 5 格连续存放 1、3、2、5、4;右侧链表节点分散在网格各处,每个节点占相邻的值格和指针格。</p><p>浅灰格表示可用空间;数组占用格为绿色,链表的值格为青色、指针格为浅蓝色。下表按链表访问次序列出各节点位置。</p><table><caption>链表节点的示意位置与后继;行列从1起</caption><thead><tr><th scope="col">节点值</th><th scope="col">值格位置</th><th scope="col">指针格位置</th><th scope="col">后继</th></tr></thead><tbody><tr><th scope="row">1</th><td>第2行第2列</td><td>第2行第3列</td><td>3</td></tr><tr><th scope="row">3</th><td>第3行第3列</td><td>第3行第4列</td><td>2</td></tr><tr><th scope="row">2</th><td>第5行第2列</td><td>第5行第3列</td><td>5</td></tr><tr><th scope="row">5</th><td>第5行第5列</td><td>第5行第6列</td><td>4</td></tr><tr><th scope="row">4</th><td>第1行第5列</td><td>第1行第6列</td><td>未画后续箭头</td></tr></tbody></table><p>数组的先后次序由相邻位置表达,链表则沿后继指针访问。因此节点 4 虽在最上方,仍位于 1 → 3 → 2 → 5 → 4 的最后。图未注明它的具体指针值。连续存储无须为每个元素另存后继指针;分散节点能利用零散空间,但要额外保存指针。</p></div>
|
||||
<p>值得说明的是,<strong>所有数据结构都是基于数组、链表或二者的组合实现的</strong>。例如,栈和队列既可以使用数组实现,也可以使用链表实现;而哈希表的实现可能同时包含数组和链表。</p>
|
||||
<ul>
|
||||
<li><strong>基于数组可实现</strong>:栈、队列、哈希表、树、堆、图、矩阵、张量(维度 <span class="arithmatex">\(\geq 3\)</span> 的数组)等。</li>
|
||||
@@ -2099,5 +2099,5 @@
|
||||
<script src="../../javascripts/mathjax.js"></script>
|
||||
<script src="https://cdnjs.cloudflare.com/ajax/libs/mathjax/3.2.2/es5/tex-mml-chtml.min.js"></script>
|
||||
<script src="../../javascripts/animation_player.js"></script>
|
||||
<script defer="" src="/assets/figure-svg/js/bcdea376f01bc2bc0cac7c6c.js"></script><script defer="" src="/assets/figure-svg/js/e84f59519170c0f5ef238548.js"></script><script defer="" src="/assets/figure-svg/js/99a707e9f0a14d8280f16808.js"></script><script defer="" src="/assets/figure-svg/js/b789dc8b01e43e756fc89964.js"></script></body>
|
||||
<script defer="" src="/assets/figure-svg/js/bcdea376f01bc2bc0cac7c6c.js"></script><script defer="" src="/assets/figure-svg/js/e84f59519170c0f5ef238548.js"></script><script defer="" src="/assets/figure-svg/js/a704d8e80a80ddc71f2a03a7.js"></script><script defer="" src="/assets/figure-svg/js/bff17b8259339758cb1c4a06.js"></script></body>
|
||||
</html>
|
||||
Binary file not shown.
Reference in New Issue
Block a user