Python内置堆是指在Python标准库中提供的heapq模块,它利用heapq算法来实现最小堆。堆是二叉树的一种特殊形式,分为最大堆和最小堆,最小堆的特点是父节点的值小于或等于左右子节点的值。Python内置堆通过不断调整节点的顺序,使得根节点的值永远是堆中的最小值。
具体实现过程如下:
- 创建一个空列表作为堆。
heap = []
- 使用heapq库的函数heappush将元素插入堆中,该函数会根据元素大小进行排序。
import heapq
heap = []
heapq.heappush(heap, 3)
heapq.heappush(heap, 1)
heapq.heappush(heap, 2)
- 使用heapq库的函数heapq.heappop从堆中取出最小元素。
import heapq
heap = []
heapq.heappush(heap, 3)
heapq.heappush(heap, 1)
heapq.heappush(heap, 2)
print(heapq.heappop(heap)) # output: 1
- 使用heapq库的函数heapq.heapify将列表转换成最小堆。
import heapq
lst = [3, 1, 2]
heapq.heapify(lst)
print(lst) # output: [1, 3, 2]
示例一:使用Python内置堆实现Top K问题
Top K问题指在大数据集中找出前K个最大或最小的元素,Python内置堆可以方便快速地解决该问题。示例代码如下:
import heapq
def top_k(arr, k):
heap = []
for i in range(len(arr)):
if i < k:
heapq.heappush(heap, arr[i])
else:
if arr[i] > heap[0]:
heapq.heappop(heap)
heapq.heappush(heap, arr[i])
return heap
arr = [4, 1, 3, 5, 2, 6, 7]
k = 3
print(top_k(arr, k)) # output: [5, 6, 7]
示例二:使用Python内置堆实现合并K个有序数组
合并K个有序数组是经典的面试题,Python内置堆可以将时间复杂度从O(NKlog(NK))降低到O(Nlog(K)),其中N是元素总数。示例代码如下:
import heapq
def merge_k(arrs):
heap = []
res = []
for i in range(len(arrs)):
heapq.heappush(heap, (arrs[i][0], i, 0))
while heap:
num, arr_idx, idx = heapq.heappop(heap)
res.append(num)
if idx+1 < len(arrs[arr_idx]):
heapq.heappush(heap, (arrs[arr_idx][idx+1], arr_idx, idx+1))
return res
arrs = [[1, 4, 7], [2, 5, 8], [3, 6, 9]]
print(merge_k(arrs)) # output: [1, 2, 3, 4, 5, 6, 7, 8, 9]
本站文章如无特殊说明,均为本站原创,如若转载,请注明出处:python内置堆的具体实现 - Python技术站