-
Notifications
You must be signed in to change notification settings - Fork 2
/
bst.py
253 lines (208 loc) · 6.1 KB
/
bst.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
import math
def EuclideanDistance(a, b):
"""
Compute the euclidean distance between two points.
Parameters
----------
a : tuple
Pair of float (x, y)
b : tuple
Pair of float (x, y)
Returns
-------
float
"""
return math.sqrt( (a[0] - b[0])**2 + (a[1] - b[1])**2 )
## ## ##### ###### #########
### ## ## ## ## ## ##
## ## ## ## ## ## ## ##
## ## ## ## ## ## ## ######
## ## ## ## ## ## ## ##
## ### ## ## ## ## ##
## ## ##### ###### #########
class Node:
"""
Node of the BST
"""
def __init__(self, d):
self.left = None
self.right = None
self.data = d
def insert(self, d):
"""
Insert the data as a node, left or right.
Parameters
----------
d : tuple
Pair of float (x, y)
Return
------
True if new Node created
False if the data is already in a node
"""
if self.data == d:
return False
elif d < self.data:
if self.left:
return self.left.insert(d)
else:
self.left = Node(d)
return True
elif d > self.data:
if self.right:
return self.right.insert(d)
else:
self.right = Node(d)
return True
def findClosest(self, d):
"""
Find the closest Node to given data.
It returns the closest Node data found.
Parameter
---------
d : tuple
Pair of float (x, y)
Return
------
tuple
Pair of float (x, y)
"""
if self.data == d:
return d
elif d < self.data:
if self.left:
return self.left.findClosest(d)
else:
return self.data
elif d > self.data:
if self.right:
return self.right.findClosest(d)
else:
return self.data
def nnsearch(self, p, minDist, bestNode):
"""
Nearest neighbour search
TODO Doc
"""
if self.data == p:
minDist = 0
bestNode = self.data
return minDist, bestNode
dist = EuclideanDistance(self.data, p)
if dist < minDist:
minDist = dist
bestNode = self.data
if self.data > p:
# Check if hypersphere crosses hyperplane
if ((abs(self.data[0] - p[0]) < dist) or (abs(self.data[1] - p[1]) < dist)) and self.right:
minDist, bestNode = self.right.nnsearch(p, minDist, bestNode)
# Explore closer to the point
if self.left:
minDist, bestNode = self.left.nnsearch(p, minDist, bestNode)
elif self.data < p:
if ((abs(self.data[0] - p[0]) < dist) or (abs(self.data[1] - p[1]) < dist)) and self.left:
minDist, bestNode = self.left.nnsearch(p, minDist, bestNode)
# Explore closer to the point
if self.right:
minDist, bestNode = self.right.nnsearch(p, minDist, bestNode)
return minDist, bestNode
def isLeaf(self):
"""
Tells if the Node is a leaf in the tree.
Return
------
True if the Node is a leaf
"""
return not self.left and not self.right
def printNodes(self):
"""
Recursively build an array of node data.
Return
------
array
"""
arr = list()
arr.append(self.data)
if self.left:
arr.append(self.left.printNodes())
if self.right:
arr.append(self.right.printNodes())
return arr
######## ####### ##########
## ## ## ## ##
## ## ## ##
######## ####### ##
## ## ## ##
## ## ## ## ##
######## ####### ##
class BST:
"""
Binary Search Tree implementation for tuples of floats.
"""
def __init__(self):
self.root = None
def insert(self, data):
"""
Add a node to the bst.
If the root has not been set yet, this data becomes the root.
Otherwise, insert it inside the tree using the Node.insert() method.
Parameters
----------
node : tuple
Pair of float (x, y)
"""
if self.root:
self.root.insert(data)
else:
self.root = Node(data)
def findClosest(self, d):
"""
Find the closest Node to the given data in the current bst.
Parameter
---------
d : tuple
Pair of float (x, y)
Return
------
tuple of pair of float if the tree has a root
None otherwise
"""
if self.root:
return self.root.findClosest(d)
else:
return None
def nnsearch(self, p):
"""
TODO Doc
"""
dist, center = self.root.nnsearch(p, float('inf'), self.root.data)
return center
def bstToArray(self):
"""
[root, [left], [right]] for the simplest tree.
Each element is the data of the node.
A more intricate tree could be
[root, [left1, [left2], [right2, [left3]]], [right1, [left4]]]
Return
------
array of tuples
"""
arr = list()
if self.root:
arr = self.root.printNodes()
return arr
def buildBalancedBST(array, bst):
"""
Recursive function populating a bst in a balanced way.
There isn't much verification that needs to be done, you can't overflow with an array slice.
Parameter
---------
array : list()
Non-balanced array
"""
if len(array) > 0:
array.sort()
mid = int(len(array)/2)
bst.insert(array[mid])
buildBalancedBST(array[:mid], bst)
buildBalancedBST(array[mid+1:], bst)