Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
31 changes: 31 additions & 0 deletions DSA/Heaps/insertion_in_min_max_heap.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
# Insertion in Min Heap and Max Heap
# Author: Suhaani Garg
# Description: Demonstrates how to insert an element in both Min Heap and Max Heap in Python.

import heapq

# ----- MIN HEAP -----
def insert_min_heap(heap, value):
"""Insert an element into a Min Heap."""
heapq.heappush(heap, value)
return heap

# ----- MAX HEAP -----
def insert_max_heap(heap, value):
"""Insert an element into a Max Heap."""
# Python heapq is a Min Heap, so we invert the value for Max Heap
heapq.heappush(heap, -value)
return heap

# ----- DEMO -----
if __name__ == "__main__":
min_heap = []
max_heap = []

# Inserting elements
for num in [10, 4, 15, 20, 0]:
insert_min_heap(min_heap, num)
insert_max_heap(max_heap, num)

print("Min Heap (ascending order):", min_heap)
print("Max Heap (descending order):", [-x for x in max_heap])