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

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")
主站蜘蛛池模板: 抚顺市| 星座| 新泰市| 宁陵县| 津南区| 开阳县| 兴文县| 孝义市| 盈江县| 怀远县| 兴仁县| 南陵县| 永宁县| 遵义县| 富锦市| 齐河县| 隆化县| 织金县| 绥棱县| 连南| 邵阳县| 遵化市| 嵊泗县| 绍兴县| 曲沃县| 河津市| 四会市| 邹平县| 新丰县| 五莲县| 黔江区| 册亨县| 两当县| 扎赉特旗| 泽库县| 莆田市| 汉川市| 岚皋县| 禹州市| 阳曲县| 合肥市|