-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmin_max-heap.py
52 lines (40 loc) · 1.14 KB
/
min_max-heap.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
import heapq
from termcolor import colored
class MinHeap:
def __init__(self, arr):
self.heap = arr
heapq.heapify(self.heap)
def __repr__(self):
return colored(f"Min-Heap: {self.heap}", 'green')
def pop(self):
smallest = heapq.heappop(self.heap)
print(colored(f"Popped element: {smallest}", 'yellow'))
return smallest
def push(self, element):
heapq.heappush(self.heap, element)
print(colored(f"Heap after push: {self.heap}", 'blue'))
def __call__(self):
print(self)
class MaxHeap:
def __init__(self, arr):
self.heap = [-num for num in arr]
heapq.heapify(self.heap)
def __repr__(self):
return colored(f"Max-Heap: {[-num for num in self.heap]}", 'green')
def pop(self):
largest = -heapq.heappop(self.heap)
print(colored(f"Popped element: {largest}", 'yellow'))
return largest
def __call__(self):
print(self)
# Example usage:
arr = [10, 1, 4, 6, 8, 5]
min_heap = MinHeap(arr[:])
min_heap()
min_heap.pop()
min_heap.push(2)
min_heap()
max_heap = MaxHeap(arr[:])
max_heap()
max_heap.pop()
max_heap()