mirror of
https://github.com/krahets/hello-algo.git
synced 2024-12-25 02:16:28 +08:00
build
This commit is contained in:
parent
97733f1a7d
commit
dbd1e4a750
12 changed files with 78 additions and 64 deletions
|
@ -9,9 +9,9 @@ icon: material/text-search
|
|||
|
||||
!!! abstract
|
||||
|
||||
Searching is an unknown adventure, where we may need to traverse every corner of a mysterious space, or perhaps quickly pinpoint our target.
|
||||
Searching is an adventure into the unknown; where we may need to traverse every corner of a mysterious space, or perhaps we’ll quickly locate our target.
|
||||
|
||||
In this journey of discovery, each exploration may yield an unexpected answer.
|
||||
On this journey of discovery, each exploration may end up with an unexpected answer.
|
||||
|
||||
## Chapter contents
|
||||
|
||||
|
|
|
@ -5,8 +5,8 @@ comments: true
|
|||
# 10.6 Summary
|
||||
|
||||
- Binary search depends on the order of data and performs the search by iteratively halving the search interval. It requires the input data to be sorted and is only applicable to arrays or array-based data structures.
|
||||
- Brute force search locates data by traversing the data structure. Linear search is suitable for arrays and linked lists, while breadth-first search and depth-first search are suitable for graphs and trees. These algorithms are highly versatile, requiring no preprocessing of data, but have a higher time complexity of $O(n)$.
|
||||
- Hash search, tree search, and binary search are efficient searching methods, capable of quickly locating target elements in specific data structures. These algorithms are highly efficient, with time complexities reaching $O(\log n)$ or even $O(1)$, but they usually require additional data structures.
|
||||
- In practice, we need to analyze factors such as data volume, search performance requirements, data query and update frequencies, etc., to choose the appropriate search method.
|
||||
- Linear search is suitable for small or frequently updated data; binary search is suitable for large, sorted data; hash search is suitable for scenarios requiring high query efficiency without the need for range queries; tree search is appropriate for large dynamic data that needs to maintain order and support range queries.
|
||||
- Replacing linear search with hash search is a common strategy to optimize runtime, reducing the time complexity from $O(n)$ to $O(1)$.
|
||||
- Brute force search may be required to locate an entry in an unordered dataset. Different search algorithms can be applied based on the data structure: Linear search is suitable for arrays and linked lists, while breadth-first search (BFS) and depth-first search (DFS) are suitable for graphs and trees. These algorithms are highly versatile, requiring no preprocessing of data, but they have a higher time complexity of $O(n)$.
|
||||
- Hash search, tree search, and binary search are efficient search methods that can quickly locate target elements within specific data structures. These algorithms are highly efficient, with time complexities reaching $O(\log n)$ or even $O(1)$, but they usually require extra space to accommodate additional data structures.
|
||||
- In practice, we need to analyze factors such as data volume, search performance requirements, data query and update frequencies, etc., to choose an appropriate search method.
|
||||
- Linear search is ideal for small or frequently updated (volatile) data. Binary search works well for large and sorted data. Hash search is suitable for data that requires high query efficiency and does not need range queries. Tree search is best suited for large dynamic data that require maintaining order and need to support range queries.
|
||||
- Replacing linear search with hash search is a common strategy to optimize runtime performance, reducing the time complexity from $O(n)$ to $O(1)$.
|
||||
|
|
|
@ -4,7 +4,7 @@ comments: true
|
|||
|
||||
# 7.1 Binary tree
|
||||
|
||||
A <u>binary tree</u> is a non-linear data structure that represents the hierarchical relationship between ancestors and descendants, embodying the divide-and-conquer logic of "splitting into two". Similar to a linked list, the basic unit of a binary tree is a node, each containing a value, a reference to the left child node, and a reference to the right child node.
|
||||
A <u>binary tree</u> is a non-linear data structure that represents the hierarchical relationship between ancestors and descendants and embodies the divide-and-conquer logic of "splitting into two". Similar to a linked list, the basic unit of a binary tree is a node, and each node contains a value, a reference to its left child node, and a reference to its right child node.
|
||||
|
||||
=== "Python"
|
||||
|
||||
|
@ -202,9 +202,9 @@ A <u>binary tree</u> is a non-linear data structure that represents the hierarch
|
|||
|
||||
```
|
||||
|
||||
Each node has two references (pointers), pointing to the <u>left-child node</u> and <u>right-child node</u>, respectively. This node is called the <u>parent node</u> of these two child nodes. When given a node of a binary tree, we call the tree formed by this node's left child and all nodes under it the <u>left subtree</u> of this node. Similarly, the <u>right subtree</u> can be defined.
|
||||
Each node has two references (pointers), pointing respectively to the <u>left-child node</u> and <u>right-child node</u>. This node is called the <u>parent node</u> of these two child nodes. When given a node of a binary tree, we call the tree formed by this node's left child and all nodes below it the <u>left subtree</u> of this node. Similarly, the <u>right subtree</u> can be defined.
|
||||
|
||||
**In a binary tree, except for leaf nodes, all other nodes contain child nodes and non-empty subtrees.** As shown in Figure 7-1, if "Node 2" is considered as the parent node, then its left and right child nodes are "Node 4" and "Node 5," respectively. The left subtree is "the tree formed by Node 4 and all nodes under it," and the right subtree is "the tree formed by Node 5 and all nodes under it."
|
||||
**In a binary tree, except leaf nodes, all other nodes contain child nodes and non-empty subtrees.** As shown in Figure 7-1, if "Node 2" is regarded as a parent node, its left and right child nodes are "Node 4" and "Node 5" respectively. The left subtree is formed by "Node 4" and all nodes beneath it, while the right subtree is formed by "Node 5" and all nodes beneath it.
|
||||
|
||||
![Parent Node, child Node, subtree](binary_tree.assets/binary_tree_definition.png){ class="animation-figure" }
|
||||
|
||||
|
@ -214,13 +214,13 @@ Each node has two references (pointers), pointing to the <u>left-child node</u>
|
|||
|
||||
The commonly used terminology of binary trees is shown in Figure 7-2.
|
||||
|
||||
- <u>Root node</u>: The node at the top level of the binary tree, which has no parent node.
|
||||
- <u>Leaf node</u>: A node with no children, both of its pointers point to `None`.
|
||||
- <u>Edge</u>: The line segment connecting two nodes, i.e., node reference (pointer).
|
||||
- The <u>level</u> of a node: Incrementing from top to bottom, with the root node's level being 1.
|
||||
- The <u>degree</u> of a node: The number of children a node has. In a binary tree, the degree can be 0, 1, or 2.
|
||||
- The <u>height</u> of a binary tree: The number of edges passed from the root node to the farthest leaf node.
|
||||
- The <u>depth</u> of a node: The number of edges passed from the root node to the node.
|
||||
- <u>Root node</u>: The node at the top level of a binary tree, which does not have a parent node.
|
||||
- <u>Leaf node</u>: A node that does not have any child nodes, with both of its pointers pointing to `None`.
|
||||
- <u>Edge</u>: A line segment that connects two nodes, representing a reference (pointer) between the nodes.
|
||||
- The <u>level</u> of a node: It increases from top to bottom, with the root node being at level 1.
|
||||
- The <u>degree</u> of a node: The number of child nodes that a node has. In a binary tree, the degree can be 0, 1, or 2.
|
||||
- The <u>height</u> of a binary tree: The number of edges from the root node to the farthest leaf node.
|
||||
- The <u>depth</u> of a node: The number of edges from the root node to the node.
|
||||
- The <u>height</u> of a node: The number of edges from the farthest leaf node to the node.
|
||||
|
||||
![Common Terminology of Binary Trees](binary_tree.assets/binary_tree_terminology.png){ class="animation-figure" }
|
||||
|
@ -229,13 +229,13 @@ The commonly used terminology of binary trees is shown in Figure 7-2.
|
|||
|
||||
!!! tip
|
||||
|
||||
Please note that we typically define "height" and "depth" as "the number of edges traversed", but some problems or textbooks may define them as "the number of nodes traversed". In such cases, both height and depth need to be incremented by 1.
|
||||
Please note that we usually define "height" and "depth" as "the number of edges traversed", but some questions or textbooks may define them as "the number of nodes traversed". In this case, both height and depth need to be incremented by 1.
|
||||
|
||||
## 7.1.2 Basic operations of binary trees
|
||||
|
||||
### 1. Initializing a binary tree
|
||||
|
||||
Similar to a linked list, begin by initialize nodes, then construct references (pointers).
|
||||
Similar to a linked list, the initialization of a binary tree involves first creating the nodes and then establishing the references (pointers) between them.
|
||||
|
||||
=== "Python"
|
||||
|
||||
|
@ -619,13 +619,13 @@ Similar to a linked list, inserting and removing nodes in a binary tree can be a
|
|||
|
||||
!!! tip
|
||||
|
||||
It's important to note that inserting nodes may change the original logical structure of the binary tree, while removing nodes typically involves removing the node and all its subtrees. Therefore, in a binary tree, insertion and removal are usually performed through a coordinated set of operations to achieve meaningful outcomes.
|
||||
It should be noted that inserting nodes may change the original logical structure of the binary tree, while removing nodes typically involves removing the node and all its subtrees. Therefore, in a binary tree, insertion and removal are usually performed through a set of operations to achieve meaningful outcomes.
|
||||
|
||||
## 7.1.3 Common types of binary trees
|
||||
|
||||
### 1. Perfect binary tree
|
||||
|
||||
As shown in Figure 7-4, in a <u>perfect binary tree</u>, all levels of nodes are fully filled. In a perfect binary tree, the degree of leaf nodes is $0$, while the degree of all other nodes is $2$; if the tree's height is $h$, then the total number of nodes is $2^{h+1} - 1$, showing a standard exponential relationship, reflecting the common phenomenon of cell division in nature.
|
||||
As shown in Figure 7-4, in a <u>perfect binary tree</u>, all levels are completely filled with nodes. In a perfect binary tree, leaf nodes have a degree of $0$, while all other nodes have a degree of $2$. The total number of nodes can be calculated as $2^{h+1} - 1$, where $h$ is the height of the tree. This exhibits a standard exponential relationship, reflecting the common phenomenon of cell division in nature.
|
||||
|
||||
!!! tip
|
||||
|
||||
|
@ -637,7 +637,7 @@ As shown in Figure 7-4, in a <u>perfect binary tree</u>, all levels of nodes are
|
|||
|
||||
### 2. Complete binary tree
|
||||
|
||||
As shown in Figure 7-5, a <u>complete binary tree</u> has only the bottom level nodes not fully filled, and the bottom level nodes are filled as far left as possible.
|
||||
As shown in Figure 7-5, a <u>complete binary tree</u> is a binary tree where only the nodes in the bottom level are not completely filled, and the nodes in the bottom level are filled from left to right as much as possible. Please note that a perfect binary tree is also a complete binary tree.
|
||||
|
||||
![Complete binary tree](binary_tree.assets/complete_binary_tree.png){ class="animation-figure" }
|
||||
|
||||
|
@ -645,7 +645,7 @@ As shown in Figure 7-5, a <u>complete binary tree</u> has only the bottom level
|
|||
|
||||
### 3. Full binary tree
|
||||
|
||||
As shown in Figure 7-6, a <u>full binary tree</u> has all nodes except leaf nodes having two children.
|
||||
As shown in Figure 7-6, a <u>full binary tree</u>, except for the leaf nodes, has two child nodes for all other nodes.
|
||||
|
||||
![Full binary tree](binary_tree.assets/full_binary_tree.png){ class="animation-figure" }
|
||||
|
||||
|
@ -653,7 +653,7 @@ As shown in Figure 7-6, a <u>full binary tree</u> has all nodes except leaf node
|
|||
|
||||
### 4. Balanced binary tree
|
||||
|
||||
As shown in Figure 7-7, in a <u>balanced binary tree</u>, the absolute difference in height between the left and right subtrees of any node does not exceed 1.
|
||||
As shown in Figure 7-7, in a <u>balanced binary tree</u>, the absolute difference between the height of the left and right subtrees of any node does not exceed 1.
|
||||
|
||||
![Balanced binary tree](binary_tree.assets/balanced_binary_tree.png){ class="animation-figure" }
|
||||
|
||||
|
@ -663,14 +663,14 @@ As shown in Figure 7-7, in a <u>balanced binary tree</u>, the absolute differenc
|
|||
|
||||
Figure 7-8 shows the ideal and degenerate structures of binary trees. A binary tree becomes a "perfect binary tree" when every level is filled; while it degenerates into a "linked list" when all nodes are biased toward one side.
|
||||
|
||||
- The perfect binary tree is the ideal situation, fully leveraging the "divide and conquer" advantage of binary trees.
|
||||
- A linked list is another extreme, where operations become linear, degrading the time complexity to $O(n)$.
|
||||
- A perfect binary tree is an ideal scenario where the "divide and conquer" advantage of a binary tree can be fully utilized.
|
||||
- On the other hand, a linked list represents another extreme where all operations become linear, resulting in a time complexity of $O(n)$.
|
||||
|
||||
![The Best and Worst Structures of Binary Trees](binary_tree.assets/binary_tree_best_worst_cases.png){ class="animation-figure" }
|
||||
|
||||
<p align="center"> Figure 7-8 The Best and Worst Structures of Binary Trees </p>
|
||||
|
||||
As shown in Table 7-1, in the best and worst structures, the number of leaf nodes, total number of nodes, and height of the binary tree reach their maximum or minimum values.
|
||||
As shown in Table 7-1, in the best and worst structures, the binary tree achieves either maximum or minimum values for leaf node count, total number of nodes, and height.
|
||||
|
||||
<p align="center"> Table 7-1 The Best and Worst Structures of Binary Trees </p>
|
||||
|
||||
|
|
|
@ -414,7 +414,7 @@
|
|||
<!-- contributors -->
|
||||
<div style="margin: 2em auto;">
|
||||
<h3>Contributors</h3>
|
||||
<p>This book has been optimized by the efforts of over 180 contributors. We sincerely thank them for their invaluable time and contributions!</p>
|
||||
<p>This book has been refined by the efforts of over 180 contributors. We sincerely thank them for their invaluable time and contributions!</p>
|
||||
<a href="https://github.com/krahets/hello-algo/graphs/contributors">
|
||||
<img src="https://contrib.rocks/image?repo=krahets/hello-algo&max=300&columns=16" alt="Contributors" style="width: 100%; max-width: 38.5em;">
|
||||
</a>
|
||||
|
|
|
@ -167,8 +167,8 @@ comments: true
|
|||
/* 初始化堆積 */
|
||||
// 初始化小頂堆積
|
||||
PriorityQueue<int, int> minHeap = new();
|
||||
// 初始化大頂堆積(使用 lambda 表示式修改 Comparator 即可)
|
||||
PriorityQueue<int, int> maxHeap = new(Comparer<int>.Create((x, y) => y - x));
|
||||
// 初始化大頂堆積(使用 lambda 表示式修改 Comparer 即可)
|
||||
PriorityQueue<int, int> maxHeap = new(Comparer<int>.Create((x, y) => y.CompareTo(x)));
|
||||
|
||||
/* 元素入堆積 */
|
||||
maxHeap.Enqueue(1, 1);
|
||||
|
|
|
@ -490,12 +490,12 @@ comments: true
|
|||
}
|
||||
|
||||
/* 雜湊表元素插入 */
|
||||
void insert(HashTable *h, int key, int val) {
|
||||
HashTable *t = find(h, key);
|
||||
void insert(HashTable **h, int key, int val) {
|
||||
HashTable *t = find(*h, key);
|
||||
if (t == NULL) {
|
||||
HashTable *tmp = malloc(sizeof(HashTable));
|
||||
tmp->key = key, tmp->val = val;
|
||||
HASH_ADD_INT(h, key, tmp);
|
||||
HASH_ADD_INT(*h, key, tmp);
|
||||
} else {
|
||||
t->val = val;
|
||||
}
|
||||
|
@ -512,7 +512,7 @@ comments: true
|
|||
*returnSize = 2;
|
||||
return res;
|
||||
}
|
||||
insert(hashtable, nums[i], i);
|
||||
insert(&hashtable, nums[i], i);
|
||||
}
|
||||
*returnSize = 0;
|
||||
return NULL;
|
||||
|
|
|
@ -825,6 +825,7 @@ $$
|
|||
// 使用結果陣列 res 覆蓋原陣列 nums
|
||||
memcpy(nums, res, size * sizeof(int));
|
||||
// 5. 釋放記憶體
|
||||
free(res);
|
||||
free(counter);
|
||||
}
|
||||
```
|
||||
|
|
|
@ -574,6 +574,7 @@ $$
|
|||
void countingSortDigit(int nums[], int size, int exp) {
|
||||
// 十進位制的位範圍為 0~9 ,因此需要長度為 10 的桶陣列
|
||||
int *counter = (int *)malloc((sizeof(int) * 10));
|
||||
memset(counter, 0, sizeof(int) * 10); // 初始化為 0 以支持後續記憶體釋放
|
||||
// 統計 0~9 各數字的出現次數
|
||||
for (int i = 0; i < size; i++) {
|
||||
// 獲取 nums[i] 第 k 位,記為 d
|
||||
|
@ -597,13 +598,16 @@ $$
|
|||
for (int i = 0; i < size; i++) {
|
||||
nums[i] = res[i];
|
||||
}
|
||||
// 釋放記憶體
|
||||
free(res);
|
||||
free(counter);
|
||||
}
|
||||
|
||||
/* 基數排序 */
|
||||
void radixSort(int nums[], int size) {
|
||||
// 獲取陣列的最大元素,用於判斷最大位數
|
||||
int max = INT32_MIN;
|
||||
for (size_t i = 0; i < size - 1; i++) {
|
||||
for (int i = 0; i < size; i++) {
|
||||
if (nums[i] > max) {
|
||||
max = nums[i];
|
||||
}
|
||||
|
|
|
@ -1020,18 +1020,11 @@ comments: true
|
|||
|
||||
/* 層序走訪 */
|
||||
fn level_order(&self) -> Vec<i32> {
|
||||
let mut res = vec![];
|
||||
// 直接走訪陣列
|
||||
for i in 0..self.size() {
|
||||
if let Some(val) = self.val(i) {
|
||||
res.push(val)
|
||||
}
|
||||
}
|
||||
res
|
||||
self.tree.iter().filter_map(|&x| x).collect()
|
||||
}
|
||||
|
||||
/* 深度優先走訪 */
|
||||
fn dfs(&self, i: i32, order: &str, res: &mut Vec<i32>) {
|
||||
fn dfs(&self, i: i32, order: &'static str, res: &mut Vec<i32>) {
|
||||
if self.val(i).is_none() {
|
||||
return;
|
||||
}
|
||||
|
|
|
@ -188,7 +188,7 @@ AVL 樹既是二元搜尋樹,也是平衡二元樹,同時滿足這兩類二
|
|||
|
||||
```c title=""
|
||||
/* AVL 樹節點結構體 */
|
||||
TreeNode struct TreeNode {
|
||||
typedef struct TreeNode {
|
||||
int val;
|
||||
int height;
|
||||
struct TreeNode *left;
|
||||
|
|
|
@ -1447,7 +1447,7 @@ comments: true
|
|||
// 刪除節點 cur
|
||||
if !Rc::ptr_eq(&cur, self.root.as_ref().unwrap()) {
|
||||
let left = pre.borrow().left.clone();
|
||||
if left.is_some() && Rc::ptr_eq(&left.as_ref().unwrap(), &cur) {
|
||||
if left.is_some() && Rc::ptr_eq(left.as_ref().unwrap(), &cur) {
|
||||
pre.borrow_mut().left = child;
|
||||
} else {
|
||||
pre.borrow_mut().right = child;
|
||||
|
@ -1468,11 +1468,11 @@ comments: true
|
|||
break;
|
||||
}
|
||||
}
|
||||
let tmpval = tmp.unwrap().borrow().val;
|
||||
let tmp_val = tmp.unwrap().borrow().val;
|
||||
// 遞迴刪除節點 tmp
|
||||
self.remove(tmpval);
|
||||
self.remove(tmp_val);
|
||||
// 用 tmp 覆蓋 cur
|
||||
cur.borrow_mut().val = tmpval;
|
||||
cur.borrow_mut().val = tmp_val;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
@ -700,12 +700,17 @@ comments: true
|
|||
fn pre_order(root: Option<&Rc<RefCell<TreeNode>>>) -> Vec<i32> {
|
||||
let mut result = vec![];
|
||||
|
||||
fn dfs(root: Option<&Rc<RefCell<TreeNode>>>, res: &mut Vec<i32>) {
|
||||
if let Some(node) = root {
|
||||
// 訪問優先順序:根節點 -> 左子樹 -> 右子樹
|
||||
result.push(node.borrow().val);
|
||||
result.extend(pre_order(node.borrow().left.as_ref()));
|
||||
result.extend(pre_order(node.borrow().right.as_ref()));
|
||||
let node = node.borrow();
|
||||
res.push(node.val);
|
||||
dfs(node.left.as_ref(), res);
|
||||
dfs(node.right.as_ref(), res);
|
||||
}
|
||||
}
|
||||
dfs(root, &mut result);
|
||||
|
||||
result
|
||||
}
|
||||
|
||||
|
@ -713,12 +718,17 @@ comments: true
|
|||
fn in_order(root: Option<&Rc<RefCell<TreeNode>>>) -> Vec<i32> {
|
||||
let mut result = vec![];
|
||||
|
||||
fn dfs(root: Option<&Rc<RefCell<TreeNode>>>, res: &mut Vec<i32>) {
|
||||
if let Some(node) = root {
|
||||
// 訪問優先順序:左子樹 -> 根節點 -> 右子樹
|
||||
result.extend(in_order(node.borrow().left.as_ref()));
|
||||
result.push(node.borrow().val);
|
||||
result.extend(in_order(node.borrow().right.as_ref()));
|
||||
let node = node.borrow();
|
||||
dfs(node.left.as_ref(), res);
|
||||
res.push(node.val);
|
||||
dfs(node.right.as_ref(), res);
|
||||
}
|
||||
}
|
||||
dfs(root, &mut result);
|
||||
|
||||
result
|
||||
}
|
||||
|
||||
|
@ -726,12 +736,18 @@ comments: true
|
|||
fn post_order(root: Option<&Rc<RefCell<TreeNode>>>) -> Vec<i32> {
|
||||
let mut result = vec![];
|
||||
|
||||
fn dfs(root: Option<&Rc<RefCell<TreeNode>>>, res: &mut Vec<i32>) {
|
||||
if let Some(node) = root {
|
||||
// 訪問優先順序:左子樹 -> 右子樹 -> 根節點
|
||||
result.extend(post_order(node.borrow().left.as_ref()));
|
||||
result.extend(post_order(node.borrow().right.as_ref()));
|
||||
result.push(node.borrow().val);
|
||||
let node = node.borrow();
|
||||
dfs(node.left.as_ref(), res);
|
||||
dfs(node.right.as_ref(), res);
|
||||
res.push(node.val);
|
||||
}
|
||||
}
|
||||
|
||||
dfs(root, &mut result);
|
||||
|
||||
result
|
||||
}
|
||||
```
|
||||
|
|
Loading…
Reference in a new issue