-
Notifications
You must be signed in to change notification settings - Fork 1
/
Heap.py
235 lines (171 loc) · 5.54 KB
/
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
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
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
"""
Author: Marko Njegomir sw-38-2018
"""
class Heap_node(object):
def __init__(self, key, value):
self._key = key
self._value = value
def get_key(self):
return self._key
def set_key(self, key):
self._key = key
def get_value(self):
return self._value
def set_value(self, value):
self._value = value
def __eq__(self, other):
return self._key == other.get_key()
def __ne__(self, other):
return self._key != other.get_key()
def __lt__(self, other):
return self._key < other.get_key()
def __le__(self, other):
return self._key <= other.get_key()
def __gt__(self, other):
return self._key > other.get_key()
def __ge__(self, other):
return self._key >= other.get_key()
def __str__(self):
return str(self._key)
class MinHeap(object):
def __init__(self):
self._data = []
self._heap_size = 0
def is_empty(self):
return self._heap_size == 0
def min(self):
if self.is_empty():
raise Exception
return self._data[0].get_key(), self._data[0].get_value()
def left(self, position):
return position * 2 + 1
def right(self, position):
return position * 2 + 2
def parent(self, position):
return (position - 1) // 2
def swap(self, i, j):
self._data[i], self._data[j] = self._data[j], self._data[i]
def add(self, key, value=None):
node = Heap_node(key, value)
self._data.append(node)
self._heap_size += 1
self.upheap(self._heap_size - 1)
def upheap(self, index):
p_index = self.parent(index)
if p_index < 0 or self._data[index] > self._data[p_index]:
return
self.swap(index, p_index)
self.upheap(p_index)
def min(self):
if self.is_empty():
raise Exception
return self._data[0].get_key(), self._data[0].get_value()
def remove_min(self):
if self.is_empty():
raise Exception
self.swap(0, self._heap_size - 1)
ret_val = self._data.pop(self._heap_size - 1)
ret_val = ret_val.get_key(), ret_val.get_value()
self._heap_size -= 1
self.downheap(0)
return ret_val
def has_left(self, index):
l = self.left(index)
return l < self._heap_size - 1
def has_right(self, index):
r = self.right(index)
return r < self._heap_size - 1
def downheap(self, index):
l_index = self.left(index)
r_index = self.right(index)
min_index = index
if l_index < self._heap_size and self._data[l_index] < self._data[index]:
min_index = l_index
if r_index < self._heap_size and self._data[r_index] < self._data[min_index]:
min_index = r_index
if min_index != index:
self.swap(min_index, index)
self.downheap(min_index)
def __str__(self):
ret_val = "["
for node in self._data:
if ret_val == "[":
ret_val = ret_val + str(node)
continue
ret_val = ret_val + ", " + str(node)
ret_val = ret_val + "]"
return ret_val
def return_data(self):
return self._data
class MaxHeap(object):
def __init__(self, data):
self._data = data
self._heap_size = len(data)
self._build_max_heap()
def has_left(self, index):
l = self.left(index)
return l < self._heap_size
def has_right(self, index):
r = self.right(index)
return r < self._heap_size
def left(self, position):
return position * 2 + 1
def right(self, position):
return position * 2 + 2
def parent(self, position):
return (position - 1) // 2
def swap(self, i, j):
self._data[i], self._data[j] = self._data[j], self._data[i]
# def _build_max_heap(self):
# self._size = len(self._data)
#
# start = (self._size - 1) // 2
# for i in range(start, -1, -1):
# self.m_heapify(i)
def _build_max_heap(self):
self._size = len(self._data)
start = (self._size - 1) // 2
for i in range(start, -1, -1):
self.m_heapify(i)
def _max_heapify(self, i):
i_left = self.left(i)
i_right = self.right(i)
max = i
if self.has_left(i) and self._data[i_left] > self._data[i]:
max = i_left
if self.has_right(i) and self._data[i_right] > self._data[max]:
max = i_right
if max != i:
self.swap(max, i)
self._max_heapify(max)
def m_heapify(self, i):
l_index = self.left(i)
r_index = self.right(i)
max = i
if l_index < self._heap_size and self._data[l_index] < self._data[max]:
max = l_index
if self.has_right(i) and self._data[r_index] < self._data[max]:
max = r_index
if max != i:
self.swap(max, i)
self.m_heapify(max)
def __str__(self):
ret_val = "["
for node in self._data:
if ret_val == "[":
ret_val = ret_val + str(node)
continue
ret_val = ret_val + ", " + str(node)
ret_val = ret_val + "]"
return ret_val
def get_data(self):
return self._data
def sort(self):
for i in range(self._size-1, 0, -1):
self.swap(0, i)
self._heap_size -= 1
self.m_heapify(0)
def main():
pass
if __name__ == '__main__':
main()