Cấu trúc dữ liệu HEAP chủ yếu được sử dụng để thể hiện hàng đợi ưu tiên. Trong Python, nó có sẵn bằng cách sử dụng mô -đun Feap Feapq. Thuộc tính của cấu trúc dữ liệu này trong Python là mỗi lần phần tử heap nhỏ nhất được bật ra [min-heap]. Bất cứ khi nào các yếu tố được đẩy hoặc bật lên, cấu trúc heap được duy trì. Phần tử Heap [0] cũng trả về phần tử nhỏ nhất mỗi lần. Hãy cùng xem các hoạt động khác nhau trên đống trong Python.
Tạo một đống đơn giản
Các heapify [có thể lặp lại]:- Hàm này được sử dụng để chuyển đổi có thể lặp lại thành cấu trúc dữ liệu heap. tức là theo thứ tự đống.heapify[iterable]:- This function is used to convert the iterable into a heap data structure. i.e. in heap order.
Python3
import
heapq
li
=
The created heap is : [1, 3, 9, 7, 5] The modified heap after push is : [1, 3, 4, 7, 5, 9] The popped and smallest element is : 19
The popped item using heappushpop[] is : 1 The popped item using heapreplace[] is : 30
The popped item using heappushpop[] is : 1 The popped item using heapreplace[] is : 31
The popped item using heappushpop[] is : 1 The popped item using heapreplace[] is : 32223
The popped item using heappushpop[] is : 1 The popped item using heapreplace[] is : 34
The popped item using heappushpop[] is : 1 The popped item using heapreplace[] is : 35
Output:
The created heap is : [1, 3, 9, 7, 5]
& nbsp; các mặt hàng nối tiếp và popping một cách hiệu quả
- Heappush [Heap, Ele]: Hàm này được sử dụng để chèn phần tử được đề cập trong các đối số của nó thành một đống. Bộ điều chỉnh được điều chỉnh, để cấu trúc heap được duy trì.: This function is used to insert the element mentioned in its arguments into a heap. Theorder is adjusted, so that heap structure is maintained.
- Heppop [Heap]: Hàm này được sử dụng để loại bỏ và trả lại phần tử nhỏ nhất từ đống. Thứ tự được điều chỉnh, để cấu trúc heap được duy trì.: This function is used to remove and return the smallest element from the heap. The order is adjusted, so that heap structure is maintained.
Python3
import
heapq
li
=
The created heap is : [1, 3, 9, 7, 5] The modified heap after push is : [1, 3, 4, 7, 5, 9] The popped and smallest element is : 19
The popped item using heappushpop[] is : 1 The popped item using heapreplace[] is : 30
The popped item using heappushpop[] is : 1 The popped item using heapreplace[] is : 31
The popped item using heappushpop[] is : 1 The popped item using heapreplace[] is : 32
import
5=
import
7The popped item using heappushpop[] is : 1 The popped item using heapreplace[] is : 30
The popped item using heappushpop[] is : 1 The popped item using heapreplace[] is : 31
The popped item using heappushpop[] is : 1 The popped item using heapreplace[] is : 34
heapq
1heapq
2heapq
3heapq
4
The popped item using heappushpop[] is : 1 The popped item using heapreplace[] is : 30
The popped item using heappushpop[] is : 1 The popped item using heapreplace[] is : 31
heapq
7import
5=
import
7The popped item using heappushpop[] is : 1 The popped item using heapreplace[] is : 30
The popped item using heappushpop[] is : 1 The popped item using heapreplace[] is : 31
The popped item using heappushpop[] is : 1 The popped item using heapreplace[] is : 34
heapq
1The created heap is : [1, 3, 9, 7, 5] The modified heap after push is : [1, 3, 4, 7, 5, 9] The popped and smallest element is : 19
The popped item using heappushpop[] is : 1 The popped item using heapreplace[] is : 30
=
2The popped item using heappushpop[] is : 1
The popped item using heapreplace[] is : 3
0 The popped item using heappushpop[] is : 1
The popped item using heapreplace[] is : 3
1The popped item using heappushpop[] is : 1
The popped item using heapreplace[] is : 3
2223The popped item using heappushpop[] is : 1
The popped item using heapreplace[] is : 3
4The popped item using heappushpop[] is : 1
The popped item using heapreplace[] is : 3
5
The created heap is : [1, 3, 9, 7, 5] The modified heap after push is : [1, 3, 4, 7, 5, 9] The popped and smallest element is : 1
& nbsp; các mặt hàng nối tiếp và popping một cách hiệu quảsimultaneously
- Heappush [Heap, Ele]: Hàm này được sử dụng để chèn phần tử được đề cập trong các đối số của nó thành một đống. Bộ điều chỉnh được điều chỉnh, để cấu trúc heap được duy trì.:- This function combines the functioning of both push and pop operations in one statement, increasing efficiency. Heap order is maintained after this operation.
- Heppop [Heap]: Hàm này được sử dụng để loại bỏ và trả lại phần tử nhỏ nhất từ đống. Thứ tự được điều chỉnh, để cấu trúc heap được duy trì.:- This function also inserts and pops elements in one statement, but it is different from the above function. In this, the element is first popped, then the element is pushed. i.e, the value larger than the pushed value can be returned. heapreplace[] returns the smallest value originally in the heap regardless of the pushed element as opposed to heappushpop[].
Python3
import
heapq
li
=
The created heap is : [1, 3, 9, 7, 5] The modified heap after push is : [1, 3, 4, 7, 5, 9] The popped and smallest element is : 19
The created heap is : [1, 3, 9, 7, 5] The modified heap after push is : [1, 3, 4, 7, 5, 9] The popped and smallest element is : 101
The created heap is : [1, 3, 9, 7, 5] The modified heap after push is : [1, 3, 4, 7, 5, 9] The popped and smallest element is : 102
The popped item using heappushpop[] is : 1 The popped item using heapreplace[] is : 30
The popped item using heappushpop[] is : 1 The popped item using heapreplace[] is : 31
The created heap is : [1, 3, 9, 7, 5] The modified heap after push is : [1, 3, 4, 7, 5, 9] The popped and smallest element is : 105
import
5=
import
7The popped item using heappushpop[] is : 1 The popped item using heapreplace[] is : 30
The created heap is : [1, 3, 9, 7, 5] The modified heap after push is : [1, 3, 4, 7, 5, 9] The popped and smallest element is : 110
The created heap is : [1, 3, 9, 7, 5] The modified heap after push is : [1, 3, 4, 7, 5, 9] The popped and smallest element is : 111
The created heap is : [1, 3, 9, 7, 5] The modified heap after push is : [1, 3, 4, 7, 5, 9] The popped and smallest element is : 112
The popped item using heappushpop[] is : 1 The popped item using heapreplace[] is : 30
The popped item using heappushpop[] is : 1 The popped item using heapreplace[] is : 31
The created heap is : [1, 3, 9, 7, 5] The modified heap after push is : [1, 3, 4, 7, 5, 9] The popped and smallest element is : 115
import
5=
import
7The popped item using heappushpop[] is : 1 The popped item using heapreplace[] is : 30
The created heap is : [1, 3, 9, 7, 5] The modified heap after push is : [1, 3, 4, 7, 5, 9] The popped and smallest element is : 120
The created heap is : [1, 3, 9, 7, 5] The modified heap after push is : [1, 3, 4, 7, 5, 9] The popped and smallest element is : 111
The created heap is : [1, 3, 9, 7, 5] The modified heap after push is : [1, 3, 4, 7, 5, 9] The popped and smallest element is : 112
Output:
The popped item using heappushpop[] is : 1 The popped item using heapreplace[] is : 3
The popped item using heappushpop[] is : 1
The popped item using heapreplace[] is : 3
0 The popped item using heappushpop[] is : 1
The popped item using heapreplace[] is : 3
1The popped item using heappushpop[] is : 1
The popped item using heapreplace[] is : 3
2223The popped item using heappushpop[] is : 1
The popped item using heapreplace[] is : 3
4The popped item using heappushpop[] is : 1
The popped item using heapreplace[] is : 3
5
- & nbsp; các mặt hàng nối tiếp và popping một cách hiệu quả: This function is used to return the k largest elements from the iterable specified and satisfy the key if mentioned.
- Heappush [Heap, Ele]: Hàm này được sử dụng để chèn phần tử được đề cập trong các đối số của nó thành một đống. Bộ điều chỉnh được điều chỉnh, để cấu trúc heap được duy trì.: This function is used to return the k smallest elements from the iterable specified and satisfy the key if mentioned.
Python3
import
heapq
li
=
The created heap is : [1, 3, 9, 7, 5] The modified heap after push is : [1, 3, 4, 7, 5, 9] The popped and smallest element is : 101
The popped item using heappushpop[] is : 1 The popped item using heapreplace[] is : 30
The popped item using heappushpop[] is : 1 The popped item using heapreplace[] is : 31
The created heap is : [1, 3, 9, 7, 5] The modified heap after push is : [1, 3, 4, 7, 5, 9] The popped and smallest element is : 149
import
5=
import
7The popped item using heappushpop[] is : 1 The popped item using heapreplace[] is : 30
The created heap is : [1, 3, 9, 7, 5] The modified heap after push is : [1, 3, 4, 7, 5, 9] The popped and smallest element is : 154
The created heap is : [1, 3, 9, 7, 5] The modified heap after push is : [1, 3, 4, 7, 5, 9] The popped and smallest element is : 17
The created heap is : [1, 3, 9, 7, 5] The modified heap after push is : [1, 3, 4, 7, 5, 9] The popped and smallest element is : 156
The popped item using heappushpop[] is : 1 The popped item using heapreplace[] is : 30
The popped item using heappushpop[] is : 1 The popped item using heapreplace[] is : 31
The created heap is : [1, 3, 9, 7, 5] The modified heap after push is : [1, 3, 4, 7, 5, 9] The popped and smallest element is : 159
import
5=
import
7The popped item using heappushpop[] is : 1 The popped item using heapreplace[] is : 30
The created heap is : [1, 3, 9, 7, 5] The modified heap after push is : [1, 3, 4, 7, 5, 9] The popped and smallest element is : 164
The created heap is : [1, 3, 9, 7, 5] The modified heap after push is : [1, 3, 4, 7, 5, 9] The popped and smallest element is : 17
The created heap is : [1, 3, 9, 7, 5] The modified heap after push is : [1, 3, 4, 7, 5, 9] The popped and smallest element is : 156
The popped item using heappushpop[] is : 1
The popped item using heapreplace[] is : 3
0 The popped item using heappushpop[] is : 1
The popped item using heapreplace[] is : 3
1The popped item using heappushpop[] is : 1
The popped item using heapreplace[] is : 3
2223The popped item using heappushpop[] is : 1
The popped item using heapreplace[] is : 3
4The popped item using heappushpop[] is : 1
The popped item using heapreplace[] is : 3
5
The 3 largest numbers in list are : [10, 9, 8] The 3 smallest numbers in list are : [1, 3, 4]