-
Notifications
You must be signed in to change notification settings - Fork 15
/
Copy pathkdtree.cpp
103 lines (76 loc) · 2.21 KB
/
kdtree.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
#include <iostream>
#include <queue>
#include <cmath>
using namespace std;
#define DIM 2
#define KNB 3
struct Node
{
int point[DIM]; //to store DIM dimensional point
Node *left, *right; //binary tree
};
struct Node* newNode(int arr[])
{
struct Node * temp = new Node;
for (int i = 0; i < DIM; ++i)
{
temp->point[i] = arr[i];
}
temp->left = temp->right = nullptr;
return temp;
}
Node* insert(Node *root, int point[], unsigned int depth)
{
if (root == nullptr) {return newNode(point);}
//cd = current dimension
unsigned int cd = depth % DIM;
//insert node recursively (similar to BST)
if (point[cd] < (root->point[cd]))
root->left = insert(root->left, point, depth+1);
else
root->right = insert(root->right, point, depth+1);
return root;
}
bool samePoint(int point1[], int point2[])
{
for (int i = 0; i < DIM; ++i)
{
if (point1[i] != point2[i])
return false;
}
return true;
}
bool search(Node* root, int point[], unsigned int depth)
{
//base case
if (root == nullptr) return false;
if (samePoint(root->point, point)) return true;
//cd = current dimension
unsigned int cd = depth % DIM;
//BST
if (point[cd] < root->point[cd])
return search(root->left, point, depth+1);
else
return search(root->right, point, depth+1);
}
int main()
{
struct Node *root = nullptr;
int points[][DIM] = {{3, 6}, {17, 15}, {13, 15}, {6, 12}, {9, 1}, {2, 7}, {10, 19}};
int n = sizeof(points) / sizeof(points[0]);
cout << "building kd tree..." << endl;
for (int i = 0; i < n; ++i)
{
root = insert(root, points[i], 0);
cout << "(" << points[i][0] << ", " << points[i][1] << ")" << " ";
}
cout << endl;
cout << "querying points..." << endl;
int point1[] = {10, 19};
cout << "Point (" << point1[0] << ", " << point1[1] << ") : ";
(search(root, point1, 0)) ? cout << "found" << endl : cout << "not found" << endl;
int point2[] = {12, 19};
cout << "Point (" << point2[0] << ", " << point2[1] << ") : ";
(search(root, point2, 0)) ? cout << "found" << endl : cout << "not found" << endl;
return 0;
}