parent
bd0535ab5a
commit
20f79f5f32
@ -0,0 +1,103 @@
|
|||||||
|
---
|
||||||
|
comments: true
|
||||||
|
---
|
||||||
|
|
||||||
|
# 9.1 Graph
|
||||||
|
|
||||||
|
A "graph" is a type of nonlinear data structure, consisting of "vertices" and "edges". A graph $G$ can be abstractly represented as a collection of a set of vertices $V$ and a set of edges $E$. The following example shows a graph containing 5 vertices and 7 edges.
|
||||||
|
|
||||||
|
$$
|
||||||
|
\begin{aligned}
|
||||||
|
V & = \{ 1, 2, 3, 4, 5 \} \newline
|
||||||
|
E & = \{ (1,2), (1,3), (1,5), (2,3), (2,4), (2,5), (4,5) \} \newline
|
||||||
|
G & = \{ V, E \} \newline
|
||||||
|
\end{aligned}
|
||||||
|
$$
|
||||||
|
|
||||||
|
If vertices are viewed as nodes and edges as references (pointers) connecting the nodes, graphs can be seen as a data structure that extends from linked lists. As shown below, **compared to linear relationships (linked lists) and divide-and-conquer relationships (trees), network relationships (graphs) are more complex due to their higher degree of freedom**.
|
||||||
|
|
||||||
|
![Relationship between linked lists, trees, and graphs](graph.assets/linkedlist_tree_graph.png){ class="animation-figure" }
|
||||||
|
|
||||||
|
<p align="center"> Figure 9-1 Relationship between linked lists, trees, and graphs </p>
|
||||||
|
|
||||||
|
## 9.1.1 Common types of graphs
|
||||||
|
|
||||||
|
Based on whether edges have direction, graphs can be divided into "undirected graphs" and "directed graphs", as shown below.
|
||||||
|
|
||||||
|
- In undirected graphs, edges represent a "bidirectional" connection between two vertices, for example, the "friendship" in WeChat or QQ.
|
||||||
|
- In directed graphs, edges have directionality, that is, the edges $A \rightarrow B$ and $A \leftarrow B$ are independent of each other, for example, the "follow" and "be followed" relationship on Weibo or TikTok.
|
||||||
|
|
||||||
|
![Directed and undirected graphs](graph.assets/directed_graph.png){ class="animation-figure" }
|
||||||
|
|
||||||
|
<p align="center"> Figure 9-2 Directed and undirected graphs </p>
|
||||||
|
|
||||||
|
Based on whether all vertices are connected, graphs can be divided into "connected graphs" and "disconnected graphs", as shown below.
|
||||||
|
|
||||||
|
- For connected graphs, it is possible to reach any other vertex starting from a certain vertex.
|
||||||
|
- For disconnected graphs, there is at least one vertex that cannot be reached from a certain starting vertex.
|
||||||
|
|
||||||
|
![Connected and disconnected graphs](graph.assets/connected_graph.png){ class="animation-figure" }
|
||||||
|
|
||||||
|
<p align="center"> Figure 9-3 Connected and disconnected graphs </p>
|
||||||
|
|
||||||
|
We can also add a "weight" variable to edges, resulting in "weighted graphs" as shown below. For example, in mobile games like "Honor of Kings", the system calculates the "closeness" between players based on shared gaming time, and this closeness network can be represented with a weighted graph.
|
||||||
|
|
||||||
|
![Weighted and unweighted graphs](graph.assets/weighted_graph.png){ class="animation-figure" }
|
||||||
|
|
||||||
|
<p align="center"> Figure 9-4 Weighted and unweighted graphs </p>
|
||||||
|
|
||||||
|
Graph data structures include the following commonly used terms.
|
||||||
|
|
||||||
|
- "Adjacency": When there is an edge connecting two vertices, these two vertices are said to be "adjacent". In the above figure, the adjacent vertices of vertex 1 are vertices 2, 3, and 5.
|
||||||
|
- "Path": The sequence of edges passed from vertex A to vertex B is called a "path" from A to B. In the above figure, the edge sequence 1-5-2-4 is a path from vertex 1 to vertex 4.
|
||||||
|
- "Degree": The number of edges a vertex has. For directed graphs, "in-degree" refers to how many edges point to the vertex, and "out-degree" refers to how many edges point out from the vertex.
|
||||||
|
|
||||||
|
## 9.1.2 Representation of graphs
|
||||||
|
|
||||||
|
Common representations of graphs include "adjacency matrices" and "adjacency lists". The following examples use undirected graphs.
|
||||||
|
|
||||||
|
### 1. Adjacency matrix
|
||||||
|
|
||||||
|
Let the number of vertices in the graph be $n$, the "adjacency matrix" uses an $n \times n$ matrix to represent the graph, where each row (column) represents a vertex, and the matrix elements represent edges, with $1$ or $0$ indicating whether there is an edge between two vertices.
|
||||||
|
|
||||||
|
As shown below, let the adjacency matrix be $M$, and the list of vertices be $V$, then the matrix element $M[i, j] = 1$ indicates there is an edge between vertex $V[i]$ and vertex $V[j]$, conversely $M[i, j] = 0$ indicates there is no edge between the two vertices.
|
||||||
|
|
||||||
|
![Representation of a graph with an adjacency matrix](graph.assets/adjacency_matrix.png){ class="animation-figure" }
|
||||||
|
|
||||||
|
<p align="center"> Figure 9-5 Representation of a graph with an adjacency matrix </p>
|
||||||
|
|
||||||
|
Adjacency matrices have the following characteristics.
|
||||||
|
|
||||||
|
- A vertex cannot be connected to itself, so the elements on the main diagonal of the adjacency matrix are meaningless.
|
||||||
|
- For undirected graphs, edges in both directions are equivalent, thus the adjacency matrix is symmetric about the main diagonal.
|
||||||
|
- By replacing the elements of the adjacency matrix from $1$ and $0$ to weights, it can represent weighted graphs.
|
||||||
|
|
||||||
|
When representing graphs with adjacency matrices, it is possible to directly access matrix elements to obtain edges, thus operations of addition, deletion, lookup, and modification are very efficient, all with a time complexity of $O(1)$. However, the space complexity of the matrix is $O(n^2)$, which consumes more memory.
|
||||||
|
|
||||||
|
### 2. Adjacency list
|
||||||
|
|
||||||
|
The "adjacency list" uses $n$ linked lists to represent the graph, with each linked list node representing a vertex. The $i$-th linked list corresponds to vertex $i$ and contains all adjacent vertices (vertices connected to that vertex). The Figure 9-6 shows an example of a graph stored using an adjacency list.
|
||||||
|
|
||||||
|
![Representation of a graph with an adjacency list](graph.assets/adjacency_list.png){ class="animation-figure" }
|
||||||
|
|
||||||
|
<p align="center"> Figure 9-6 Representation of a graph with an adjacency list </p>
|
||||||
|
|
||||||
|
The adjacency list only stores actual edges, and the total number of edges is often much less than $n^2$, making it more space-efficient. However, finding edges in the adjacency list requires traversing the linked list, so its time efficiency is not as good as that of the adjacency matrix.
|
||||||
|
|
||||||
|
Observing the above figure, **the structure of the adjacency list is very similar to the "chaining" in hash tables, hence we can use similar methods to optimize efficiency**. For example, when the linked list is long, it can be transformed into an AVL tree or red-black tree, thus optimizing the time efficiency from $O(n)$ to $O(\log n)$; the linked list can also be transformed into a hash table, thus reducing the time complexity to $O(1)$.
|
||||||
|
|
||||||
|
## 9.1.3 Common applications of graphs
|
||||||
|
|
||||||
|
As shown in the Table 9-1 , many real-world systems can be modeled with graphs, and corresponding problems can be reduced to graph computing problems.
|
||||||
|
|
||||||
|
<p align="center"> Table 9-1 Common graphs in real life </p>
|
||||||
|
|
||||||
|
<div class="center-table" markdown>
|
||||||
|
|
||||||
|
| | Vertices | Edges | Graph Computing Problem |
|
||||||
|
| --------------- | ---------------- | --------------------------------------------- | -------------------------------- |
|
||||||
|
| Social Networks | Users | Friendships | Potential Friend Recommendations |
|
||||||
|
| Subway Lines | Stations | Connectivity Between Stations | Shortest Route Recommendations |
|
||||||
|
| Solar System | Celestial Bodies | Gravitational Forces Between Celestial Bodies | Planetary Orbit Calculations |
|
||||||
|
|
||||||
|
</div>
|
File diff suppressed because one or more lines are too long
File diff suppressed because one or more lines are too long
@ -0,0 +1,21 @@
|
|||||||
|
---
|
||||||
|
comments: true
|
||||||
|
icon: material/graphql
|
||||||
|
---
|
||||||
|
|
||||||
|
# Chapter 9. Graph
|
||||||
|
|
||||||
|
![Graph](../assets/covers/chapter_graph.jpg){ class="cover-image" }
|
||||||
|
|
||||||
|
!!! abstract
|
||||||
|
|
||||||
|
In the journey of life, we are like individual nodes, connected by countless invisible edges.
|
||||||
|
|
||||||
|
Every encountering and parting leaves a unique mark on this vast network graph.
|
||||||
|
|
||||||
|
## Chapter Contents
|
||||||
|
|
||||||
|
- [9.1 Graph](https://www.hello-algo.com/en/chapter_graph/graph/)
|
||||||
|
- [9.2 Basic Graph Operations](https://www.hello-algo.com/en/chapter_graph/graph_operations/)
|
||||||
|
- [9.3 Graph Traversal](https://www.hello-algo.com/en/chapter_graph/graph_traversal/)
|
||||||
|
- [9.4 Summary](https://www.hello-algo.com/en/chapter_graph/summary/)
|
@ -0,0 +1,35 @@
|
|||||||
|
---
|
||||||
|
comments: true
|
||||||
|
---
|
||||||
|
|
||||||
|
# 9.4 Summary
|
||||||
|
|
||||||
|
### 1. Key review
|
||||||
|
|
||||||
|
- A graph consists of vertices and edges and can be represented as a set comprising a group of vertices and a group of edges.
|
||||||
|
- Compared to linear relationships (linked lists) and divide-and-conquer relationships (trees), network relationships (graphs) have a higher degree of freedom and are therefore more complex.
|
||||||
|
- The edges of a directed graph have directionality, any vertex in a connected graph is reachable, and each edge in a weighted graph contains a weight variable.
|
||||||
|
- Adjacency matrices use matrices to represent graphs, with each row (column) representing a vertex and matrix elements representing edges, using $1$ or $0$ to indicate the presence or absence of an edge between two vertices. Adjacency matrices are highly efficient for add, delete, find, and modify operations, but they consume more space.
|
||||||
|
- Adjacency lists use multiple linked lists to represent graphs, with the $i^{th}$ list corresponding to vertex $i$, containing all its adjacent vertices. Adjacency lists save more space compared to adjacency matrices, but since it is necessary to traverse the list to find edges, their time efficiency is lower.
|
||||||
|
- When the linked lists in the adjacency list are too long, they can be converted into red-black trees or hash tables to improve query efficiency.
|
||||||
|
- From the perspective of algorithmic thinking, adjacency matrices embody the principle of "space for time," while adjacency lists embody "time for space."
|
||||||
|
- Graphs can be used to model various real systems, such as social networks, subway routes, etc.
|
||||||
|
- A tree is a special case of a graph, and tree traversal is also a special case of graph traversal.
|
||||||
|
- Breadth-first traversal of a graph is a search method that expands layer by layer from near to far, usually implemented with a queue.
|
||||||
|
- Depth-first traversal of a graph is a search method that prefers to go as deep as possible and backtracks when no further paths are available, often based on recursion.
|
||||||
|
|
||||||
|
### 2. Q & A
|
||||||
|
|
||||||
|
**Q**: Is a path defined as a sequence of vertices or a sequence of edges?
|
||||||
|
|
||||||
|
Definitions vary between different language versions on Wikipedia: the English version defines a path as "a sequence of edges," while the Chinese version defines it as "a sequence of vertices." Here is the original text from the English version: In graph theory, a path in a graph is a finite or infinite sequence of edges which joins a sequence of vertices.
|
||||||
|
|
||||||
|
In this document, a path is considered a sequence of edges, rather than a sequence of vertices. This is because there might be multiple edges connecting two vertices, in which case each edge corresponds to a path.
|
||||||
|
|
||||||
|
**Q**: In a disconnected graph, are there points that cannot be traversed to?
|
||||||
|
|
||||||
|
In a disconnected graph, starting from a certain vertex, there is at least one vertex that cannot be reached. Traversing a disconnected graph requires setting multiple starting points to traverse all connected components of the graph.
|
||||||
|
|
||||||
|
**Q**: In an adjacency list, does the order of "all vertices connected to that vertex" matter?
|
||||||
|
|
||||||
|
It can be in any order. However, in practical applications, it might be necessary to sort according to certain rules, such as the order in which vertices are added, or the order of vertex values, etc., to facilitate the quick search for vertices with certain extremal values.
|
File diff suppressed because one or more lines are too long
@ -0,0 +1,21 @@
|
|||||||
|
---
|
||||||
|
comments: true
|
||||||
|
icon: material/family-tree
|
||||||
|
---
|
||||||
|
|
||||||
|
# Chapter 8. Heap
|
||||||
|
|
||||||
|
![Heap](../assets/covers/chapter_heap.jpg){ class="cover-image" }
|
||||||
|
|
||||||
|
!!! abstract
|
||||||
|
|
||||||
|
The heap is like mountain peaks, stacked and undulating, each with its unique shape.
|
||||||
|
|
||||||
|
Among these peaks, the highest one always catches the eye first.
|
||||||
|
|
||||||
|
## Chapter Contents
|
||||||
|
|
||||||
|
- [8.1 Heap](https://www.hello-algo.com/en/chapter_heap/heap/)
|
||||||
|
- [8.2 Building a Heap](https://www.hello-algo.com/en/chapter_heap/build_heap/)
|
||||||
|
- [8.3 Top-k Problem](https://www.hello-algo.com/en/chapter_heap/top_k/)
|
||||||
|
- [8.4 Summary](https://www.hello-algo.com/en/chapter_heap/summary/)
|
@ -0,0 +1,21 @@
|
|||||||
|
---
|
||||||
|
comments: true
|
||||||
|
---
|
||||||
|
|
||||||
|
# 8.4 Summary
|
||||||
|
|
||||||
|
### 1. Key review
|
||||||
|
|
||||||
|
- A heap is a complete binary tree, which can be divided into a max heap and a min heap based on its property. The top element of a max (min) heap is the largest (smallest).
|
||||||
|
- A priority queue is defined as a queue with dequeue priority, usually implemented using a heap.
|
||||||
|
- Common operations of a heap and their corresponding time complexities include: element insertion into the heap $O(\log n)$, removing the top element from the heap $O(\log n)$, and accessing the top element of the heap $O(1)$.
|
||||||
|
- A complete binary tree is well-suited to be represented by an array, thus heaps are commonly stored using arrays.
|
||||||
|
- Heapify operations are used to maintain the properties of the heap and are used in both heap insertion and removal operations.
|
||||||
|
- The time complexity of inserting $n$ elements into a heap and building the heap can be optimized to $O(n)$, which is highly efficient.
|
||||||
|
- Top-k is a classic algorithm problem that can be efficiently solved using the heap data structure, with a time complexity of $O(n \log k)$.
|
||||||
|
|
||||||
|
### 2. Q & A
|
||||||
|
|
||||||
|
**Q**: Is the "heap" in data structures the same concept as the "heap" in memory management?
|
||||||
|
|
||||||
|
The two are not the same concept, even though they are both referred to as "heap". The heap in computer system memory is part of dynamic memory allocation, where the program can use it to store data during execution. The program can request a certain amount of heap memory to store complex structures like objects and arrays. When these data are no longer needed, the program needs to release this memory to prevent memory leaks. Compared to stack memory, the management and usage of heap memory need to be more cautious, as improper use may lead to memory leaks and dangling pointers.
|
@ -0,0 +1,456 @@
|
|||||||
|
---
|
||||||
|
comments: true
|
||||||
|
---
|
||||||
|
|
||||||
|
# 8.3 Top-k problem
|
||||||
|
|
||||||
|
!!! question
|
||||||
|
|
||||||
|
Given an unordered array `nums` of length $n$, return the largest $k$ elements in the array.
|
||||||
|
|
||||||
|
For this problem, we will first introduce two straightforward solutions, then explain a more efficient heap-based method.
|
||||||
|
|
||||||
|
## 8.3.1 Method 1: Iterative selection
|
||||||
|
|
||||||
|
We can perform $k$ rounds of iterations as shown in the Figure 8-6 , extracting the $1^{st}$, $2^{nd}$, $\dots$, $k^{th}$ largest elements in each round, with a time complexity of $O(nk)$.
|
||||||
|
|
||||||
|
This method is only suitable when $k \ll n$, as the time complexity approaches $O(n^2)$ when $k$ is close to $n$, which is very time-consuming.
|
||||||
|
|
||||||
|
![Iteratively finding the largest k elements](top_k.assets/top_k_traversal.png){ class="animation-figure" }
|
||||||
|
|
||||||
|
<p align="center"> Figure 8-6 Iteratively finding the largest k elements </p>
|
||||||
|
|
||||||
|
!!! tip
|
||||||
|
|
||||||
|
When $k = n$, we can obtain a complete ordered sequence, which is equivalent to the "selection sort" algorithm.
|
||||||
|
|
||||||
|
## 8.3.2 Method 2: Sorting
|
||||||
|
|
||||||
|
As shown in the Figure 8-7 , we can first sort the array `nums` and then return the last $k$ elements, with a time complexity of $O(n \log n)$.
|
||||||
|
|
||||||
|
Clearly, this method "overachieves" the task, as we only need to find the largest $k$ elements, without the need to sort the other elements.
|
||||||
|
|
||||||
|
![Sorting to find the largest k elements](top_k.assets/top_k_sorting.png){ class="animation-figure" }
|
||||||
|
|
||||||
|
<p align="center"> Figure 8-7 Sorting to find the largest k elements </p>
|
||||||
|
|
||||||
|
## 8.3.3 Method 3: Heap
|
||||||
|
|
||||||
|
We can solve the Top-k problem more efficiently based on heaps, as shown in the following process.
|
||||||
|
|
||||||
|
1. Initialize a min heap, where the top element is the smallest.
|
||||||
|
2. First, insert the first $k$ elements of the array into the heap.
|
||||||
|
3. Starting from the $k + 1^{th}$ element, if the current element is greater than the top element of the heap, remove the top element of the heap and insert the current element into the heap.
|
||||||
|
4. After completing the traversal, the heap contains the largest $k$ elements.
|
||||||
|
|
||||||
|
=== "<1>"
|
||||||
|
![Find the largest k elements based on heap](top_k.assets/top_k_heap_step1.png){ class="animation-figure" }
|
||||||
|
|
||||||
|
=== "<2>"
|
||||||
|
![top_k_heap_step2](top_k.assets/top_k_heap_step2.png){ class="animation-figure" }
|
||||||
|
|
||||||
|
=== "<3>"
|
||||||
|
![top_k_heap_step3](top_k.assets/top_k_heap_step3.png){ class="animation-figure" }
|
||||||
|
|
||||||
|
=== "<4>"
|
||||||
|
![top_k_heap_step4](top_k.assets/top_k_heap_step4.png){ class="animation-figure" }
|
||||||
|
|
||||||
|
=== "<5>"
|
||||||
|
![top_k_heap_step5](top_k.assets/top_k_heap_step5.png){ class="animation-figure" }
|
||||||
|
|
||||||
|
=== "<6>"
|
||||||
|
![top_k_heap_step6](top_k.assets/top_k_heap_step6.png){ class="animation-figure" }
|
||||||
|
|
||||||
|
=== "<7>"
|
||||||
|
![top_k_heap_step7](top_k.assets/top_k_heap_step7.png){ class="animation-figure" }
|
||||||
|
|
||||||
|
=== "<8>"
|
||||||
|
![top_k_heap_step8](top_k.assets/top_k_heap_step8.png){ class="animation-figure" }
|
||||||
|
|
||||||
|
=== "<9>"
|
||||||
|
![top_k_heap_step9](top_k.assets/top_k_heap_step9.png){ class="animation-figure" }
|
||||||
|
|
||||||
|
<p align="center"> Figure 8-8 Find the largest k elements based on heap </p>
|
||||||
|
|
||||||
|
Example code is as follows:
|
||||||
|
|
||||||
|
=== "Python"
|
||||||
|
|
||||||
|
```python title="top_k.py"
|
||||||
|
def top_k_heap(nums: list[int], k: int) -> list[int]:
|
||||||
|
"""基于堆查找数组中最大的 k 个元素"""
|
||||||
|
# 初始化小顶堆
|
||||||
|
heap = []
|
||||||
|
# 将数组的前 k 个元素入堆
|
||||||
|
for i in range(k):
|
||||||
|
heapq.heappush(heap, nums[i])
|
||||||
|
# 从第 k+1 个元素开始,保持堆的长度为 k
|
||||||
|
for i in range(k, len(nums)):
|
||||||
|
# 若当前元素大于堆顶元素,则将堆顶元素出堆、当前元素入堆
|
||||||
|
if nums[i] > heap[0]:
|
||||||
|
heapq.heappop(heap)
|
||||||
|
heapq.heappush(heap, nums[i])
|
||||||
|
return heap
|
||||||
|
```
|
||||||
|
|
||||||
|
=== "C++"
|
||||||
|
|
||||||
|
```cpp title="top_k.cpp"
|
||||||
|
/* 基于堆查找数组中最大的 k 个元素 */
|
||||||
|
priority_queue<int, vector<int>, greater<int>> topKHeap(vector<int> &nums, int k) {
|
||||||
|
// 初始化小顶堆
|
||||||
|
priority_queue<int, vector<int>, greater<int>> heap;
|
||||||
|
// 将数组的前 k 个元素入堆
|
||||||
|
for (int i = 0; i < k; i++) {
|
||||||
|
heap.push(nums[i]);
|
||||||
|
}
|
||||||
|
// 从第 k+1 个元素开始,保持堆的长度为 k
|
||||||
|
for (int i = k; i < nums.size(); i++) {
|
||||||
|
// 若当前元素大于堆顶元素,则将堆顶元素出堆、当前元素入堆
|
||||||
|
if (nums[i] > heap.top()) {
|
||||||
|
heap.pop();
|
||||||
|
heap.push(nums[i]);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return heap;
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
|
=== "Java"
|
||||||
|
|
||||||
|
```java title="top_k.java"
|
||||||
|
/* 基于堆查找数组中最大的 k 个元素 */
|
||||||
|
Queue<Integer> topKHeap(int[] nums, int k) {
|
||||||
|
// 初始化小顶堆
|
||||||
|
Queue<Integer> heap = new PriorityQueue<Integer>();
|
||||||
|
// 将数组的前 k 个元素入堆
|
||||||
|
for (int i = 0; i < k; i++) {
|
||||||
|
heap.offer(nums[i]);
|
||||||
|
}
|
||||||
|
// 从第 k+1 个元素开始,保持堆的长度为 k
|
||||||
|
for (int i = k; i < nums.length; i++) {
|
||||||
|
// 若当前元素大于堆顶元素,则将堆顶元素出堆、当前元素入堆
|
||||||
|
if (nums[i] > heap.peek()) {
|
||||||
|
heap.poll();
|
||||||
|
heap.offer(nums[i]);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return heap;
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
|
=== "C#"
|
||||||
|
|
||||||
|
```csharp title="top_k.cs"
|
||||||
|
/* 基于堆查找数组中最大的 k 个元素 */
|
||||||
|
PriorityQueue<int, int> TopKHeap(int[] nums, int k) {
|
||||||
|
// 初始化小顶堆
|
||||||
|
PriorityQueue<int, int> heap = new();
|
||||||
|
// 将数组的前 k 个元素入堆
|
||||||
|
for (int i = 0; i < k; i++) {
|
||||||
|
heap.Enqueue(nums[i], nums[i]);
|
||||||
|
}
|
||||||
|
// 从第 k+1 个元素开始,保持堆的长度为 k
|
||||||
|
for (int i = k; i < nums.Length; i++) {
|
||||||
|
// 若当前元素大于堆顶元素,则将堆顶元素出堆、当前元素入堆
|
||||||
|
if (nums[i] > heap.Peek()) {
|
||||||
|
heap.Dequeue();
|
||||||
|
heap.Enqueue(nums[i], nums[i]);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return heap;
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
|
=== "Go"
|
||||||
|
|
||||||
|
```go title="top_k.go"
|
||||||
|
/* 基于堆查找数组中最大的 k 个元素 */
|
||||||
|
func topKHeap(nums []int, k int) *minHeap {
|
||||||
|
// 初始化小顶堆
|
||||||
|
h := &minHeap{}
|
||||||
|
heap.Init(h)
|
||||||
|
// 将数组的前 k 个元素入堆
|
||||||
|
for i := 0; i < k; i++ {
|
||||||
|
heap.Push(h, nums[i])
|
||||||
|
}
|
||||||
|
// 从第 k+1 个元素开始,保持堆的长度为 k
|
||||||
|
for i := k; i < len(nums); i++ {
|
||||||
|
// 若当前元素大于堆顶元素,则将堆顶元素出堆、当前元素入堆
|
||||||
|
if nums[i] > h.Top().(int) {
|
||||||
|
heap.Pop(h)
|
||||||
|
heap.Push(h, nums[i])
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return h
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
|
=== "Swift"
|
||||||
|
|
||||||
|
```swift title="top_k.swift"
|
||||||
|
/* 基于堆查找数组中最大的 k 个元素 */
|
||||||
|
func topKHeap(nums: [Int], k: Int) -> [Int] {
|
||||||
|
// 初始化一个小顶堆,并将前 k 个元素建堆
|
||||||
|
var heap = Heap(nums.prefix(k))
|
||||||
|
// 从第 k+1 个元素开始,保持堆的长度为 k
|
||||||
|
for i in nums.indices.dropFirst(k) {
|
||||||
|
// 若当前元素大于堆顶元素,则将堆顶元素出堆、当前元素入堆
|
||||||
|
if nums[i] > heap.min()! {
|
||||||
|
_ = heap.removeMin()
|
||||||
|
heap.insert(nums[i])
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return heap.unordered
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
|
=== "JS"
|
||||||
|
|
||||||
|
```javascript title="top_k.js"
|
||||||
|
/* 元素入堆 */
|
||||||
|
function pushMinHeap(maxHeap, val) {
|
||||||
|
// 元素取反
|
||||||
|
maxHeap.push(-val);
|
||||||
|
}
|
||||||
|
|
||||||
|
/* 元素出堆 */
|
||||||
|
function popMinHeap(maxHeap) {
|
||||||
|
// 元素取反
|
||||||
|
return -maxHeap.pop();
|
||||||
|
}
|
||||||
|
|
||||||
|
/* 访问堆顶元素 */
|
||||||
|
function peekMinHeap(maxHeap) {
|
||||||
|
// 元素取反
|
||||||
|
return -maxHeap.peek();
|
||||||
|
}
|
||||||
|
|
||||||
|
/* 取出堆中元素 */
|
||||||
|
function getMinHeap(maxHeap) {
|
||||||
|
// 元素取反
|
||||||
|
return maxHeap.getMaxHeap().map((num) => -num);
|
||||||
|
}
|
||||||
|
|
||||||
|
/* 基于堆查找数组中最大的 k 个元素 */
|
||||||
|
function topKHeap(nums, k) {
|
||||||
|
// 初始化小顶堆
|
||||||
|
// 请注意:我们将堆中所有元素取反,从而用大顶堆来模拟小顶堆
|
||||||
|
const maxHeap = new MaxHeap([]);
|
||||||
|
// 将数组的前 k 个元素入堆
|
||||||
|
for (let i = 0; i < k; i++) {
|
||||||
|
pushMinHeap(maxHeap, nums[i]);
|
||||||
|
}
|
||||||
|
// 从第 k+1 个元素开始,保持堆的长度为 k
|
||||||
|
for (let i = k; i < nums.length; i++) {
|
||||||
|
// 若当前元素大于堆顶元素,则将堆顶元素出堆、当前元素入堆
|
||||||
|
if (nums[i] > peekMinHeap(maxHeap)) {
|
||||||
|
popMinHeap(maxHeap);
|
||||||
|
pushMinHeap(maxHeap, nums[i]);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
// 返回堆中元素
|
||||||
|
return getMinHeap(maxHeap);
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
|
=== "TS"
|
||||||
|
|
||||||
|
```typescript title="top_k.ts"
|
||||||
|
/* 元素入堆 */
|
||||||
|
function pushMinHeap(maxHeap: MaxHeap, val: number): void {
|
||||||
|
// 元素取反
|
||||||
|
maxHeap.push(-val);
|
||||||
|
}
|
||||||
|
|
||||||
|
/* 元素出堆 */
|
||||||
|
function popMinHeap(maxHeap: MaxHeap): number {
|
||||||
|
// 元素取反
|
||||||
|
return -maxHeap.pop();
|
||||||
|
}
|
||||||
|
|
||||||
|
/* 访问堆顶元素 */
|
||||||
|
function peekMinHeap(maxHeap: MaxHeap): number {
|
||||||
|
// 元素取反
|
||||||
|
return -maxHeap.peek();
|
||||||
|
}
|
||||||
|
|
||||||
|
/* 取出堆中元素 */
|
||||||
|
function getMinHeap(maxHeap: MaxHeap): number[] {
|
||||||
|
// 元素取反
|
||||||
|
return maxHeap.getMaxHeap().map((num: number) => -num);
|
||||||
|
}
|
||||||
|
|
||||||
|
/* 基于堆查找数组中最大的 k 个元素 */
|
||||||
|
function topKHeap(nums: number[], k: number): number[] {
|
||||||
|
// 初始化小顶堆
|
||||||
|
// 请注意:我们将堆中所有元素取反,从而用大顶堆来模拟小顶堆
|
||||||
|
const maxHeap = new MaxHeap([]);
|
||||||
|
// 将数组的前 k 个元素入堆
|
||||||
|
for (let i = 0; i < k; i++) {
|
||||||
|
pushMinHeap(maxHeap, nums[i]);
|
||||||
|
}
|
||||||
|
// 从第 k+1 个元素开始,保持堆的长度为 k
|
||||||
|
for (let i = k; i < nums.length; i++) {
|
||||||
|
// 若当前元素大于堆顶元素,则将堆顶元素出堆、当前元素入堆
|
||||||
|
if (nums[i] > peekMinHeap(maxHeap)) {
|
||||||
|
popMinHeap(maxHeap);
|
||||||
|
pushMinHeap(maxHeap, nums[i]);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
// 返回堆中元素
|
||||||
|
return getMinHeap(maxHeap);
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
|
=== "Dart"
|
||||||
|
|
||||||
|
```dart title="top_k.dart"
|
||||||
|
/* 基于堆查找数组中最大的 k 个元素 */
|
||||||
|
MinHeap topKHeap(List<int> nums, int k) {
|
||||||
|
// 初始化小顶堆,将数组的前 k 个元素入堆
|
||||||
|
MinHeap heap = MinHeap(nums.sublist(0, k));
|
||||||
|
// 从第 k+1 个元素开始,保持堆的长度为 k
|
||||||
|
for (int i = k; i < nums.length; i++) {
|
||||||
|
// 若当前元素大于堆顶元素,则将堆顶元素出堆、当前元素入堆
|
||||||
|
if (nums[i] > heap.peek()) {
|
||||||
|
heap.pop();
|
||||||
|
heap.push(nums[i]);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return heap;
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
|
=== "Rust"
|
||||||
|
|
||||||
|
```rust title="top_k.rs"
|
||||||
|
/* 基于堆查找数组中最大的 k 个元素 */
|
||||||
|
fn top_k_heap(nums: Vec<i32>, k: usize) -> BinaryHeap<Reverse<i32>> {
|
||||||
|
// BinaryHeap 是大顶堆,使用 Reverse 将元素取反,从而实现小顶堆
|
||||||
|
let mut heap = BinaryHeap::<Reverse<i32>>::new();
|
||||||
|
// 将数组的前 k 个元素入堆
|
||||||
|
for &num in nums.iter().take(k) {
|
||||||
|
heap.push(Reverse(num));
|
||||||
|
}
|
||||||
|
// 从第 k+1 个元素开始,保持堆的长度为 k
|
||||||
|
for &num in nums.iter().skip(k) {
|
||||||
|
// 若当前元素大于堆顶元素,则将堆顶元素出堆、当前元素入堆
|
||||||
|
if num > heap.peek().unwrap().0 {
|
||||||
|
heap.pop();
|
||||||
|
heap.push(Reverse(num));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
heap
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
|
=== "C"
|
||||||
|
|
||||||
|
```c title="top_k.c"
|
||||||
|
/* 元素入堆 */
|
||||||
|
void pushMinHeap(MaxHeap *maxHeap, int val) {
|
||||||
|
// 元素取反
|
||||||
|
push(maxHeap, -val);
|
||||||
|
}
|
||||||
|
|
||||||
|
/* 元素出堆 */
|
||||||
|
int popMinHeap(MaxHeap *maxHeap) {
|
||||||
|
// 元素取反
|
||||||
|
return -pop(maxHeap);
|
||||||
|
}
|
||||||
|
|
||||||
|
/* 访问堆顶元素 */
|
||||||
|
int peekMinHeap(MaxHeap *maxHeap) {
|
||||||
|
// 元素取反
|
||||||
|
return -peek(maxHeap);
|
||||||
|
}
|
||||||
|
|
||||||
|
/* 取出堆中元素 */
|
||||||
|
int *getMinHeap(MaxHeap *maxHeap) {
|
||||||
|
// 将堆中所有元素取反并存入 res 数组
|
||||||
|
int *res = (int *)malloc(maxHeap->size * sizeof(int));
|
||||||
|
for (int i = 0; i < maxHeap->size; i++) {
|
||||||
|
res[i] = -maxHeap->data[i];
|
||||||
|
}
|
||||||
|
return res;
|
||||||
|
}
|
||||||
|
|
||||||
|
/* 取出堆中元素 */
|
||||||
|
int *getMinHeap(MaxHeap *maxHeap) {
|
||||||
|
// 将堆中所有元素取反并存入 res 数组
|
||||||
|
int *res = (int *)malloc(maxHeap->size * sizeof(int));
|
||||||
|
for (int i = 0; i < maxHeap->size; i++) {
|
||||||
|
res[i] = -maxHeap->data[i];
|
||||||
|
}
|
||||||
|
return res;
|
||||||
|
}
|
||||||
|
|
||||||
|
// 基于堆查找数组中最大的 k 个元素的函数
|
||||||
|
int *topKHeap(int *nums, int sizeNums, int k) {
|
||||||
|
// 初始化小顶堆
|
||||||
|
// 请注意:我们将堆中所有元素取反,从而用大顶堆来模拟小顶堆
|
||||||
|
int *empty = (int *)malloc(0);
|
||||||
|
MaxHeap *maxHeap = newMaxHeap(empty, 0);
|
||||||
|
// 将数组的前 k 个元素入堆
|
||||||
|
for (int i = 0; i < k; i++) {
|
||||||
|
pushMinHeap(maxHeap, nums[i]);
|
||||||
|
}
|
||||||
|
// 从第 k+1 个元素开始,保持堆的长度为 k
|
||||||
|
for (int i = k; i < sizeNums; i++) {
|
||||||
|
// 若当前元素大于堆顶元素,则将堆顶元素出堆、当前元素入堆
|
||||||
|
if (nums[i] > peekMinHeap(maxHeap)) {
|
||||||
|
popMinHeap(maxHeap);
|
||||||
|
pushMinHeap(maxHeap, nums[i]);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
int *res = getMinHeap(maxHeap);
|
||||||
|
// 释放内存
|
||||||
|
delMaxHeap(maxHeap);
|
||||||
|
return res;
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
|
=== "Kotlin"
|
||||||
|
|
||||||
|
```kotlin title="top_k.kt"
|
||||||
|
/* 基于堆查找数组中最大的 k 个元素 */
|
||||||
|
fun topKHeap(nums: IntArray, k: Int): Queue<Int> {
|
||||||
|
// 初始化小顶堆
|
||||||
|
val heap = PriorityQueue<Int>()
|
||||||
|
// 将数组的前 k 个元素入堆
|
||||||
|
for (i in 0..<k) {
|
||||||
|
heap.offer(nums[i])
|
||||||
|
}
|
||||||
|
// 从第 k+1 个元素开始,保持堆的长度为 k
|
||||||
|
for (i in k..<nums.size) {
|
||||||
|
// 若当前元素大于堆顶元素,则将堆顶元素出堆、当前元素入堆
|
||||||
|
if (nums[i] > heap.peek()) {
|
||||||
|
heap.poll()
|
||||||
|
heap.offer(nums[i])
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return heap
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
|
=== "Ruby"
|
||||||
|
|
||||||
|
```ruby title="top_k.rb"
|
||||||
|
[class]{}-[func]{top_k_heap}
|
||||||
|
```
|
||||||
|
|
||||||
|
=== "Zig"
|
||||||
|
|
||||||
|
```zig title="top_k.zig"
|
||||||
|
[class]{}-[func]{topKHeap}
|
||||||
|
```
|
||||||
|
|
||||||
|
??? pythontutor "Code Visualization"
|
||||||
|
|
||||||
|
<div style="height: 549px; width: 100%;"><iframe class="pythontutor-iframe" src="https://pythontutor.com/iframe-embed.html#code=import%20heapq%0A%0Adef%20top_k_heap%28nums%3A%20list%5Bint%5D,%20k%3A%20int%29%20-%3E%20list%5Bint%5D%3A%0A%20%20%20%20%22%22%22%E5%9F%BA%E4%BA%8E%E5%A0%86%E6%9F%A5%E6%89%BE%E6%95%B0%E7%BB%84%E4%B8%AD%E6%9C%80%E5%A4%A7%E7%9A%84%20k%20%E4%B8%AA%E5%85%83%E7%B4%A0%22%22%22%0A%20%20%20%20%23%20%E5%88%9D%E5%A7%8B%E5%8C%96%E5%B0%8F%E9%A1%B6%E5%A0%86%0A%20%20%20%20heap%20%3D%20%5B%5D%0A%20%20%20%20%23%20%E5%B0%86%E6%95%B0%E7%BB%84%E7%9A%84%E5%89%8D%20k%20%E4%B8%AA%E5%85%83%E7%B4%A0%E5%85%A5%E5%A0%86%0A%20%20%20%20for%20i%20in%20range%28k%29%3A%0A%20%20%20%20%20%20%20%20heapq.heappush%28heap,%20nums%5Bi%5D%29%0A%20%20%20%20%23%20%E4%BB%8E%E7%AC%AC%20k%2B1%20%E4%B8%AA%E5%85%83%E7%B4%A0%E5%BC%80%E5%A7%8B%EF%BC%8C%E4%BF%9D%E6%8C%81%E5%A0%86%E7%9A%84%E9%95%BF%E5%BA%A6%E4%B8%BA%20k%0A%20%20%20%20for%20i%20in%20range%28k,%20len%28nums%29%29%3A%0A%20%20%20%20%20%20%20%20%23%20%E8%8B%A5%E5%BD%93%E5%89%8D%E5%85%83%E7%B4%A0%E5%A4%A7%E4%BA%8E%E5%A0%86%E9%A1%B6%E5%85%83%E7%B4%A0%EF%BC%8C%E5%88%99%E5%B0%86%E5%A0%86%E9%A1%B6%E5%85%83%E7%B4%A0%E5%87%BA%E5%A0%86%E3%80%81%E5%BD%93%E5%89%8D%E5%85%83%E7%B4%A0%E5%85%A5%E5%A0%86%0A%20%20%20%20%20%20%20%20if%20nums%5Bi%5D%20%3E%20heap%5B0%5D%3A%0A%20%20%20%20%20%20%20%20%20%20%20%20heapq.heappop%28heap%29%0A%20%20%20%20%20%20%20%20%20%20%20%20heapq.heappush%28heap,%20nums%5Bi%5D%29%0A%20%20%20%20return%20heap%0A%0A%22%22%22Driver%20Code%22%22%22%0Aif%20__name__%20%3D%3D%20%22__main__%22%3A%0A%20%20%20%20nums%20%3D%20%5B1,%207,%206,%203,%202%5D%0A%20%20%20%20k%20%3D%203%0A%0A%20%20%20%20res%20%3D%20top_k_heap%28nums,%20k%29&codeDivHeight=472&codeDivWidth=350&cumulative=false&curInstr=6&heapPrimitives=nevernest&origin=opt-frontend.js&py=311&rawInputLstJSON=%5B%5D&textReferences=false"> </iframe></div>
|
||||||
|
<div style="margin-top: 5px;"><a href="https://pythontutor.com/iframe-embed.html#code=import%20heapq%0A%0Adef%20top_k_heap%28nums%3A%20list%5Bint%5D,%20k%3A%20int%29%20-%3E%20list%5Bint%5D%3A%0A%20%20%20%20%22%22%22%E5%9F%BA%E4%BA%8E%E5%A0%86%E6%9F%A5%E6%89%BE%E6%95%B0%E7%BB%84%E4%B8%AD%E6%9C%80%E5%A4%A7%E7%9A%84%20k%20%E4%B8%AA%E5%85%83%E7%B4%A0%22%22%22%0A%20%20%20%20%23%20%E5%88%9D%E5%A7%8B%E5%8C%96%E5%B0%8F%E9%A1%B6%E5%A0%86%0A%20%20%20%20heap%20%3D%20%5B%5D%0A%20%20%20%20%23%20%E5%B0%86%E6%95%B0%E7%BB%84%E7%9A%84%E5%89%8D%20k%20%E4%B8%AA%E5%85%83%E7%B4%A0%E5%85%A5%E5%A0%86%0A%20%20%20%20for%20i%20in%20range%28k%29%3A%0A%20%20%20%20%20%20%20%20heapq.heappush%28heap,%20nums%5Bi%5D%29%0A%20%20%20%20%23%20%E4%BB%8E%E7%AC%AC%20k%2B1%20%E4%B8%AA%E5%85%83%E7%B4%A0%E5%BC%80%E5%A7%8B%EF%BC%8C%E4%BF%9D%E6%8C%81%E5%A0%86%E7%9A%84%E9%95%BF%E5%BA%A6%E4%B8%BA%20k%0A%20%20%20%20for%20i%20in%20range%28k,%20len%28nums%29%29%3A%0A%20%20%20%20%20%20%20%20%23%20%E8%8B%A5%E5%BD%93%E5%89%8D%E5%85%83%E7%B4%A0%E5%A4%A7%E4%BA%8E%E5%A0%86%E9%A1%B6%E5%85%83%E7%B4%A0%EF%BC%8C%E5%88%99%E5%B0%86%E5%A0%86%E9%A1%B6%E5%85%83%E7%B4%A0%E5%87%BA%E5%A0%86%E3%80%81%E5%BD%93%E5%89%8D%E5%85%83%E7%B4%A0%E5%85%A5%E5%A0%86%0A%20%20%20%20%20%20%20%20if%20nums%5Bi%5D%20%3E%20heap%5B0%5D%3A%0A%20%20%20%20%20%20%20%20%20%20%20%20heapq.heappop%28heap%29%0A%20%20%20%20%20%20%20%20%20%20%20%20heapq.heappush%28heap,%20nums%5Bi%5D%29%0A%20%20%20%20return%20heap%0A%0A%22%22%22Driver%20Code%22%22%22%0Aif%20__name__%20%3D%3D%20%22__main__%22%3A%0A%20%20%20%20nums%20%3D%20%5B1,%207,%206,%203,%202%5D%0A%20%20%20%20k%20%3D%203%0A%0A%20%20%20%20res%20%3D%20top_k_heap%28nums,%20k%29&codeDivHeight=800&codeDivWidth=600&cumulative=false&curInstr=6&heapPrimitives=nevernest&origin=opt-frontend.js&py=311&rawInputLstJSON=%5B%5D&textReferences=false" target="_blank" rel="noopener noreferrer">Full Screen ></a></div>
|
||||||
|
|
||||||
|
A total of $n$ rounds of heap insertions and deletions are performed, with the maximum heap size being $k$, hence the time complexity is $O(n \log k)$. This method is very efficient; when $k$ is small, the time complexity tends towards $O(n)$; when $k$ is large, the time complexity will not exceed $O(n \log n)$.
|
||||||
|
|
||||||
|
Additionally, this method is suitable for scenarios with dynamic data streams. By continuously adding data, we can maintain the elements within the heap, thereby achieving dynamic updates of the largest $k$ elements.
|
File diff suppressed because one or more lines are too long
File diff suppressed because it is too large
Load Diff
File diff suppressed because one or more lines are too long
@ -0,0 +1,686 @@
|
|||||||
|
---
|
||||||
|
comments: true
|
||||||
|
---
|
||||||
|
|
||||||
|
# 7.1 Binary tree
|
||||||
|
|
||||||
|
A "binary tree" is a non-linear data structure that represents the ancestral and descendent relationships, embodying the "divide and conquer" logic. 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.
|
||||||
|
|
||||||
|
=== "Python"
|
||||||
|
|
||||||
|
```python title=""
|
||||||
|
class TreeNode:
|
||||||
|
"""Binary tree node"""
|
||||||
|
def __init__(self, val: int):
|
||||||
|
self.val: int = val # Node value
|
||||||
|
self.left: TreeNode | None = None # Reference to left child node
|
||||||
|
self.right: TreeNode | None = None # Reference to right child node
|
||||||
|
```
|
||||||
|
|
||||||
|
=== "C++"
|
||||||
|
|
||||||
|
```cpp title=""
|
||||||
|
/* Binary tree node */
|
||||||
|
struct TreeNode {
|
||||||
|
int val; // Node value
|
||||||
|
TreeNode *left; // Pointer to left child node
|
||||||
|
TreeNode *right; // Pointer to right child node
|
||||||
|
TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}
|
||||||
|
};
|
||||||
|
```
|
||||||
|
|
||||||
|
=== "Java"
|
||||||
|
|
||||||
|
```java title=""
|
||||||
|
/* Binary tree node */
|
||||||
|
class TreeNode {
|
||||||
|
int val; // Node value
|
||||||
|
TreeNode left; // Reference to left child node
|
||||||
|
TreeNode right; // Reference to right child node
|
||||||
|
TreeNode(int x) { val = x; }
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
|
=== "C#"
|
||||||
|
|
||||||
|
```csharp title=""
|
||||||
|
/* Binary tree node */
|
||||||
|
class TreeNode(int? x) {
|
||||||
|
public int? val = x; // Node value
|
||||||
|
public TreeNode? left; // Reference to left child node
|
||||||
|
public TreeNode? right; // Reference to right child node
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
|
=== "Go"
|
||||||
|
|
||||||
|
```go title=""
|
||||||
|
/* Binary tree node */
|
||||||
|
type TreeNode struct {
|
||||||
|
Val int
|
||||||
|
Left *TreeNode
|
||||||
|
Right *TreeNode
|
||||||
|
}
|
||||||
|
/* 构造方法 */
|
||||||
|
func NewTreeNode(v int) *TreeNode {
|
||||||
|
return &TreeNode{
|
||||||
|
Left: nil, // Pointer to left child node
|
||||||
|
Right: nil, // Pointer to right child node
|
||||||
|
Val: v, // Node value
|
||||||
|
}
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
|
=== "Swift"
|
||||||
|
|
||||||
|
```swift title=""
|
||||||
|
/* Binary tree node */
|
||||||
|
class TreeNode {
|
||||||
|
var val: Int // Node value
|
||||||
|
var left: TreeNode? // Reference to left child node
|
||||||
|
var right: TreeNode? // Reference to right child node
|
||||||
|
|
||||||
|
init(x: Int) {
|
||||||
|
val = x
|
||||||
|
}
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
|
=== "JS"
|
||||||
|
|
||||||
|
```javascript title=""
|
||||||
|
/* Binary tree node */
|
||||||
|
class TreeNode {
|
||||||
|
val; // Node value
|
||||||
|
left; // Pointer to left child node
|
||||||
|
right; // Pointer to right child node
|
||||||
|
constructor(val, left, right) {
|
||||||
|
this.val = val === undefined ? 0 : val;
|
||||||
|
this.left = left === undefined ? null : left;
|
||||||
|
this.right = right === undefined ? null : right;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
|
=== "TS"
|
||||||
|
|
||||||
|
```typescript title=""
|
||||||
|
/* Binary tree node */
|
||||||
|
class TreeNode {
|
||||||
|
val: number;
|
||||||
|
left: TreeNode | null;
|
||||||
|
right: TreeNode | null;
|
||||||
|
|
||||||
|
constructor(val?: number, left?: TreeNode | null, right?: TreeNode | null) {
|
||||||
|
this.val = val === undefined ? 0 : val; // Node value
|
||||||
|
this.left = left === undefined ? null : left; // Reference to left child node
|
||||||
|
this.right = right === undefined ? null : right; // Reference to right child node
|
||||||
|
}
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
|
=== "Dart"
|
||||||
|
|
||||||
|
```dart title=""
|
||||||
|
/* Binary tree node */
|
||||||
|
class TreeNode {
|
||||||
|
int val; // Node value
|
||||||
|
TreeNode? left; // Reference to left child node
|
||||||
|
TreeNode? right; // Reference to right child node
|
||||||
|
TreeNode(this.val, [this.left, this.right]);
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
|
=== "Rust"
|
||||||
|
|
||||||
|
```rust title=""
|
||||||
|
use std::rc::Rc;
|
||||||
|
use std::cell::RefCell;
|
||||||
|
|
||||||
|
/* Binary tree node */
|
||||||
|
struct TreeNode {
|
||||||
|
val: i32, // Node value
|
||||||
|
left: Option<Rc<RefCell<TreeNode>>>, // Reference to left child node
|
||||||
|
right: Option<Rc<RefCell<TreeNode>>>, // Reference to right child node
|
||||||
|
}
|
||||||
|
|
||||||
|
impl TreeNode {
|
||||||
|
/* 构造方法 */
|
||||||
|
fn new(val: i32) -> Rc<RefCell<Self>> {
|
||||||
|
Rc::new(RefCell::new(Self {
|
||||||
|
val,
|
||||||
|
left: None,
|
||||||
|
right: None
|
||||||
|
}))
|
||||||
|
}
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
|
=== "C"
|
||||||
|
|
||||||
|
```c title=""
|
||||||
|
/* Binary tree node */
|
||||||
|
typedef struct TreeNode {
|
||||||
|
int val; // Node value
|
||||||
|
int height; // 节点高度
|
||||||
|
struct TreeNode *left; // Pointer to left child node
|
||||||
|
struct TreeNode *right; // Pointer to right child node
|
||||||
|
} TreeNode;
|
||||||
|
|
||||||
|
/* 构造函数 */
|
||||||
|
TreeNode *newTreeNode(int val) {
|
||||||
|
TreeNode *node;
|
||||||
|
|
||||||
|
node = (TreeNode *)malloc(sizeof(TreeNode));
|
||||||
|
node->val = val;
|
||||||
|
node->height = 0;
|
||||||
|
node->left = NULL;
|
||||||
|
node->right = NULL;
|
||||||
|
return node;
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
|
=== "Kotlin"
|
||||||
|
|
||||||
|
```kotlin title=""
|
||||||
|
/* Binary tree node */
|
||||||
|
class TreeNode(val _val: Int) { // Node value
|
||||||
|
val left: TreeNode? = null // Reference to left child node
|
||||||
|
val right: TreeNode? = null // Reference to right child node
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
|
=== "Ruby"
|
||||||
|
|
||||||
|
```ruby title=""
|
||||||
|
|
||||||
|
```
|
||||||
|
|
||||||
|
=== "Zig"
|
||||||
|
|
||||||
|
```zig title=""
|
||||||
|
|
||||||
|
```
|
||||||
|
|
||||||
|
Each node has two references (pointers), pointing to the "left-child node" and "right-child node," respectively. This node is called the "parent node" 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 "left subtree" of this node. Similarly, the "right subtree" can be defined.
|
||||||
|
|
||||||
|
**In a binary tree, except for leaf nodes, all other nodes contain child nodes and non-empty subtrees.** As shown in the 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."
|
||||||
|
|
||||||
|
![Parent Node, child Node, subtree](binary_tree.assets/binary_tree_definition.png){ class="animation-figure" }
|
||||||
|
|
||||||
|
<p align="center"> Figure 7-1 Parent Node, child Node, subtree </p>
|
||||||
|
|
||||||
|
## 7.1.1 Common terminology of binary trees
|
||||||
|
|
||||||
|
The commonly used terminology of binary trees is shown in the following figure.
|
||||||
|
|
||||||
|
- "Root node": The node at the top level of the binary tree, which has no parent node.
|
||||||
|
- "Leaf node": A node with no children, both of its pointers point to `None`.
|
||||||
|
- "Edge": The line segment connecting two nodes, i.e., node reference (pointer).
|
||||||
|
- The "level" of a node: Incrementing from top to bottom, with the root node's level being 1.
|
||||||
|
- The "degree" of a node: The number of a node's children. In a binary tree, the degree can be 0, 1, or 2.
|
||||||
|
- The "height" of a binary tree: The number of edges passed from the root node to the farthest leaf node.
|
||||||
|
- The "depth" of a node: The number of edges passed from the root node to the node.
|
||||||
|
- The "height" 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" }
|
||||||
|
|
||||||
|
<p align="center"> Figure 7-2 Common Terminology of Binary Trees </p>
|
||||||
|
|
||||||
|
!!! tip
|
||||||
|
|
||||||
|
Please note that we usually define "height" and "depth" as "the number of edges passed," but some problems or textbooks may define them as "the number of nodes passed." 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, initialize nodes first, then construct references (pointers).
|
||||||
|
|
||||||
|
=== "Python"
|
||||||
|
|
||||||
|
```python title="binary_tree.py"
|
||||||
|
# Initializing a binary tree
|
||||||
|
# Initializing nodes
|
||||||
|
n1 = TreeNode(val=1)
|
||||||
|
n2 = TreeNode(val=2)
|
||||||
|
n3 = TreeNode(val=3)
|
||||||
|
n4 = TreeNode(val=4)
|
||||||
|
n5 = TreeNode(val=5)
|
||||||
|
# Linking references (pointers) between nodes
|
||||||
|
n1.left = n2
|
||||||
|
n1.right = n3
|
||||||
|
n2.left = n4
|
||||||
|
n2.right = n5
|
||||||
|
```
|
||||||
|
|
||||||
|
=== "C++"
|
||||||
|
|
||||||
|
```cpp title="binary_tree.cpp"
|
||||||
|
/* Initializing a binary tree */
|
||||||
|
// Initializing nodes
|
||||||
|
TreeNode* n1 = new TreeNode(1);
|
||||||
|
TreeNode* n2 = new TreeNode(2);
|
||||||
|
TreeNode* n3 = new TreeNode(3);
|
||||||
|
TreeNode* n4 = new TreeNode(4);
|
||||||
|
TreeNode* n5 = new TreeNode(5);
|
||||||
|
// Linking references (pointers) between nodes
|
||||||
|
n1->left = n2;
|
||||||
|
n1->right = n3;
|
||||||
|
n2->left = n4;
|
||||||
|
n2->right = n5;
|
||||||
|
```
|
||||||
|
|
||||||
|
=== "Java"
|
||||||
|
|
||||||
|
```java title="binary_tree.java"
|
||||||
|
// Initializing nodes
|
||||||
|
TreeNode n1 = new TreeNode(1);
|
||||||
|
TreeNode n2 = new TreeNode(2);
|
||||||
|
TreeNode n3 = new TreeNode(3);
|
||||||
|
TreeNode n4 = new TreeNode(4);
|
||||||
|
TreeNode n5 = new TreeNode(5);
|
||||||
|
// Linking references (pointers) between nodes
|
||||||
|
n1.left = n2;
|
||||||
|
n1.right = n3;
|
||||||
|
n2.left = n4;
|
||||||
|
n2.right = n5;
|
||||||
|
```
|
||||||
|
|
||||||
|
=== "C#"
|
||||||
|
|
||||||
|
```csharp title="binary_tree.cs"
|
||||||
|
/* Initializing a binary tree */
|
||||||
|
// Initializing nodes
|
||||||
|
TreeNode n1 = new(1);
|
||||||
|
TreeNode n2 = new(2);
|
||||||
|
TreeNode n3 = new(3);
|
||||||
|
TreeNode n4 = new(4);
|
||||||
|
TreeNode n5 = new(5);
|
||||||
|
// Linking references (pointers) between nodes
|
||||||
|
n1.left = n2;
|
||||||
|
n1.right = n3;
|
||||||
|
n2.left = n4;
|
||||||
|
n2.right = n5;
|
||||||
|
```
|
||||||
|
|
||||||
|
=== "Go"
|
||||||
|
|
||||||
|
```go title="binary_tree.go"
|
||||||
|
/* Initializing a binary tree */
|
||||||
|
// Initializing nodes
|
||||||
|
n1 := NewTreeNode(1)
|
||||||
|
n2 := NewTreeNode(2)
|
||||||
|
n3 := NewTreeNode(3)
|
||||||
|
n4 := NewTreeNode(4)
|
||||||
|
n5 := NewTreeNode(5)
|
||||||
|
// Linking references (pointers) between nodes
|
||||||
|
n1.Left = n2
|
||||||
|
n1.Right = n3
|
||||||
|
n2.Left = n4
|
||||||
|
n2.Right = n5
|
||||||
|
```
|
||||||
|
|
||||||
|
=== "Swift"
|
||||||
|
|
||||||
|
```swift title="binary_tree.swift"
|
||||||
|
// Initializing nodes
|
||||||
|
let n1 = TreeNode(x: 1)
|
||||||
|
let n2 = TreeNode(x: 2)
|
||||||
|
let n3 = TreeNode(x: 3)
|
||||||
|
let n4 = TreeNode(x: 4)
|
||||||
|
let n5 = TreeNode(x: 5)
|
||||||
|
// Linking references (pointers) between nodes
|
||||||
|
n1.left = n2
|
||||||
|
n1.right = n3
|
||||||
|
n2.left = n4
|
||||||
|
n2.right = n5
|
||||||
|
```
|
||||||
|
|
||||||
|
=== "JS"
|
||||||
|
|
||||||
|
```javascript title="binary_tree.js"
|
||||||
|
/* Initializing a binary tree */
|
||||||
|
// Initializing nodes
|
||||||
|
let n1 = new TreeNode(1),
|
||||||
|
n2 = new TreeNode(2),
|
||||||
|
n3 = new TreeNode(3),
|
||||||
|
n4 = new TreeNode(4),
|
||||||
|
n5 = new TreeNode(5);
|
||||||
|
// Linking references (pointers) between nodes
|
||||||
|
n1.left = n2;
|
||||||
|
n1.right = n3;
|
||||||
|
n2.left = n4;
|
||||||
|
n2.right = n5;
|
||||||
|
```
|
||||||
|
|
||||||
|
=== "TS"
|
||||||
|
|
||||||
|
```typescript title="binary_tree.ts"
|
||||||
|
/* Initializing a binary tree */
|
||||||
|
// Initializing nodes
|
||||||
|
let n1 = new TreeNode(1),
|
||||||
|
n2 = new TreeNode(2),
|
||||||
|
n3 = new TreeNode(3),
|
||||||
|
n4 = new TreeNode(4),
|
||||||
|
n5 = new TreeNode(5);
|
||||||
|
// Linking references (pointers) between nodes
|
||||||
|
n1.left = n2;
|
||||||
|
n1.right = n3;
|
||||||
|
n2.left = n4;
|
||||||
|
n2.right = n5;
|
||||||
|
```
|
||||||
|
|
||||||
|
=== "Dart"
|
||||||
|
|
||||||
|
```dart title="binary_tree.dart"
|
||||||
|
/* Initializing a binary tree */
|
||||||
|
// Initializing nodes
|
||||||
|
TreeNode n1 = new TreeNode(1);
|
||||||
|
TreeNode n2 = new TreeNode(2);
|
||||||
|
TreeNode n3 = new TreeNode(3);
|
||||||
|
TreeNode n4 = new TreeNode(4);
|
||||||
|
TreeNode n5 = new TreeNode(5);
|
||||||
|
// Linking references (pointers) between nodes
|
||||||
|
n1.left = n2;
|
||||||
|
n1.right = n3;
|
||||||
|
n2.left = n4;
|
||||||
|
n2.right = n5;
|
||||||
|
```
|
||||||
|
|
||||||
|
=== "Rust"
|
||||||
|
|
||||||
|
```rust title="binary_tree.rs"
|
||||||
|
// Initializing nodes
|
||||||
|
let n1 = TreeNode::new(1);
|
||||||
|
let n2 = TreeNode::new(2);
|
||||||
|
let n3 = TreeNode::new(3);
|
||||||
|
let n4 = TreeNode::new(4);
|
||||||
|
let n5 = TreeNode::new(5);
|
||||||
|
// Linking references (pointers) between nodes
|
||||||
|
n1.borrow_mut().left = Some(n2.clone());
|
||||||
|
n1.borrow_mut().right = Some(n3);
|
||||||
|
n2.borrow_mut().left = Some(n4);
|
||||||
|
n2.borrow_mut().right = Some(n5);
|
||||||
|
```
|
||||||
|
|
||||||
|
=== "C"
|
||||||
|
|
||||||
|
```c title="binary_tree.c"
|
||||||
|
/* Initializing a binary tree */
|
||||||
|
// Initializing nodes
|
||||||
|
TreeNode *n1 = newTreeNode(1);
|
||||||
|
TreeNode *n2 = newTreeNode(2);
|
||||||
|
TreeNode *n3 = newTreeNode(3);
|
||||||
|
TreeNode *n4 = newTreeNode(4);
|
||||||
|
TreeNode *n5 = newTreeNode(5);
|
||||||
|
// Linking references (pointers) between nodes
|
||||||
|
n1->left = n2;
|
||||||
|
n1->right = n3;
|
||||||
|
n2->left = n4;
|
||||||
|
n2->right = n5;
|
||||||
|
```
|
||||||
|
|
||||||
|
=== "Kotlin"
|
||||||
|
|
||||||
|
```kotlin title="binary_tree.kt"
|
||||||
|
// Initializing nodes
|
||||||
|
val n1 = TreeNode(1)
|
||||||
|
val n2 = TreeNode(2)
|
||||||
|
val n3 = TreeNode(3)
|
||||||
|
val n4 = TreeNode(4)
|
||||||
|
val n5 = TreeNode(5)
|
||||||
|
// Linking references (pointers) between nodes
|
||||||
|
n1.left = n2
|
||||||
|
n1.right = n3
|
||||||
|
n2.left = n4
|
||||||
|
n2.right = n5
|
||||||
|
```
|
||||||
|
|
||||||
|
=== "Ruby"
|
||||||
|
|
||||||
|
```ruby title="binary_tree.rb"
|
||||||
|
|
||||||
|
```
|
||||||
|
|
||||||
|
=== "Zig"
|
||||||
|
|
||||||
|
```zig title="binary_tree.zig"
|
||||||
|
|
||||||
|
```
|
||||||
|
|
||||||
|
??? pythontutor "Code visualization"
|
||||||
|
|
||||||
|
https://pythontutor.com/render.html#code=class%20TreeNode%3A%0A%20%20%20%20%22%22%22%E4%BA%8C%E5%8F%89%E6%A0%91%E8%8A%82%E7%82%B9%E7%B1%BB%22%22%22%0A%20%20%20%20def%20__init__%28self,%20val%3A%20int%29%3A%0A%20%20%20%20%20%20%20%20self.val%3A%20int%20%3D%20val%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%23%20%E8%8A%82%E7%82%B9%E5%80%BC%0A%20%20%20%20%20%20%20%20self.left%3A%20TreeNode%20%7C%20None%20%3D%20None%20%20%23%20%E5%B7%A6%E5%AD%90%E8%8A%82%E7%82%B9%E5%BC%95%E7%94%A8%0A%20%20%20%20%20%20%20%20self.right%3A%20TreeNode%20%7C%20None%20%3D%20None%20%23%20%E5%8F%B3%E5%AD%90%E8%8A%82%E7%82%B9%E5%BC%95%E7%94%A8%0A%0A%22%22%22Driver%20Code%22%22%22%0Aif%20__name__%20%3D%3D%20%22__main__%22%3A%0A%20%20%20%20%23%20%E5%88%9D%E5%A7%8B%E5%8C%96%E4%BA%8C%E5%8F%89%E6%A0%91%0A%20%20%20%20%23%20%E5%88%9D%E5%A7%8B%E5%8C%96%E8%8A%82%E7%82%B9%0A%20%20%20%20n1%20%3D%20TreeNode%28val%3D1%29%0A%20%20%20%20n2%20%3D%20TreeNode%28val%3D2%29%0A%20%20%20%20n3%20%3D%20TreeNode%28val%3D3%29%0A%20%20%20%20n4%20%3D%20TreeNode%28val%3D4%29%0A%20%20%20%20n5%20%3D%20TreeNode%28val%3D5%29%0A%20%20%20%20%23%20%E6%9E%84%E5%BB%BA%E8%8A%82%E7%82%B9%E4%B9%8B%E9%97%B4%E7%9A%84%E5%BC%95%E7%94%A8%EF%BC%88%E6%8C%87%E9%92%88%EF%BC%89%0A%20%20%20%20n1.left%20%3D%20n2%0A%20%20%20%20n1.right%20%3D%20n3%0A%20%20%20%20n2.left%20%3D%20n4%0A%20%20%20%20n2.right%20%3D%20n5&cumulative=false&curInstr=3&heapPrimitives=nevernest&mode=display&origin=opt-frontend.js&py=311&rawInputLstJSON=%5B%5D&textReferences=false
|
||||||
|
|
||||||
|
### 2. Inserting and removing nodes
|
||||||
|
|
||||||
|
Similar to a linked list, inserting and removing nodes in a binary tree can be achieved by modifying pointers. The Figure 7-3 provides an example.
|
||||||
|
|
||||||
|
![Inserting and removing nodes in a binary tree](binary_tree.assets/binary_tree_add_remove.png){ class="animation-figure" }
|
||||||
|
|
||||||
|
<p align="center"> Figure 7-3 Inserting and removing nodes in a binary tree </p>
|
||||||
|
|
||||||
|
=== "Python"
|
||||||
|
|
||||||
|
```python title="binary_tree.py"
|
||||||
|
# Inserting and removing nodes
|
||||||
|
p = TreeNode(0)
|
||||||
|
# Inserting node P between n1 -> n2
|
||||||
|
n1.left = p
|
||||||
|
p.left = n2
|
||||||
|
# Removing node P
|
||||||
|
n1.left = n2
|
||||||
|
```
|
||||||
|
|
||||||
|
=== "C++"
|
||||||
|
|
||||||
|
```cpp title="binary_tree.cpp"
|
||||||
|
/* Inserting and removing nodes */
|
||||||
|
TreeNode* P = new TreeNode(0);
|
||||||
|
// Inserting node P between n1 and n2
|
||||||
|
n1->left = P;
|
||||||
|
P->left = n2;
|
||||||
|
// Removing node P
|
||||||
|
n1->left = n2;
|
||||||
|
```
|
||||||
|
|
||||||
|
=== "Java"
|
||||||
|
|
||||||
|
```java title="binary_tree.java"
|
||||||
|
TreeNode P = new TreeNode(0);
|
||||||
|
// Inserting node P between n1 and n2
|
||||||
|
n1.left = P;
|
||||||
|
P.left = n2;
|
||||||
|
// Removing node P
|
||||||
|
n1.left = n2;
|
||||||
|
```
|
||||||
|
|
||||||
|
=== "C#"
|
||||||
|
|
||||||
|
```csharp title="binary_tree.cs"
|
||||||
|
/* Inserting and removing nodes */
|
||||||
|
TreeNode P = new(0);
|
||||||
|
// Inserting node P between n1 and n2
|
||||||
|
n1.left = P;
|
||||||
|
P.left = n2;
|
||||||
|
// Removing node P
|
||||||
|
n1.left = n2;
|
||||||
|
```
|
||||||
|
|
||||||
|
=== "Go"
|
||||||
|
|
||||||
|
```go title="binary_tree.go"
|
||||||
|
/* Inserting and removing nodes */
|
||||||
|
// Inserting node P between n1 and n2
|
||||||
|
p := NewTreeNode(0)
|
||||||
|
n1.Left = p
|
||||||
|
p.Left = n2
|
||||||
|
// Removing node P
|
||||||
|
n1.Left = n2
|
||||||
|
```
|
||||||
|
|
||||||
|
=== "Swift"
|
||||||
|
|
||||||
|
```swift title="binary_tree.swift"
|
||||||
|
let P = TreeNode(x: 0)
|
||||||
|
// Inserting node P between n1 and n2
|
||||||
|
n1.left = P
|
||||||
|
P.left = n2
|
||||||
|
// Removing node P
|
||||||
|
n1.left = n2
|
||||||
|
```
|
||||||
|
|
||||||
|
=== "JS"
|
||||||
|
|
||||||
|
```javascript title="binary_tree.js"
|
||||||
|
/* Inserting and removing nodes */
|
||||||
|
let P = new TreeNode(0);
|
||||||
|
// Inserting node P between n1 and n2
|
||||||
|
n1.left = P;
|
||||||
|
P.left = n2;
|
||||||
|
// Removing node P
|
||||||
|
n1.left = n2;
|
||||||
|
```
|
||||||
|
|
||||||
|
=== "TS"
|
||||||
|
|
||||||
|
```typescript title="binary_tree.ts"
|
||||||
|
/* Inserting and removing nodes */
|
||||||
|
const P = new TreeNode(0);
|
||||||
|
// Inserting node P between n1 and n2
|
||||||
|
n1.left = P;
|
||||||
|
P.left = n2;
|
||||||
|
// Removing node P
|
||||||
|
n1.left = n2;
|
||||||
|
```
|
||||||
|
|
||||||
|
=== "Dart"
|
||||||
|
|
||||||
|
```dart title="binary_tree.dart"
|
||||||
|
/* Inserting and removing nodes */
|
||||||
|
TreeNode P = new TreeNode(0);
|
||||||
|
// Inserting node P between n1 and n2
|
||||||
|
n1.left = P;
|
||||||
|
P.left = n2;
|
||||||
|
// Removing node P
|
||||||
|
n1.left = n2;
|
||||||
|
```
|
||||||
|
|
||||||
|
=== "Rust"
|
||||||
|
|
||||||
|
```rust title="binary_tree.rs"
|
||||||
|
let p = TreeNode::new(0);
|
||||||
|
// Inserting node P between n1 and n2
|
||||||
|
n1.borrow_mut().left = Some(p.clone());
|
||||||
|
p.borrow_mut().left = Some(n2.clone());
|
||||||
|
// Removing node P
|
||||||
|
n1.borrow_mut().left = Some(n2);
|
||||||
|
```
|
||||||
|
|
||||||
|
=== "C"
|
||||||
|
|
||||||
|
```c title="binary_tree.c"
|
||||||
|
/* Inserting and removing nodes */
|
||||||
|
TreeNode *P = newTreeNode(0);
|
||||||
|
// Inserting node P between n1 and n2
|
||||||
|
n1->left = P;
|
||||||
|
P->left = n2;
|
||||||
|
// Removing node P
|
||||||
|
n1->left = n2;
|
||||||
|
```
|
||||||
|
|
||||||
|
=== "Kotlin"
|
||||||
|
|
||||||
|
```kotlin title="binary_tree.kt"
|
||||||
|
val P = TreeNode(0)
|
||||||
|
// Inserting node P between n1 and n2
|
||||||
|
n1.left = P
|
||||||
|
P.left = n2
|
||||||
|
// Removing node P
|
||||||
|
n1.left = n2
|
||||||
|
```
|
||||||
|
|
||||||
|
=== "Ruby"
|
||||||
|
|
||||||
|
```ruby title="binary_tree.rb"
|
||||||
|
|
||||||
|
```
|
||||||
|
|
||||||
|
=== "Zig"
|
||||||
|
|
||||||
|
```zig title="binary_tree.zig"
|
||||||
|
|
||||||
|
```
|
||||||
|
|
||||||
|
??? pythontutor "Code visualization"
|
||||||
|
|
||||||
|
https://pythontutor.com/render.html#code=class%20TreeNode%3A%0A%20%20%20%20%22%22%22%E4%BA%8C%E5%8F%89%E6%A0%91%E8%8A%82%E7%82%B9%E7%B1%BB%22%22%22%0A%20%20%20%20def%20__init__%28self,%20val%3A%20int%29%3A%0A%20%20%20%20%20%20%20%20self.val%3A%20int%20%3D%20val%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%23%20%E8%8A%82%E7%82%B9%E5%80%BC%0A%20%20%20%20%20%20%20%20self.left%3A%20TreeNode%20%7C%20None%20%3D%20None%20%20%23%20%E5%B7%A6%E5%AD%90%E8%8A%82%E7%82%B9%E5%BC%95%E7%94%A8%0A%20%20%20%20%20%20%20%20self.right%3A%20TreeNode%20%7C%20None%20%3D%20None%20%23%20%E5%8F%B3%E5%AD%90%E8%8A%82%E7%82%B9%E5%BC%95%E7%94%A8%0A%0A%22%22%22Driver%20Code%22%22%22%0Aif%20__name__%20%3D%3D%20%22__main__%22%3A%0A%20%20%20%20%23%20%E5%88%9D%E5%A7%8B%E5%8C%96%E4%BA%8C%E5%8F%89%E6%A0%91%0A%20%20%20%20%23%20%E5%88%9D%E5%A7%8B%E5%8C%96%E8%8A%82%E7%82%B9%0A%20%20%20%20n1%20%3D%20TreeNode%28val%3D1%29%0A%20%20%20%20n2%20%3D%20TreeNode%28val%3D2%29%0A%20%20%20%20n3%20%3D%20TreeNode%28val%3D3%29%0A%20%20%20%20n4%20%3D%20TreeNode%28val%3D4%29%0A%20%20%20%20n5%20%3D%20TreeNode%28val%3D5%29%0A%20%20%20%20%23%20%E6%9E%84%E5%BB%BA%E8%8A%82%E7%82%B9%E4%B9%8B%E9%97%B4%E7%9A%84%E5%BC%95%E7%94%A8%EF%BC%88%E6%8C%87%E9%92%88%EF%BC%89%0A%20%20%20%20n1.left%20%3D%20n2%0A%20%20%20%20n1.right%20%3D%20n3%0A%20%20%20%20n2.left%20%3D%20n4%0A%20%20%20%20n2.right%20%3D%20n5%0A%0A%20%20%20%20%23%20%E6%8F%92%E5%85%A5%E4%B8%8E%E5%88%A0%E9%99%A4%E8%8A%82%E7%82%B9%0A%20%20%20%20p%20%3D%20TreeNode%280%29%0A%20%20%20%20%23%20%E5%9C%A8%20n1%20-%3E%20n2%20%E4%B8%AD%E9%97%B4%E6%8F%92%E5%85%A5%E8%8A%82%E7%82%B9%20P%0A%20%20%20%20n1.left%20%3D%20p%0A%20%20%20%20p.left%20%3D%20n2%0A%20%20%20%20%23%20%E5%88%A0%E9%99%A4%E8%8A%82%E7%82%B9%20P%0A%20%20%20%20n1.left%20%3D%20n2&cumulative=false&curInstr=37&heapPrimitives=nevernest&mode=display&origin=opt-frontend.js&py=311&rawInputLstJSON=%5B%5D&textReferences=false
|
||||||
|
|
||||||
|
!!! note
|
||||||
|
|
||||||
|
It's important to note that inserting nodes may change the original logical structure of the binary tree, while removing nodes usually means 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 actions.
|
||||||
|
|
||||||
|
## 7.1.3 Common types of binary trees
|
||||||
|
|
||||||
|
### 1. Perfect binary tree
|
||||||
|
|
||||||
|
As shown in the Figure 7-4 , in a "perfect binary tree," all levels of nodes are fully filled. In a perfect binary tree, the degree of leaf nodes is $0$, and 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.
|
||||||
|
|
||||||
|
!!! tip
|
||||||
|
|
||||||
|
Please note that in the Chinese community, a perfect binary tree is often referred to as a "full binary tree."
|
||||||
|
|
||||||
|
![Perfect binary tree](binary_tree.assets/perfect_binary_tree.png){ class="animation-figure" }
|
||||||
|
|
||||||
|
<p align="center"> Figure 7-4 Perfect binary tree </p>
|
||||||
|
|
||||||
|
### 2. Complete binary tree
|
||||||
|
|
||||||
|
As shown in the Figure 7-5 , a "complete binary tree" has only the bottom level nodes not fully filled, and the bottom level nodes are filled as far left as possible.
|
||||||
|
|
||||||
|
![Complete binary tree](binary_tree.assets/complete_binary_tree.png){ class="animation-figure" }
|
||||||
|
|
||||||
|
<p align="center"> Figure 7-5 Complete binary tree </p>
|
||||||
|
|
||||||
|
### 3. Full binary tree
|
||||||
|
|
||||||
|
As shown in the Figure 7-6 , a "full binary tree" has all nodes except leaf nodes having two children.
|
||||||
|
|
||||||
|
![Full binary tree](binary_tree.assets/full_binary_tree.png){ class="animation-figure" }
|
||||||
|
|
||||||
|
<p align="center"> Figure 7-6 Full binary tree </p>
|
||||||
|
|
||||||
|
### 4. Balanced binary tree
|
||||||
|
|
||||||
|
As shown in the Figure 7-7 , in a "balanced binary tree," the absolute difference in height between 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" }
|
||||||
|
|
||||||
|
<p align="center"> Figure 7-7 Balanced binary tree </p>
|
||||||
|
|
||||||
|
## 7.1.4 Degeneration of binary trees
|
||||||
|
|
||||||
|
The Figure 7-8 shows the ideal and degenerate structures of binary trees. When every level of a binary tree is filled, it reaches the "perfect binary tree"; when all nodes are biased towards one side, the binary tree degenerates into a "linked list".
|
||||||
|
|
||||||
|
- 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)$.
|
||||||
|
|
||||||
|
![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 the 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.
|
||||||
|
|
||||||
|
<p align="center"> Table 7-1 The Best and Worst Structures of Binary Trees </p>
|
||||||
|
|
||||||
|
<div class="center-table" markdown>
|
||||||
|
|
||||||
|
| | Perfect binary tree | Linked list |
|
||||||
|
| ----------------------------------------------- | ------------------- | ----------- |
|
||||||
|
| Number of nodes at level $i$ | $2^{i-1}$ | $1$ |
|
||||||
|
| Number of leaf nodes in a tree with height $h$ | $2^h$ | $1$ |
|
||||||
|
| Total number of nodes in a tree with height $h$ | $2^{h+1} - 1$ | $h + 1$ |
|
||||||
|
| Height of a tree with $n$ total nodes | $\log_2 (n+1) - 1$ | $n - 1$ |
|
||||||
|
|
||||||
|
</div>
|
File diff suppressed because one or more lines are too long
@ -0,0 +1,23 @@
|
|||||||
|
---
|
||||||
|
comments: true
|
||||||
|
icon: material/graph-outline
|
||||||
|
---
|
||||||
|
|
||||||
|
# Chapter 7. Tree
|
||||||
|
|
||||||
|
![Tree](../assets/covers/chapter_tree.jpg){ class="cover-image" }
|
||||||
|
|
||||||
|
!!! abstract
|
||||||
|
|
||||||
|
The towering tree, full of vitality with its roots deep and leaves lush, branches spreading wide.
|
||||||
|
|
||||||
|
It vividly demonstrates the form of data divide-and-conquer.
|
||||||
|
|
||||||
|
## Chapter Contents
|
||||||
|
|
||||||
|
- [7.1 Binary Tree](https://www.hello-algo.com/en/chapter_tree/binary_tree/)
|
||||||
|
- [7.2 Binary Tree Traversal](https://www.hello-algo.com/en/chapter_tree/binary_tree_traversal/)
|
||||||
|
- [7.3 Array Representation of Tree](https://www.hello-algo.com/en/chapter_tree/array_representation_of_tree/)
|
||||||
|
- [7.4 Binary Search Tree](https://www.hello-algo.com/en/chapter_tree/binary_search_tree/)
|
||||||
|
- [7.5 AVL Tree *](https://www.hello-algo.com/en/chapter_tree/avl_tree/)
|
||||||
|
- [7.6 Summary](https://www.hello-algo.com/en/chapter_tree/summary/)
|
@ -0,0 +1,58 @@
|
|||||||
|
---
|
||||||
|
comments: true
|
||||||
|
---
|
||||||
|
|
||||||
|
# 7.6 Summary
|
||||||
|
|
||||||
|
### 1. Key review
|
||||||
|
|
||||||
|
- A binary tree is a non-linear data structure that reflects the "divide and conquer" logic of splitting one into two. Each binary tree node contains a value and two pointers, which point to its left and right child nodes, respectively.
|
||||||
|
- For a node in a binary tree, the tree formed by its left (right) child node and all nodes under it is called the node's left (right) subtree.
|
||||||
|
- Related terminology of binary trees includes root node, leaf node, level, degree, edge, height, and depth, among others.
|
||||||
|
- The operations of initializing a binary tree, inserting nodes, and removing nodes are similar to those of linked list operations.
|
||||||
|
- Common types of binary trees include perfect binary trees, complete binary trees, full binary trees, and balanced binary trees. The perfect binary tree represents the ideal state, while the linked list is the worst state after degradation.
|
||||||
|
- A binary tree can be represented using an array by arranging the node values and empty slots in a level-order traversal sequence and implementing pointers based on the index mapping relationship between parent nodes and child nodes.
|
||||||
|
- The level-order traversal of a binary tree is a breadth-first search method, which reflects a layer-by-layer traversal manner of "expanding circle by circle." It is usually implemented using a queue.
|
||||||
|
- Pre-order, in-order, and post-order traversals are all depth-first search methods, reflecting the traversal manner of "going to the end first, then backtracking to continue." They are usually implemented using recursion.
|
||||||
|
- A binary search tree is an efficient data structure for element searching, with the time complexity of search, insert, and remove operations all being $O(\log n)$. When a binary search tree degrades into a linked list, these time complexities deteriorate to $O(n)$.
|
||||||
|
- An AVL tree, also known as a balanced binary search tree, ensures that the tree remains balanced after continuous node insertions and removals through rotation operations.
|
||||||
|
- Rotation operations in an AVL tree include right rotation, left rotation, right-then-left rotation, and left-then-right rotation. After inserting or removing nodes, an AVL tree performs rotation operations from bottom to top to rebalance the tree.
|
||||||
|
|
||||||
|
### 2. Q & A
|
||||||
|
|
||||||
|
**Q**: For a binary tree with only one node, are both the height of the tree and the depth of the root node $0$?
|
||||||
|
|
||||||
|
Yes, because height and depth are typically defined as "the number of edges passed."
|
||||||
|
|
||||||
|
**Q**: The insertion and removal in a binary tree are generally completed by a set of operations. What does "a set of operations" refer to here? Can it be understood as the release of resources of the child nodes?
|
||||||
|
|
||||||
|
Taking the binary search tree as an example, the operation of removing a node needs to be handled in three different scenarios, each requiring multiple steps of node operations.
|
||||||
|
|
||||||
|
**Q**: Why are there three sequences: pre-order, in-order, and post-order for DFS traversal of a binary tree, and what are their uses?
|
||||||
|
|
||||||
|
Similar to sequential and reverse traversal of arrays, pre-order, in-order, and post-order traversals are three methods of traversing a binary tree, allowing us to obtain a traversal result in a specific order. For example, in a binary search tree, since the node sizes satisfy `left child node value < root node value < right child node value`, we can obtain an ordered node sequence by traversing the tree in the "left → root → right" priority.
|
||||||
|
|
||||||
|
**Q**: In a right rotation operation that deals with the relationship between the imbalance nodes `node`, `child`, `grand_child`, isn't the connection between `node` and its parent node and the original link of `node` lost after the right rotation?
|
||||||
|
|
||||||
|
We need to view this problem from a recursive perspective. The `right_rotate(root)` operation passes the root node of the subtree and eventually returns the root node of the rotated subtree with `return child`. The connection between the subtree's root node and its parent node is established after this function returns, which is outside the scope of the right rotation operation's maintenance.
|
||||||
|
|
||||||
|
**Q**: In C++, functions are divided into `private` and `public` sections. What considerations are there for this? Why are the `height()` function and the `updateHeight()` function placed in `public` and `private`, respectively?
|
||||||
|
|
||||||
|
It depends on the scope of the method's use. If a method is only used within the class, then it is designed to be `private`. For example, it makes no sense for users to call `updateHeight()` on their own, as it is just a step in the insertion or removal operations. However, `height()` is for accessing node height, similar to `vector.size()`, thus it is set to `public` for use.
|
||||||
|
|
||||||
|
**Q**: How do you build a binary search tree from a set of input data? Is the choice of root node very important?
|
||||||
|
|
||||||
|
Yes, the method for building the tree is provided in the `build_tree()` method in the binary search tree code. As for the choice of the root node, we usually sort the input data and then select the middle element as the root node, recursively building the left and right subtrees. This approach maximizes the balance of the tree.
|
||||||
|
|
||||||
|
**Q**: In Java, do you always have to use the `equals()` method for string comparison?
|
||||||
|
|
||||||
|
In Java, for primitive data types, `==` is used to compare whether the values of two variables are equal. For reference types, the working principles of the two symbols are different.
|
||||||
|
|
||||||
|
- `==`: Used to compare whether two variables point to the same object, i.e., whether their positions in memory are the same.
|
||||||
|
- `equals()`: Used to compare whether the values of two objects are equal.
|
||||||
|
|
||||||
|
Therefore, to compare values, we should use `equals()`. However, strings initialized with `String a = "hi"; String b = "hi";` are stored in the string constant pool and point to the same object, so `a == b` can also be used to compare the contents of two strings.
|
||||||
|
|
||||||
|
**Q**: Before reaching the bottom level, is the number of nodes in the queue $2^h$ in breadth-first traversal?
|
||||||
|
|
||||||
|
Yes, for example, a full binary tree with height $h = 2$ has a total of $n = 7$ nodes, then the bottom level has $4 = 2^h = (n + 1) / 2$ nodes.
|
Loading…
Reference in new issue