forked from indy256/codelibrary
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbinary_heap.py
More file actions
49 lines (41 loc) · 1.2 KB
/
binary_heap.py
File metadata and controls
49 lines (41 loc) · 1.2 KB
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
class BinaryHeap:
def __init__(self, n):
self.heap = [0] * n
self.size = 0
def remove_min(self):
removed = self.heap[0]
self.size -= 1
self.heap[0] = self.heap[self.size]
self._down(0)
return removed
def add(self, value):
self.heap[self.size] = value
self._up(self.size)
self.size += 1
def _up(self, pos):
while pos > 0:
parent = (pos - 1) // 2
if self.heap[pos] >= self.heap[parent]:
break
self.heap[pos], self.heap[parent] = self.heap[parent], self.heap[pos]
pos = parent
def _down(self, pos):
while True:
child = 2 * pos + 1
if child >= self.size:
break
if child + 1 < self.size and self.heap[child + 1] < self.heap[child]:
child += 1
if self.heap[pos] <= self.heap[child]:
break
self.heap[pos], self.heap[child] = self.heap[child], self.heap[pos]
pos = child
def test():
h = BinaryHeap(10)
h.add(3)
h.add(1)
h.add(2)
print(h.remove_min())
print(h.remove_min())
print(h.remove_min())
test()