-
Notifications
You must be signed in to change notification settings - Fork 1
/
intervalt.py
403 lines (334 loc) · 10.7 KB
/
intervalt.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
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
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
"""
========
Intervalt
Interval tree implementation in Python.
Interval trees hold intervals in tree structure
so that overlaps can be observed faster.
"""
import sys
ver = sys.version_info.major
# If 2 change property logic TODO
class InvalidInterval(Exception):
pass
class Interval(object):
""" Interval class for holding interval boundaries """
def __init__(self, l, r):
if l > r:
raise InvalidInterval
self._l = l
self._r = r
@property
def left(self):
return self._l
@property
def right(self):
return self._r
def check_overlap(self, other):
if self.right > other.left or self.left < other.right:
return True
return False
def __repr__(self):
return "Interval[" str(self.left) ", " str(self.right) "]"
class Node(object):
""" Node structure holds an interval """
def __init__(self, interval):
self._interval = interval
self._parent = None
self._left_node = None
self._right_node = None
self._maxval = interval.right
@property
def interval(self):
""" Interval getter """
return self._interval
@property
def parent(self):
""" Parent node getter """
return self._parent
@property
def left_node(self):
""" Left node getter """
return self._left_node
@property
def right_node(self):
""" Right node getter """
return self._right_node
@property
def maxval(self):
""" Max value in subtree getter """
return self._maxval
@parent.setter
def parent(self, value):
""" Parent node setter """
self._parent = value
@left_node.setter
def left_node(self, value):
""" Left node setter """
self._left_node = value
if value != None:
value.parent = self
self.maxval = max(self.maxval, value.maxval)
@right_node.setter
def right_node(self, value):
""" Right node setter """
self._right_node = value
if value != None:
value.parent = self
self.maxval = max(self.maxval, value.maxval)
@right_node.deleter
def right_node(self):
""" Right node deleter """
self._right_node = None
@left_node.deleter
def left_node(self):
""" Left node deleter """
self._left_node = None
@maxval.setter
def maxval(self, value):
""" Max value in subtree setter """
self._maxval = value
def __repr__(self):
st = "Node holding " repr(self.interval)
if self.left_node != None:
st = " Has left child "
if self.right_node != None:
st = " Has right child "
return st
class IntervalTree(object):
"""
Generic abstract tree structure.
"""
def __init__(self, root=None):
self._root = root
@property
def root(self):
""" Tree root getter """
return self._root
@root.setter
def root(self, val):
""" Tree root setter """
self._root = val
def add(self, node):
""" Tree add rule """
pass
def remove(self, node):
""" Tree remove rule """
pass
def check_overlap(self, node):
""" Check if node overlaps """
pass
def __repr__(self):
return "Interval tree with root: " repr(self.root)
class IntervalBSTree(IntervalTree):
"""
Interval tree structure that uses simple
Binary Search Tree as underlying tree.
Supports adding, removing and checking overlaps.
"""
def add(self, node):
"""
BST Tree add rule
Add according to left value of interval
"""
self._add(self.root, node)
def _add(self, parent, node):
pl = parent.interval.left
nl = node.interval.left
if pl > nl:
if parent.left_node == None:
parent.left_node = node
else:
self._add(parent.left_node, node)
else:
if parent.right_node == None:
parent.right_node = node
else:
self._add(parent.right_node, node)
def remove(self, node):
"""
BST Tree remove rule
Search according to left value of interval
"""
self._remove(self.root, node)
def _del(self, gp, p, c):
if gp == None:
self.root = c
elif p == gp.left_node:
del gp.left_node # if node is left child
gp.left_node = c
else:
del gp.right_node # if node is left child
gp.right_node = c
def _findMin(self, node):
""" Find leftmost min """
n = node
while n.left_node != None:
n = n.left_node
return n
def _remove(self, parent, node):
pl = parent.interval.left
nl = node.interval.left
if parent == node:
# We have found target node
if node.left_node == None and node.right_node==None:
self._del(node.parent, node, None)
elif node.left_node != None and node.right_node==None:
temp = node.left_node
self._del(node.parent, node, temp)
elif node.left_node == None and node.right_node!=None:
temp = node.right
self._del(node.parent, node, temp)
else:
self._del(node.parent, node,self._findMin(node.right_node))
if pl > nl:
if parent.left_node == None:
return None
else:
self._remove(parent.left_node, node)
else:
if parent.right_node == None:
return None
else:
self._remove(parent.right_node, node)
def check_overlap(self, interval):
"""
Check if node overlaps
Steps:
Recursively ->
If node overlaps. Return node.
Else check condition below
condition: node->left is not null and node-left->maxval >= interval->left
If condition
Check left
Else:
Check right
"""
if self.root == None:
return None
else:
return self._check_overlap(self.root, interval)
def _check_overlap(self, node, interval):
if node.interval.check_overlap(interval):
return node.interval
else:
if node.left_node != None and node.left_node.maxval >= interval.left:
return self._check_overlap(node.left_node, interval)
elif node.right_node !=None:
return self._check_overlap(node.right_node, interval)
else:
return None
class IntervalAVLTree(IntervalTree):
"""
Interval tree structure that uses simple
AVL Tree as underlying tree.
Supports adding, removing and checking overlaps.
"""
def add(self, node):
""" BST Tree add rule """
pass
def remove(self, node):
""" BST Tree remove rule """
pass
def check_overlap(self, node):
""" Check if node overlaps """
pass
class IntervalRBTree(IntervalTree):
"""
Interval tree structure that uses simple
Red Black Tree as underlying tree.
Supports adding, removing and checking overlaps.
"""
def add(self, node):
""" BST Tree add rule """
pass
def remove(self, node):
""" BST Tree remove rule """
pass
def check_overlap(self, node):
""" Check if node overlaps """
pass
bintrees_enabled = False
# if Python 3
if ver == 3:
import importlib
from importlib import util
bintrees_enabled = util.find_spec("bintrees") != None # some serious shit going on here
# if Python 2
if ver == 2:
import imp
try:
imp.find_module("bintrees")
bintrees_enabled = True
except ImportError:
bintrees_enabled = False
if bintrees_enabled:
import bintrees
print("Bintrees is enabled")
class IntervalBSTreeBintreesAdapter(IntervalTree):
"""
I do not know how this adapter pattern should work.
Or if it is even possible to augment bintrees classes.
Will try to interact underlying bintrees.BinaryTree with
IntervalTree interface
"""
def __init__(self, root):
# super init TODO
self._tree = bintrees.BinaryTree([(root.interval, root)])
@property
def tree():
return self._tree
def add(self, node):
self._tree.insert(node.interval, node)
def remove(self, node):
self._tree.remove(node.interval)
def check_overlap(self, interval):
for k,v in self._tree.items():
if k.check_overlap(interval):
return v
return None
class IntervalRBTreeBintreesAdapter(IntervalTree):
"""
I do not know how this adapter pattern should work.
Or if it is even possible to augment bintrees classes.
Will try to interact underlying bintrees.BinaryTree with
IntervalTree interface
"""
def __init__(self, root):
# super init TODO
self._tree = bintrees.RBTree([(root.interval, root)])
@property
def tree():
return self._tree
def add(self, node):
self._tree.insert(node.interval, node)
def remove(self, node):
self._tree.remove(node.interval)
def check_overlap(self, interval):
for k,v in self._tree.items():
if k.check_overlap(interval):
return v
return None
class IntervalAVLTreeBintreesAdapter(IntervalTree):
"""
I do not know how this adapter pattern should work.
Or if it is even possible to augment bintrees classes.
Will try to interact underlying bintrees.BinaryTree with
IntervalTree interface
"""
def __init__(self, root):
# super init TODO
self._tree = bintrees.AVLTree([(root.interval, root)])
@property
def tree():
return self._tree
def add(self, node):
self._tree.insert(node.interval, node)
def remove(self, node):
self._tree.remove(node.interval)
def check_overlap(self, interval):
for k,v in self._tree.items():
if k.check_overlap(interval):
return v
return None
# create Strategy Pattern for each tree type TODO
# create a Strategy Factory TODO