heapq – 堆队列算法

This module implements a subset of the corresponding CPython module, as described below. For more information, refer to the original CPython documentation: heapq。

此模块实现了 最小堆队列算法。

堆队列本质上是一个列表,其元素以这样的方式存储:列表的第一个项目始终是最小的。

函数

heapq.heappush(heap, item)

将 item 推入 heap 中。

heapq.heappop(heap)

从 heap 中弹出第一个项目,并返回它。如果 heap 为空,则引发 IndexError 。

返回的项目将是 heap 中最小的项目。

heapq.heapify(x)

将列表 x 转换为堆。这是一个就地操作。