-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbinary_heap.py
60 lines (51 loc) · 1.54 KB
/
binary_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
53
54
55
56
57
58
59
60
class BinaryHeap:
MAX_HEAP_SIZE = 100
MIN_VALUE = -1
def __init__(self, *args, **kwargs):
self.heap = [None] * self.MAX_HEAP_SIZE
self.heap[0] = self.MIN_VALUE
self.last_index = 0
def insert(self, value):
self.last_index += 1
index = self.last_index
while self.heap[index // 2] > value:
self.heap[index] = self.heap[index // 2]
index = index // 2
self.heap[index] = value
def getmin(self):
min_value = self.heap[1]
last_value = self.heap[self.last_index]
self.last_index -= 1
index = 1
while 2 * index <= self.last_index:
child = 2 * index
if child < self.last_index and self.heap[child + 1] < self.heap[child]:
child += 1
if last_value > self.heap[child]:
self.heap[index] = self.heap[child]
index = child
else:
break
self.heap[index] = last_value
return min_value
def __str__(self):
s = ''
n = 1
levels = []
for i in range(1, self.last_index + 1):
v = self.heap[i]
s += '{} '.format(v)
if i == n:
n = (n << 1) + 1
s += '\n'
return s
def test():
import random
bh = BinaryHeap()
for i in range(1, 10):
bh.insert(random.randint(0, 100))
print(bh)
print('min:', bh.getmin())
print(bh)
if __name__ == '__main__':
test()