-
Notifications
You must be signed in to change notification settings - Fork 0
/
binary_heap.py
163 lines (129 loc) · 4.86 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
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
import numpy as np
class Heap:
def __init__(self, items=None, max_items=1000):
self._items = items
self._keys = np.ones(max_items) * -np.Inf
self._indexes = np.array(range(max_items))
if items is not None:
self._keys[:len(items)] = np.array([item.key for item in items])
self._heapify()
def __len__(self):
if self._items is None:
return 0
else:
return len(self._items)
def __repr__(self):
return "{}({!r})".format(self.__class__.__name__, self.__dict__)
def __str__(self):
index = 0
draw = '(' + str(self._keys[index]) + ')\n'
if 2 * index + 1 < len(self):
draw = self._traverse_tree(draw, ' ', 2 * index + 1, 0, False)
if 2 * index + 2 < len(self):
draw = self._traverse_tree(draw, ' ', 2 * index + 2, 0, True)
return draw
def insert(self, item):
self._keys[len(self)] = item.key
self._items.append(item)
possible_shift, ix = True, len(self) - 1
while possible_shift:
ix, possible_shift = self._shift_up(ix)
def _shift_down(self, ix):
if self._keys[2*ix + 1] > self._keys[2*ix + 2]:
return 2*ix + 1, self._swap(2*ix + 1, ix)
else:
return 2*ix + 2, self._swap(2*ix + 2, ix)
def _shift_up(self, ix):
return int((ix - 1)/2), self._swap(ix, int((ix-1)/2))
def _swap(self, i1, i2):
if self._keys[i1] > self._keys[i2]:
self._keys[i1], self._keys[i2] = self._keys[i2], self._keys[i1]
self._indexes[i1], self._indexes[i2] = self._indexes[i2], self._indexes[i1]
return True
else:
return False
def find_max(self):
return self._keys[0], self._items[self._indexes[0]]
def delete_max(self):
max_index = self._indexes[0]
self._keys[0], self._keys[len(self)-1] = self._keys[len(self)-1], self._keys[0]
self._indexes[0], self._indexes[len(self)-1] = self._indexes[len(self)-1], self._indexes[0]
self._indexes[:len(self)][self._indexes[:len(self)] > max_index] -= 1
self._indexes[len(self)-1] = len(self) - 1
del self._items[max_index]
self._keys[len(self)] = -np.Inf
possible_shift, ix = True, 0
while possible_shift:
ix, possible_shift = self._shift_down(ix)
def extract_max(self):
max_key, max_item = self.find_max()
self.delete_max()
return max_key, max_item
def _heapify(self):
for i in reversed(range(int(len(self) / 2))):
possible_shift = True
ix = i
while possible_shift:
ix, possible_shift = self._shift_down(ix)
def _traverse_tree(self, draw, pad, index, level, is_right):
draw += pad
left = True if 2*index + 1 < len(self) else False
right = True if 2*index + 2 < len(self) else False
if is_right or index + 1 >= len(self):
pad += ' '
draw += ' └──'
else:
pad += ' | '
draw += ' ├──'
draw += '(' + str(self._keys[index]) + ') \n'
if left:
draw = self._traverse_tree(draw, pad, 2*index+1, level+1, False)
if right:
draw = self._traverse_tree(draw, pad, 2*index+2, level+1, True)
return draw
class Event:
def __init__(self, key, point):
self.key = key
self.point = point
def __str__(self):
return str(self.point)
def log_queue(queue, iz):
if iz:
print(' --------------------- ')
print(f'keys: {queue._keys[:len(queue)]}')
print(f'indexes: {queue._indexes[:len(queue)]}')
print(f'items: {[str(item) for item in queue._items]}')
if __name__ == '__main__':
points = [(2.5, 9), (5, 8.5), (8, 7), (6, 5.75), (3.5, 4), (1, 2.5), (7, 2), (9, 1.5), (3, 0.75)]
queue = Heap([Event(key=point[1], point=point) for point in points])
iz = 1
log_queue(queue, iz)
sweep_line, event = queue.extract_max()
print(event.point)
log_queue(queue, iz)
sweep_line, event = queue.extract_max()
print(event.point)
log_queue(queue, iz)
sweep_line, event = queue.extract_max()
print(event.point)
log_queue(queue, iz)
point = (2, -13.2045)
point2=(3, -13.2045)
queue.insert(Event(point[1], point))
log_queue(queue, iz)
queue.insert(Event(point2[1], point2))
log_queue(queue, iz)
sweep_line, event = queue.extract_max()
print(event.point)
point = (4.111, 3.51658717)
point2 = (6.334027, 5.715896)
queue.insert(Event(point[1], point))
log_queue(queue, iz)
queue.insert(Event(point2[1], point2))
log_queue(queue, iz)
sweep_line, event = queue.extract_max()
print(event.point)
log_queue(queue, iz)
print(queue)
sweep_line, event = queue.extract_max()
print(event.point)