-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathkdTree.js
More file actions
338 lines (292 loc) · 9.17 KB
/
kdTree.js
File metadata and controls
338 lines (292 loc) · 9.17 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
function kdTree(points, _metric, _dimensions) {
var metric = _metric || function(a, b) {
var dx = a.x - b.x;
var dy = a.y - b.y;
return Math.sqrt(dx*dx + dy*dy);
}
var dimensions = _dimensions || ['x', 'y'];
function Node(obj, d, parent) {
this.obj = obj;
this.left = null;
this.right = null;
this.parent = parent;
this.dimension = d;
}
var self = this;
this.root = buildTree(points, 0, null);
function buildTree(points, depth, parent) {
var dim = depth % dimensions.length;
if(points.length == 0) return null;
if(points.length == 1) return new Node(points[0], dim, parent);
points.sort(function(a,b){ return a[dimensions[dim]] - b[dimensions[dim]]; });
var median = Math.floor(points.length/2);
var node = new Node(points[median], dim, parent);
node.left = buildTree(points.slice(0,median), depth+1, node);
node.right = buildTree(points.slice(median+1), depth+1, node);
return node;
}
this.insert = function(point) {
var insertPosition = innerSearch(self.root, null);
if(insertPosition == null) {
self.root = new Node(point, 0, null);
return;
}
var newNode = new Node(point, (insertPosition.dimension+1)%dimensions.length, insertPosition);
var dimension = dimensions[insertPosition.dimension];
if(point[dimension] < insertPosition.obj[dimension]) {
insertPosition.left = newNode;
} else {
insertPosition.right = newNode;
}
function innerSearch(node, parent) {
if(node == null) return parent;
var dimension = dimensions[node.dimension];
if(point[dimension] < node.obj[dimension]) {
return innerSearch(node.left, node);
} else {
return innerSearch(node.right, node);
}
}
}
this.remove = function(point) {
var node = nodeSearch(self.root);
if(node == null) return;
removeNode(node);
function nodeSearch(node) {
if(node == null) return null;
if(node.obj === point) return node;
var dimension = dimensions[node.dimension];
if(point[dimension] < node.obj[dimension]) {
return nodeSearch(node.left, node);
} else {
return nodeSearch(node.right, node);
}
}
function removeNode(node) {
if(node.left == null && node.right == null) {
if(node.parent == null) {
self.root = null;
return;
}
var pDimension = dimensions[node.parent.dimension];
if(node.obj[pDimension] < node.parent.obj[pDimension]) {
node.parent.left = null;
} else {
node.parent.right = null;
}
return;
}
if(node.left != null) {
var nextNode = findMax(node.left, node.dimension);
} else {
var nextNode = findMin(node.right, node.dimension);
}
var nextObj = nextNode.obj;
removeNode(nextNode);
node.obj = nextObj;
function findMax(node, dim) {
if(node == null) return null;
var dimension = dimensions[dim];
if(node.dimension == dim) {
if(node.right != null) return findMax(node.right, dim);
return node;
}
var own = node.obj[dimension]
var left = findMax(node.left, dim);
var right = findMax(node.right, dim);
var max = node;
if(left != null && left.obj[dimension] > own) max = left;
if(right != null && right.obj[dimension] > max.obj[dimension]) max = right;
return max;
}
function findMin(node, dim) {
if(node == null) return null;
var dimension = dimensions[dim];
if(node.dimension == dim) {
if(node.left != null) return findMin(node.left, dim);
return node;
}
var own = node.obj[dimension]
var left = findMin(node.left, dim);
var right = findMin(node.right, dim);
var min = node;
if(left != null && left.obj[dimension] < own) min = left;
if(right != null && right.obj[dimension] < min.obj[dimension]) min = right;
return min;
}
}
}
this.nearest = function(point, maxNodes, maxDistance, _dimension) {
bestNodes = new BinaryHeap(function(e){ return -e[1]; });
if(maxDistance) {
for(var i=0; i<maxNodes; i++) {
bestNodes.push([null, maxDistance]);
}
}
nearestSearch(self.root);
function nearestSearch(node) {
var bestChild;
var dimension = dimensions[node.dimension];
var ownDistance = metric(point, node.obj);
var linearPoint = {};
for(var i=0; i<dimensions.length; i++) {
if(i == node.dimension) {
linearPoint[dimensions[i]] = point[dimensions[i]];
} else {
linearPoint[dimensions[i]] = node.obj[dimensions[i]];
}
}
var linearDistance = metric(linearPoint, node.obj);
if(node.right == null && node.left == null) {
if(bestNodes.size() < maxNodes || ownDistance < bestNodes.peek()[1]) {
saveNode(node, ownDistance);
}
return;
}
if(node.right == null) {
bestChild = node.left;
} else if(node.left == null) {
bestChild = node.right;
} else {
if(point[dimension] < node.obj[dimension]) {
bestChild = node.left;
} else {
bestChild = node.right;
}
}
nearestSearch(bestChild);
if(bestNodes.size() < maxNodes || ownDistance < bestNodes.peek()[1]) {
saveNode(node, ownDistance);
}
if(bestNodes.size() < maxNodes || Math.abs(linearDistance) < bestNodes.peek()[1]) {
var otherChild;
if(bestChild == node.left) {
otherChild = node.right;
} else {
otherChild = node.left;
}
if(otherChild != null) nearestSearch(otherChild);
}
function saveNode(node, distance) {
if (_dimension) {
var d = dimensions.indexOf(_dimension);
for(var i = 0; i < dimensions.length; i++) {
if (i == d && node.obj[dimensions[i]] != point[dimensions[i]]) {
return;
}
}
}
bestNodes.push([node, distance]);
if(bestNodes.size() > maxNodes) {
bestNodes.pop();
}
}
}
var result = [];
for(var i=0; i<maxNodes; i++) {
if(bestNodes.content[i][0]) {
result.push([bestNodes.content[i][0].obj, bestNodes.content[i][1]]);
}
}
return result;
}
this.balanceFactor = function() {
return height(self.root)/(Math.log(count(self.root))/Math.log(2));
function height(node) {
if(node == null) return 0;
return Math.max(height(node.left), height(node.right)) + 1;
}
function count(node) {
if(node == null) return 0;
return count(node.left) + count(node.right) + 1;
}
}
// Binary heap implementation from:
// http://eloquentjavascript.net/appendix2.html
function BinaryHeap(scoreFunction){
this.content = [];
this.scoreFunction = scoreFunction;
}
BinaryHeap.prototype = {
push: function(element) {
this.content.push(element);
this.bubbleUp(this.content.length - 1);
},
pop: function() {
var result = this.content[0];
var end = this.content.pop();
if (this.content.length > 0) {
this.content[0] = end;
this.sinkDown(0);
}
return result;
},
peek: function() {
return this.content[0];
},
remove: function(node) {
var len = this.content.length;
for (var i = 0; i < len; i++) {
if (this.content[i] == node) {
var end = this.content.pop();
if (i != len - 1) {
this.content[i] = end;
if (this.scoreFunction(end) < this.scoreFunction(node))
this.bubbleUp(i);
else
this.sinkDown(i);
}
return;
}
}
throw new Error("Node not found.");
},
size: function() {
return this.content.length;
},
bubbleUp: function(n) {
var element = this.content[n];
while (n > 0) {
var parentN = Math.floor((n + 1) / 2) - 1,
parent = this.content[parentN];
if (this.scoreFunction(element) < this.scoreFunction(parent)) {
this.content[parentN] = element;
this.content[n] = parent;
n = parentN;
}
else {
break;
}
}
},
sinkDown: function(n) {
var length = this.content.length,
element = this.content[n],
elemScore = this.scoreFunction(element);
while(true) {
var child2N = (n + 1) * 2, child1N = child2N - 1;
var swap = null;
if (child1N < length) {
var child1 = this.content[child1N],
child1Score = this.scoreFunction(child1);
if (child1Score < elemScore)
swap = child1N;
}
if (child2N < length) {
var child2 = this.content[child2N],
child2Score = this.scoreFunction(child2);
if (child2Score < (swap == null ? elemScore : child1Score))
swap = child2N;
}
if (swap != null) {
this.content[n] = this.content[swap];
this.content[swap] = element;
n = swap;
}
else {
break;
}
}
}
};
}