官术网_书友最值得收藏!

Heaps

Heaps are data structures designed to quickly find and extract the maximum (or minimum) value in a collection. A typical use-case for heaps is to process a series of incoming tasks in order of maximum priority.

One can theoretically use a sorted list using the tools in the bisect module; however, while extracting the maximum value will take O(1) time (using list.pop), insertion will still take O(N) time (remember that, even if finding the insertion point takes O(log(N)) time, inserting an element in the middle of a list is still a O(N) operation). A heap is a more efficient data structure that allows for insertion and extraction of maximum values with O(log(N)) time complexity.

In Python, heaps are built using the procedures contained in the heapq module on an underlying list. For example, if we have a list of 10 elements, we can reorganize it into a heap with the heapq.heapify function:

    import heapq

collection = [10, 3, 3, 4, 5, 6]
heapq.heapify(collection)

To perform the insertion and extraction operations on the heap, we can use the heapq.heappush and heapq.heappop functions. The heapq.heappop function will extract the minimum value in the collection in O(log(N)) time and can be used in the following way:

    heapq.heappop(collection)
# Returns: 3

Similarly, you can push the integer 1, with the heapq.heappush function, as follows:

    heapq.heappush(collection, 1)

Another easy-to-use option is the queue.PriorityQueue class that, as a bonus, is thread and process-safe. The PriorityQueue class can be filled up with elements using the PriorityQueue.put method, while PriorityQueue.get can be used to extract the minimum value in the collection:

    from queue import PriorityQueue

queue = PriorityQueue()
for element in collection:
queue.put(element)

queue.get()
# Returns: 3

If the maximum element is required, a simple trick is to multiply each element of the list by -1. In this way, the order of the elements will be inverted. Also, if you want to associate an object (for example, a task to run) to each number (which can represent the priority), one can insert tuples of the (number, object) form; the comparison operator for the tuple will be ordered with respect to its first element, as shown in the following example:

    queue = PriorityQueue()
queue.put((3, "priority 3"))
queue.put((2, "priority 2"))
queue.put((1, "priority 1"))

queue.get()
# Returns: (1, "priority 1")
主站蜘蛛池模板: 蓬莱市| 隆化县| 林口县| 聊城市| 绥滨县| 正蓝旗| 顺平县| 宁城县| 泸州市| 宁德市| 尤溪县| 西丰县| 灵宝市| 甘德县| 滦平县| 太湖县| 多伦县| 绵阳市| 科技| 临漳县| 固始县| 上林县| 旅游| 叶城县| 当雄县| 三河市| 米易县| 沐川县| 调兵山市| 南溪县| 高邑县| 湘阴县| 黑龙江省| 聂荣县| 安塞县| 民勤县| 马山县| 安义县| 叙永县| 南京市| 高阳县|