-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathKTree.cpp
More file actions
187 lines (138 loc) · 4.08 KB
/
KTree.cpp
File metadata and controls
187 lines (138 loc) · 4.08 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
#include <math.h>
#include <iostream>
#include "KTree.h"
KTree::KTree(int k) {
_k = k;
_bpq = new BPQ(_k);
_root = new TreeNode();
}
KTree::~KTree() {}
void KTree::refreshBPQ() {
delete _bpq;
_bpq = new BPQ(_k);
}
void KTree::insert(int x, int y, TreeNode *leaf, TreeNode *parent)
{
if(leaf == NULL)
{
std::cout << "leaf is null so adding new node for " << x << "\n";
leaf = new TreeNode();
leaf->setX(x);
leaf->setY(y);
leaf->setLeft(0);
leaf->setRight(0);
leaf->setParent(parent);
// add to parent either left or right
// DEBUG - must check y too
if(x > parent->getX()) {
parent->setRight(leaf);
} else {
parent->setLeft(leaf);
}
}
else {
if (0) {
if(y < leaf->getY()){
insert(x,y, leaf->getLeft(), leaf);
}
else if(y > leaf->getY()){
insert(x,y, leaf->getRight(), leaf);
}
} else {
if(x < leaf->getX()){
std::cout << "traversing left\n";
std::cout << "x is " << x << " and leaf x is " << leaf->getX() << "so \n";
insert(x,y, leaf->getLeft(), leaf);
}
else if(x > leaf->getX()){
std::cout << "x is " << x << " and leaf x is " << leaf->getX() << "so \n";
std::cout << "traversing right\n";
insert(x,y, leaf->getRight(), leaf );
}
}
}
}
void KTree::KNN(int x, int y, TreeNode* leaf, int forward) {
std::cout << "finding nearest neighbour to x " << x << " " << y << "\n";
if(leaf == NULL) {
std::cout << "exiting KNN at this level" << "\n";
return;
}
//if distance(curr, guess) < bestDist
// bestDist = distance(curr, guess)
// guess = curr
int currentx = leaf->getX();
int currenty = leaf->getY();
int diffx = x - currentx;
int diffy = y - currenty;
float r = 0.0;
r = sqrt((diffx * diffx) + (diffy * diffy)); // distance from currentGuess to target
TreeNode* parent = leaf->getParent();
// for k > n need to keep a priority queue of NNs
//if(r < currentBestDist) {
// printf("setting best \n");
// currentBestDist = r;
// currentBest = leaf;
//}
BPQPoint* bpqp = new BPQPoint();
bpqp->setX(currentx);
bpqp->setY(currenty);
bpqp->setP(kdround(r));
_bpq->enqueue(bpqp);
/* Recursively search the half of the tree that contains the test point. */
//if ai < curri
// recursively search the left subtree on the next axis
//else
// recursively search the right subtree on the next axis
if (0) {
if(y < leaf->getY()){
KNN(x,y, leaf->getLeft(),0);
}
else if(y > leaf->getY()){
KNN(x,y, leaf->getRight(),1);
}
} else {
if(x < leaf->getX()){
KNN(x,y, leaf->getLeft(),0);
}
else if(x > leaf->getX()){
KNN(x,y,leaf->getRight(),1);
}
}
std::cout << "checking hypersphere \n";
//printf("parent is %d\n",parent);
if(parent==NULL) {
std::cout << "parent is null\n";
} else {
std::cout << "parent x " << parent->getX() << "\n";
/* If the candidate hypersphere crosses this splitting plane, look on the
* * other side of the plane by examining the other subtree.
* */
// if |curri – ai| < bestDist
// recursively search the other subtree on the next axis
// is there a possibility that the best node is on the other side of the hyperplane
float dist = 0.0;
// get the nearest point on the hyperplane
dist = parent->getSY() == 1 ? abs(parent->getY() - y) : abs(parent->getX() - x);
// if the radius around the piont touches the plane then need to search other side
//if(dist <= currentBestDist) {
//
BPQPoint* lpp = _bpq->min_priority_elem(); // this is max distance so increases radius for k search
std::cout << "lppp " << lpp->getP() << "\n";
int lpp_dist = lpp->getP();
if(dist <= lpp_dist || (!_bpq->is_full())) {
// then need to search the of parent
// on the opposite side - finish this
std::cout << "nearest neighbour might be on other side\n";
// I was searching right so search left
// if the node is null then it is going to keep coming back in
if(forward == 0) {
std::cout << "getting parent right";
KNN(x, y, parent->getRight(), 1);
} else {
std::cout << "getting parent left";
KNN(x, y, parent->getLeft(), 0);
}
}
}
}