This commit is contained in:
krahets
2024-04-06 03:02:26 +08:00
parent 5988d20958
commit a263b839b9
875 changed files with 484736 additions and 2893 deletions

View File

@@ -26,7 +26,7 @@
<title>3.1 Classification of Data Structures - Hello Algo</title>
<title>3.1 Classification of data structures - Hello Algo</title>
@@ -153,7 +153,7 @@
<div class="md-header__topic" data-md-component="header-topic">
<span class="md-ellipsis">
3.1 Classification of Data Structures
3.1 Classification of data structures
</span>
</div>
@@ -201,7 +201,13 @@
<li class="md-select__item">
<a href="/" hreflang="zh" class="md-select__link">
中文
简体中文
</a>
</li>
<li class="md-select__item">
<a href="/zh-hant/" hreflang="zh-Hant" class="md-select__link">
繁體中文
</a>
</li>
@@ -393,7 +399,7 @@
<span class="md-ellipsis">
0.1 About This Book
0.1 About this book
</span>
@@ -414,7 +420,7 @@
<span class="md-ellipsis">
0.2 How to Read
0.2 How to read
</span>
@@ -491,7 +497,7 @@
<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 24 24"><path d="M19 3H5c-1.1 0-2 .9-2 2v14c0 1.1.9 2 2 2h14c1.1 0 2-.9 2-2V5c0-1.1-.9-2-2-2m0 16H5V5h14v14M6.2 7.7h5v1.5h-5V7.7m6.8 8.1h5v1.5h-5v-1.5m0-2.6h5v1.5h-5v-1.5M8 18h1.5v-2h2v-1.5h-2v-2H8v2H6V16h2v2m6.1-7.1 1.4-1.4 1.4 1.4 1.1-1-1.4-1.4L18 7.1 16.9 6l-1.4 1.4L14.1 6 13 7.1l1.4 1.4L13 9.9l1.1 1Z"/></svg>
<span class="md-ellipsis">
Chapter 1. Introduction to Algorithms
Chapter 1. Introduction to algorithms
</span>
@@ -507,7 +513,7 @@
<nav class="md-nav" data-md-level="1" aria-labelledby="__nav_2_label" aria-expanded="false">
<label class="md-nav__title" for="__nav_2">
<span class="md-nav__icon md-icon"></span>
Chapter 1. Introduction to Algorithms
Chapter 1. Introduction to algorithms
</label>
<ul class="md-nav__list" data-md-scrollfix>
@@ -524,7 +530,7 @@
<span class="md-ellipsis">
1.1 Algorithms are Everywhere
1.1 Algorithms are everywhere
</span>
@@ -545,7 +551,7 @@
<span class="md-ellipsis">
1.2 What is an Algorithm
1.2 What is an algorithm
</span>
@@ -626,7 +632,7 @@
<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 24 24"><path d="M6 2h12v6l-4 4 4 4v6H6v-6l4-4-4-4V2m10 14.5-4-4-4 4V20h8v-3.5m-4-5 4-4V4H8v3.5l4 4M10 6h4v.75l-2 2-2-2V6Z"/></svg>
<span class="md-ellipsis">
Chapter 2. Complexity Analysis
Chapter 2. Complexity analysis
</span>
@@ -642,7 +648,7 @@
<nav class="md-nav" data-md-level="1" aria-labelledby="__nav_3_label" aria-expanded="false">
<label class="md-nav__title" for="__nav_3">
<span class="md-nav__icon md-icon"></span>
Chapter 2. Complexity Analysis
Chapter 2. Complexity analysis
</label>
<ul class="md-nav__list" data-md-scrollfix>
@@ -659,7 +665,7 @@
<span class="md-ellipsis">
2.1 Algorithm Efficiency Assessment
2.1 Algorithm efficiency assessment
</span>
@@ -680,7 +686,7 @@
<span class="md-ellipsis">
2.2 Iteration and Recursion
2.2 Iteration and recursion
</span>
@@ -701,7 +707,7 @@
<span class="md-ellipsis">
2.3 Time Complexity
2.3 Time complexity
</span>
@@ -722,7 +728,7 @@
<span class="md-ellipsis">
2.4 Space Complexity
2.4 Space complexity
</span>
@@ -805,7 +811,7 @@
<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 24 24"><path d="M11 13.5v8H3v-8h8m-2 2H5v4h4v-4M12 2l5.5 9h-11L12 2m0 3.86L10.08 9h3.84L12 5.86M17.5 13c2.5 0 4.5 2 4.5 4.5S20 22 17.5 22 13 20 13 17.5s2-4.5 4.5-4.5m0 2a2.5 2.5 0 0 0-2.5 2.5 2.5 2.5 0 0 0 2.5 2.5 2.5 2.5 0 0 0 2.5-2.5 2.5 2.5 0 0 0-2.5-2.5Z"/></svg>
<span class="md-ellipsis">
Chapter 3. Data Structures
Chapter 3. Data structures
</span>
@@ -821,7 +827,7 @@
<nav class="md-nav" data-md-level="1" aria-labelledby="__nav_4_label" aria-expanded="true">
<label class="md-nav__title" for="__nav_4">
<span class="md-nav__icon md-icon"></span>
Chapter 3. Data Structures
Chapter 3. Data structures
</label>
<ul class="md-nav__list" data-md-scrollfix>
@@ -847,7 +853,7 @@
<span class="md-ellipsis">
3.1 Classification of Data Structures
3.1 Classification of data structures
</span>
@@ -858,7 +864,7 @@
<span class="md-ellipsis">
3.1 Classification of Data Structures
3.1 Classification of data structures
</span>
@@ -882,7 +888,7 @@
<li class="md-nav__item">
<a href="#311-logical-structure-linear-and-non-linear" class="md-nav__link">
<span class="md-ellipsis">
3.1.1 &nbsp; Logical Structure: Linear and Non-Linear
3.1.1 &nbsp; Logical structure: linear and non-linear
</span>
</a>
@@ -891,7 +897,7 @@
<li class="md-nav__item">
<a href="#312-physical-structure-contiguous-and-dispersed" class="md-nav__link">
<span class="md-ellipsis">
3.1.2 &nbsp; Physical Structure: Contiguous and Dispersed
3.1.2 &nbsp; Physical structure: contiguous and dispersed
</span>
</a>
@@ -917,7 +923,7 @@
<span class="md-ellipsis">
3.2 Fundamental Data Types
3.2 Fundamental data types
</span>
@@ -938,7 +944,7 @@
<span class="md-ellipsis">
3.3 Number Encoding *
3.3 Number encoding *
</span>
@@ -959,7 +965,7 @@
<span class="md-ellipsis">
3.4 Character Encoding *
3.4 Character encoding *
</span>
@@ -1040,7 +1046,7 @@
<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 24 24"><path d="M3 5v14h17V5H3m4 2v2H5V7h2m-2 6v-2h2v2H5m0 2h2v2H5v-2m13 2H9v-2h9v2m0-4H9v-2h9v2m0-4H9V7h9v2Z"/></svg>
<span class="md-ellipsis">
Chapter 4. Array and Linked List
Chapter 4. Array and linked list
</span>
@@ -1056,7 +1062,7 @@
<nav class="md-nav" data-md-level="1" aria-labelledby="__nav_5_label" aria-expanded="false">
<label class="md-nav__title" for="__nav_5">
<span class="md-nav__icon md-icon"></span>
Chapter 4. Array and Linked List
Chapter 4. Array and linked list
</label>
<ul class="md-nav__list" data-md-scrollfix>
@@ -1094,7 +1100,7 @@
<span class="md-ellipsis">
4.2 Linked List
4.2 Linked list
</span>
@@ -1136,7 +1142,7 @@
<span class="md-ellipsis">
4.4 Memory and Cache
4.4 Memory and cache
</span>
@@ -1215,7 +1221,7 @@
<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 24 24"><path d="M17.36 20.2v-5.38h1.79V22H3v-7.18h1.8v5.38h12.56M6.77 14.32l.37-1.76 8.79 1.85-.37 1.76-8.79-1.85m1.16-4.21.76-1.61 8.14 3.78-.76 1.62-8.14-3.79m2.26-3.99 1.15-1.38 6.9 5.76-1.15 1.37-6.9-5.75m4.45-4.25L20 9.08l-1.44 1.07-5.36-7.21 1.44-1.07M6.59 18.41v-1.8h8.98v1.8H6.59Z"/></svg>
<span class="md-ellipsis">
Chapter 5. Stack and Queue
Chapter 5. Stack and queue
</span>
@@ -1231,7 +1237,7 @@
<nav class="md-nav" data-md-level="1" aria-labelledby="__nav_6_label" aria-expanded="false">
<label class="md-nav__title" for="__nav_6">
<span class="md-nav__icon md-icon"></span>
Chapter 5. Stack and Queue
Chapter 5. Stack and queue
</label>
<ul class="md-nav__list" data-md-scrollfix>
@@ -1290,7 +1296,7 @@
<span class="md-ellipsis">
5.3 Double-ended Queue
5.3 Double-ended queue
</span>
@@ -1369,7 +1375,7 @@
<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 24 24"><path d="M19.3 17.89c1.32-2.1.7-4.89-1.41-6.21a4.52 4.52 0 0 0-6.21 1.41C10.36 15.2 11 18 13.09 19.3c1.47.92 3.33.92 4.8 0L21 22.39 22.39 21l-3.09-3.11m-2-.62c-.98.98-2.56.97-3.54 0-.97-.98-.97-2.56.01-3.54.97-.97 2.55-.97 3.53 0 .96.99.95 2.57-.03 3.54h.03M19 4H5a2 2 0 0 0-2 2v12a2 2 0 0 0 2 2h5.81a6.3 6.3 0 0 1-1.31-2H5v-4h4.18c.16-.71.43-1.39.82-2H5V8h6v2.81a6.3 6.3 0 0 1 2-1.31V8h6v2a6.499 6.499 0 0 1 2 2V6a2 2 0 0 0-2-2Z"/></svg>
<span class="md-ellipsis">
Chapter 6. Hash Table
Chapter 6. Hash table
</span>
@@ -1385,7 +1391,7 @@
<nav class="md-nav" data-md-level="1" aria-labelledby="__nav_7_label" aria-expanded="false">
<label class="md-nav__title" for="__nav_7">
<span class="md-nav__icon md-icon"></span>
Chapter 6. Hash Table
Chapter 6. Hash table
</label>
<ul class="md-nav__list" data-md-scrollfix>
@@ -1402,7 +1408,7 @@
<span class="md-ellipsis">
6.1 Hash Table
6.1 Hash table
</span>
@@ -1423,7 +1429,7 @@
<span class="md-ellipsis">
6.2 Hash Collision
6.2 Hash collision
</span>
@@ -1444,7 +1450,7 @@
<span class="md-ellipsis">
6.3 Hash Algorithm
6.3 Hash algorithm
</span>
@@ -2019,7 +2025,7 @@
<li class="md-nav__item">
<a href="#311-logical-structure-linear-and-non-linear" class="md-nav__link">
<span class="md-ellipsis">
3.1.1 &nbsp; Logical Structure: Linear and Non-Linear
3.1.1 &nbsp; Logical structure: linear and non-linear
</span>
</a>
@@ -2028,7 +2034,7 @@
<li class="md-nav__item">
<a href="#312-physical-structure-contiguous-and-dispersed" class="md-nav__link">
<span class="md-ellipsis">
3.1.2 &nbsp; Physical Structure: Contiguous and Dispersed
3.1.2 &nbsp; Physical structure: contiguous and dispersed
</span>
</a>
@@ -2070,29 +2076,29 @@
<!-- Page content -->
<h1 id="31-classification-of-data-structures">3.1 &nbsp; Classification of Data Structures<a class="headerlink" href="#31-classification-of-data-structures" title="Permanent link">&para;</a></h1>
<h1 id="31-classification-of-data-structures">3.1 &nbsp; Classification of data structures<a class="headerlink" href="#31-classification-of-data-structures" title="Permanent link">&para;</a></h1>
<p>Common data structures include arrays, linked lists, stacks, queues, hash tables, trees, heaps, and graphs. They can be classified into "logical structure" and "physical structure".</p>
<h2 id="311-logical-structure-linear-and-non-linear">3.1.1 &nbsp; Logical Structure: Linear and Non-Linear<a class="headerlink" href="#311-logical-structure-linear-and-non-linear" title="Permanent link">&para;</a></h2>
<h2 id="311-logical-structure-linear-and-non-linear">3.1.1 &nbsp; Logical structure: linear and non-linear<a class="headerlink" href="#311-logical-structure-linear-and-non-linear" title="Permanent link">&para;</a></h2>
<p><strong>The logical structures reveal the logical relationships between data elements</strong>. In arrays and linked lists, data are arranged in a specific sequence, demonstrating the linear relationship between data; while in trees, data are arranged hierarchically from the top down, showing the derived relationship between "ancestors" and "descendants"; and graphs are composed of nodes and edges, reflecting the intricate network relationship.</p>
<p>As shown in the Figure 3-1 , logical structures can be divided into two major categories: "linear" and "non-linear". Linear structures are more intuitive, indicating data is arranged linearly in logical relationships; non-linear structures, conversely, are arranged non-linearly.</p>
<ul>
<li><strong>Linear Data Structures</strong>: Arrays, Linked Lists, Stacks, Queues, Hash Tables.</li>
<li><strong>Non-Linear Data Structures</strong>: Trees, Heaps, Graphs, Hash Tables.</li>
<li><strong>Linear data structures</strong>: Arrays, Linked Lists, Stacks, Queues, Hash Tables.</li>
<li><strong>Non-linear data structures</strong>: Trees, Heaps, Graphs, Hash Tables.</li>
</ul>
<p><a class="glightbox" href="../classification_of_data_structure.assets/classification_logic_structure.png" data-type="image" data-width="100%" data-height="auto" data-desc-position="bottom"><img alt="Linear and Non-Linear Data Structures" class="animation-figure" src="../classification_of_data_structure.assets/classification_logic_structure.png" /></a></p>
<p align="center"> Figure 3-1 &nbsp; Linear and Non-Linear Data Structures </p>
<p><a class="glightbox" href="../classification_of_data_structure.assets/classification_logic_structure.png" data-type="image" data-width="100%" data-height="auto" data-desc-position="bottom"><img alt="Linear and non-linear data structures" class="animation-figure" src="../classification_of_data_structure.assets/classification_logic_structure.png" /></a></p>
<p align="center"> Figure 3-1 &nbsp; Linear and non-linear data structures </p>
<p>Non-linear data structures can be further divided into tree structures and network structures.</p>
<ul>
<li><strong>Linear Structures</strong>: Arrays, linked lists, queues, stacks, and hash tables, where elements have a one-to-one sequential relationship.</li>
<li><strong>Tree Structures</strong>: Trees, Heaps, Hash Tables, where elements have a one-to-many relationship.</li>
<li><strong>Network Structures</strong>: Graphs, where elements have a many-to-many relationships.</li>
<li><strong>Linear structures</strong>: Arrays, linked lists, queues, stacks, and hash tables, where elements have a one-to-one sequential relationship.</li>
<li><strong>Tree structures</strong>: Trees, Heaps, Hash Tables, where elements have a one-to-many relationship.</li>
<li><strong>Network structures</strong>: Graphs, where elements have a many-to-many relationships.</li>
</ul>
<h2 id="312-physical-structure-contiguous-and-dispersed">3.1.2 &nbsp; Physical Structure: Contiguous and Dispersed<a class="headerlink" href="#312-physical-structure-contiguous-and-dispersed" title="Permanent link">&para;</a></h2>
<h2 id="312-physical-structure-contiguous-and-dispersed">3.1.2 &nbsp; Physical structure: contiguous and dispersed<a class="headerlink" href="#312-physical-structure-contiguous-and-dispersed" title="Permanent link">&para;</a></h2>
<p><strong>During the execution of an algorithm, the data being processed is stored in memory</strong>. The Figure 3-2 shows a computer memory stick where each black square is a physical memory space. We can think of memory as a vast Excel spreadsheet, with each cell capable of storing a certain amount of data.</p>
<p><strong>The system accesses the data at the target location by means of a memory address</strong>. As shown in the Figure 3-2 , the computer assigns a unique identifier to each cell in the table according to specific rules, ensuring that each memory space has a unique memory address. With these addresses, the program can access the data stored in memory.</p>
<p><a class="glightbox" href="../classification_of_data_structure.assets/computer_memory_location.png" data-type="image" data-width="100%" data-height="auto" data-desc-position="bottom"><img alt="Memory Stick, Memory Spaces, Memory Addresses" class="animation-figure" src="../classification_of_data_structure.assets/computer_memory_location.png" /></a></p>
<p align="center"> Figure 3-2 &nbsp; Memory Stick, Memory Spaces, Memory Addresses </p>
<p><a class="glightbox" href="../classification_of_data_structure.assets/computer_memory_location.png" data-type="image" data-width="100%" data-height="auto" data-desc-position="bottom"><img alt="Memory stick, memory spaces, memory addresses" class="animation-figure" src="../classification_of_data_structure.assets/computer_memory_location.png" /></a></p>
<p align="center"> Figure 3-2 &nbsp; Memory stick, memory spaces, memory addresses </p>
<div class="admonition tip">
<p class="admonition-title">Tip</p>
@@ -2100,8 +2106,8 @@
</div>
<p>Memory is a shared resource for all programs. When a block of memory is occupied by one program, it cannot be simultaneously used by other programs. <strong>Therefore, considering memory resources is crucial in designing data structures and algorithms</strong>. For instance, the algorithm's peak memory usage should not exceed the remaining free memory of the system; if there is a lack of contiguous memory blocks, then the data structure chosen must be able to be stored in non-contiguous memory blocks.</p>
<p>As illustrated in the Figure 3-3 , <strong>the physical structure reflects the way data is stored in computer memory</strong> and it can be divided into contiguous space storage (arrays) and non-contiguous space storage (linked lists). The two types of physical structures exhibit complementary characteristics in terms of time efficiency and space efficiency.</p>
<p><a class="glightbox" href="../classification_of_data_structure.assets/classification_phisical_structure.png" data-type="image" data-width="100%" data-height="auto" data-desc-position="bottom"><img alt="Contiguous Space Storage and Dispersed Space Storage" class="animation-figure" src="../classification_of_data_structure.assets/classification_phisical_structure.png" /></a></p>
<p align="center"> Figure 3-3 &nbsp; Contiguous Space Storage and Dispersed Space Storage </p>
<p><a class="glightbox" href="../classification_of_data_structure.assets/classification_phisical_structure.png" data-type="image" data-width="100%" data-height="auto" data-desc-position="bottom"><img alt="Contiguous space storage and dispersed space storage" class="animation-figure" src="../classification_of_data_structure.assets/classification_phisical_structure.png" /></a></p>
<p align="center"> Figure 3-3 &nbsp; Contiguous space storage and dispersed space storage </p>
<p><strong>It is worth noting that all data structures are implemented based on arrays, linked lists, or a combination of both</strong>. For example, stacks and queues can be implemented using either arrays or linked lists; while implementations of hash tables may involve both arrays and linked lists.
- <strong>Array-based implementations</strong>: Stacks, Queues, Hash Tables, Trees, Heaps, Graphs, Matrices, Tensors (arrays with dimensions <span class="arithmatex">\(\geq 3\)</span>).
@@ -2133,7 +2139,7 @@ aria-label="Footer"
<a
href="../"
class="md-footer__link md-footer__link--prev"
aria-label="Previous: Chapter 3. &amp;nbsp; Data Structures"
aria-label="Previous: Chapter 3. &amp;nbsp; Data structures"
rel="prev"
>
<div class="md-footer__button md-icon">
@@ -2145,7 +2151,7 @@ aria-label="Footer"
Previous
</span>
<div class="md-ellipsis">
Chapter 3. &nbsp; Data Structures
Chapter 3. &nbsp; Data structures
</div>
</div>
</a>
@@ -2157,7 +2163,7 @@ aria-label="Footer"
<a
href="../basic_data_types/"
class="md-footer__link md-footer__link--next"
aria-label="Next: 3.2 Fundamental Data Types"
aria-label="Next: 3.2 Fundamental data types"
rel="next"
>
<div class="md-footer__title">
@@ -2165,7 +2171,7 @@ aria-label="Footer"
Next
</span>
<div class="md-ellipsis">
3.2 Fundamental Data Types
3.2 Fundamental data types
</div>
</div>
<div class="md-footer__button md-icon">
@@ -2258,7 +2264,7 @@ aria-label="Footer"
<nav class="md-footer__inner md-grid" aria-label="Footer" >
<a href="../" class="md-footer__link md-footer__link--prev" aria-label="Previous: Chapter 3. &amp;nbsp; Data Structures">
<a href="../" class="md-footer__link md-footer__link--prev" aria-label="Previous: Chapter 3. &amp;nbsp; Data structures">
<div class="md-footer__button md-icon">
<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 24 24"><path d="M20 11v2H8l5.5 5.5-1.42 1.42L4.16 12l7.92-7.92L13.5 5.5 8 11h12Z"/></svg>
@@ -2268,20 +2274,20 @@ aria-label="Footer"
Previous
</span>
<div class="md-ellipsis">
Chapter 3. &nbsp; Data Structures
Chapter 3. &nbsp; Data structures
</div>
</div>
</a>
<a href="../basic_data_types/" class="md-footer__link md-footer__link--next" aria-label="Next: 3.2 Fundamental Data Types">
<a href="../basic_data_types/" class="md-footer__link md-footer__link--next" aria-label="Next: 3.2 Fundamental data types">
<div class="md-footer__title">
<span class="md-footer__direction">
Next
</span>
<div class="md-ellipsis">
3.2 Fundamental Data Types
3.2 Fundamental data types
</div>
</div>
<div class="md-footer__button md-icon">