Professional Documents
Culture Documents
KD Tree Doc
KD Tree Doc
I. BallTree,
II. Binary Tree,
III. KDTree,
IV. LinearNNSearch
Ball Tree
The connection to dataset is only a reference. For the tree structure the indexes are stored in an
array.
See the implementing classes of different construction methods of the trees for details on its
construction.
Binary Tree
In Java, the key points in the recursion are exactly the same as in C or C++. In fact, I created the
Java solutions by just copying the C solutions, and then making the syntactic changes. The
recursion is the same, however the outer structure is slightly different.
In Java, we will have a BinaryTree object that contains a single root pointer. The root pointer
points to an internal Node class that behaves just like the node struct in the C/C++ version. The
Node class is private -- it is used only for internal storage inside the BinaryTree and is not
exposed to clients. With this OOP structure, almost every operation has two methods: a one-line
method on the BinaryTree that starts the computation, and a recursive method that works on the
Node objects. For the lookup() operation, there is a BinaryTree.lookup() method that the client
uses to start a lookup operation. Internal to the BinaryTree class, there is a private recursive
lookup(Node) method that implements the recursion down the Node structure. This second,
private recursive method is basically the same as the recursive C/C++ functions above -- it takes
a Node argument and uses recursion to iterate over the pointer structure.
KD Tree
k-d tree (short for k-dimensional tree) is a space-partitioning data structure for organizing points
in a k-dimensional space. k-d trees are a useful data structure for several applications, such as
searches involving a multidimensional search key (e.g. range searches and nearest neighbor
searches). k-d trees are a special case of binary space partitioning trees.
Linear search
Linear search is one of the basic search techniques that we've now. Although this is not a very
good search technique, one should understand this concept. Let's consider our aim to search for a
key element in an array of elements. We loop through all the array elements and check for
existence of the key element. Since we go element by element, this search is called as Linear
search or sequential search. Search element is called as key element.
BEGIN
ACCEPT key
ASSIGN i with 1
LOOP
IF array[i] = key
THEN
END IF
END LOOP
IF found = true
THEN
ELSE
END IF
END
Implementation of Kd-tree
import java.util.Date;
import java.util.Random;
class kdtime {
if (args.length < 3) {
System.exit(1);
int n = Integer.parseInt(args[0]);
int k = Integer.parseInt(args[1]);
int t = Integer.parseInt(args[2]);
new Random();
double [][] x = new double[n][k];
x[i][j] = r.nextDouble();
try {
kd.insert(x[i], i);
catch (Exception e) {
System.err.println(e);
System.exit(0);
// compare search
linear_search(x, t, r);
// compare nearest-neighbor
linear_nearest(x, t, r);
kd_nearest(x, t, kd, r);
// do linear search
search(x, targ);
// do KD-tree search
try {
kd.search(targ);
catch (Exception e) {
System.err.println(e);
System.exit(0);
int k = x[0].length;
int k = x[0].length;
try {
int n = nbr.intValue();
catch (Exception e) {
System.err.println(e);
System.exit(0);
}
return -1;
if (d < mindst) {
mindst = d;
minidx = i;
}
return minidx;
double dst = 0;
dst += dif*dif;
return dst;
return true;
x[i] = r.nextDouble();
return x;
return d.getTime();
import java.util.List;
import java.util.Iterator;
class kdnbrs {
System.exit(1);
int m = Integer.parseInt(args[0]);
int d = Integer.parseInt(args[1]);
int n = Integer.parseInt(args[2]);
targ[k] = 0.5;
try {
keys[i][j] = r.nextDouble();
kd.insert(keys[i], i);
//System.exit(0);
System.err.println();
catch (Exception e) {
System.err.println(e);
}
return cal.getTimeInMillis();
import java.util.Arrays;
import java.util.List;
import java.util.Iterator;
class kdrange {
// check arguments
if (args.length < 3) {
"<yradius>");
System.exit(1);
// make a KD-tree
int n = i * gsize + j + 1;
System.out.print("*\t");
else {
System.out.print(n + "\t");
try {
kd.insert(key, n);
catch (Exception e) {
System.err.println(e);
System.out.println();
try {
double [] lo = {gsize/2-xrad,gsize/2-yrad};
double [] hi = {gsize/2+xrad,gsize/2+yrad};
for (int i : o) {
System.out.println(i);
catch (Exception e) {
System.err.println(e);
class kddemo {
try {
catch (Exception e) {
System.err.println(e);
try {
String n = kd.search(B);
System.err.println(n);
catch (Exception e) {
System.err.println(e);
}
try {
String n = kd.nearest(T);
System.err.println(n);
kd.delete(C);
n = kd.nearest(T);
System.err.println(n);
catch (Exception e) {
System.err.println(e);
}
4. Behavior of KDTree class
class kddemo {
try {
catch (Exception e) {
System.err.println(e);
try {
String n = kd.search(B);
System.err.println(n);
catch (Exception e) {
System.err.println(e);
try {
String n = kd.nearest(T);
System.err.println(n);
kd.delete(C);
n = kd.nearest(T);
System.err.println(n);
catch (Exception e) {
System.err.println(e);