-
Notifications
You must be signed in to change notification settings - Fork 830
/
maxheap.py
33 lines (29 loc) · 1.04 KB
/
maxheap.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
from minheap import minheap
class maxheap(minheap):
"""
Heap class - made of keys and items
methods: build_heap, heappush, heappop
"""
MAX_HEAP = True
def __str__(self):
return "Max-heap with %s items" % (len(self.heap))
def heapify(self, i):
l = self.leftchild(i)
r = self.rightchild(i)
largest = i
if l < self.max_elements() and self.heap[l] > self.heap[largest]:
largest = l
if r < self.max_elements() and self.heap[r] > self.heap[largest]:
largest = r
if largest != i:
self.heap[i], self.heap[largest] = self.heap[largest], self.heap[i]
self.heapify(largest)
def heappush(self, x):
""" Adds a new item x in the heap"""
i = len(self.heap)
self.heap.append(x)
parent = self.parent(i)
while parent != -1 and self.heap[i] > self.heap[parent]:
self.heap[i], self.heap[parent] = self.heap[parent], self.heap[i]
i = parent
parent = self.parent(i)