-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbinary_heap.py
106 lines (66 loc) · 1.87 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
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
'''
BINARY HEAP
min heap implementation
formulas starting from index 1:
parent node --> i // 2
children --> (2 * i) and (2 * i) + 1
'''
class BinHeap(object):
def __init__(self):
self.heaplist = [0]
self.current_size = 0
def _up_heap(self, idx):
while idx // 2 > 0:
# check if parent node is greater
if self.heaplist[idx] < self.heaplist[idx // 2]:
# swap with parent node
temp = self.heaplist[idx // 2]
self.heaplist[idx // 2] = self.heaplist[idx]
self.heaplist[idx] = temp
# decrese to check every parent node
idx = idx // 2
def _min_child(self, i):
# check if right node exists
if (i * 2) + 1 > self.current_size:
return i * 2
else:
# return the smaller child
if self.heaplist[i * 2] < self.heaplist[(i * 2) + 1]:
return i * 2
else
return (i * 2) + 1
def _down_heap(self, idx):
while (idx * 2) <= self.current_size:
mc = self._min_child(idx)
# swap the parent node with the smaller child
if self.heaplist[idx] > self.heaplist[mc]:
temp = self.heaplist[idx]
self.heaplist[idx] = self.heaplist[mc]
self.heaplist[mc] = temp
# move idx to
idx = mc
def insert(self, new_value):
# add new value to list --> keep tree property
self.heaplist.append(new_value)
# update current size
self.current_size += 1
# update nodes values --> keep heap property
self._up_heap(self.current_size)
def delete_min(self):
# get root of list
root = self.heaplist[1]
# replace root with last leaf
self.heaplist[1] = self.heaplist[self.current_size]
# reduce list size
self.current_size -= 1
self.heaplist.pop()
# update root value --> keep heap property
self._down_heap(1)
return root
def build_heap(self, values):
idx = len(valuse) // 2
self.current_size = len(values)
self.heaplist = [0] + value[0:]
while (i > 0):
self._down_heap(i)
i -= 1