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

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")
主站蜘蛛池模板: 景德镇市| 宜阳县| 惠来县| 临湘市| 黄石市| 胶州市| 株洲县| 永和县| 邮箱| 治多县| 麻栗坡县| 镇安县| 中牟县| 额尔古纳市| 凤台县| 张家口市| 安塞县| 策勒县| 宁城县| 福贡县| 临高县| 夏邑县| 涡阳县| 行唐县| 广元市| 忻州市| 会泽县| 成都市| 泸西县| 廊坊市| 永善县| 维西| 皮山县| 阳高县| 昔阳县| 龙游县| 峡江县| 许昌市| 扶沟县| 靖西县| 兴文县|