Introduction to the usage of the Python heapq library

Introduction to the usage of the Python heapq library

1. Introduction to the heapq library

The heapq library is one of the Python standard libraries. It provides methods for building small top heaps and some basic operations on small top heaps (such as heap input, heap output, etc.), which can be used to implement heap sorting algorithms.

The heap is a basic data structure. The structure of the heap is a complete binary tree and satisfies the nature of accumulation: the value of each node (except leaf nodes) is greater than or equal to (or less than or equal to) its child nodes.

The heap structure is divided into a large top heap and a small top heap. The small top heap is used in heapq:

1. Big top pile: The value of each node (except leaf nodes) is greater than or equal to the value of its child nodes, and the value of the root node is the largest of all nodes.

2. Small top heap: The value of each node (except leaf nodes) is less than or equal to the value of its child nodes, and the value of the root node is the smallest of all nodes.

In the heapq library, the data type used by heapq is Python's basic data type list. To satisfy the nature of accumulation, in this list, the value of index k must be less than or equal to the value of index 2*k+1 and index 2*k The value of +2 (in a complete binary tree, the data is inserted first in breadth, and the child node indexes of the node with index k are 2*k+1 and 2*k+2 respectively). It is also introduced in the source code of the heapq library, you can read the source code of heapq, there is not much code.

Use Python to achieve heap sorting can refer to: https://blog.csdn.net/weixin_43790276/article/details/104033696

The characteristics of the complete binary tree can be referred to: https://blog.csdn.net/weixin_43790276/article/details/104737870

Second, use heapq to create a heap

# coding=utf-8
import heapq


array = [10, 17, 50, 7, 30, 24, 27, 45, 15, 5, 36, 21]
heap = []
for num in array:
    heapq.heappush(heap, num)
print("array:", array)
print("heap: ", heap)

heapq.heapify(array)
print("array:", array)

operation result:

array: [10, 17, 50, 7, 30, 24, 27, 45, 15, 5, 36, 21]
heap:  [5, 7, 21, 15, 10, 24, 27, 45, 17, 30, 36, 50]
array: [5, 7, 21, 10, 17, 24, 27, 45, 15, 30, 36, 50]

There are two ways to create a heap in heapq.

heappush(heap, num), first create an empty heap, and then add data to the heap one by one. After each data is added, the heap satisfies the characteristics of the small top heap.

heapify(array), directly adjust the data list into a small top heap (refer to the article on heap sorting above for the principle of adjustment, the heapq library has been implemented).

The results of the two methods will be different. For example, in the above code, the heap structure obtained by using heappush(heap, num) is as follows.

The heap structure obtained using heapify(array) is as follows.

However, these two results meet the characteristics of the small top heap and do not affect the use of the heap (the heap will only fetch data from the top of the heap, and the structure will be readjusted after the data is fetched).

Three, use heapq to achieve heap sort

array = [10, 17, 50, 7, 30, 24, 27, 45, 15, 5, 36, 21]
heap = []
for num in array:
    heapq.heappush(heap, num)
print(heap[0])
# print(heapq.heappop(heap))
heap_sort = [heapq.heappop(heap) for _ in range(len(heap))]
print("heap sort result: ", heap_sort)

operation result:

5
heap sort result:  [5, 7, 10, 15, 17, 21, 24, 27, 30, 36, 45, 50]

First add the data in the list to be sorted to the heap, construct a small top heap, and print the first data to confirm that it is the minimum value. Then take out the values ​​at the top of the heap one by one and add them to a new list until the data in the heap is exhausted, and the new list is the sorted list.

Heappop (heap), the data at the top of the heap is out of the heap and the remaining data in the heap is constructed into a new small top heap.

Four, get the minimum or maximum value in the heap

array = [10, 17, 50, 7, 30, 24, 27, 45, 15, 5, 36, 21]
heapq.heapify(array)
print(heapq.nlargest(2, array))
print(heapq.nsmallest(3, array))

operation result:

[50, 45]
[5, 7, 10]

nlargest(num, heap), take num data from the heap, start with the largest data, and return the result as a list (even if only one data is taken). If num is greater than or equal to the number of data in the heap, then all data in the heap will be taken out from largest to smallest, no error will be reported, which is equivalent to a descending sort.

nsmallest(num, heap), remove num data from the heap, start with the smallest data, and return the result as a list.

These two methods can be used in the heap, and can also be directly used in the list, the function is the same.

Five, use heapq to merge two ordered lists

array_a = [10, 7, 15, 8]
array_b = [17, 3, 8, 20, 13]
array_merge = heapq.merge(sorted(array_a), sorted(array_b))
print("merge result:", list(array_merge))

operation result:

merge result: [3, 7, 8, 8, 10, 13, 15, 17, 20]

merge(list1, list2), merge two ordered lists into a new ordered list, and the return result is an iterator. This method can be used for merging and sorting.

Six, the method of replacing data with heapq

array_c = [10, 7, 15, 8]
heapq.heapify(array_c)
print("before:", array_c)
# 先push再pop
item = heapq.heappushpop(array_c, 5)
print("after: ", array_c)
print(item)

array_d = [10, 7, 15, 8]
heapq.heapify(array_d)
print("before:", array_d)
# 先pop再push
item = heapq.heapreplace(array_d, 5)
print("after: ", array_d)
print(item)

operation result:

before: [7, 8, 15, 10]
after:  [7, 8, 15, 10]
5
before: [7, 8, 15, 10]
after:  [5, 8, 15, 10]
7

heappushpop(heap, num), first add num to the heap, and then pop the top data out of the heap.

heapreplace(heap, num), first remove the top data from the heap, and then add num to the heap.

The two methods both enter the heap and exit the heap, but the order is different, and can be used to replace the data in the heap. The specific difference can be seen in the code example.

 

 

Guess you like

Origin blog.csdn.net/weixin_43790276/article/details/107741332