-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathheap.py
More file actions
365 lines (295 loc) · 10.1 KB
/
Copy pathheap.py
File metadata and controls
365 lines (295 loc) · 10.1 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
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
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
import numpy as np
import math
from copy import deepcopy
from typing import List
class HeapNode(object):
def __init__(self,
data,
heap_index=-1,
in_heap=False,
):
self.data = data
self.heap_index = heap_index
self.in_heap = in_heap
def key_default(node):
return node.data
def less_than_default(a, b):
return a.data < b.data
def greater_than_default(a, b):
return a.data > b.data
def mark_default(node):
node.in_heap = True
def unmark_default(node):
node.in_heap = False
def marked_default(node):
return node.in_heap
def set_index_default(node, val):
node.heap_index = val
def unset_index_default(node):
node.heap_index = -1
def get_index_default(node):
return node.heap_index
class BinaryHeap(object):
def __init__(self,
max_size=None,
key=key_default,
less_than=less_than_default,
greater_than=greater_than_default,
mark=mark_default,
unmark=unmark_default,
marked=marked_default,
set_index=set_index_default,
unset_index=unset_index_default,
get_index=get_index_default):
if max_size is None:
self.max_size = 64
else:
self.max_size = max_size
self.heap_node: List[HeapNode] = []
self.index_of_last = -1
self.parent_of_last = -1
self.key = key
self.less_than = less_than
self.greater_than = greater_than
self.mark = mark
self.unmark = unmark
self.marked = marked
self.set_index = set_index
self.unset_index = unset_index
self.get_index = get_index
def bubble_up(H: BinaryHeap, n: int):
if (n==0):
return
if n > H.index_of_last:
return
parent = (n-1)//2
while n != 0 and H.greater_than(H.heap_node[parent], H.heap_node[n]):
temp_node = H.heap_node[parent]
H.heap_node[parent] = H.heap_node[n]
H.heap_node[n] = temp_node
H.set_index(H.heap_node[parent], parent)
H.set_index(H.heap_node[n], n)
n = parent
parent = (n-1)//2
def bubble_down(H: BinaryHeap, n: int):
if 2*n+1 == H.index_of_last:
child = 2*n+1
elif 2*n+2 > H.index_of_last:
return
elif H.less_than(H.heap_node[2*n+1], H.heap_node[2*n+2]):
child = 2*n+1
else:
child = 2*n+2
while n <= H.parent_of_last and H.less_than(H.heap_node[child], H.heap_node[n]):
temp_node = H.heap_node[child]
H.heap_node[child] = H.heap_node[n]
H.heap_node[n] = temp_node
H.set_index(H.heap_node[child], child)
H.set_index(H.heap_node[n], n)
n = child
if 2*n+1 == H.index_of_last:
child = 2*n+1
elif 2*n+2 > H.index_of_last:
return
elif H.less_than(H.heap_node[2*n+1], H.heap_node[2*n+2]):
child = 2*n+1
else:
child = 2*n+2
def add_to_heap(H: BinaryHeap, this_node):
if not H.marked(this_node):
H.heap_node.append(this_node)
H.index_of_last = len(H.heap_node) - 1
H.parent_of_last = (H.index_of_last-1)//2
H.set_index(this_node, H.index_of_last)
bubble_up(H, H.index_of_last)
H.mark(this_node)
if len(H.heap_node) > H.max_size:
remove_from_heap(H, H.heap_node[-1])
else:
raise ValueError("problems")
def top_heap(H: BinaryHeap):
if H.index_of_last < 0:
return False
return H.heap_node[0]
def pop_heap(H: BinaryHeap):
if H.index_of_last < 0:
return False
old_top_node = H.heap_node[0]
H.heap_node[0] = H.heap_node[H.index_of_last]
H.set_index(H.heap_node[0], 0)
H.heap_node.pop(H.index_of_last)
H.index_of_last = len(H.heap_node) - 1
H.parent_of_last = (H.index_of_last-1)//2
bubble_down(H, 0)
H.unmark(old_top_node)
H.unset_index(old_top_node)
return old_top_node
def remove_from_heap(H: BinaryHeap, this_node):
n = H.get_index(this_node)
moved_node = H.heap_node[H.index_of_last]
H.heap_node[n] = moved_node
H.heap_node.pop(H.index_of_last)
H.set_index(moved_node, n)
H.index_of_last = len(H.heap_node) - 1
H.parent_of_last = (H.index_of_last-1)//2
bubble_up(H, n)
bubble_down(H, H.get_index(moved_node))
H.unmark(this_node)
H.unset_index(this_node)
def update_heap(H: BinaryHeap, this_node):
if not H.marked(this_node):
raise ValueError("trying to update a node that is not in the heap")
bubble_up(H, H.get_index(this_node))
bubble_down(H, H.get_index(this_node))
def print_heap(H: BinaryHeap):
i = 0
p = 0
while i <= H.index_of_last:
print(H.key(H.heap_node[i]), " -> ")
if 2*i <= H.index_of_last and i != 0:
print(H.key(H.heap_node[2*i]))
else:
print("NULL")
print(" ")
if 2*i+1 <= H.index_of_last:
print(H.key(H.heap_node[2*i+1]))
else:
print("NULL")
i+=1
def print_pop_all_heap(H: BinaryHeap):
while H.index_of_last >= 0:
node = pop_heap(H)
print(H.key(node))
def check_heap(H: BinaryHeap):
i = 1
if H.index_of_last < 0:
print("Heap is empty")
return True
elif H.get_index(H.heap_node[0]) != 0:
print("There is a problem with the heap (root)")
return False
while i <= H.index_of_last:
if (H.less_than(H.heap_node[i], H.heap_node[(i-1)//2])):
print("There is a problem with the heap order")
return False
elif (H.get_index(H.heap_node[i]) != i):
print("There is a problem with the heap node data ", H.get_index(H.heap_node[i]), " != ", i)
return False
i+=1
print("The heap is OK")
return True
def clean_heap(H: BinaryHeap):
ret_nodes = H.heap_node
for i in range(H.index_of_last + 1):
H.unmark(H.heap_node[i])
H.unset_index(H.heap_node[i])
H.heap_node.clear()
H.index_of_last = -1
H.parent_of_last = -1
return ret_nodes
def bubble_up_b(H: BinaryHeap, n: int):
if (n==0):
return
if n > H.index_of_last:
return
parent = (n-1)//2
while n != 0 and H.less_than(H.heap_node[parent], H.heap_node[n]):
temp_node = H.heap_node[parent]
H.heap_node[parent] = H.heap_node[n]
H.heap_node[n] = temp_node
H.set_index(H.heap_node[parent], parent)
H.set_index(H.heap_node[n], n)
n = parent
parent = (n-1)//2
def bubble_down_b(H: BinaryHeap, n: int):
if 2*n+1 == H.index_of_last:
child = 2*n+1
elif 2*n+2 > H.index_of_last:
return
elif H.greater_than(H.heap_node[2*n+1], H.heap_node[2*n+2]):
child = 2*n+1
else:
child = 2*n+2
while n <= H.parent_of_last and H.greater_than(H.heap_node[child], H.heap_node[n]):
temp_node = H.heap_node[child]
H.heap_node[child] = H.heap_node[n]
H.heap_node[n] = temp_node
H.set_index(H.heap_node[child], child)
H.set_index(H.heap_node[n], n)
n = child
if 2*n+1 == H.index_of_last:
child = 2*n+1
elif 2*n+2 > H.index_of_last:
return
elif H.greater_than(H.heap_node[2*n+1], H.heap_node[2*n+2]):
child = 2*n+1
else:
child = 2*n+2
def add_to_heap_b(H: BinaryHeap, this_node):
if not H.marked(this_node):
H.heap_node.append(this_node)
H.index_of_last = len(H.heap_node) - 1
H.parent_of_last = (H.index_of_last-1)//2
H.set_index(this_node, H.index_of_last)
bubble_up_b(H, H.index_of_last)
H.mark(this_node)
if len(H.heap_node) > H.max_size:
remove_from_heap_b(H, H.heap_node[-1])
else:
raise ValueError("problems")
def top_heap_b(H: BinaryHeap):
return top_heap(H)
def pop_heap_b(H: BinaryHeap):
if H.index_of_last < 0:
return False
old_top_node = H.heap_node[0]
H.heap_node[0] = H.heap_node[H.index_of_last]
H.set_index(H.heap_node[0], 0)
H.heap_node.pop(H.index_of_last)
H.index_of_last = len(H.heap_node) - 1
H.parent_of_last = (H.index_of_last-1)//2
bubble_down_b(H, 0)
H.unmark(old_top_node)
H.unset_index(old_top_node)
return old_top_node
def remove_from_heap_b(H: BinaryHeap, this_node):
n = H.get_index(this_node)
moved_node = H.heap_node[H.index_of_last]
H.heap_node[n] = moved_node
H.heap_node.pop(H.index_of_last)
H.set_index(moved_node, n)
H.index_of_last = len(H.heap_node) - 1
H.parent_of_last = (H.index_of_last-1)//2
bubble_up_b(H, n)
bubble_down_b(H, H.get_index(moved_node))
H.unmark(this_node)
H.unset_index(this_node)
def update_heap_b(H: BinaryHeap, this_node):
if not H.marked(this_node):
raise ValueError("trying to update a node that is not in the heap\n")
bubble_up_b(H, H.get_index(this_node))
bubble_down_b(H, H.get_index(this_node))
def print_heap_b(H: BinaryHeap):
print_heap(H)
def print_pop_all_heap_b(H: BinaryHeap):
print_pop_all_heap(H)
def check_heap_b(H: BinaryHeap):
i = 1
if H.index_of_last < 0:
print("Heap is empty")
return True
elif H.get_index(H.heap_node[0]) != 0:
print("There is a problem with the heap (root)")
return False
while i<= H.index_of_last:
if (H.greater_than(H.heap_node[i], H.heap_node[(i-1)//2])):
print("There is a problem with the heap order")
return False
elif (H.get_index(H.heap_node[i]) != i):
print("There is a problem with the heap node data ", H.get_index(H.heap_node[i]), " != ", i)
return False
i+=1
print("The heap is OK")
return True
def clean_heap_b(H: BinaryHeap):
clean_heap(H)