|
| 1 | +import heapq |
| 2 | + |
| 3 | +# heapq is a module in Python that provides a collection of heap queue |
| 4 | +# algorithms. It allows you to efficiently manipulate and maintain a |
| 5 | +# heap data structure, which is a specialized tree-based structure that |
| 6 | +# satisfies the heap property (either min-heap or max-heap). |
| 7 | + |
| 8 | +# A min-heap and a max-heap are both specialized forms of binary heap data |
| 9 | +# structures used in computer science and data processing for efficient |
| 10 | +# management of elements with respect to their values. They are often used |
| 11 | +# in priority queues, heapsort, and various other algorithms. |
| 12 | + |
| 13 | +# Here are the key differences between a min-heap and a max-heap: |
| 14 | + |
| 15 | +# **Min-Heap:** |
| 16 | +# 1. In a min-heap, the value of each node (including the root) is less than |
| 17 | +# or equal to the values of its children. |
| 18 | +# 2. The smallest element is at the root of the heap, which makes it suitable |
| 19 | +# for finding the minimum element quickly. |
| 20 | +# 3. Min-heaps are used in algorithms where you need to extract the smallest |
| 21 | +# element efficiently, such as Dijkstra's algorithm for finding the shortest |
| 22 | +# path in a graph. |
| 23 | + |
| 24 | +# **Max-Heap:** |
| 25 | +# 1. In a max-heap, the value of each node (including the root) is greater |
| 26 | +# than or equal to the values of its children. |
| 27 | +# 2. The largest element is at the root of the heap, which makes it suitable |
| 28 | +# for finding the maximum element quickly. |
| 29 | +# 3. Max-heaps are used in algorithms where you need to extract the largest |
| 30 | +# element efficiently, such as heapsort, which is an in-place sorting |
| 31 | +# algorithm. |
| 32 | + |
| 33 | +# Both min-heaps and max-heaps are typically implemented as binary trees, |
| 34 | +# with the heap property enforced such that the parent node's value is |
| 35 | +# compared with its children in a way that maintains the desired ordering |
| 36 | +# (minimum or maximum). |
| 37 | + |
| 38 | +# Here's a visual representation of a min-heap and a max-heap: |
| 39 | + |
| 40 | +# **Min-Heap:** |
| 41 | + |
| 42 | +# ``` |
| 43 | +# 1 |
| 44 | +# / \ |
| 45 | +# 2 3 |
| 46 | +# / \ / \ |
| 47 | +# 4 5 6 7 |
| 48 | +# ``` |
| 49 | + |
| 50 | +# **Max-Heap:** |
| 51 | + |
| 52 | +# ``` |
| 53 | +# 7 |
| 54 | +# / \ |
| 55 | +# 5 6 |
| 56 | +# / \ / \ |
| 57 | +# 2 4 3 1 |
| 58 | +# ``` |
| 59 | + |
| 60 | +# In summary, min-heaps and max-heaps are used for maintaining ordered |
| 61 | +# collections of elements, with the primary difference being the order |
| 62 | +# in which elements are organized. Min-heaps prioritize smaller elements, |
| 63 | +# while max-heaps prioritize larger elements. |
| 64 | + |
| 65 | +# Create an empty list to represent a min-heap |
| 66 | +min_heap = [] |
| 67 | + |
| 68 | +# Insert elements into the min-heap |
| 69 | +elements = [3, 1, 4, 1, 5, 9, 2, 6, 5, 3, 5] |
| 70 | +for element in elements: |
| 71 | + heapq.heappush(min_heap, element) |
| 72 | + |
| 73 | +print("Min-Heap after inserting elements:", min_heap) |
| 74 | + |
| 75 | +# Get the smallest element (root of the min-heap) |
| 76 | +smallest = heapq.heappop(min_heap) |
| 77 | +print("Smallest element:", smallest) |
| 78 | + |
| 79 | +# Insert a new element into the min-heap |
| 80 | +new_element = 0 |
| 81 | +heapq.heappush(min_heap, new_element) |
| 82 | +print("Min-Heap after inserting", new_element, ":", min_heap) |
| 83 | + |
| 84 | +# Find the smallest element without removing it |
| 85 | +smallest = heapq.heappop(min_heap) |
| 86 | +print("Smallest element (without removal):", smallest) |
| 87 | + |
| 88 | +# Convert a list into a valid min-heap in-place |
| 89 | +unordered_list = [7, 8, 2, 3, 1] |
| 90 | +heapq.heapify(unordered_list) |
| 91 | +print("Min-Heap created from an unordered list:", unordered_list) |
| 92 | + |
| 93 | +# Find the n largest elements in a list using nlargest |
| 94 | +n_largest = heapq.nlargest(3, elements) |
| 95 | +print("3 largest elements:", n_largest) |
| 96 | + |
| 97 | +# Find the n smallest elements in a list using nsmallest |
| 98 | +n_smallest = heapq.nsmallest(3, elements) |
| 99 | +print("3 largest elements:", n_smallest) |
0 commit comments