This commit is contained in:
krahets
2023-02-23 20:22:16 +08:00
parent 8abc6ce1d5
commit b6ebc67824
3 changed files with 327 additions and 15 deletions

View File

@@ -52,7 +52,7 @@ comments: true
// 初始化小顶堆
Queue<Integer> minHeap = new PriorityQueue<>();
// 初始化大顶堆(使用 lambda 表达式修改 Comparator 即可)
Queue<Integer> maxHeap = new PriorityQueue<>((a, b) -> { return b - a; });
Queue<Integer> maxHeap = new PriorityQueue<>((a, b) -> b - a);
/* 元素入堆 */
maxHeap.add(1);
@@ -123,7 +123,41 @@ comments: true
=== "Python"
```python title="heap.py"
# 初始化小顶堆
min_heap, flag = [], 1
# 初始化大顶堆
max_heap, flag = [], -1
# Python 的 heapq 模块默认实现小顶堆
# 考虑将“元素取负”后再入堆,这样就可以将大小关系颠倒,从而实现大顶堆
# 在本示例中flag = 1 时对应小顶堆flag = -1 时对应大顶堆
""" 元素入堆 """
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)
""" 获取堆顶元素 """
peek = flag * max_heap[0] # 5
""" 堆顶元素出堆 """
# 出堆元素会形成一个从大到小的序列
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
""" 获取堆大小 """
size = len(max_heap)
""" 判断堆是否为空 """
is_empty = not max_heap
""" 输入列表并建堆 """
min_heap = [1, 3, 2, 5, 4]
heapq.heapify(min_heap)
```
=== "Go"
@@ -329,7 +363,17 @@ comments: true
=== "Python"
```python title="my_heap.py"
""" 获取左子结点索引 """
def left(self, i: int) -> int:
return 2 * i + 1
""" 获取右子结点索引 """
def right(self, i: int) -> int:
return 2 * i + 2
""" 获取父结点索引 """
def parent(self, i: int) -> int:
return (i - 1) // 2 # 向下整除
```
=== "Go"
@@ -486,7 +530,9 @@ comments: true
=== "Python"
```python title="my_heap.py"
""" 访问堆顶元素 """
def peek(self) -> int:
return self.max_heap[0]
```
=== "Go"
@@ -633,7 +679,25 @@ comments: true
=== "Python"
```python title="my_heap.py"
""" 元素入堆 """
def push(self, val: int):
# 添加结点
self.max_heap.append(val)
# 从底至顶堆化
self.sift_up(self.size() - 1)
""" 从结点 i 开始,从底至顶堆化 """
def sift_up(self, i: int):
while True:
# 获取结点 i 的父结点
p = self.parent(i)
# 当“越过根结点”或“结点无需修复”时,结束堆化
if p < 0 or self.max_heap[i] <= self.max_heap[p]:
break
# 交换两结点
self.swap(i, p)
# 循环向上堆化
i = p
```
=== "Go"
@@ -929,7 +993,35 @@ comments: true
=== "Python"
```python title="my_heap.py"
""" 元素出堆 """
def poll(self) -> int:
# 判空处理
assert not self.is_empty()
# 交换根结点与最右叶结点(即交换首元素与尾元素)
self.swap(0, self.size() - 1)
# 删除结点
val = self.max_heap.pop()
# 从顶至底堆化
self.sift_down(0)
# 返回堆顶元素
return val
""" 从结点 i 开始,从顶至底堆化 """
def sift_down(self, i: int):
while True:
# 判断结点 i, l, r 中值最大的结点,记为 ma
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
# 若结点 i 最大或索引 l, r 越界,则无需继续堆化,跳出
if ma == i:
break
# 交换两结点
self.swap(i, ma)
# 循环向下堆化
i = ma
```
=== "Go"
@@ -1216,7 +1308,13 @@ comments: true
=== "Python"
```python title="my_heap.py"
""" 构造方法 """
def __init__(self, nums: List[int]):
# 将列表元素原封不动添加进堆
self.max_heap = nums
# 堆化除叶结点以外的其他所有结点
for i in range(self.parent(self.size() - 1), -1, -1):
self.sift_down(i)
```
=== "Go"