-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.cpp
485 lines (417 loc) · 14.6 KB
/
main.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
#include<bits/stdc++.h>
#include <iostream>
#include <cmath>
#include <limits>
#include<algorithm>
using namespace std;
const int k = 2;
const int MAX_POINTS = 100; // Maximum number of points for range search
// A structure to represent node of kd tree
struct Node
{
int point[k]; // To store k dimensional point
Node *left, *right;
};
// A method to create a node of K D tree
struct Node* newNode(int arr[])
{
struct Node* temp = new Node;
for (int i=0; i<k; i++)
temp->point[i] = arr[i];
temp->left = temp->right = NULL;
return temp;
}
// Inserts a new node and returns root of modified tree
// The parameter depth is used to decide axis of comparison
Node *insertRec(Node *root, int point[], unsigned depth)
{
// Tree is empty?
if (root == NULL)
return newNode(point);
// Calculate current dimension (cd) of comparison
unsigned cd = depth % k;
// Compare the new point with root on current dimension 'cd'
// and decide the left or right subtree
if (point[cd] < (root->point[cd]))
root->left = insertRec(root->left, point, depth + 1);
else
root->right = insertRec(root->right, point, depth + 1);
return root;
}
// Function to insert a new point with given point in
// KD Tree and return new root. It mainly uses above recursive
// function "insertRec()"
Node* insert(Node *root, int point[])
{
return insertRec(root, point, 0);
}
// A utility method to determine if two Points are same
// in K Dimensional space
bool arePointsSame(int point1[], int point2[])
{
// Compare individual coordinate values
for (int i = 0; i < k; ++i)
if (point1[i] != point2[i])
return false;
return true;
}
// Searches a Point represented by "point[]" in the K D tree.
// The parameter depth is used to determine current axis.
bool searchRec(Node* root, int point[], unsigned depth)
{
// Base cases
if (root == NULL)
return false;
if (arePointsSame(root->point, point))
return true;
// Current dimension is computed using current depth and total
// dimensions (k)
unsigned cd = depth % k;
// Compare point with root with respect to cd (Current dimension)
if (point[cd] < root->point[cd])
return searchRec(root->left, point, depth + 1);
return searchRec(root->right, point, depth + 1);
}
// Searches a Point in the K D tree. It mainly uses
// searchRec()
bool search(Node* root, int point[])
{
// Pass current depth as 0
return searchRec(root, point, 0);
}
// A utility function to find minimum of three integers
Node *minNode(Node *x, Node *y, Node *z, int d)
{
Node *res = x;
if (y != NULL && y->point[d] < res->point[d])
res = y;
if (z != NULL && z->point[d] < res->point[d])
res = z;
return res;
}
// Recursively finds minimum of d'th dimension in KD tree
// The parameter depth is used to determine current axis.
Node *findMinRec(Node* root, int d, unsigned depth)
{
// Base cases
if (root == NULL)
return NULL;
// Current dimension is computed using current depth and total
// dimensions (k)
unsigned cd = depth % k;
// Compare point with root with respect to cd (Current dimension)
if (cd == d)
{
if (root->left == NULL)
return root;
return findMinRec(root->left, d, depth+1);
}
// If current dimension is different then minimum can be anywhere
// in this subtree
return minNode(root,
findMinRec(root->left, d, depth+1),
findMinRec(root->right, d, depth+1), d);
}
// A wrapper over findMinRec(). Returns minimum of d'th dimension
Node *findMin(Node* root, int d)
{
// Pass current level or depth as 0
return findMinRec(root, d, 0);
}
// Copies point p2 to p1
void copyPoint(int p1[], int p2[])
{
for (int i=0; i<k; i++)
p1[i] = p2[i];
}
// Function to delete a given point 'point[]' from tree with root
// as 'root'. depth is current depth and passed as 0 initially.
// Returns root of the modified tree.
Node *deleteNodeRec(Node *root, int point[], int depth)
{
// Given point is not present
if (root == NULL)
return NULL;
// Find dimension of current node
int cd = depth % k;
// If the point to be deleted is present at root
if (arePointsSame(root->point, point))
{
// 2.b) If right child is not NULL
if (root->right != NULL)
{
// Find minimum of root's dimension in right subtree
Node *min = findMin(root->right, cd);
// Copy the minimum to root
copyPoint(root->point, min->point);
// Recursively delete the minimum
root->right = deleteNodeRec(root->right, min->point, depth+1);
}
else if (root->left != NULL) // same as above
{
Node *min = findMin(root->left, cd);
copyPoint(root->point, min->point);
root->right = deleteNodeRec(root->left, min->point, depth+1);
}
else // If node to be deleted is leaf node
{
delete root;
return NULL;
}
return root;
}
// 2) If current node doesn't contain point, search downward
if (point[cd] < root->point[cd])
root->left = deleteNodeRec(root->left, point, depth+1);
else
root->right = deleteNodeRec(root->right, point, depth+1);
return root;
}
// Function to delete a given point from K D Tree with 'root'
Node* deleteNode(Node *root, int point[])
{
// Pass depth as 0
return deleteNodeRec(root, point, 0);
}
bool isInRange(int point[], int low[], int high[]) {
for (int i = 0; i < k; i++) {
if (point[i] < low[i] || point[i] > high[i])
return false;
}
return true;
}
// Recursive function for range search
void rangeSearchRec(Node* root, int low[], int high[], int result[][k], int& count, unsigned depth) {
if (root == NULL)
return;
// Check if current node's point is in range
if (isInRange(root->point, low, high)) {
for (int i = 0; i < k; i++) {
result[count][i] = root->point[i]; // Store the point in the result array
}
count++; // Increment the count of found points
}
unsigned cd = depth % k;
// Check left subtree
if (low[cd] < root->point[cd]) {
rangeSearchRec(root->left, low, high, result, count, depth + 1);
}
// Check right subtree
if (high[cd] > root->point[cd]) {
rangeSearchRec(root->right, low, high, result, count, depth + 1);
}
}
// Function to perform range search in KD tree
void rangeSearch(Node* root, int low[], int high[], int result[][k], int& count) {
count = 0; // Initialize count to zero
rangeSearchRec(root, low, high, result, count, 0);
}
// Function to calculate squared distance between two points
double squaredDistance(int point1[], int point2[]) {
double dist = 0.0;
for (int i = 0; i < k; i++) {
dist += pow(point1[i] - point2[i], 2);
}
return dist;
}
// Nearest neighbor search recursive function
void nearestNeighborRec(Node* root, int target[], Node*& bestNode,
double& bestDistSq, unsigned depth) {
if (root == NULL)
return;
double distSq = squaredDistance(root->point, target);
// Update best node and distance if current node is closer
if (distSq < bestDistSq) {
bestDistSq = distSq;
bestNode = root;
}
unsigned cd = depth % k;
// Determine which side of the tree to search first
Node *nextBranch = target[cd] < root->point[cd] ? root->left : root->right;
Node *otherBranch = target[cd] < root->point[cd] ? root->right : root->left;
nearestNeighborRec(nextBranch, target, bestNode, bestDistSq, depth + 1);
// Check if we need to explore the other branch
if ((target[cd] - root->point[cd]) * (target[cd] - root->point[cd]) < bestDistSq) {
nearestNeighborRec(otherBranch, target, bestNode, bestDistSq, depth + 1);
}
}
// Function to perform nearest neighbor search in KD tree
Node* nearestNeighbor(Node* root, int target[]) {
Node* bestNode = NULL;
double bestDistSq = 10000000000;
nearestNeighborRec(root, target, bestNode, bestDistSq, 0);
return bestNode;
}
// Utility function to display points in KD tree using inorder traversal
void displayInorder(Node* root)
{
if (root == NULL)
return;
// Display left subtree
displayInorder(root->left);
// Display current node
cout << "(";
for (int i = 0; i < k - 1; i++)
cout << root->point[i] << ", ";
cout << root->point[k - 1] << ")\n";
// Display right subtree
displayInorder(root->right);
}
// Utility function to display points in KD tree using preorder traversal
void displayPreorder(Node* root)
{
if (root == NULL)
return;
// Display current node
cout << "(";
for (int i = 0; i < k - 1; i++)
cout << root->point[i] << ", ";
cout << root->point[k - 1] << ")\n";
// Display left subtree
displayPreorder(root->left);
// Display right subtree
displayPreorder(root->right);
}
// Utility function to display points in KD tree using postorder traversal
void displayPostorder(Node* root)
{
if (root == NULL)
return;
// Display left subtree
displayPostorder(root->left);
// Display right subtree
displayPostorder(root->right);
// Display current node
cout << "(";
for (int i = 0; i < k - 1; i++)
cout << root->point[i] << ", ";
cout << root->point[k - 1] << ")\n";
}
// Driver program to test above functions
int main()
{
struct Node *root = NULL;
int points[][k] = {{30, 40}, {5, 25}, {70, 70},
{10, 12}, {50, 30}, {35, 45}};
int choice;
do {
cout << "\nMenu:\n";
cout << "1. Insert a point\n";
cout << "2. Search for a point\n";
cout << "3. Delete a point\n";
cout << "4. Range Search\n";
cout << "5. Nearest Neighbor Search\n";
cout << "6. Display points (Inorder)\n";
cout << "7. Display points (Preorder)\n";
cout << "8. Display points (Postorder)\n";
cout << "9. Find minimum of a dimension\n";
cout << "10.Exit\n";
cout << "Enter your choice: ";
cin >> choice;
switch(choice) {
case 1: {
int point[k];
cout << "Enter point coordinates: ";
for (int i = 0; i < k; i++)
cin >> point[i];
root = insert(root, point);
cout << "Point inserted successfully!\n";
break;
}
case 2: {
int point[k];
cout << "Enter point to search for: ";
for (int i = 0; i < k; i++)
cin >> point[i];
if (search(root, point))
cout << "Point found!\n";
else
cout << "Point not found!\n";
break;
}
case 3: {
int point[k];
cout << "Enter point to delete: ";
for (int i = 0; i < k; i++)
cin >> point[i];
root = deleteNode(root, point);
cout << "Point deleted successfully!\n";
break;
}
case 4: { // Range Search
int low[k], high[k];
cout << "Enter lower bound coordinates: ";
for (int i = 0; i < k; ++i) cin >> low[i];
cout << "Enter upper bound coordinates: ";
for (int i = 0; i < k; ++i) cin >> high[i];
int result[MAX_POINTS][k], count;
rangeSearch(root , low , high , result , count);
cout << "Points in range [" << low[0] << ", " << high[0] << "] x ["
<< low[1] << ", " << high[1] << "]:" << endl;
for (int i = 0; i < count; ++i) {
cout << "(" << result[i][0] << ", " << result[i][1] << ")" << endl;
}
break;
}
case 5: { // Nearest Neighbor Search
int target[k];
cout << "Enter coordinates of the target point: ";
for (int i = 0; i < k; ++i) cin >> target[i];
Node* nearestNode = nearestNeighbor(root , target);
if(nearestNode != NULL) {
cout << "Nearest neighbor to ("
<< target[0] << ", "
<< target[1] << ") is ("
<< nearestNode->point[0]
<< ", "
<< nearestNode->point[1]
<< ")"
<< endl;
} else {
cout << "No points in the KD Tree."
<< endl;
}
break;
}
case 6: {
cout << "Points in the KD tree (Inorder):\n";
displayInorder(root);
break;
}
case 7: {
cout << "Points in the KD tree (Preorder):\n";
displayPreorder(root);
break;
}
case 8: {
cout << "Points in the KD tree (Postorder):\n";
displayPostorder(root);
break;
}
case 9: {
int dim;
cout << "Enter dimension to find minimum: ";
cin >> dim;
if (dim >= 0 && dim < k) {
Node* minNode = findMin(root, dim);
cout << "Minimum of dimension " << dim << " is (";
for (int i = 0; i < k - 1; i++)
cout << minNode->point[i] << ", ";
cout << minNode->point[k - 1] << ")\n";
} else {
cout << "Invalid dimension!\n";
}
break;
}
case 10: {
cout << "Exiting...\n";
break;
}
default: {
cout << "Invalid choice! Please try again.\n";
break;
}
}
} while (choice != 10);
return 0;
}