diff --git a/chapter_array_and_linkedlist/array.md b/chapter_array_and_linkedlist/array.md index f2e934d6a..2843c2211 100755 --- a/chapter_array_and_linkedlist/array.md +++ b/chapter_array_and_linkedlist/array.md @@ -1098,8 +1098,10 @@ elementAddr = firtstElementAddr + elementLength * elementIndex ## 4.1.4.   数组典型应用 -**随机访问**。如果我们想要随机抽取一些样本,那么可以用数组存储,并生成一个随机序列,根据索引实现样本的随机抽取。 +数组是最基础的数据结构,在各类数据结构和算法中都有广泛应用。 -**二分查找**。例如前文查字典的例子,我们可以将字典中的所有字按照拼音顺序存储在数组中,然后使用与日常查纸质字典相同的“翻开中间,排除一半”的方式,来实现一个查电子字典的算法。 - -**深度学习**。神经网络中大量使用了向量、矩阵、张量之间的线性代数运算,这些数据都是以数组的形式构建的。数组是神经网络编程中最常使用的数据结构。 +- **随机访问**:如果我们想要随机抽取一些样本,那么可以用数组存储,并生成一个随机序列,根据索引实现样本的随机抽取。 +- **排序和搜索**:数组是排序和搜索算法最常用的数据结构。例如,快速排序、归并排序、二分查找等都需要在数组上进行。 +- **查找表**:当我们需要快速查找一个元素或者需要查找一个元素的对应关系时,可以使用数组作为查找表。例如,我们有一个字符到其 ASCII 码的映射,可以将字符的 ASCII 码值作为索引,对应的元素存放在数组中的对应位置。 +- **机器学习**:神经网络中大量使用了向量、矩阵、张量之间的线性代数运算,这些数据都是以数组的形式构建的。数组是神经网络编程中最常使用的数据结构。 +- **数据结构实现**:数组可以用于实现栈、队列、哈希表、堆、图等数据结构。例如,邻接矩阵是图的常见表示之一,它实质上是一个二维数组。 diff --git a/chapter_array_and_linkedlist/index.md b/chapter_array_and_linkedlist/index.md index 15c9594a1..d9d71aa7a 100644 --- a/chapter_array_and_linkedlist/index.md +++ b/chapter_array_and_linkedlist/index.md @@ -1,6 +1,6 @@ --- comments: true -icon: material/view-grid-outline +icon: material/view-list-outline --- # 4.   数组与链表 diff --git a/chapter_array_and_linkedlist/linked_list.md b/chapter_array_and_linkedlist/linked_list.md index d769cdc83..b34d5f694 100755 --- a/chapter_array_and_linkedlist/linked_list.md +++ b/chapter_array_and_linkedlist/linked_list.md @@ -1188,3 +1188,22 @@ comments: true ![常见链表种类](linked_list.assets/linkedlist_common_types.png)

Fig. 常见链表种类

+ +## 4.2.5.   链表典型应用 + +单向链表通常用于实现栈、队列、散列表和图等数据结构。 + +- **栈与队列**:当插入和删除操作都在链表的一端进行时,它表现出先进后出的的特性,对应栈;当插入操作在链表的一端进行,删除操作在链表的另一端进行,它表现出先进先出的特性,对应队列。 +- **散列表**:链地址法是解决哈希冲突的主流方案之一,在该方案中,所有冲突的元素都会被放到一个链表中。 +- **图**:邻接表是表示图的一种常用方式,在其中,图的每个顶点都与一个链表相关联,链表中的每个元素都代表与该顶点相连的其他顶点。 + +双向链表常被用于需要快速查找前一个和下一个元素的场景。 + +- **高级数据结构**:比如在红黑树、B 树中,我们需要知道一个节点的父节点,这可以通过在节点中保存一个指向父节点的指针来实现,类似于双向链表。 +- **浏览器历史**:在网页浏览器中,当用户点击前进或后退按钮时,浏览器需要知道用户访问过的前一个和后一个网页。双向链表的特性使得这种操作变得简单。 +- **LRU 算法**:在缓存淘汰算法(LRU)中,我们需要快速找到最近最少使用的数据,以及支持快速地添加和删除节点。这时候使用双向链表就非常合适。 + +循环链表常被用于需要周期性操作的场景,比如操作系统的资源调度。 + +- **时间片轮转调度算法**:在操作系统中,时间片轮转调度算法是一种常见的 CPU 调度算法,它需要对一组进程进行循环。每个进程被赋予一个时间片,当时间片用完时,CPU 将切换到下一个进程。这种循环的操作就可以通过循环链表来实现。 +- **数据缓冲区**:在某些数据缓冲区的实现中,也可能会使用到循环链表。比如在音频、视频播放器中,数据流可能会被分成多个缓冲块并放入一个循环链表,以便实现无缝播放。 diff --git a/chapter_data_structure/index.md b/chapter_data_structure/index.md index 69108e573..a1b9997ae 100644 --- a/chapter_data_structure/index.md +++ b/chapter_data_structure/index.md @@ -1,6 +1,6 @@ --- comments: true -icon: material/database-outline +icon: material/shape-outline --- # 3.   数据结构 diff --git a/chapter_divide_and_conquer/index.md b/chapter_divide_and_conquer/index.md index 2ca2195a5..d018d17ca 100644 --- a/chapter_divide_and_conquer/index.md +++ b/chapter_divide_and_conquer/index.md @@ -1,6 +1,6 @@ --- comments: true -icon: material/file-tree-outline +icon: material/set-split status: new --- diff --git a/chapter_introduction/index.md b/chapter_introduction/index.md index 4187a156d..6f321a25f 100644 --- a/chapter_introduction/index.md +++ b/chapter_introduction/index.md @@ -1,6 +1,6 @@ --- comments: true -icon: material/code-tags +icon: material/calculator-variant-outline --- # 1.   初识算法 diff --git a/chapter_tree/array_representation_of_tree.md b/chapter_tree/array_representation_of_tree.md index fb6768d3a..7d7fda1ef 100644 --- a/chapter_tree/array_representation_of_tree.md +++ b/chapter_tree/array_representation_of_tree.md @@ -42,7 +42,7 @@ comments: true ```cpp title="" /* 二叉树的数组表示 */ - // 使用 int 最大值标记空位,因此要求节点值不能为 INT_MAX + // 使用 int 最大值 INT_MAX 标记空位 vector tree = {1, 2, 3, 4, INT_MAX, 6, 7, 8, 9, INT_MAX, INT_MAX, 12, INT_MAX, INT_MAX, 15}; ``` @@ -120,6 +120,322 @@ comments: true

Fig. 任意类型二叉树的数组表示

+以下为数组表示下二叉树的实现,包括: + +- 获取节点数量、节点值、左(右)子节点、父节点等基础操作; +- 获取前序遍历、中序遍历、后序遍历、层序遍历的节点值序列; + +=== "Java" + + ```java title="array_binary_tree.java" + /* 数组表示下的二叉树类 */ + class ArrayBinaryTree { + private List tree; + + /* 构造方法 */ + public ArrayBinaryTree(List arr) { + tree = new ArrayList<>(arr); + } + + /* 节点数量 */ + public int size() { + return tree.size(); + } + + /* 获取索引为 i 节点的值 */ + public Integer val(int i) { + // 若索引越界,则返回 null ,代表空位 + if (i < 0 || i >= size()) + return null; + return tree.get(i); + } + + /* 获取索引为 i 节点的左子节点的索引 */ + public Integer left(int i) { + return 2 * i + 1; + } + + /* 获取索引为 i 节点的右子节点的索引 */ + public Integer right(int i) { + return 2 * i + 2; + } + + /* 获取索引为 i 节点的父节点的索引 */ + public Integer parent(int i) { + return (i - 1) / 2; + } + + /* 层序遍历 */ + public List levelOrder() { + List res = new ArrayList<>(); + // 直接遍历数组 + for (int i = 0; i < size(); i++) { + if (val(i) != null) + res.add(val(i)); + } + return res; + } + + /* 深度优先遍历 */ + private void dfs(Integer i, String order, List res) { + // 若为空位,则返回 + if (val(i) == null) + return; + // 前序遍历 + if (order == "pre") + res.add(val(i)); + dfs(left(i), order, res); + // 中序遍历 + if (order == "in") + res.add(val(i)); + dfs(right(i), order, res); + // 后序遍历 + if (order == "post") + res.add(val(i)); + } + + /* 前序遍历 */ + public List preOrder() { + List res = new ArrayList<>(); + dfs(0, "pre", res); + return res; + } + + /* 中序遍历 */ + public List inOrder() { + List res = new ArrayList<>(); + dfs(0, "in", res); + return res; + } + + /* 后序遍历 */ + public List postOrder() { + List res = new ArrayList<>(); + dfs(0, "post", res); + return res; + } + } + ``` + +=== "C++" + + ```cpp title="array_binary_tree.cpp" + /* 数组表示下的二叉树类 */ + class ArrayBinaryTree { + public: + /* 构造方法 */ + ArrayBinaryTree(vector arr) { + tree = arr; + } + + /* 节点数量 */ + int size() { + return tree.size(); + } + + /* 获取索引为 i 节点的值 */ + int val(int i) { + // 若索引越界,则返回 INT_MAX ,代表空位 + if (i < 0 || i >= size()) + return INT_MAX; + return tree[i]; + } + + /* 获取索引为 i 节点的左子节点的索引 */ + int left(int i) { + return 2 * i + 1; + } + + /* 获取索引为 i 节点的右子节点的索引 */ + int right(int i) { + return 2 * i + 2; + } + + /* 获取索引为 i 节点的父节点的索引 */ + int parent(int i) { + return (i - 1) / 2; + } + + /* 层序遍历 */ + vector levelOrder() { + vector res; + // 直接遍历数组 + for (int i = 0; i < size(); i++) { + if (val(i) != INT_MAX) + res.push_back(val(i)); + } + return res; + } + + /* 前序遍历 */ + vector preOrder() { + vector res; + dfs(0, "pre", res); + return res; + } + + /* 中序遍历 */ + vector inOrder() { + vector res; + dfs(0, "in", res); + return res; + } + + /* 后序遍历 */ + vector postOrder() { + vector res; + dfs(0, "post", res); + return res; + } + + private: + vector tree; + + /* 深度优先遍历 */ + void dfs(int i, string order, vector &res) { + // 若为空位,则返回 + if (val(i) == INT_MAX) + return; + // 前序遍历 + if (order == "pre") + res.push_back(val(i)); + dfs(left(i), order, res); + // 中序遍历 + if (order == "in") + res.push_back(val(i)); + dfs(right(i), order, res); + // 后序遍历 + if (order == "post") + res.push_back(val(i)); + } + }; + ``` + +=== "Python" + + ```python title="array_binary_tree.py" + class ArrayBinaryTree: + """数组表示下的二叉树类""" + + def __init__(self, arr: list[int | None]): + """构造方法""" + self.__tree = list(arr) + + def size(self): + """节点数量""" + return len(self.__tree) + + def val(self, i: int) -> int: + """获取索引为 i 节点的值""" + # 若索引越界,则返回 None ,代表空位 + if i < 0 or i >= self.size(): + return None + return self.__tree[i] + + def left(self, i: int) -> int | None: + """获取索引为 i 节点的左子节点的索引""" + return 2 * i + 1 + + def right(self, i: int) -> int | None: + """获取索引为 i 节点的右子节点的索引""" + return 2 * i + 2 + + def parent(self, i: int) -> int | None: + """获取索引为 i 节点的父节点的索引""" + return (i - 1) // 2 + + def level_order(self) -> list[int]: + """层序遍历""" + self.res = [] + # 直接遍历数组 + for i in range(self.size()): + if self.val(i) is not None: + self.res.append(self.val(i)) + return self.res + + def __dfs(self, i: int, order: str): + """深度优先遍历""" + if self.val(i) is None: + return + # 前序遍历 + if order == "pre": + self.res.append(self.val(i)) + self.__dfs(self.left(i), order) + # 中序遍历 + if order == "in": + self.res.append(self.val(i)) + self.__dfs(self.right(i), order) + # 后序遍历 + if order == "post": + self.res.append(self.val(i)) + + def pre_order(self) -> list[int]: + """前序遍历""" + self.res = [] + self.__dfs(0, order="pre") + return self.res + + def in_order(self) -> list[int]: + """中序遍历""" + self.res = [] + self.__dfs(0, order="in") + return self.res + + def post_order(self) -> list[int]: + """后序遍历""" + self.res = [] + self.__dfs(0, order="post") + return self.res + ``` + +=== "Go" + + ```go title="array_binary_tree.go" + [class]{arrayBinaryTree}-[func]{} + ``` + +=== "JavaScript" + + ```javascript title="array_binary_tree.js" + [class]{ArrayBinaryTree}-[func]{} + ``` + +=== "TypeScript" + + ```typescript title="array_binary_tree.ts" + [class]{ArrayBinaryTree}-[func]{} + ``` + +=== "C" + + ```c title="array_binary_tree.c" + [class]{arrayBinaryTree}-[func]{} + ``` + +=== "C#" + + ```csharp title="array_binary_tree.cs" + [class]{ArrayBinaryTree}-[func]{} + ``` + +=== "Swift" + + ```swift title="array_binary_tree.swift" + [class]{ArrayBinaryTree}-[func]{} + ``` + +=== "Zig" + + ```zig title="array_binary_tree.zig" + [class]{ArrayBinaryTree}-[func]{} + ``` + +=== "Dart" + + ```dart title="array_binary_tree.dart" + [class]{ArrayBinaryTree}-[func]{} + ``` + ## 7.3.3.   优势与局限性 二叉树的数组表示存在以下优点: