-
Notifications
You must be signed in to change notification settings - Fork 1.4k
/
Copy pathMaxHeap.py
56 lines (49 loc) · 1.65 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
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
class MaxHeap:
def __init__(self, items=[]):
super().__init__()
self.heap = [0] # Used a dummy element at index 0
for i in items:
self.heap.append(i)
self._float_up(len(self.heap) - 1)
def push(self, data):
self.heap.append(data)
self._float_up(len(self.heap) - 1)
def peek(self):
if len(self.heap) > 1:
return self.heap[1]
else:
return None
def pop(self):
if len(self.heap) > 2:
self._swap(1, len(self.heap) - 1)
max_val = self.heap.pop()
self._bubble_down(1)
elif len(self.heap) == 2:
max_val = self.heap.pop()
else:
max_val = None
return max_val
def _swap(self, i, j):
self.heap[i], self.heap[j] = self.heap[j], self.heap[i]
def _float_up(self, index):
parent = index // 2
if index <= 1:
return
elif self.heap[index] > self.heap[parent]:
self._swap(index, parent)
self._float_up(parent)
def _bubble_down(self, index):
left = index * 2
right = index * 2 + 1
largest = index
if len(self.heap) > left and self.heap[largest] < self.heap[left]:
largest = left
if len(self.heap) > right and self.heap[largest] < self.heap[right]:
largest = right
if largest != index:
self._swap(index, largest)
self._bubble_down(largest)
m = MaxHeap([95, 3, 21])
m.push(10)
print(str(m.heap[1:])) # Excluding the dummy element when printing heap elements
print(str(m.pop())) # Output of pop operation