forked from thatguyintech/100-day-coding-challenge
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmaxHeap.py
151 lines (113 loc) · 3.21 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
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
from collections import deque
class MaxHeap:
def __init__(self, arr=[]):
self.data = deque()
self.size = 0
if len(arr) > 0:
self.size = len(arr)
self.__heapify(arr)
def getMax(self):
if self.size > 0:
ret = self.data.popleft()
self.size -= 1
self.data.appendleft(self.data.pop())
self.__bubbleDown(0)
return ret
def peek(self):
if self.size > 0:
return self.data[0]
def push(self, val):
self.data.append(val)
self.size += 1
self.__bubbleUp(self.size-1)
def isEmpty(self):
return self.size == 0
def __heapify(self, arr):
self.data = deque(arr)
for i in xrange(len(arr)-1, -1, -1):
self.__bubbleDown(i)
def __bubbleDown(self, index):
left = self.__left(index)
right = self.__right(index)
larger = index
if left < self.size and self.data[index] < self.data[left]:
larger = left
if right < self.size and self.data[left] < self.data[right]:
larger = right
if larger != index:
self.data[larger], self.data[index] = self.data[index], self.data[larger]
self.__bubbleDown(larger)
def __bubbleUp(self, index):
p = self.__parent(index)
if p >= 0 and self.data[index] > self.data[p]:
self.data[index], self.data[p] = self.data[p], self.data[index]
self.__bubbleUp(p)
def __parent(self, i):
return i/2
def __left(self, i):
return 2*i + 1
def __right(self, i):
return 2*i + 2
def testPush():
h = MaxHeap()
h.push(2)
assert h.data[0] == 2
h.push(3)
assert h.data[0] == 3
h.push(1)
assert h.data[0] == 3
h.push(5)
assert h.data[0] == 5
h.push(-1)
assert h.data[0] == 5
def testGetMax():
heap = MaxHeap([2, 5, 20])
assert heap.getMax() == 20
assert heap.getMax() == 5
heap1 = MaxHeap([1, 2, 3, 4, 5])
assert heap1.getMax() == 5
assert heap1.getMax() == 4
heap2 = MaxHeap([5, 4, 3, 2, 1])
assert heap2.getMax() == 5
assert heap2.getMax() == 4
heap3 = MaxHeap([2, 5, 3, 7, -1])
assert heap3.getMax() == 7
assert heap3.getMax() == 5
def testPeek():
heap = MaxHeap([2, 5, 20])
assert heap.peek() == 20
assert heap.peek() == 20
heap1 = MaxHeap([1, 2, 3, 4, 5])
assert heap1.peek() == 5
assert heap1.peek() == 5
heap2 = MaxHeap([5, 4, 3, 2, 1])
assert heap2.peek() == 5
assert heap2.peek() == 5
heap3 = MaxHeap([2, 5, 3, 7, -1])
assert heap3.peek() == 7
assert heap3.peek() == 7
def testSize():
heap = MaxHeap()
assert heap.size == 0
heap2 = MaxHeap([1])
assert heap2.size == 1
heap3 = MaxHeap([1, 2, 3, 4])
assert heap3.size == 4
def testIsEmpty():
heap = MaxHeap()
assert heap.isEmpty()
heap2 = MaxHeap([1])
assert not heap2.isEmpty()
heap3 = MaxHeap([1, 2, 3])
assert not heap3.isEmpty()
def testEverything():
assert True
def tests():
testPush()
testGetMax()
testPeek()
testSize()
testIsEmpty()
testEverything()
if __name__ == "__main__":
tests()