/
kdtree.h
310 lines (251 loc) · 7.39 KB
/
kdtree.h
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
//
// Created by Shuyang Shi on 16/5/26.
//
#ifndef HANDWRITINGDIGITS_KDTREE_H
#define HANDWRITINGDIGITS_KDTREE_H
#include "common.h"
#include "dataBasic.h"
/*
* KDTree Node Compare (Single Dimension)
* Used in KD-Tree data split,
* and only this way.
*/
template <typename DataType>
class kdnode_cmp {
private:
/*
* Dimension index
*/
int idx;
public:
/*
* Constructor: specify dimension index
*/
kdnode_cmp(int _idx): idx(_idx) {}
/*
* Comparison using the dimension data
*/
bool operator ()(const Data<DataType> &a,
const Data<DataType> &b);
};
/*
* KD-Tree class template
*/
template <typename DataType, typename LabelType>
class KD_Tree {
/*
* Function type: Data2int
* return as an int
* take 2 DataType as parameters
*/
typedef int (*Data2int) (const DataType &, const DataType &);
/*
* Function type: DataInt2int
* return as an int
* take 2 DataType + 1 int as parameters
*/
typedef int (*DataInt2int) (const DataType &, const DataType &, int);
/*
* Calculate Data Distance (a, b)
* return as an integer
*/
Data2int DataDist;
/*
* Calculate Data Distance(a, b, idx)
* of dimension idx
* return as an integer
*/
DataInt2int DataDistSingleDim;
/*
* Node Class
*/
struct kd_node {
/* The dimension used to compare */
int dir;
/* The point data in the node */
DataWithLabel<DataType, LabelType> data;
/* Negative and Positive */
kd_node *ngtv, *pstv;
/* Constructor with direction */
kd_node(int D=0)
: dir(D), ngtv(0), pstv(0) {}
};
/*
* Tree root node pointer
*/
kd_node *root;
/*
* Build K-D Tree according to certain point sequence
*/
struct kd_node * build_node(
vector < DataWithLabel<DataType, LabelType> > &points);
/*
* Free memory
*/
void erase(kd_node *root);
/*
* Pair of distance and candidate labels
*/
struct pair_type {
/* Distance */
int dist;
/* The candidate point label */
LabelType data;
/* constructor */
pair_type(int d=0, LabelType *p=0);
/* cmp (used in priority_queue) */
bool operator < (const pair_type &b) const;
};
/*
* Candidate set with size K
* Used in 'query'
*/
priority_queue < pair_type > que;
/*
* Query in the subtree whose root is 'root'
* Called recursively
* Result stored in 'que', as this is of type void
*/
void query(kd_node *root, const Data<DataType> &query_point, int m);
public:
/*
* Empty Constructor
*/
KD_Tree() {}
/*
* Configure and construct the tree
* Parameters:
* - _DataDist: distance calculation function
* - _DataDistSingleDim: distance calculation of one dimension
* - points: data points (with labels)
*/
void construct(Data2int _DataDist,
DataInt2int _DataDistSingleDim,
vector < DataWithLabel<DataType, LabelType> > &points);
/*
* Destructor
*/
~KD_Tree();
/*
* Query for the M-Nearest-Neighbor of point 'query_point'
* Return a vector of Labels
* Use 'que' to store intermediate results
*/
vector < LabelType > query(const Data<DataType> &query_point, int m);
};
/****************************************************************************/
template <typename DataType>
bool kdnode_cmp<DataType>::operator ()(const Data<DataType> &a,
const Data<DataType> &b){
return a.val[idx] < b.val[idx];
}
template <typename DataType, typename LabelType>
typename KD_Tree<DataType, LabelType>::kd_node *
KD_Tree<DataType, LabelType>::build_node(
vector < DataWithLabel<DataType, LabelType> > &points) {
if (points.empty())
return NULL;
kd_node *root;
/* If there is only one point */
if (points.size()==1){
root = new kd_node(0);
root->data = *points.begin();
}
/* Multiple points */
else {
int cur_dimension;
root = new kd_node(cur_dimension =
rand() % points[0].val.size());
/* Split the vector according to the (size/2)th element */
std::nth_element(points.begin(),
points.begin()+points.size()/2,
points.end(),
kdnode_cmp<DataType>(cur_dimension));
root->data = points[points.size()/2];
vector < DataWithLabel<DataType, LabelType> > vec_ng(
points.begin(),
points.begin()+points.size()/2);
vector < DataWithLabel<DataType, LabelType> > vec_ps(
points.begin()+points.size()/2+1,
points.end());
/* Build recursively */
root->ngtv = build_node(vec_ng);
root->pstv = build_node(vec_ps);
}
return root;
}
template <typename DataType, typename LabelType>
void KD_Tree<DataType, LabelType>::erase(kd_node *root){
if (root->ngtv)
erase(root->ngtv);
if (root->pstv)
erase(root->pstv);
delete root;
}
template <typename DataType, typename LabelType>
void KD_Tree<DataType, LabelType>::construct(Data2int _DataDist,
DataInt2int _DataDistSingleDim,
vector < DataWithLabel<DataType, LabelType> > &points) {
DataDist = _DataDist;
DataDistSingleDim = _DataDistSingleDim;
root = build_node(points);
}
template <typename DataType, typename LabelType>
KD_Tree<DataType, LabelType>::~KD_Tree(){
erase(root);
}
template <typename DataType, typename LabelType>
vector < LabelType > KD_Tree<DataType, LabelType>::query(
const Data<DataType> &query_point, int m){
for (; !que.empty(); que.pop());
query(root, query_point, m);
vector < LabelType > seq;
while (!que.empty()){
seq.push_back(que.top().data);
que.pop();
}
return seq;
}
template <typename DataType, typename LabelType>
KD_Tree<DataType, LabelType>::pair_type::pair_type(int d, LabelType *p){
dist = d;
if (p) data=*p;
}
template <typename DataType, typename LabelType>
bool KD_Tree<DataType, LabelType>::pair_type::operator < (
const pair_type &b) const {
return dist < b.dist;
}
template <typename DataType, typename LabelType>
void KD_Tree<DataType, LabelType>::query(
kd_node *root,
const Data<DataType> &query_point, int m){
kd_node *n=root->ngtv, *p=root->pstv, *r=root;
bool flag=0;
pair_type cur(DataDist(query_point.val, r->data.val), &r->data.label);
int cur_dimension = r->dir;
if (! kdnode_cmp<DataType>(cur_dimension)(query_point, r->data))
swap(n, p);
/* Check in the closer subtree */
if (n)
query(n, query_point, m);
/* Candidate set is not full */
if (que.size() < m){
que.push(cur);
flag=1;
}
/* Check whether replace someone in the candidate set */
else {
if (cur.dist < que.top().dist){
que.pop();
que.push(cur);
}
if (DataDistSingleDim(query_point.val, r->data.val, r->dir)
< que.top().dist)
flag = 1;
}
/* Check in the farther subtree */
if (p && flag)
query(p, query_point, m);
}
#endif //HANDWRITINGDIGITS_KDTREE_H