2023-02-08 15:20:18 +08:00
|
|
|
|
---
|
|
|
|
|
comments: true
|
|
|
|
|
---
|
|
|
|
|
|
2023-04-17 18:24:20 +08:00
|
|
|
|
# 9.1. 堆
|
2023-02-08 15:20:18 +08:00
|
|
|
|
|
2023-04-10 03:12:02 +08:00
|
|
|
|
「堆 Heap」是一种满足特定条件的完全二叉树,可分为两种类型:
|
2023-02-08 15:20:18 +08:00
|
|
|
|
|
2023-04-09 04:34:58 +08:00
|
|
|
|
- 「大顶堆 Max Heap」,任意节点的值 $\geq$ 其子节点的值;
|
|
|
|
|
- 「小顶堆 Min Heap」,任意节点的值 $\leq$ 其子节点的值;
|
2023-02-08 15:20:18 +08:00
|
|
|
|
|
2023-02-26 18:17:57 +08:00
|
|
|
|
![小顶堆与大顶堆](heap.assets/min_heap_and_max_heap.png)
|
2023-02-08 15:20:18 +08:00
|
|
|
|
|
2023-02-26 19:53:26 +08:00
|
|
|
|
<p align="center"> Fig. 小顶堆与大顶堆 </p>
|
|
|
|
|
|
2023-04-10 03:12:02 +08:00
|
|
|
|
堆作为完全二叉树的一个特例,具有以下特性:
|
2023-02-08 15:20:18 +08:00
|
|
|
|
|
2023-04-10 03:12:02 +08:00
|
|
|
|
- 最底层节点靠左填充,其他层的节点都被填满。
|
|
|
|
|
- 我们将二叉树的根节点称为「堆顶」,将底层最靠右的节点称为「堆底」。
|
|
|
|
|
- 对于大顶堆(小顶堆),堆顶元素(即根节点)的值分别是最大(最小)的。
|
2023-02-08 15:20:18 +08:00
|
|
|
|
|
2023-04-17 18:24:20 +08:00
|
|
|
|
## 9.1.1. 堆常用操作
|
2023-02-08 15:20:18 +08:00
|
|
|
|
|
2023-04-10 03:12:02 +08:00
|
|
|
|
需要指出的是,许多编程语言提供的是「优先队列 Priority Queue」,这是一种抽象数据结构,定义为具有优先级排序的队列。
|
2023-02-08 15:20:18 +08:00
|
|
|
|
|
2023-04-10 03:12:02 +08:00
|
|
|
|
实际上,**堆通常用作实现优先队列,大顶堆相当于元素按从大到小顺序出队的优先队列**。从使用角度来看,我们可以将「优先队列」和「堆」看作等价的数据结构。因此,本书对两者不做特别区分,统一使用「堆」来命名。
|
2023-02-08 15:20:18 +08:00
|
|
|
|
|
2023-04-10 03:12:02 +08:00
|
|
|
|
堆的常用操作见下表,方法名需要根据编程语言来确定。
|
2023-02-08 15:20:18 +08:00
|
|
|
|
|
|
|
|
|
<div class="center-table" markdown>
|
|
|
|
|
|
2023-03-13 22:40:57 +08:00
|
|
|
|
| 方法名 | 描述 | 时间复杂度 |
|
|
|
|
|
| --------- | ------------------------------------------ | ----------- |
|
|
|
|
|
| push() | 元素入堆 | $O(\log n)$ |
|
|
|
|
|
| pop() | 堆顶元素出堆 | $O(\log n)$ |
|
|
|
|
|
| peek() | 访问堆顶元素(大 / 小顶堆分别为最大 / 小值) | $O(1)$ |
|
|
|
|
|
| size() | 获取堆的元素数量 | $O(1)$ |
|
|
|
|
|
| isEmpty() | 判断堆是否为空 | $O(1)$ |
|
2023-02-08 15:20:18 +08:00
|
|
|
|
|
|
|
|
|
</div>
|
|
|
|
|
|
2023-04-10 03:12:02 +08:00
|
|
|
|
在实际应用中,我们可以直接使用编程语言提供的堆类(或优先队列类)。
|
2023-02-08 15:20:18 +08:00
|
|
|
|
|
|
|
|
|
!!! tip
|
|
|
|
|
|
2023-04-10 03:12:02 +08:00
|
|
|
|
类似于排序算法中的“从小到大排列”和“从大到小排列”,我们可以通过修改 Comparator 来实现“小顶堆”与“大顶堆”之间的转换。
|
2023-02-08 15:20:18 +08:00
|
|
|
|
|
|
|
|
|
=== "Java"
|
|
|
|
|
|
|
|
|
|
```java title="heap.java"
|
|
|
|
|
/* 初始化堆 */
|
|
|
|
|
// 初始化小顶堆
|
|
|
|
|
Queue<Integer> minHeap = new PriorityQueue<>();
|
|
|
|
|
// 初始化大顶堆(使用 lambda 表达式修改 Comparator 即可)
|
2023-02-23 20:22:16 +08:00
|
|
|
|
Queue<Integer> maxHeap = new PriorityQueue<>((a, b) -> b - a);
|
2023-02-08 15:20:18 +08:00
|
|
|
|
|
|
|
|
|
/* 元素入堆 */
|
2023-03-13 22:40:57 +08:00
|
|
|
|
maxHeap.offer(1);
|
|
|
|
|
maxHeap.offer(3);
|
|
|
|
|
maxHeap.offer(2);
|
|
|
|
|
maxHeap.offer(5);
|
|
|
|
|
maxHeap.offer(4);
|
2023-02-08 15:20:18 +08:00
|
|
|
|
|
|
|
|
|
/* 获取堆顶元素 */
|
|
|
|
|
int peek = maxHeap.peek(); // 5
|
|
|
|
|
|
|
|
|
|
/* 堆顶元素出堆 */
|
|
|
|
|
// 出堆元素会形成一个从大到小的序列
|
|
|
|
|
peek = heap.poll(); // 5
|
|
|
|
|
peek = heap.poll(); // 4
|
|
|
|
|
peek = heap.poll(); // 3
|
|
|
|
|
peek = heap.poll(); // 2
|
|
|
|
|
peek = heap.poll(); // 1
|
|
|
|
|
|
|
|
|
|
/* 获取堆大小 */
|
|
|
|
|
int size = maxHeap.size();
|
|
|
|
|
|
|
|
|
|
/* 判断堆是否为空 */
|
|
|
|
|
boolean isEmpty = maxHeap.isEmpty();
|
|
|
|
|
|
|
|
|
|
/* 输入列表并建堆 */
|
|
|
|
|
minHeap = new PriorityQueue<>(Arrays.asList(1, 3, 2, 5, 4));
|
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
=== "C++"
|
|
|
|
|
|
|
|
|
|
```cpp title="heap.cpp"
|
|
|
|
|
/* 初始化堆 */
|
|
|
|
|
// 初始化小顶堆
|
|
|
|
|
priority_queue<int, vector<int>, greater<int>> minHeap;
|
|
|
|
|
// 初始化大顶堆
|
|
|
|
|
priority_queue<int, vector<int>, less<int>> maxHeap;
|
|
|
|
|
|
|
|
|
|
/* 元素入堆 */
|
|
|
|
|
maxHeap.push(1);
|
|
|
|
|
maxHeap.push(3);
|
|
|
|
|
maxHeap.push(2);
|
|
|
|
|
maxHeap.push(5);
|
|
|
|
|
maxHeap.push(4);
|
|
|
|
|
|
|
|
|
|
/* 获取堆顶元素 */
|
|
|
|
|
int peek = maxHeap.top(); // 5
|
|
|
|
|
|
|
|
|
|
/* 堆顶元素出堆 */
|
|
|
|
|
// 出堆元素会形成一个从大到小的序列
|
|
|
|
|
maxHeap.pop(); // 5
|
|
|
|
|
maxHeap.pop(); // 4
|
|
|
|
|
maxHeap.pop(); // 3
|
|
|
|
|
maxHeap.pop(); // 2
|
|
|
|
|
maxHeap.pop(); // 1
|
|
|
|
|
|
|
|
|
|
/* 获取堆大小 */
|
|
|
|
|
int size = maxHeap.size();
|
|
|
|
|
|
|
|
|
|
/* 判断堆是否为空 */
|
|
|
|
|
bool isEmpty = maxHeap.empty();
|
|
|
|
|
|
|
|
|
|
/* 输入列表并建堆 */
|
|
|
|
|
vector<int> input{1, 3, 2, 5, 4};
|
|
|
|
|
priority_queue<int, vector<int>, greater<int>> minHeap(input.begin(), input.end());
|
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
=== "Python"
|
|
|
|
|
|
|
|
|
|
```python title="heap.py"
|
2023-02-23 20:22:16 +08:00
|
|
|
|
# 初始化小顶堆
|
|
|
|
|
min_heap, flag = [], 1
|
|
|
|
|
# 初始化大顶堆
|
|
|
|
|
max_heap, flag = [], -1
|
|
|
|
|
|
|
|
|
|
# Python 的 heapq 模块默认实现小顶堆
|
|
|
|
|
# 考虑将“元素取负”后再入堆,这样就可以将大小关系颠倒,从而实现大顶堆
|
|
|
|
|
# 在本示例中,flag = 1 时对应小顶堆,flag = -1 时对应大顶堆
|
2023-04-09 05:30:47 +08:00
|
|
|
|
|
|
|
|
|
# 元素入堆
|
2023-02-23 20:22:16 +08:00
|
|
|
|
heapq.heappush(max_heap, flag * 1)
|
|
|
|
|
heapq.heappush(max_heap, flag * 3)
|
|
|
|
|
heapq.heappush(max_heap, flag * 2)
|
|
|
|
|
heapq.heappush(max_heap, flag * 5)
|
|
|
|
|
heapq.heappush(max_heap, flag * 4)
|
|
|
|
|
|
2023-04-09 05:30:47 +08:00
|
|
|
|
# 获取堆顶元素
|
2023-03-12 18:46:03 +08:00
|
|
|
|
peek: int = flag * max_heap[0] # 5
|
2023-02-23 20:22:16 +08:00
|
|
|
|
|
2023-04-09 05:30:47 +08:00
|
|
|
|
# 堆顶元素出堆
|
2023-02-23 20:22:16 +08:00
|
|
|
|
# 出堆元素会形成一个从大到小的序列
|
|
|
|
|
val = flag * heapq.heappop(max_heap) # 5
|
|
|
|
|
val = flag * heapq.heappop(max_heap) # 4
|
|
|
|
|
val = flag * heapq.heappop(max_heap) # 3
|
|
|
|
|
val = flag * heapq.heappop(max_heap) # 2
|
|
|
|
|
val = flag * heapq.heappop(max_heap) # 1
|
|
|
|
|
|
2023-04-09 05:30:47 +08:00
|
|
|
|
# 获取堆大小
|
2023-03-12 18:46:03 +08:00
|
|
|
|
size: int = len(max_heap)
|
2023-02-23 20:22:16 +08:00
|
|
|
|
|
2023-04-09 05:30:47 +08:00
|
|
|
|
# 判断堆是否为空
|
2023-03-12 18:46:03 +08:00
|
|
|
|
is_empty: bool = not max_heap
|
2023-02-23 20:22:16 +08:00
|
|
|
|
|
2023-04-09 05:30:47 +08:00
|
|
|
|
# 输入列表并建堆
|
2023-03-12 18:46:03 +08:00
|
|
|
|
min_heap: List[int] = [1, 3, 2, 5, 4]
|
2023-02-23 20:22:16 +08:00
|
|
|
|
heapq.heapify(min_heap)
|
2023-02-08 15:20:18 +08:00
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
=== "Go"
|
|
|
|
|
|
|
|
|
|
```go title="heap.go"
|
|
|
|
|
// Go 语言中可以通过实现 heap.Interface 来构建整数大顶堆
|
|
|
|
|
// 实现 heap.Interface 需要同时实现 sort.Interface
|
|
|
|
|
type intHeap []any
|
|
|
|
|
|
|
|
|
|
// Push heap.Interface 的方法,实现推入元素到堆
|
|
|
|
|
func (h *intHeap) Push(x any) {
|
|
|
|
|
// Push 和 Pop 使用 pointer receiver 作为参数
|
|
|
|
|
// 因为它们不仅会对切片的内容进行调整,还会修改切片的长度。
|
|
|
|
|
*h = append(*h, x.(int))
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Pop heap.Interface 的方法,实现弹出堆顶元素
|
|
|
|
|
func (h *intHeap) Pop() any {
|
|
|
|
|
// 待出堆元素存放在最后
|
|
|
|
|
last := (*h)[len(*h)-1]
|
|
|
|
|
*h = (*h)[:len(*h)-1]
|
|
|
|
|
return last
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Len sort.Interface 的方法
|
|
|
|
|
func (h *intHeap) Len() int {
|
|
|
|
|
return len(*h)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Less sort.Interface 的方法
|
|
|
|
|
func (h *intHeap) Less(i, j int) bool {
|
|
|
|
|
// 如果实现小顶堆,则需要调整为小于号
|
|
|
|
|
return (*h)[i].(int) > (*h)[j].(int)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Swap sort.Interface 的方法
|
|
|
|
|
func (h *intHeap) Swap(i, j int) {
|
|
|
|
|
(*h)[i], (*h)[j] = (*h)[j], (*h)[i]
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Top 获取堆顶元素
|
|
|
|
|
func (h *intHeap) Top() any {
|
|
|
|
|
return (*h)[0]
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/* Driver Code */
|
|
|
|
|
func TestHeap(t *testing.T) {
|
|
|
|
|
/* 初始化堆 */
|
|
|
|
|
// 初始化大顶堆
|
|
|
|
|
maxHeap := &intHeap{}
|
|
|
|
|
heap.Init(maxHeap)
|
|
|
|
|
/* 元素入堆 */
|
|
|
|
|
// 调用 heap.Interface 的方法,来添加元素
|
|
|
|
|
heap.Push(maxHeap, 1)
|
|
|
|
|
heap.Push(maxHeap, 3)
|
|
|
|
|
heap.Push(maxHeap, 2)
|
|
|
|
|
heap.Push(maxHeap, 4)
|
|
|
|
|
heap.Push(maxHeap, 5)
|
|
|
|
|
|
|
|
|
|
/* 获取堆顶元素 */
|
|
|
|
|
top := maxHeap.Top()
|
|
|
|
|
fmt.Printf("堆顶元素为 %d\n", top)
|
|
|
|
|
|
|
|
|
|
/* 堆顶元素出堆 */
|
|
|
|
|
// 调用 heap.Interface 的方法,来移除元素
|
2023-03-13 22:40:57 +08:00
|
|
|
|
heap.Pop(maxHeap) // 5
|
|
|
|
|
heap.Pop(maxHeap) // 4
|
|
|
|
|
heap.Pop(maxHeap) // 3
|
|
|
|
|
heap.Pop(maxHeap) // 2
|
|
|
|
|
heap.Pop(maxHeap) // 1
|
2023-02-08 15:20:18 +08:00
|
|
|
|
|
|
|
|
|
/* 获取堆大小 */
|
|
|
|
|
size := len(*maxHeap)
|
|
|
|
|
fmt.Printf("堆元素数量为 %d\n", size)
|
|
|
|
|
|
|
|
|
|
/* 判断堆是否为空 */
|
|
|
|
|
isEmpty := len(*maxHeap) == 0
|
|
|
|
|
fmt.Printf("堆是否为空 %t\n", isEmpty)
|
|
|
|
|
}
|
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
=== "JavaScript"
|
|
|
|
|
|
|
|
|
|
```javascript title="heap.js"
|
2023-03-13 22:40:57 +08:00
|
|
|
|
// JavaScript 未提供内置 Heap 类
|
2023-02-08 15:20:18 +08:00
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
=== "TypeScript"
|
|
|
|
|
|
|
|
|
|
```typescript title="heap.ts"
|
2023-03-13 22:40:57 +08:00
|
|
|
|
// TypeScript 未提供内置 Heap 类
|
2023-02-08 15:20:18 +08:00
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
=== "C"
|
|
|
|
|
|
|
|
|
|
```c title="heap.c"
|
|
|
|
|
|
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
=== "C#"
|
|
|
|
|
|
|
|
|
|
```csharp title="heap.cs"
|
2023-02-15 21:48:22 +08:00
|
|
|
|
/* 初始化堆 */
|
|
|
|
|
// 初始化小顶堆
|
|
|
|
|
PriorityQueue<int, int> minHeap = new PriorityQueue<int, int>();
|
|
|
|
|
// 初始化大顶堆(使用 lambda 表达式修改 Comparator 即可)
|
|
|
|
|
PriorityQueue<int, int> maxHeap = new PriorityQueue<int, int>(Comparer<int>.Create((x, y) => y - x));
|
|
|
|
|
|
|
|
|
|
/* 元素入堆 */
|
|
|
|
|
maxHeap.Enqueue(1, 1);
|
|
|
|
|
maxHeap.Enqueue(3, 3);
|
|
|
|
|
maxHeap.Enqueue(2, 2);
|
|
|
|
|
maxHeap.Enqueue(5, 5);
|
|
|
|
|
maxHeap.Enqueue(4, 4);
|
|
|
|
|
|
|
|
|
|
/* 获取堆顶元素 */
|
|
|
|
|
int peek = maxHeap.Peek();//5
|
|
|
|
|
|
|
|
|
|
/* 堆顶元素出堆 */
|
|
|
|
|
// 出堆元素会形成一个从大到小的序列
|
|
|
|
|
peek = maxHeap.Dequeue(); // 5
|
|
|
|
|
peek = maxHeap.Dequeue(); // 4
|
|
|
|
|
peek = maxHeap.Dequeue(); // 3
|
|
|
|
|
peek = maxHeap.Dequeue(); // 2
|
|
|
|
|
peek = maxHeap.Dequeue(); // 1
|
|
|
|
|
|
|
|
|
|
/* 获取堆大小 */
|
|
|
|
|
int size = maxHeap.Count;
|
|
|
|
|
|
|
|
|
|
/* 判断堆是否为空 */
|
|
|
|
|
bool isEmpty = maxHeap.Count == 0;
|
2023-02-08 15:20:18 +08:00
|
|
|
|
|
2023-02-15 21:48:22 +08:00
|
|
|
|
/* 输入列表并建堆 */
|
|
|
|
|
minHeap = new PriorityQueue<int, int>(new List<(int, int)> { (1, 1), (3, 3), (2, 2), (5, 5), (4, 4), });
|
2023-02-08 15:20:18 +08:00
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
=== "Swift"
|
|
|
|
|
|
|
|
|
|
```swift title="heap.swift"
|
2023-03-13 22:40:57 +08:00
|
|
|
|
// Swift 未提供内置 Heap 类
|
2023-02-08 15:20:18 +08:00
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
=== "Zig"
|
|
|
|
|
|
|
|
|
|
```zig title="heap.zig"
|
|
|
|
|
|
|
|
|
|
```
|
|
|
|
|
|
2023-04-17 18:24:20 +08:00
|
|
|
|
## 9.1.2. 堆的实现
|
2023-02-08 15:20:18 +08:00
|
|
|
|
|
2023-04-10 03:12:02 +08:00
|
|
|
|
下文实现的是大顶堆。若要将其转换为小顶堆,只需将所有大小逻辑判断取逆(例如,将 $\geq$ 替换为 $\leq$ )。感兴趣的读者可以自行实现。
|
2023-02-08 15:20:18 +08:00
|
|
|
|
|
|
|
|
|
### 堆的存储与表示
|
|
|
|
|
|
2023-04-10 03:12:02 +08:00
|
|
|
|
我们在二叉树章节中学习到,完全二叉树非常适合用数组来表示。由于堆正是一种完全二叉树,**我们将采用数组来存储堆**。
|
2023-02-08 15:20:18 +08:00
|
|
|
|
|
2023-04-10 03:12:02 +08:00
|
|
|
|
当使用数组表示二叉树时,元素代表节点值,索引代表节点在二叉树中的位置。**节点指针通过索引映射公式来实现**。
|
2023-02-08 15:20:18 +08:00
|
|
|
|
|
2023-04-10 03:12:02 +08:00
|
|
|
|
具体而言,给定索引 $i$ ,其左子节点索引为 $2i + 1$ ,右子节点索引为 $2i + 2$ ,父节点索引为 $(i - 1) / 2$(向下取整)。当索引越界时,表示空节点或节点不存在。
|
2023-02-08 15:20:18 +08:00
|
|
|
|
|
2023-02-26 18:17:57 +08:00
|
|
|
|
![堆的表示与存储](heap.assets/representation_of_heap.png)
|
2023-02-08 15:20:18 +08:00
|
|
|
|
|
2023-02-26 19:53:26 +08:00
|
|
|
|
<p align="center"> Fig. 堆的表示与存储 </p>
|
|
|
|
|
|
2023-04-10 03:12:02 +08:00
|
|
|
|
我们可以将索引映射公式封装成函数,方便后续使用。
|
2023-02-08 15:20:18 +08:00
|
|
|
|
|
|
|
|
|
=== "Java"
|
|
|
|
|
|
|
|
|
|
```java title="my_heap.java"
|
2023-04-09 04:34:58 +08:00
|
|
|
|
/* 获取左子节点索引 */
|
2023-02-08 15:20:18 +08:00
|
|
|
|
int left(int i) {
|
|
|
|
|
return 2 * i + 1;
|
|
|
|
|
}
|
|
|
|
|
|
2023-04-09 04:34:58 +08:00
|
|
|
|
/* 获取右子节点索引 */
|
2023-02-08 15:20:18 +08:00
|
|
|
|
int right(int i) {
|
|
|
|
|
return 2 * i + 2;
|
|
|
|
|
}
|
|
|
|
|
|
2023-04-09 04:34:58 +08:00
|
|
|
|
/* 获取父节点索引 */
|
2023-02-08 15:20:18 +08:00
|
|
|
|
int parent(int i) {
|
|
|
|
|
return (i - 1) / 2; // 向下整除
|
|
|
|
|
}
|
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
=== "C++"
|
|
|
|
|
|
|
|
|
|
```cpp title="my_heap.cpp"
|
2023-04-09 04:34:58 +08:00
|
|
|
|
/* 获取左子节点索引 */
|
2023-02-08 15:20:18 +08:00
|
|
|
|
int left(int i) {
|
|
|
|
|
return 2 * i + 1;
|
|
|
|
|
}
|
|
|
|
|
|
2023-04-09 04:34:58 +08:00
|
|
|
|
/* 获取右子节点索引 */
|
2023-02-08 15:20:18 +08:00
|
|
|
|
int right(int i) {
|
|
|
|
|
return 2 * i + 2;
|
2023-04-14 04:01:38 +08:00
|
|
|
|
}
|
2023-02-08 15:20:18 +08:00
|
|
|
|
|
2023-04-09 04:34:58 +08:00
|
|
|
|
/* 获取父节点索引 */
|
2023-02-08 15:20:18 +08:00
|
|
|
|
int parent(int i) {
|
|
|
|
|
return (i - 1) / 2; // 向下取整
|
|
|
|
|
}
|
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
=== "Python"
|
|
|
|
|
|
|
|
|
|
```python title="my_heap.py"
|
2023-02-23 20:22:16 +08:00
|
|
|
|
def left(self, i: int) -> int:
|
2023-04-09 05:12:22 +08:00
|
|
|
|
"""获取左子节点索引"""
|
2023-02-23 20:22:16 +08:00
|
|
|
|
return 2 * i + 1
|
|
|
|
|
|
|
|
|
|
def right(self, i: int) -> int:
|
2023-04-09 05:12:22 +08:00
|
|
|
|
"""获取右子节点索引"""
|
2023-02-23 20:22:16 +08:00
|
|
|
|
return 2 * i + 2
|
2023-02-08 15:20:18 +08:00
|
|
|
|
|
2023-02-23 20:22:16 +08:00
|
|
|
|
def parent(self, i: int) -> int:
|
2023-04-09 05:12:22 +08:00
|
|
|
|
"""获取父节点索引"""
|
2023-02-23 20:22:16 +08:00
|
|
|
|
return (i - 1) // 2 # 向下整除
|
2023-02-08 15:20:18 +08:00
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
=== "Go"
|
|
|
|
|
|
|
|
|
|
```go title="my_heap.go"
|
2023-04-09 04:34:58 +08:00
|
|
|
|
/* 获取左子节点索引 */
|
2023-02-08 15:20:18 +08:00
|
|
|
|
func (h *maxHeap) left(i int) int {
|
|
|
|
|
return 2*i + 1
|
|
|
|
|
}
|
|
|
|
|
|
2023-04-09 04:34:58 +08:00
|
|
|
|
/* 获取右子节点索引 */
|
2023-02-08 15:20:18 +08:00
|
|
|
|
func (h *maxHeap) right(i int) int {
|
|
|
|
|
return 2*i + 2
|
|
|
|
|
}
|
|
|
|
|
|
2023-04-09 04:34:58 +08:00
|
|
|
|
/* 获取父节点索引 */
|
2023-02-08 15:20:18 +08:00
|
|
|
|
func (h *maxHeap) parent(i int) int {
|
|
|
|
|
// 向下整除
|
|
|
|
|
return (i - 1) / 2
|
|
|
|
|
}
|
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
=== "JavaScript"
|
|
|
|
|
|
|
|
|
|
```javascript title="my_heap.js"
|
2023-04-09 04:34:58 +08:00
|
|
|
|
/* 获取左子节点索引 */
|
2023-02-08 15:20:18 +08:00
|
|
|
|
#left(i) {
|
|
|
|
|
return 2 * i + 1;
|
|
|
|
|
}
|
|
|
|
|
|
2023-04-09 04:34:58 +08:00
|
|
|
|
/* 获取右子节点索引 */
|
2023-02-08 15:20:18 +08:00
|
|
|
|
#right(i) {
|
|
|
|
|
return 2 * i + 2;
|
|
|
|
|
}
|
|
|
|
|
|
2023-04-09 04:34:58 +08:00
|
|
|
|
/* 获取父节点索引 */
|
2023-02-08 15:20:18 +08:00
|
|
|
|
#parent(i) {
|
|
|
|
|
return Math.floor((i - 1) / 2); // 向下整除
|
|
|
|
|
}
|
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
=== "TypeScript"
|
|
|
|
|
|
|
|
|
|
```typescript title="my_heap.ts"
|
2023-04-09 04:34:58 +08:00
|
|
|
|
/* 获取左子节点索引 */
|
2023-02-08 16:47:52 +08:00
|
|
|
|
left(i: number): number {
|
2023-02-08 15:20:18 +08:00
|
|
|
|
return 2 * i + 1;
|
|
|
|
|
}
|
|
|
|
|
|
2023-04-09 04:34:58 +08:00
|
|
|
|
/* 获取右子节点索引 */
|
2023-02-08 16:47:52 +08:00
|
|
|
|
right(i: number): number {
|
2023-02-08 15:20:18 +08:00
|
|
|
|
return 2 * i + 2;
|
|
|
|
|
}
|
|
|
|
|
|
2023-04-09 04:34:58 +08:00
|
|
|
|
/* 获取父节点索引 */
|
2023-02-08 16:47:52 +08:00
|
|
|
|
parent(i: number): number {
|
2023-02-08 15:20:18 +08:00
|
|
|
|
return Math.floor((i - 1) / 2); // 向下整除
|
|
|
|
|
}
|
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
=== "C"
|
|
|
|
|
|
|
|
|
|
```c title="my_heap.c"
|
2023-02-11 18:21:44 +08:00
|
|
|
|
[class]{maxHeap}-[func]{left}
|
2023-02-08 15:20:18 +08:00
|
|
|
|
|
2023-02-11 18:21:44 +08:00
|
|
|
|
[class]{maxHeap}-[func]{right}
|
|
|
|
|
|
|
|
|
|
[class]{maxHeap}-[func]{parent}
|
2023-02-08 15:20:18 +08:00
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
=== "C#"
|
|
|
|
|
|
|
|
|
|
```csharp title="my_heap.cs"
|
2023-04-09 04:34:58 +08:00
|
|
|
|
/* 获取左子节点索引 */
|
2023-02-15 21:48:22 +08:00
|
|
|
|
int left(int i)
|
|
|
|
|
{
|
|
|
|
|
return 2 * i + 1;
|
|
|
|
|
}
|
2023-02-08 15:20:18 +08:00
|
|
|
|
|
2023-04-09 04:34:58 +08:00
|
|
|
|
/* 获取右子节点索引 */
|
2023-02-15 21:48:22 +08:00
|
|
|
|
int right(int i)
|
|
|
|
|
{
|
|
|
|
|
return 2 * i + 2;
|
|
|
|
|
}
|
2023-02-08 22:16:25 +08:00
|
|
|
|
|
2023-04-09 04:34:58 +08:00
|
|
|
|
/* 获取父节点索引 */
|
2023-02-15 21:48:22 +08:00
|
|
|
|
int parent(int i)
|
|
|
|
|
{
|
|
|
|
|
return (i - 1) / 2; // 向下整除
|
|
|
|
|
}
|
2023-02-08 15:20:18 +08:00
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
=== "Swift"
|
|
|
|
|
|
|
|
|
|
```swift title="my_heap.swift"
|
2023-04-09 04:34:58 +08:00
|
|
|
|
/* 获取左子节点索引 */
|
2023-02-08 15:20:18 +08:00
|
|
|
|
func left(i: Int) -> Int {
|
|
|
|
|
2 * i + 1
|
|
|
|
|
}
|
|
|
|
|
|
2023-04-09 04:34:58 +08:00
|
|
|
|
/* 获取右子节点索引 */
|
2023-02-08 15:20:18 +08:00
|
|
|
|
func right(i: Int) -> Int {
|
|
|
|
|
2 * i + 2
|
|
|
|
|
}
|
|
|
|
|
|
2023-04-09 04:34:58 +08:00
|
|
|
|
/* 获取父节点索引 */
|
2023-02-08 15:20:18 +08:00
|
|
|
|
func parent(i: Int) -> Int {
|
|
|
|
|
(i - 1) / 2 // 向下整除
|
|
|
|
|
}
|
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
=== "Zig"
|
|
|
|
|
|
|
|
|
|
```zig title="my_heap.zig"
|
2023-04-09 04:34:58 +08:00
|
|
|
|
// 获取左子节点索引
|
2023-02-09 22:55:29 +08:00
|
|
|
|
fn left(i: usize) usize {
|
|
|
|
|
return 2 * i + 1;
|
|
|
|
|
}
|
|
|
|
|
|
2023-04-09 04:34:58 +08:00
|
|
|
|
// 获取右子节点索引
|
2023-02-09 22:55:29 +08:00
|
|
|
|
fn right(i: usize) usize {
|
|
|
|
|
return 2 * i + 2;
|
|
|
|
|
}
|
2023-02-08 15:20:18 +08:00
|
|
|
|
|
2023-04-09 04:34:58 +08:00
|
|
|
|
// 获取父节点索引
|
2023-02-09 22:55:29 +08:00
|
|
|
|
fn parent(i: usize) usize {
|
|
|
|
|
// return (i - 1) / 2; // 向下整除
|
|
|
|
|
return @divFloor(i - 1, 2);
|
|
|
|
|
}
|
2023-02-08 15:20:18 +08:00
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
### 访问堆顶元素
|
|
|
|
|
|
2023-04-10 03:12:02 +08:00
|
|
|
|
堆顶元素即为二叉树的根节点,也就是列表的首个元素。
|
2023-02-08 15:20:18 +08:00
|
|
|
|
|
|
|
|
|
=== "Java"
|
|
|
|
|
|
|
|
|
|
```java title="my_heap.java"
|
|
|
|
|
/* 访问堆顶元素 */
|
|
|
|
|
int peek() {
|
|
|
|
|
return maxHeap.get(0);
|
|
|
|
|
}
|
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
=== "C++"
|
|
|
|
|
|
|
|
|
|
```cpp title="my_heap.cpp"
|
|
|
|
|
/* 访问堆顶元素 */
|
|
|
|
|
int peek() {
|
|
|
|
|
return maxHeap[0];
|
|
|
|
|
}
|
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
=== "Python"
|
|
|
|
|
|
|
|
|
|
```python title="my_heap.py"
|
2023-02-23 20:22:16 +08:00
|
|
|
|
def peek(self) -> int:
|
2023-04-09 05:12:22 +08:00
|
|
|
|
"""访问堆顶元素"""
|
2023-02-23 20:22:16 +08:00
|
|
|
|
return self.max_heap[0]
|
2023-02-08 15:20:18 +08:00
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
=== "Go"
|
|
|
|
|
|
|
|
|
|
```go title="my_heap.go"
|
|
|
|
|
/* 访问堆顶元素 */
|
|
|
|
|
func (h *maxHeap) peek() any {
|
|
|
|
|
return h.data[0]
|
|
|
|
|
}
|
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
=== "JavaScript"
|
|
|
|
|
|
|
|
|
|
```javascript title="my_heap.js"
|
|
|
|
|
/* 访问堆顶元素 */
|
|
|
|
|
peek() {
|
|
|
|
|
return this.#maxHeap[0];
|
|
|
|
|
}
|
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
=== "TypeScript"
|
|
|
|
|
|
|
|
|
|
```typescript title="my_heap.ts"
|
|
|
|
|
/* 访问堆顶元素 */
|
2023-02-08 16:47:52 +08:00
|
|
|
|
peek(): number {
|
2023-02-08 15:20:18 +08:00
|
|
|
|
return this.maxHeap[0];
|
|
|
|
|
}
|
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
=== "C"
|
|
|
|
|
|
|
|
|
|
```c title="my_heap.c"
|
2023-02-11 18:21:44 +08:00
|
|
|
|
[class]{maxHeap}-[func]{peek}
|
2023-02-08 15:20:18 +08:00
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
=== "C#"
|
|
|
|
|
|
|
|
|
|
```csharp title="my_heap.cs"
|
2023-02-15 21:48:22 +08:00
|
|
|
|
/* 访问堆顶元素 */
|
|
|
|
|
int peek()
|
|
|
|
|
{
|
|
|
|
|
return maxHeap[0];
|
|
|
|
|
}
|
2023-02-08 15:20:18 +08:00
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
=== "Swift"
|
|
|
|
|
|
|
|
|
|
```swift title="my_heap.swift"
|
|
|
|
|
/* 访问堆顶元素 */
|
|
|
|
|
func peek() -> Int {
|
|
|
|
|
maxHeap[0]
|
|
|
|
|
}
|
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
=== "Zig"
|
|
|
|
|
|
|
|
|
|
```zig title="my_heap.zig"
|
2023-02-09 22:55:29 +08:00
|
|
|
|
// 访问堆顶元素
|
|
|
|
|
fn peek(self: *Self) T {
|
2023-03-15 03:12:57 +08:00
|
|
|
|
return self.max_heap.?.items[0];
|
2023-02-09 22:55:29 +08:00
|
|
|
|
}
|
2023-02-08 15:20:18 +08:00
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
### 元素入堆
|
|
|
|
|
|
2023-04-10 03:12:02 +08:00
|
|
|
|
给定元素 `val` ,我们首先将其添加到堆底。添加之后,由于 val 可能大于堆中其他元素,堆的成立条件可能已被破坏。因此,**需要修复从插入节点到根节点的路径上的各个节点**,这个操作被称为「堆化 Heapify」。
|
2023-02-08 15:20:18 +08:00
|
|
|
|
|
2023-04-10 03:12:02 +08:00
|
|
|
|
考虑从入堆节点开始,**从底至顶执行堆化**。具体来说,我们比较插入节点与其父节点的值,如果插入节点更大,则将它们交换。然后继续执行此操作,从底至顶修复堆中的各个节点,直至越过根节点或遇到无需交换的节点时结束。
|
2023-02-08 15:20:18 +08:00
|
|
|
|
|
2023-02-22 00:57:04 +08:00
|
|
|
|
=== "<1>"
|
2023-02-26 19:22:52 +08:00
|
|
|
|
![元素入堆步骤](heap.assets/heap_push_step1.png)
|
2023-02-08 15:20:18 +08:00
|
|
|
|
|
2023-02-22 00:57:04 +08:00
|
|
|
|
=== "<2>"
|
2023-02-08 15:20:18 +08:00
|
|
|
|
![heap_push_step2](heap.assets/heap_push_step2.png)
|
|
|
|
|
|
2023-02-22 00:57:04 +08:00
|
|
|
|
=== "<3>"
|
2023-02-08 15:20:18 +08:00
|
|
|
|
![heap_push_step3](heap.assets/heap_push_step3.png)
|
|
|
|
|
|
2023-02-22 00:57:04 +08:00
|
|
|
|
=== "<4>"
|
2023-02-08 15:20:18 +08:00
|
|
|
|
![heap_push_step4](heap.assets/heap_push_step4.png)
|
|
|
|
|
|
2023-02-22 00:57:04 +08:00
|
|
|
|
=== "<5>"
|
2023-02-08 15:20:18 +08:00
|
|
|
|
![heap_push_step5](heap.assets/heap_push_step5.png)
|
|
|
|
|
|
2023-02-22 00:57:04 +08:00
|
|
|
|
=== "<6>"
|
2023-02-08 15:20:18 +08:00
|
|
|
|
![heap_push_step6](heap.assets/heap_push_step6.png)
|
|
|
|
|
|
2023-04-10 03:12:02 +08:00
|
|
|
|
设节点总数为 $n$ ,则树的高度为 $O(\log n)$ 。由此可知,堆化操作的循环轮数最多为 $O(\log n)$ ,**元素入堆操作的时间复杂度为 $O(\log n)$** 。
|
2023-02-08 15:20:18 +08:00
|
|
|
|
|
|
|
|
|
=== "Java"
|
|
|
|
|
|
|
|
|
|
```java title="my_heap.java"
|
|
|
|
|
/* 元素入堆 */
|
|
|
|
|
void push(int val) {
|
2023-04-09 04:34:58 +08:00
|
|
|
|
// 添加节点
|
2023-02-08 15:20:18 +08:00
|
|
|
|
maxHeap.add(val);
|
|
|
|
|
// 从底至顶堆化
|
|
|
|
|
siftUp(size() - 1);
|
|
|
|
|
}
|
|
|
|
|
|
2023-04-09 04:34:58 +08:00
|
|
|
|
/* 从节点 i 开始,从底至顶堆化 */
|
2023-02-08 15:20:18 +08:00
|
|
|
|
void siftUp(int i) {
|
|
|
|
|
while (true) {
|
2023-04-09 04:34:58 +08:00
|
|
|
|
// 获取节点 i 的父节点
|
2023-02-08 15:20:18 +08:00
|
|
|
|
int p = parent(i);
|
2023-04-09 04:34:58 +08:00
|
|
|
|
// 当“越过根节点”或“节点无需修复”时,结束堆化
|
2023-02-08 15:20:18 +08:00
|
|
|
|
if (p < 0 || maxHeap.get(i) <= maxHeap.get(p))
|
|
|
|
|
break;
|
2023-04-09 04:34:58 +08:00
|
|
|
|
// 交换两节点
|
2023-02-08 15:20:18 +08:00
|
|
|
|
swap(i, p);
|
|
|
|
|
// 循环向上堆化
|
|
|
|
|
i = p;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
=== "C++"
|
|
|
|
|
|
|
|
|
|
```cpp title="my_heap.cpp"
|
|
|
|
|
/* 元素入堆 */
|
|
|
|
|
void push(int val) {
|
2023-04-09 04:34:58 +08:00
|
|
|
|
// 添加节点
|
2023-02-08 15:20:18 +08:00
|
|
|
|
maxHeap.push_back(val);
|
|
|
|
|
// 从底至顶堆化
|
|
|
|
|
siftUp(size() - 1);
|
|
|
|
|
}
|
|
|
|
|
|
2023-04-09 04:34:58 +08:00
|
|
|
|
/* 从节点 i 开始,从底至顶堆化 */
|
2023-02-08 15:20:18 +08:00
|
|
|
|
void siftUp(int i) {
|
|
|
|
|
while (true) {
|
2023-04-09 04:34:58 +08:00
|
|
|
|
// 获取节点 i 的父节点
|
2023-04-14 04:01:38 +08:00
|
|
|
|
int p = parent(i);
|
2023-04-09 04:34:58 +08:00
|
|
|
|
// 当“越过根节点”或“节点无需修复”时,结束堆化
|
2023-02-08 15:20:18 +08:00
|
|
|
|
if (p < 0 || maxHeap[i] <= maxHeap[p])
|
|
|
|
|
break;
|
2023-04-09 04:34:58 +08:00
|
|
|
|
// 交换两节点
|
2023-02-08 15:20:18 +08:00
|
|
|
|
swap(maxHeap[i], maxHeap[p]);
|
|
|
|
|
// 循环向上堆化
|
|
|
|
|
i = p;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
=== "Python"
|
|
|
|
|
|
|
|
|
|
```python title="my_heap.py"
|
2023-02-23 20:22:16 +08:00
|
|
|
|
def push(self, val: int):
|
2023-04-09 05:12:22 +08:00
|
|
|
|
"""元素入堆"""
|
2023-04-09 04:34:58 +08:00
|
|
|
|
# 添加节点
|
2023-02-23 20:22:16 +08:00
|
|
|
|
self.max_heap.append(val)
|
|
|
|
|
# 从底至顶堆化
|
|
|
|
|
self.sift_up(self.size() - 1)
|
|
|
|
|
|
|
|
|
|
def sift_up(self, i: int):
|
2023-04-09 05:12:22 +08:00
|
|
|
|
"""从节点 i 开始,从底至顶堆化"""
|
2023-02-23 20:22:16 +08:00
|
|
|
|
while True:
|
2023-04-09 04:34:58 +08:00
|
|
|
|
# 获取节点 i 的父节点
|
2023-02-23 20:22:16 +08:00
|
|
|
|
p = self.parent(i)
|
2023-04-09 04:34:58 +08:00
|
|
|
|
# 当“越过根节点”或“节点无需修复”时,结束堆化
|
2023-02-23 20:22:16 +08:00
|
|
|
|
if p < 0 or self.max_heap[i] <= self.max_heap[p]:
|
|
|
|
|
break
|
2023-04-09 04:34:58 +08:00
|
|
|
|
# 交换两节点
|
2023-02-23 20:22:16 +08:00
|
|
|
|
self.swap(i, p)
|
|
|
|
|
# 循环向上堆化
|
|
|
|
|
i = p
|
2023-02-08 15:20:18 +08:00
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
=== "Go"
|
|
|
|
|
|
|
|
|
|
```go title="my_heap.go"
|
|
|
|
|
/* 元素入堆 */
|
|
|
|
|
func (h *maxHeap) push(val any) {
|
2023-04-09 04:34:58 +08:00
|
|
|
|
// 添加节点
|
2023-02-08 15:20:18 +08:00
|
|
|
|
h.data = append(h.data, val)
|
|
|
|
|
// 从底至顶堆化
|
|
|
|
|
h.siftUp(len(h.data) - 1)
|
|
|
|
|
}
|
|
|
|
|
|
2023-04-09 04:34:58 +08:00
|
|
|
|
/* 从节点 i 开始,从底至顶堆化 */
|
2023-02-08 15:20:18 +08:00
|
|
|
|
func (h *maxHeap) siftUp(i int) {
|
|
|
|
|
for true {
|
2023-04-09 04:34:58 +08:00
|
|
|
|
// 获取节点 i 的父节点
|
2023-02-08 15:20:18 +08:00
|
|
|
|
p := h.parent(i)
|
2023-04-09 04:34:58 +08:00
|
|
|
|
// 当“越过根节点”或“节点无需修复”时,结束堆化
|
2023-02-08 15:20:18 +08:00
|
|
|
|
if p < 0 || h.data[i].(int) <= h.data[p].(int) {
|
|
|
|
|
break
|
|
|
|
|
}
|
2023-04-09 04:34:58 +08:00
|
|
|
|
// 交换两节点
|
2023-02-08 15:20:18 +08:00
|
|
|
|
h.swap(i, p)
|
|
|
|
|
// 循环向上堆化
|
|
|
|
|
i = p
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
=== "JavaScript"
|
|
|
|
|
|
|
|
|
|
```javascript title="my_heap.js"
|
|
|
|
|
/* 元素入堆 */
|
|
|
|
|
push(val) {
|
2023-04-09 04:34:58 +08:00
|
|
|
|
// 添加节点
|
2023-02-08 15:20:18 +08:00
|
|
|
|
this.#maxHeap.push(val);
|
|
|
|
|
// 从底至顶堆化
|
|
|
|
|
this.#siftUp(this.size() - 1);
|
|
|
|
|
}
|
|
|
|
|
|
2023-04-09 04:34:58 +08:00
|
|
|
|
/* 从节点 i 开始,从底至顶堆化 */
|
2023-02-08 15:20:18 +08:00
|
|
|
|
#siftUp(i) {
|
|
|
|
|
while (true) {
|
2023-04-09 04:34:58 +08:00
|
|
|
|
// 获取节点 i 的父节点
|
2023-02-08 15:20:18 +08:00
|
|
|
|
const p = this.#parent(i);
|
2023-04-09 04:34:58 +08:00
|
|
|
|
// 当“越过根节点”或“节点无需修复”时,结束堆化
|
2023-02-08 15:20:18 +08:00
|
|
|
|
if (p < 0 || this.#maxHeap[i] <= this.#maxHeap[p]) break;
|
2023-04-09 04:34:58 +08:00
|
|
|
|
// 交换两节点
|
2023-02-08 15:20:18 +08:00
|
|
|
|
this.#swap(i, p);
|
|
|
|
|
// 循环向上堆化
|
|
|
|
|
i = p;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
=== "TypeScript"
|
|
|
|
|
|
|
|
|
|
```typescript title="my_heap.ts"
|
|
|
|
|
/* 元素入堆 */
|
2023-02-08 16:47:52 +08:00
|
|
|
|
push(val: number): void {
|
2023-04-09 04:34:58 +08:00
|
|
|
|
// 添加节点
|
2023-02-08 15:20:18 +08:00
|
|
|
|
this.maxHeap.push(val);
|
|
|
|
|
// 从底至顶堆化
|
|
|
|
|
this.siftUp(this.size() - 1);
|
|
|
|
|
}
|
|
|
|
|
|
2023-04-09 04:34:58 +08:00
|
|
|
|
/* 从节点 i 开始,从底至顶堆化 */
|
2023-02-08 16:47:52 +08:00
|
|
|
|
siftUp(i: number): void {
|
2023-02-08 15:20:18 +08:00
|
|
|
|
while (true) {
|
2023-04-09 04:34:58 +08:00
|
|
|
|
// 获取节点 i 的父节点
|
2023-02-08 15:20:18 +08:00
|
|
|
|
const p = this.parent(i);
|
2023-04-09 04:34:58 +08:00
|
|
|
|
// 当“越过根节点”或“节点无需修复”时,结束堆化
|
2023-02-08 15:20:18 +08:00
|
|
|
|
if (p < 0 || this.maxHeap[i] <= this.maxHeap[p]) break;
|
2023-04-09 04:34:58 +08:00
|
|
|
|
// 交换两节点
|
2023-02-08 15:20:18 +08:00
|
|
|
|
this.swap(i, p);
|
|
|
|
|
// 循环向上堆化
|
|
|
|
|
i = p;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
=== "C"
|
|
|
|
|
|
|
|
|
|
```c title="my_heap.c"
|
2023-02-11 18:21:44 +08:00
|
|
|
|
[class]{maxHeap}-[func]{push}
|
2023-02-08 15:20:18 +08:00
|
|
|
|
|
2023-02-11 18:21:44 +08:00
|
|
|
|
[class]{maxHeap}-[func]{siftUp}
|
2023-02-08 15:20:18 +08:00
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
=== "C#"
|
|
|
|
|
|
|
|
|
|
```csharp title="my_heap.cs"
|
2023-02-15 21:48:22 +08:00
|
|
|
|
/* 元素入堆 */
|
|
|
|
|
void push(int val)
|
|
|
|
|
{
|
2023-04-09 04:34:58 +08:00
|
|
|
|
// 添加节点
|
2023-02-15 21:48:22 +08:00
|
|
|
|
maxHeap.Add(val);
|
|
|
|
|
// 从底至顶堆化
|
|
|
|
|
siftUp(size() - 1);
|
|
|
|
|
}
|
2023-02-08 15:20:18 +08:00
|
|
|
|
|
2023-04-09 04:34:58 +08:00
|
|
|
|
/* 从节点 i 开始,从底至顶堆化 */
|
2023-02-15 21:48:22 +08:00
|
|
|
|
void siftUp(int i)
|
|
|
|
|
{
|
|
|
|
|
while (true)
|
|
|
|
|
{
|
2023-04-09 04:34:58 +08:00
|
|
|
|
// 获取节点 i 的父节点
|
2023-02-15 21:48:22 +08:00
|
|
|
|
int p = parent(i);
|
2023-04-09 04:34:58 +08:00
|
|
|
|
// 若“越过根节点”或“节点无需修复”,则结束堆化
|
2023-02-15 21:48:22 +08:00
|
|
|
|
if (p < 0 || maxHeap[i] <= maxHeap[p])
|
|
|
|
|
break;
|
2023-04-09 04:34:58 +08:00
|
|
|
|
// 交换两节点
|
2023-02-15 21:48:22 +08:00
|
|
|
|
swap(i, p);
|
|
|
|
|
// 循环向上堆化
|
|
|
|
|
i = p;
|
|
|
|
|
}
|
|
|
|
|
}
|
2023-02-08 15:20:18 +08:00
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
=== "Swift"
|
|
|
|
|
|
|
|
|
|
```swift title="my_heap.swift"
|
|
|
|
|
/* 元素入堆 */
|
|
|
|
|
func push(val: Int) {
|
2023-04-09 04:34:58 +08:00
|
|
|
|
// 添加节点
|
2023-02-08 15:20:18 +08:00
|
|
|
|
maxHeap.append(val)
|
|
|
|
|
// 从底至顶堆化
|
|
|
|
|
siftUp(i: size() - 1)
|
|
|
|
|
}
|
|
|
|
|
|
2023-04-09 04:34:58 +08:00
|
|
|
|
/* 从节点 i 开始,从底至顶堆化 */
|
2023-02-08 15:20:18 +08:00
|
|
|
|
func siftUp(i: Int) {
|
|
|
|
|
var i = i
|
|
|
|
|
while true {
|
2023-04-09 04:34:58 +08:00
|
|
|
|
// 获取节点 i 的父节点
|
2023-02-08 15:20:18 +08:00
|
|
|
|
let p = parent(i: i)
|
2023-04-09 04:34:58 +08:00
|
|
|
|
// 当“越过根节点”或“节点无需修复”时,结束堆化
|
2023-02-08 15:20:18 +08:00
|
|
|
|
if p < 0 || maxHeap[i] <= maxHeap[p] {
|
|
|
|
|
break
|
|
|
|
|
}
|
2023-04-09 04:34:58 +08:00
|
|
|
|
// 交换两节点
|
2023-02-08 15:20:18 +08:00
|
|
|
|
swap(i: i, j: p)
|
|
|
|
|
// 循环向上堆化
|
|
|
|
|
i = p
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
=== "Zig"
|
|
|
|
|
|
|
|
|
|
```zig title="my_heap.zig"
|
2023-02-09 22:55:29 +08:00
|
|
|
|
// 元素入堆
|
|
|
|
|
fn push(self: *Self, val: T) !void {
|
2023-04-09 04:34:58 +08:00
|
|
|
|
// 添加节点
|
2023-03-15 03:12:57 +08:00
|
|
|
|
try self.max_heap.?.append(val);
|
2023-02-09 22:55:29 +08:00
|
|
|
|
// 从底至顶堆化
|
|
|
|
|
try self.siftUp(self.size() - 1);
|
|
|
|
|
}
|
2023-02-08 15:20:18 +08:00
|
|
|
|
|
2023-04-09 04:34:58 +08:00
|
|
|
|
// 从节点 i 开始,从底至顶堆化
|
2023-02-09 22:55:29 +08:00
|
|
|
|
fn siftUp(self: *Self, i_: usize) !void {
|
|
|
|
|
var i = i_;
|
|
|
|
|
while (true) {
|
2023-04-09 04:34:58 +08:00
|
|
|
|
// 获取节点 i 的父节点
|
2023-02-09 22:55:29 +08:00
|
|
|
|
var p = parent(i);
|
2023-04-09 04:34:58 +08:00
|
|
|
|
// 当“越过根节点”或“节点无需修复”时,结束堆化
|
2023-03-15 03:12:57 +08:00
|
|
|
|
if (p < 0 or self.max_heap.?.items[i] <= self.max_heap.?.items[p]) break;
|
2023-04-09 04:34:58 +08:00
|
|
|
|
// 交换两节点
|
2023-02-09 22:55:29 +08:00
|
|
|
|
try self.swap(i, p);
|
|
|
|
|
// 循环向上堆化
|
|
|
|
|
i = p;
|
|
|
|
|
}
|
|
|
|
|
}
|
2023-02-08 15:20:18 +08:00
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
### 堆顶元素出堆
|
|
|
|
|
|
2023-04-10 03:12:02 +08:00
|
|
|
|
堆顶元素是二叉树的根节点,即列表首元素。如果我们直接从列表中删除首元素,那么二叉树中所有节点的索引都会发生变化,这将使得后续使用堆化修复变得困难。为了尽量减少元素索引的变动,我们采取以下操作步骤:
|
2023-02-08 15:20:18 +08:00
|
|
|
|
|
2023-04-09 04:34:58 +08:00
|
|
|
|
1. 交换堆顶元素与堆底元素(即交换根节点与最右叶节点);
|
2023-04-10 03:12:02 +08:00
|
|
|
|
2. 交换完成后,将堆底从列表中删除(注意,由于已经交换,实际上删除的是原来的堆顶元素);
|
2023-04-09 04:34:58 +08:00
|
|
|
|
3. 从根节点开始,**从顶至底执行堆化**;
|
2023-02-08 15:20:18 +08:00
|
|
|
|
|
2023-04-10 03:12:02 +08:00
|
|
|
|
顾名思义,**从顶至底堆化的操作方向与从底至顶堆化相反**,我们将根节点的值与其两个子节点的值进行比较,将最大的子节点与根节点交换;然后循环执行此操作,直到越过叶节点或遇到无需交换的节点时结束。
|
2023-02-08 15:20:18 +08:00
|
|
|
|
|
2023-02-22 00:57:04 +08:00
|
|
|
|
=== "<1>"
|
2023-03-13 22:40:57 +08:00
|
|
|
|
![堆顶元素出堆步骤](heap.assets/heap_pop_step1.png)
|
2023-02-08 15:20:18 +08:00
|
|
|
|
|
2023-02-22 00:57:04 +08:00
|
|
|
|
=== "<2>"
|
2023-03-13 22:40:57 +08:00
|
|
|
|
![heap_pop_step2](heap.assets/heap_pop_step2.png)
|
2023-02-08 15:20:18 +08:00
|
|
|
|
|
2023-02-22 00:57:04 +08:00
|
|
|
|
=== "<3>"
|
2023-03-13 22:40:57 +08:00
|
|
|
|
![heap_pop_step3](heap.assets/heap_pop_step3.png)
|
2023-02-08 15:20:18 +08:00
|
|
|
|
|
2023-02-22 00:57:04 +08:00
|
|
|
|
=== "<4>"
|
2023-03-13 22:40:57 +08:00
|
|
|
|
![heap_pop_step4](heap.assets/heap_pop_step4.png)
|
2023-02-08 15:20:18 +08:00
|
|
|
|
|
2023-02-22 00:57:04 +08:00
|
|
|
|
=== "<5>"
|
2023-03-13 22:40:57 +08:00
|
|
|
|
![heap_pop_step5](heap.assets/heap_pop_step5.png)
|
2023-02-08 15:20:18 +08:00
|
|
|
|
|
2023-02-22 00:57:04 +08:00
|
|
|
|
=== "<6>"
|
2023-03-13 22:40:57 +08:00
|
|
|
|
![heap_pop_step6](heap.assets/heap_pop_step6.png)
|
2023-02-08 15:20:18 +08:00
|
|
|
|
|
2023-02-22 00:57:04 +08:00
|
|
|
|
=== "<7>"
|
2023-03-13 22:40:57 +08:00
|
|
|
|
![heap_pop_step7](heap.assets/heap_pop_step7.png)
|
2023-02-08 15:20:18 +08:00
|
|
|
|
|
2023-02-22 00:57:04 +08:00
|
|
|
|
=== "<8>"
|
2023-03-13 22:40:57 +08:00
|
|
|
|
![heap_pop_step8](heap.assets/heap_pop_step8.png)
|
2023-02-08 15:20:18 +08:00
|
|
|
|
|
2023-02-22 00:57:04 +08:00
|
|
|
|
=== "<9>"
|
2023-03-13 22:40:57 +08:00
|
|
|
|
![heap_pop_step9](heap.assets/heap_pop_step9.png)
|
2023-02-08 15:20:18 +08:00
|
|
|
|
|
2023-02-22 00:57:04 +08:00
|
|
|
|
=== "<10>"
|
2023-03-13 22:40:57 +08:00
|
|
|
|
![heap_pop_step10](heap.assets/heap_pop_step10.png)
|
2023-02-08 15:20:18 +08:00
|
|
|
|
|
2023-04-10 03:12:02 +08:00
|
|
|
|
与元素入堆操作相似,堆顶元素出堆操作的时间复杂度也为 $O(\log n)$ 。
|
2023-02-08 15:20:18 +08:00
|
|
|
|
|
|
|
|
|
=== "Java"
|
|
|
|
|
|
|
|
|
|
```java title="my_heap.java"
|
|
|
|
|
/* 元素出堆 */
|
2023-03-13 22:40:57 +08:00
|
|
|
|
int pop() {
|
2023-02-08 15:20:18 +08:00
|
|
|
|
// 判空处理
|
|
|
|
|
if (isEmpty())
|
|
|
|
|
throw new EmptyStackException();
|
2023-04-09 04:34:58 +08:00
|
|
|
|
// 交换根节点与最右叶节点(即交换首元素与尾元素)
|
2023-02-08 15:20:18 +08:00
|
|
|
|
swap(0, size() - 1);
|
2023-04-09 04:34:58 +08:00
|
|
|
|
// 删除节点
|
2023-02-08 15:20:18 +08:00
|
|
|
|
int val = maxHeap.remove(size() - 1);
|
|
|
|
|
// 从顶至底堆化
|
|
|
|
|
siftDown(0);
|
|
|
|
|
// 返回堆顶元素
|
|
|
|
|
return val;
|
|
|
|
|
}
|
|
|
|
|
|
2023-04-09 04:34:58 +08:00
|
|
|
|
/* 从节点 i 开始,从顶至底堆化 */
|
2023-02-08 15:20:18 +08:00
|
|
|
|
void siftDown(int i) {
|
|
|
|
|
while (true) {
|
2023-04-09 04:34:58 +08:00
|
|
|
|
// 判断节点 i, l, r 中值最大的节点,记为 ma
|
2023-02-08 15:20:18 +08:00
|
|
|
|
int l = left(i), r = right(i), ma = i;
|
|
|
|
|
if (l < size() && maxHeap.get(l) > maxHeap.get(ma))
|
|
|
|
|
ma = l;
|
|
|
|
|
if (r < size() && maxHeap.get(r) > maxHeap.get(ma))
|
|
|
|
|
ma = r;
|
2023-04-09 04:34:58 +08:00
|
|
|
|
// 若节点 i 最大或索引 l, r 越界,则无需继续堆化,跳出
|
2023-04-14 04:01:38 +08:00
|
|
|
|
if (ma == i)
|
|
|
|
|
break;
|
2023-04-09 04:34:58 +08:00
|
|
|
|
// 交换两节点
|
2023-02-08 15:20:18 +08:00
|
|
|
|
swap(i, ma);
|
|
|
|
|
// 循环向下堆化
|
|
|
|
|
i = ma;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
=== "C++"
|
|
|
|
|
|
|
|
|
|
```cpp title="my_heap.cpp"
|
|
|
|
|
/* 元素出堆 */
|
2023-03-13 22:40:57 +08:00
|
|
|
|
void pop() {
|
2023-02-08 15:20:18 +08:00
|
|
|
|
// 判空处理
|
|
|
|
|
if (empty()) {
|
2023-02-11 18:21:44 +08:00
|
|
|
|
throw out_of_range("堆为空");
|
2023-02-08 15:20:18 +08:00
|
|
|
|
}
|
2023-04-09 04:34:58 +08:00
|
|
|
|
// 交换根节点与最右叶节点(即交换首元素与尾元素)
|
2023-02-08 15:20:18 +08:00
|
|
|
|
swap(maxHeap[0], maxHeap[size() - 1]);
|
2023-04-09 04:34:58 +08:00
|
|
|
|
// 删除节点
|
2023-02-08 15:20:18 +08:00
|
|
|
|
maxHeap.pop_back();
|
|
|
|
|
// 从顶至底堆化
|
|
|
|
|
siftDown(0);
|
|
|
|
|
}
|
|
|
|
|
|
2023-04-09 04:34:58 +08:00
|
|
|
|
/* 从节点 i 开始,从顶至底堆化 */
|
2023-02-08 15:20:18 +08:00
|
|
|
|
void siftDown(int i) {
|
|
|
|
|
while (true) {
|
2023-04-09 04:34:58 +08:00
|
|
|
|
// 判断节点 i, l, r 中值最大的节点,记为 ma
|
2023-02-08 15:20:18 +08:00
|
|
|
|
int l = left(i), r = right(i), ma = i;
|
2023-04-09 04:34:58 +08:00
|
|
|
|
// 若节点 i 最大或索引 l, r 越界,则无需继续堆化,跳出
|
2023-04-14 04:01:38 +08:00
|
|
|
|
if (l < size() && maxHeap[l] > maxHeap[ma])
|
2023-02-08 15:20:18 +08:00
|
|
|
|
ma = l;
|
|
|
|
|
if (r < size() && maxHeap[r] > maxHeap[ma])
|
|
|
|
|
ma = r;
|
2023-04-09 04:34:58 +08:00
|
|
|
|
// 若节点 i 最大或索引 l, r 越界,则无需继续堆化,跳出
|
2023-04-14 04:01:38 +08:00
|
|
|
|
if (ma == i)
|
2023-02-08 15:20:18 +08:00
|
|
|
|
break;
|
|
|
|
|
swap(maxHeap[i], maxHeap[ma]);
|
|
|
|
|
// 循环向下堆化
|
|
|
|
|
i = ma;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
=== "Python"
|
|
|
|
|
|
|
|
|
|
```python title="my_heap.py"
|
2023-03-13 22:40:57 +08:00
|
|
|
|
def pop(self) -> int:
|
2023-04-09 05:12:22 +08:00
|
|
|
|
"""元素出堆"""
|
2023-02-23 20:22:16 +08:00
|
|
|
|
# 判空处理
|
|
|
|
|
assert not self.is_empty()
|
2023-04-09 04:34:58 +08:00
|
|
|
|
# 交换根节点与最右叶节点(即交换首元素与尾元素)
|
2023-02-23 20:22:16 +08:00
|
|
|
|
self.swap(0, self.size() - 1)
|
2023-04-09 04:34:58 +08:00
|
|
|
|
# 删除节点
|
2023-02-23 20:22:16 +08:00
|
|
|
|
val = self.max_heap.pop()
|
|
|
|
|
# 从顶至底堆化
|
|
|
|
|
self.sift_down(0)
|
|
|
|
|
# 返回堆顶元素
|
|
|
|
|
return val
|
2023-02-08 15:20:18 +08:00
|
|
|
|
|
2023-02-23 20:22:16 +08:00
|
|
|
|
def sift_down(self, i: int):
|
2023-04-09 05:12:22 +08:00
|
|
|
|
"""从节点 i 开始,从顶至底堆化"""
|
2023-02-23 20:22:16 +08:00
|
|
|
|
while True:
|
2023-04-09 04:34:58 +08:00
|
|
|
|
# 判断节点 i, l, r 中值最大的节点,记为 ma
|
2023-02-23 20:22:16 +08:00
|
|
|
|
l, r, ma = self.left(i), self.right(i), i
|
|
|
|
|
if l < self.size() and self.max_heap[l] > self.max_heap[ma]:
|
|
|
|
|
ma = l
|
|
|
|
|
if r < self.size() and self.max_heap[r] > self.max_heap[ma]:
|
|
|
|
|
ma = r
|
2023-04-09 04:34:58 +08:00
|
|
|
|
# 若节点 i 最大或索引 l, r 越界,则无需继续堆化,跳出
|
2023-02-23 20:22:16 +08:00
|
|
|
|
if ma == i:
|
|
|
|
|
break
|
2023-04-09 04:34:58 +08:00
|
|
|
|
# 交换两节点
|
2023-02-23 20:22:16 +08:00
|
|
|
|
self.swap(i, ma)
|
|
|
|
|
# 循环向下堆化
|
|
|
|
|
i = ma
|
2023-02-08 15:20:18 +08:00
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
=== "Go"
|
|
|
|
|
|
|
|
|
|
```go title="my_heap.go"
|
|
|
|
|
/* 元素出堆 */
|
2023-03-13 22:40:57 +08:00
|
|
|
|
func (h *maxHeap) pop() any {
|
2023-02-08 15:20:18 +08:00
|
|
|
|
// 判空处理
|
|
|
|
|
if h.isEmpty() {
|
|
|
|
|
fmt.Println("error")
|
|
|
|
|
return nil
|
|
|
|
|
}
|
2023-04-09 04:34:58 +08:00
|
|
|
|
// 交换根节点与最右叶节点(即交换首元素与尾元素)
|
2023-02-08 15:20:18 +08:00
|
|
|
|
h.swap(0, h.size()-1)
|
2023-04-09 04:34:58 +08:00
|
|
|
|
// 删除节点
|
2023-02-08 15:20:18 +08:00
|
|
|
|
val := h.data[len(h.data)-1]
|
|
|
|
|
h.data = h.data[:len(h.data)-1]
|
|
|
|
|
// 从顶至底堆化
|
|
|
|
|
h.siftDown(0)
|
|
|
|
|
|
|
|
|
|
// 返回堆顶元素
|
|
|
|
|
return val
|
|
|
|
|
}
|
|
|
|
|
|
2023-04-09 04:34:58 +08:00
|
|
|
|
/* 从节点 i 开始,从顶至底堆化 */
|
2023-02-08 15:20:18 +08:00
|
|
|
|
func (h *maxHeap) siftDown(i int) {
|
|
|
|
|
for true {
|
2023-04-09 04:34:58 +08:00
|
|
|
|
// 判断节点 i, l, r 中值最大的节点,记为 max
|
2023-02-08 15:20:18 +08:00
|
|
|
|
l, r, max := h.left(i), h.right(i), i
|
|
|
|
|
if l < h.size() && h.data[l].(int) > h.data[max].(int) {
|
|
|
|
|
max = l
|
|
|
|
|
}
|
|
|
|
|
if r < h.size() && h.data[r].(int) > h.data[max].(int) {
|
|
|
|
|
max = r
|
|
|
|
|
}
|
2023-04-09 04:34:58 +08:00
|
|
|
|
// 若节点 i 最大或索引 l, r 越界,则无需继续堆化,跳出
|
2023-02-08 15:20:18 +08:00
|
|
|
|
if max == i {
|
|
|
|
|
break
|
|
|
|
|
}
|
2023-04-09 04:34:58 +08:00
|
|
|
|
// 交换两节点
|
2023-02-08 15:20:18 +08:00
|
|
|
|
h.swap(i, max)
|
|
|
|
|
// 循环向下堆化
|
|
|
|
|
i = max
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
=== "JavaScript"
|
|
|
|
|
|
|
|
|
|
```javascript title="my_heap.js"
|
|
|
|
|
/* 元素出堆 */
|
2023-03-13 22:40:57 +08:00
|
|
|
|
pop() {
|
2023-02-08 15:20:18 +08:00
|
|
|
|
// 判空处理
|
2023-04-17 21:57:42 +08:00
|
|
|
|
if (this.isEmpty()) throw new Error('堆为空');
|
2023-04-09 04:34:58 +08:00
|
|
|
|
// 交换根节点与最右叶节点(即交换首元素与尾元素)
|
2023-02-08 15:20:18 +08:00
|
|
|
|
this.#swap(0, this.size() - 1);
|
2023-04-09 04:34:58 +08:00
|
|
|
|
// 删除节点
|
2023-02-08 15:20:18 +08:00
|
|
|
|
const val = this.#maxHeap.pop();
|
|
|
|
|
// 从顶至底堆化
|
|
|
|
|
this.#siftDown(0);
|
|
|
|
|
// 返回堆顶元素
|
|
|
|
|
return val;
|
|
|
|
|
}
|
|
|
|
|
|
2023-04-09 04:34:58 +08:00
|
|
|
|
/* 从节点 i 开始,从顶至底堆化 */
|
2023-02-08 15:20:18 +08:00
|
|
|
|
#siftDown(i) {
|
|
|
|
|
while (true) {
|
2023-04-09 04:34:58 +08:00
|
|
|
|
// 判断节点 i, l, r 中值最大的节点,记为 ma
|
2023-02-08 15:20:18 +08:00
|
|
|
|
const l = this.#left(i),
|
|
|
|
|
r = this.#right(i);
|
|
|
|
|
let ma = i;
|
|
|
|
|
if (l < this.size() && this.#maxHeap[l] > this.#maxHeap[ma]) ma = l;
|
|
|
|
|
if (r < this.size() && this.#maxHeap[r] > this.#maxHeap[ma]) ma = r;
|
2023-04-09 04:34:58 +08:00
|
|
|
|
// 若节点 i 最大或索引 l, r 越界,则无需继续堆化,跳出
|
2023-02-08 15:20:18 +08:00
|
|
|
|
if (ma == i) break;
|
2023-04-09 04:34:58 +08:00
|
|
|
|
// 交换两节点
|
2023-02-08 15:20:18 +08:00
|
|
|
|
this.#swap(i, ma);
|
|
|
|
|
// 循环向下堆化
|
|
|
|
|
i = ma;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
=== "TypeScript"
|
|
|
|
|
|
|
|
|
|
```typescript title="my_heap.ts"
|
|
|
|
|
/* 元素出堆 */
|
2023-03-13 22:40:57 +08:00
|
|
|
|
pop(): number {
|
2023-02-08 15:20:18 +08:00
|
|
|
|
// 判空处理
|
2023-03-03 02:46:12 +08:00
|
|
|
|
if (this.isEmpty()) throw new RangeError('Heap is empty.');
|
2023-04-09 04:34:58 +08:00
|
|
|
|
// 交换根节点与最右叶节点(即交换首元素与尾元素)
|
2023-02-08 15:20:18 +08:00
|
|
|
|
this.swap(0, this.size() - 1);
|
2023-04-09 04:34:58 +08:00
|
|
|
|
// 删除节点
|
2023-02-08 15:20:18 +08:00
|
|
|
|
const val = this.maxHeap.pop();
|
|
|
|
|
// 从顶至底堆化
|
|
|
|
|
this.siftDown(0);
|
|
|
|
|
// 返回堆顶元素
|
|
|
|
|
return val;
|
|
|
|
|
}
|
|
|
|
|
|
2023-04-09 04:34:58 +08:00
|
|
|
|
/* 从节点 i 开始,从顶至底堆化 */
|
2023-02-08 16:47:52 +08:00
|
|
|
|
siftDown(i: number): void {
|
2023-02-08 15:20:18 +08:00
|
|
|
|
while (true) {
|
2023-04-09 04:34:58 +08:00
|
|
|
|
// 判断节点 i, l, r 中值最大的节点,记为 ma
|
2023-03-03 02:46:12 +08:00
|
|
|
|
const l = this.left(i),
|
|
|
|
|
r = this.right(i);
|
2023-02-08 15:20:18 +08:00
|
|
|
|
let ma = i;
|
|
|
|
|
if (l < this.size() && this.maxHeap[l] > this.maxHeap[ma]) ma = l;
|
|
|
|
|
if (r < this.size() && this.maxHeap[r] > this.maxHeap[ma]) ma = r;
|
2023-04-09 04:34:58 +08:00
|
|
|
|
// 若节点 i 最大或索引 l, r 越界,则无需继续堆化,跳出
|
2023-02-08 15:20:18 +08:00
|
|
|
|
if (ma == i) break;
|
2023-04-09 04:34:58 +08:00
|
|
|
|
// 交换两节点
|
2023-02-08 15:20:18 +08:00
|
|
|
|
this.swap(i, ma);
|
|
|
|
|
// 循环向下堆化
|
|
|
|
|
i = ma;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
=== "C"
|
|
|
|
|
|
|
|
|
|
```c title="my_heap.c"
|
2023-03-13 22:40:57 +08:00
|
|
|
|
[class]{maxHeap}-[func]{pop}
|
2023-02-08 15:20:18 +08:00
|
|
|
|
|
2023-02-11 18:21:44 +08:00
|
|
|
|
[class]{maxHeap}-[func]{siftDown}
|
2023-02-08 15:20:18 +08:00
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
=== "C#"
|
|
|
|
|
|
|
|
|
|
```csharp title="my_heap.cs"
|
2023-02-15 21:48:22 +08:00
|
|
|
|
/* 元素出堆 */
|
2023-03-13 22:40:57 +08:00
|
|
|
|
int pop()
|
2023-02-15 21:48:22 +08:00
|
|
|
|
{
|
|
|
|
|
// 判空处理
|
|
|
|
|
if (isEmpty())
|
|
|
|
|
throw new IndexOutOfRangeException();
|
2023-04-09 04:34:58 +08:00
|
|
|
|
// 交换根节点与最右叶节点(即交换首元素与尾元素)
|
2023-02-15 21:48:22 +08:00
|
|
|
|
swap(0, size() - 1);
|
2023-04-09 04:34:58 +08:00
|
|
|
|
// 删除节点
|
2023-02-15 21:48:22 +08:00
|
|
|
|
int val = maxHeap.Last();
|
|
|
|
|
maxHeap.RemoveAt(size() - 1);
|
|
|
|
|
// 从顶至底堆化
|
|
|
|
|
siftDown(0);
|
|
|
|
|
// 返回堆顶元素
|
|
|
|
|
return val;
|
|
|
|
|
}
|
2023-02-08 15:20:18 +08:00
|
|
|
|
|
2023-04-09 04:34:58 +08:00
|
|
|
|
/* 从节点 i 开始,从顶至底堆化 */
|
2023-02-15 21:48:22 +08:00
|
|
|
|
void siftDown(int i)
|
|
|
|
|
{
|
|
|
|
|
while (true)
|
|
|
|
|
{
|
2023-04-09 04:34:58 +08:00
|
|
|
|
// 判断节点 i, l, r 中值最大的节点,记为 ma
|
2023-02-15 21:48:22 +08:00
|
|
|
|
int l = left(i), r = right(i), ma = i;
|
|
|
|
|
if (l < size() && maxHeap[l] > maxHeap[ma])
|
|
|
|
|
ma = l;
|
|
|
|
|
if (r < size() && maxHeap[r] > maxHeap[ma])
|
|
|
|
|
ma = r;
|
2023-04-09 04:34:58 +08:00
|
|
|
|
// 若“节点 i 最大”或“越过叶节点”,则结束堆化
|
2023-02-15 21:48:22 +08:00
|
|
|
|
if (ma == i) break;
|
2023-04-09 04:34:58 +08:00
|
|
|
|
// 交换两节点
|
2023-02-15 21:48:22 +08:00
|
|
|
|
swap(i, ma);
|
|
|
|
|
// 循环向下堆化
|
|
|
|
|
i = ma;
|
|
|
|
|
}
|
|
|
|
|
}
|
2023-02-08 15:20:18 +08:00
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
=== "Swift"
|
|
|
|
|
|
|
|
|
|
```swift title="my_heap.swift"
|
|
|
|
|
/* 元素出堆 */
|
2023-03-13 22:40:57 +08:00
|
|
|
|
func pop() -> Int {
|
2023-02-08 15:20:18 +08:00
|
|
|
|
// 判空处理
|
|
|
|
|
if isEmpty() {
|
|
|
|
|
fatalError("堆为空")
|
|
|
|
|
}
|
2023-04-09 04:34:58 +08:00
|
|
|
|
// 交换根节点与最右叶节点(即交换首元素与尾元素)
|
2023-02-08 15:20:18 +08:00
|
|
|
|
swap(i: 0, j: size() - 1)
|
2023-04-09 04:34:58 +08:00
|
|
|
|
// 删除节点
|
2023-02-08 15:20:18 +08:00
|
|
|
|
let val = maxHeap.remove(at: size() - 1)
|
|
|
|
|
// 从顶至底堆化
|
|
|
|
|
siftDown(i: 0)
|
|
|
|
|
// 返回堆顶元素
|
|
|
|
|
return val
|
|
|
|
|
}
|
|
|
|
|
|
2023-04-09 04:34:58 +08:00
|
|
|
|
/* 从节点 i 开始,从顶至底堆化 */
|
2023-02-08 15:20:18 +08:00
|
|
|
|
func siftDown(i: Int) {
|
|
|
|
|
var i = i
|
|
|
|
|
while true {
|
2023-04-09 04:34:58 +08:00
|
|
|
|
// 判断节点 i, l, r 中值最大的节点,记为 ma
|
2023-02-08 15:20:18 +08:00
|
|
|
|
let l = left(i: i)
|
|
|
|
|
let r = right(i: i)
|
|
|
|
|
var ma = i
|
|
|
|
|
if l < size(), maxHeap[l] > maxHeap[ma] {
|
|
|
|
|
ma = l
|
|
|
|
|
}
|
|
|
|
|
if r < size(), maxHeap[r] > maxHeap[ma] {
|
|
|
|
|
ma = r
|
|
|
|
|
}
|
2023-04-09 04:34:58 +08:00
|
|
|
|
// 若节点 i 最大或索引 l, r 越界,则无需继续堆化,跳出
|
2023-02-08 15:20:18 +08:00
|
|
|
|
if ma == i {
|
|
|
|
|
break
|
|
|
|
|
}
|
2023-04-09 04:34:58 +08:00
|
|
|
|
// 交换两节点
|
2023-02-08 15:20:18 +08:00
|
|
|
|
swap(i: i, j: ma)
|
|
|
|
|
// 循环向下堆化
|
|
|
|
|
i = ma
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
=== "Zig"
|
|
|
|
|
|
|
|
|
|
```zig title="my_heap.zig"
|
2023-02-09 22:55:29 +08:00
|
|
|
|
// 元素出堆
|
2023-03-13 22:40:57 +08:00
|
|
|
|
fn pop(self: *Self) !T {
|
2023-02-09 22:55:29 +08:00
|
|
|
|
// 判断处理
|
|
|
|
|
if (self.isEmpty()) unreachable;
|
2023-04-09 04:34:58 +08:00
|
|
|
|
// 交换根节点与最右叶节点(即交换首元素与尾元素)
|
2023-02-09 22:55:29 +08:00
|
|
|
|
try self.swap(0, self.size() - 1);
|
2023-04-09 04:34:58 +08:00
|
|
|
|
// 删除节点
|
2023-03-15 03:12:57 +08:00
|
|
|
|
var val = self.max_heap.?.pop();
|
2023-02-09 22:55:29 +08:00
|
|
|
|
// 从顶至底堆化
|
|
|
|
|
try self.siftDown(0);
|
|
|
|
|
// 返回堆顶元素
|
|
|
|
|
return val;
|
|
|
|
|
}
|
2023-02-08 15:20:18 +08:00
|
|
|
|
|
2023-04-09 04:34:58 +08:00
|
|
|
|
// 从节点 i 开始,从顶至底堆化
|
2023-02-09 22:55:29 +08:00
|
|
|
|
fn siftDown(self: *Self, i_: usize) !void {
|
|
|
|
|
var i = i_;
|
|
|
|
|
while (true) {
|
2023-04-09 04:34:58 +08:00
|
|
|
|
// 判断节点 i, l, r 中值最大的节点,记为 ma
|
2023-02-09 22:55:29 +08:00
|
|
|
|
var l = left(i);
|
|
|
|
|
var r = right(i);
|
|
|
|
|
var ma = i;
|
2023-03-15 03:12:57 +08:00
|
|
|
|
if (l < self.size() and self.max_heap.?.items[l] > self.max_heap.?.items[ma]) ma = l;
|
|
|
|
|
if (r < self.size() and self.max_heap.?.items[r] > self.max_heap.?.items[ma]) ma = r;
|
2023-04-09 04:34:58 +08:00
|
|
|
|
// 若节点 i 最大或索引 l, r 越界,则无需继续堆化,跳出
|
2023-02-09 22:55:29 +08:00
|
|
|
|
if (ma == i) break;
|
2023-04-09 04:34:58 +08:00
|
|
|
|
// 交换两节点
|
2023-02-09 22:55:29 +08:00
|
|
|
|
try self.swap(i, ma);
|
|
|
|
|
// 循环向下堆化
|
|
|
|
|
i = ma;
|
|
|
|
|
}
|
|
|
|
|
}
|
2023-02-08 15:20:18 +08:00
|
|
|
|
```
|
|
|
|
|
|
2023-04-17 18:24:20 +08:00
|
|
|
|
## 9.1.3. 堆常见应用
|
2023-02-08 15:20:18 +08:00
|
|
|
|
|
2023-04-10 03:12:02 +08:00
|
|
|
|
- **优先队列**:堆通常作为实现优先队列的首选数据结构,其入队和出队操作的时间复杂度均为 $O(\log n)$ ,而建队操作为 $O(n)$ ,这些操作都非常高效。
|
|
|
|
|
- **堆排序**:给定一组数据,我们可以用它们建立一个堆,然后依次将所有元素弹出,从而得到一个有序序列。当然,堆排序的实现方法并不需要弹出元素,而是每轮将堆顶元素交换至数组尾部并缩小堆的长度。
|
|
|
|
|
- **获取最大的 $k$ 个元素**:这是一个经典的算法问题,同时也是一种典型应用,例如选择热度前 10 的新闻作为微博热搜,选取销量前 10 的商品等。
|