-
Notifications
You must be signed in to change notification settings - Fork 2
/
kdTreeNode.cpp
590 lines (509 loc) · 16.4 KB
/
kdTreeNode.cpp
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
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
/*
* File: kdTreeNode.cpp
* Author: Koray Gocmen
*
* Created on March 25, 2016, 1:30 PM
*/
#include "kdTreeNode.h"
#include <vector>
#include <math.h>
#include <iostream>
using namespace std;
class kdTreeNode;
kdTreeNode::kdTreeNode() {
x = 0;
y = 0;
beenHere = false;
left = NULL;
right = NULL;
parent = NULL;
}
kdTreeNode::kdTreeNode(float _x, float _y){
x = _x;
y = _y;
beenHere = false;
left = NULL;
right = NULL;
parent = NULL;
}
kdTreeNode::kdTreeNode(const kdTreeNode& orig) {
x = orig.x;
y = orig.y;
left = orig.left;
right = orig.right;
parent = orig.parent;
}
kdTreeNode::~kdTreeNode() {
if(left != NULL)
delete left;
left = NULL;
if(right != NULL)
delete right;
right = NULL;
}
kdTreeNode& kdTreeNode::operator =(const kdTreeNode& R){
x = R.x;
y = R.y;
level = R.level;
beenHere = R.beenHere;
}
float kdTreeNode::getX() const{
return x;
}
float kdTreeNode::getY() const{
return y;
}
bool kdTreeNode::getLevel() const{
return level;
}
bool kdTreeNode::getBeenHere() const{
return beenHere;
}
kdTreeNode* kdTreeNode::getLeft() const{
return left;
}
kdTreeNode* kdTreeNode::getRight() const{
return right;
}
kdTreeNode* kdTreeNode::getParent() const{
return parent;
}
void kdTreeNode::setX(float _x){
x = _x;
}
void kdTreeNode::setY(float _y){
y = _y;
}
void kdTreeNode::setLevel(bool _lev){
level = _lev;
}
void kdTreeNode::setBeenHere(bool _here){
beenHere = _here;
}
void kdTreeNode::setRight(kdTreeNode* newRight){
right = newRight;
}
void kdTreeNode::setLeft(kdTreeNode* newLeft){
left = newLeft;
}
void kdTreeNode::setParent(kdTreeNode* newParent){
parent = newParent;
}
void kdTreeNode::removeNode(kdTreeNode* entry, kdTreeNode* root){
if(this->level){
//x-level
if(this->x > entry->x){
//go left
if(left != NULL){
left->removeNode(entry, root);
}
return;
}
else if(this->x < entry->x){
//go right
if(right != NULL){
right->removeNode(entry, root);
}
return;
}
else{
//this.x == entry.x
if(this->y == entry->y){
//found it. delete it and re-balance tree
if(this->left == NULL && this->right == NULL){
//it is a leaf
if(this->parent->left->x == this->x && this->parent->left->y == this->y){
//we are on parent's left node
this->parent->setLeft(NULL);
delete this;
}
else{
//we are on parent's right node
this->parent->setRight(NULL);
delete this;
}
}else{
//entry has a subtree
//disconnect subtree
if(this->parent == NULL){
//entry is the root itself
//new root is the right node
//root = root->findNewRoot(root);
//this->distributeSubtree(this, root);
//delete this;
}else{
//entry is not the root
if(this->parent->left->x == this->x && this->parent->left->y == this->y){
//we are on parent's left node
this->parent->setLeft(NULL);
}
else{
//we are on parent's right node
this->parent->setRight(NULL);
}
}
//subtree is disconnected
//call connect subtree
this->distributeSubtree(this, root);
}
}
else{
//go right
if(right != NULL){
right->removeNode(entry, root);
}
return;
}
}
}
else if(!(this->level)){
//y-level
if(this->y > entry->y){
//go left
if(left != NULL){
left->removeNode(entry, root);
}
return;
}
else if(this->y < entry->y){
//go right
if(right != NULL){
right->removeNode(entry, root);
}
return;
}
else{
//this.y == entry.y
if(this->x == entry->x){
//found it. delete it and re-balance tree
if(this->left == NULL && this->right == NULL){
//it is a leaf
if(this->parent->left->x == this->x && this->parent->left->y == this->y){
//we are on parent's left node
this->parent->setLeft(NULL);
delete this;
}
else{
//we are on parent's right node
this->parent->setRight(NULL);
delete this;
}
}else{
//entry has a subtree
//disconnect subtree
if(this->parent == NULL){
//entry is the root itself
//new root is the right node
//root = root->findNewRoot(root);
//this->distributeSubtree(this, root);
//delete this;
}else{
//entry is not the root
if(this->parent->left->x == this->x && this->parent->left->y == this->y){
//we are on parent's left node
this->parent->setLeft(NULL);
}
else{
//we are on parent's right node
this->parent->setRight(NULL);
}
}
//subtree is disconnected
//call connect subtree
this->distributeSubtree(this, root);
}
}
else{
//go right
if(right != NULL){
right->removeNode(entry, root);
}
return;
}
}
}
}
void kdTreeNode::insertNode(kdTreeNode* newNode, kdTreeNode* &root){
if(this->level){ //this is a x-level
if(this->x > newNode->x){
//go left for level x
if(left != NULL)
left->insertNode(newNode, root);
else{
newNode->setLevel(!(this->getLevel())); //change the level of the children node
newNode->setParent(this);
this->setLeft(newNode);
return;
}
}
else if(this->x < newNode->x){
//go right for level x;
if(right != NULL)
right->insertNode(newNode, root);
else{
newNode->setLevel(!(this->getLevel())); //change the level of the children node
newNode->setParent(this);
this->setRight(newNode);
return;
}
}
else{
//this.x == newNode.x
if(this->y == newNode->y){
//node already exists
return;
}
else{
//this->x == newNode->x && this.y != newNode->y
//go right for level x;
if(right != NULL)
right->insertNode(newNode, root);
else{
newNode->setLevel(!(this->getLevel())); //change the level of the children node
newNode->setParent(this);
this->setRight(newNode);
return;
}
}
}
}//end of if (x-level)
else if(!(this->level)){ //this is a y-level
if(this->y > newNode->y){
//go left for level y
if(left != NULL)
left->insertNode(newNode, root);
else{
newNode->setLevel(!(this->getLevel())); //change the level of the children node
newNode->setParent(this);
this->setLeft(newNode);
return;
}
}
else if(this->y < newNode->y){
//go right for level y
if(right != NULL)
right->insertNode(newNode, root);
else{
newNode->setLevel(!(this->getLevel())); //change the level of the children node
newNode->setParent(this);
this->setRight(newNode);
return;
}
}
else{
//this.y == newNode.y
if(this->x == newNode->x){
//node already exists
return;
}
else{
//this->y == newNode->y && this->x != newNode->y
//go right for level y
if(right != NULL)
right->insertNode(newNode, root);
else{
newNode->setLevel(!(this->getLevel())); //change the level of the children node
newNode->setParent(this);
this->setRight(newNode);
return;
}
}
}
}
}
kdTreeNode* kdTreeNode::findClosestNode(kdTreeNode* entry){
vector<kdTreeNode>path;
//start from the root vector
findPath(entry);
//previous smallest point
//initialized to null in the beginning
kdTreeNode dummy;
kdTreeNode *previousSmallest = &dummy;
//current smallest node in path
//called with the root
kdTreeNode *smallestNode = closestNodeInPath(entry, this);
while(!(previousSmallest->getX() == smallestNode->getX() && previousSmallest->getY() == smallestNode->getY())){ //visited before?
previousSmallest = smallestNode;
//find the unvisited child of the smallest node
bool checkedLeft = false;
bool checkedRight = false;
if(smallestNode->getLeft()!=NULL && smallestNode->left->beenHere){
checkedLeft = true;
}
else if(smallestNode->getRight()!=NULL && smallestNode->right->beenHere){
checkedRight = true;
}
this->clearFlags();
//find the new path
if(checkedLeft){
//add the smallest to the new path
smallestNode->setBeenHere(true);
if(smallestNode->getRight() != NULL)
(smallestNode->getRight())->findPath(entry);
}
else if(checkedRight){
//add the smallest to the new path
smallestNode->setBeenHere(true);
if(smallestNode->getLeft() != NULL)
(smallestNode->getLeft())->findPath(entry);
}else{
smallestNode->setBeenHere(true);
smallestNode->findPath(entry);
}
//update the smallest node
smallestNode = closestNodeInPath(entry, smallestNode);
}
return smallestNode;
}
kdTreeNode* kdTreeNode::closestNodeInPath(kdTreeNode* entry, kdTreeNode* root){
double distanceHolder = 100000000;
kdTreeNode* nodeHolder;
kdTreeNode* current = root;
//if there is only one node in the tree,
//if there are multiple nodes these lines will not make a difference
nodeHolder = current;
bool noAvailableNode = false;
while(!noAvailableNode){
//continue as long as we don't reach a leaf
//change the flag
double tempDistance = findDistanceBetweenTwoPoints(entry, current);
if(tempDistance < distanceHolder){
distanceHolder = tempDistance;
nodeHolder = current;
}
bool nextNodeIsLeft = false;
bool nextNodeIsRight = false;
//find the next node to go
if(current->left != NULL){
if(current->left->beenHere == true){
nextNodeIsLeft = true;
noAvailableNode = false;
current = current->left;
}else{
noAvailableNode = true;
}
}
if(current->right != NULL && !nextNodeIsLeft){
if(current->right->beenHere == true){
nextNodeIsRight = true;
noAvailableNode = false;
current = current->right;
}else{
noAvailableNode = true;
}
}
if(current->left == NULL && current->right == NULL && !nextNodeIsLeft && !nextNodeIsRight){
//we are on the leaf
noAvailableNode = true;
}
}
return nodeHolder;
}
void kdTreeNode::findPath(kdTreeNode *entry){
this->setBeenHere(true);
if(this->level){
//x-level
if(this->x > entry->getX()){
//go left
if(left != NULL)
left->findPath(entry);
}
else if(this->x < entry->getX()){
//go right
if(right != NULL)
right->findPath(entry);
}
else{
//this.x == entry.x
if(this->y == entry->getY()){
//the same point
//we don't need to check the rest of the tree
}else{
//different point but has the same x value
//go right
if(right != NULL)
right->findPath(entry);
}
}
}
else{
//y-level
if(this->y > entry->getY()){
//go left
if(left != NULL)
left->findPath(entry);
}
else if(this->y < entry->getY()){
//go right
if(right != NULL)
right->findPath(entry);
}
else{
//this.y == entry.y
if(this->x == entry->getX()){
//the same point
//we don't need to check the rest of the tree
}else{
//different point but has the same y value
//go right
if(right != NULL)
right->findPath(entry);
else{
//right empty
}
}
}
}//end of y-level
}
void kdTreeNode::printNodes(){
if(left != NULL)
left->printNodes();
if(this->parent != NULL){
cout<<"("<<this->x<<","<<this->y<<")==>level: "
<<level<<"==>parent: ("<<this->getParent()->x
<<","<<this->getParent()->y<<")"<<endl;
}else{
cout<<"("<<this->x<<","<<this->y<<")==>level: "
<<level<<"==>parent: ROOT"<<endl;
}
if(right != NULL)
right->printNodes();
}
void kdTreeNode::clearFlags(){
if(left != NULL)
left->clearFlags();
if(this->beenHere){
this->setBeenHere(false);
}
if(right != NULL)
right->clearFlags();
}
void kdTreeNode::distributeSubtree(kdTreeNode* entry, kdTreeNode* root){
if(this == NULL)
return;
if(this->left != NULL)
left->distributeSubtree(entry, root);
if(this->right != NULL)
right->distributeSubtree(entry, root);
if(!(this->x == entry->x && this->y == entry->y)){
this->setBeenHere(false);
this->setLeft(NULL);
this->setRight(NULL);
this->setParent(NULL);
root->insertNode(this, root);
}
else{
this->setRight(NULL);
this->setLeft(NULL);
this->setParent(NULL);
delete this;
}
}
double kdTreeNode::findDistanceBetweenTwoPoints(kdTreeNode* node1, kdTreeNode* node2){
float x1 = node1->getX();
float y1 = node1->getY();
float x2 = node2->getX();
float y2 = node2->getY();
double distance = sqrt(pow((x1-x2),2) + pow((y1-y2),2));
return distance;
}