-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathKDTree.java
188 lines (137 loc) · 4.44 KB
/
KDTree.java
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
package com.example.dsa;
import android.content.Context;
import android.util.Log;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import org.json.simple.parser.JSONParser;
import java.lang.*;
import java.util.*;
import java.io.*;
class Node{
static int k = 2;
double point[]=new double[k]; // To store k dimensional point
Node left, right;
public Node(double arr[])
{
point[0]=arr[0];
point[1]=arr[1];
left=right=null;
}
}
class KDT{
static double currBest=Double.MAX_VALUE;
static int k = 2;
Node root;
public KDT ()
{
root=null;
}
public void Root()
{
System.out.println(root.point[0]+" "+root.point[1]);
}
Node insertRec(Node root,double point[], int depth)
{
if (root == null)
return (new Node(point));
int cd = depth % k;
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;
}
void insert(double point[])
{
root=insertRec(root, point, 0);
}
boolean arepointsSame(double point1[], double point2[])
{
if(point1[0]==point2[0]&&point1[1]==point2[1])
return true;
return false;
}
boolean searchRec(Node root, double point[], int depth)
{
if (root == null)
{ return false; }
if (arepointsSame(root.point, point))
return true;
int cd = depth % k;
if (point[cd] < root.point[cd])
return searchRec(root.left, point, depth + 1);
return searchRec(root.right, point, depth + 1);
}
public Node nearest(Node root,double point[],Node currentBestNode,int depth)
{
if(root==null) return currentBestNode;
if(dist(root.point,point)<currBest)
{
currBest=dist(root.point,point);
currentBestNode=root;
}
Node good=null,bad=null;
int cd = depth % k;
if(point[cd]<root.point[cd])
{
good=root.left;
bad=root.right;
}
else{
bad=root.left;
good=root.right;
}
currentBestNode=nearest(good, point, currentBestNode, depth+1);
if(Math.abs(point[cd]-root.point[cd])<currBest)
return currentBestNode=nearest(bad, point, currentBestNode, depth+1);
return currentBestNode;
}
boolean search(double point[])
{
return searchRec(root, point, 0);
}
double dist(double []a,double b[])
{
return(Math.sqrt(Math.pow(a[0]-b[0],2)+Math.pow(a[1]-b[1],2)));
}
public double[] NearestNeighbour(double a[])
{
Node temp=nearest(root, a, root, 0);
currBest=Double.MAX_VALUE;
return temp.point;
}
public static double[] getNearestA(double[] input, Context mycontext)
{
Scanner sc=new Scanner(System.in);
KDT kd=new KDT();
JSONParser parser = new JSONParser();
JSONObject mJsonObject=null;
JSONArray mJsonArray = null;
try {
mJsonArray = new JSONArray((parser.parse(new BufferedReader
(new InputStreamReader(mycontext.getAssets().open("cities.json"), "UTF-8")))).toString());
} catch (Exception e) {
Log.d("DEBUG",e.getMessage()+ " 2");
e.printStackTrace();
}
double[][] podoubles=new double[mJsonArray.length()][2];
//Log.d("DEBUG", String.valueOf(mJsonArray.length()));
for(int i=0;i<mJsonArray.length();i++){
try {
mJsonObject = mJsonArray.getJSONObject(i);
podoubles[i][0]=Double.parseDouble(mJsonObject.getString("lat"));
podoubles[i][1]=Double.parseDouble(mJsonObject.getString("lng"));
kd.insert(podoubles[i]);
//Log.d("DEBUG", String.valueOf(x[0])+" "+String.valueOf(x[1]));
} catch (JSONException e) {
Log.d("DEBUG",e.getMessage());
e.printStackTrace();
}
}
kd.Root();
double nnb[]=new double[2];
nnb=kd.NearestNeighbour(input);
return nnb;
}
}