-
Notifications
You must be signed in to change notification settings - Fork 2
/
btree_search.py
51 lines (36 loc) · 1.2 KB
/
btree_search.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
# coding: utf-8
# In[120]:
import copy
import pickle
# In[122]:
def is_number(s):
try:
float(s) # for int, long and float
except ValueError:
try:
complex(s) # for complex
except ValueError:
return False
return True
def row_operation(tree, num, op):
if op == '>':
row = list(tree.values(min=num, excludemin=True))
elif op == '>=':
row = list(tree.values(min=num, excludemax=False))
elif op == '<':
row = list(tree.values(max=num, excludemax=True))
elif op == '<=':
row = list(tree.values(max=num, excludemax=False))
elif op == '=':
row = list(tree.values(min=num, max=num, excludemin=False, excludemax=False))
elif op == '<>':
row = list(tree.values(max=num, excludemax=True)) + list(tree.values(min=num, excludemax=True))
output = [j for i in row for j in i]
return output
# In[126]:
def get_rows(filename, compare_value, op):
if is_number(compare_value):
compare_value = float(compare_value)
with open(filename, 'rb') as f:
Btree = pickle.load(f)
return row_operation(Btree, compare_value, op)