I want to implement KD tree. I have defined class Node as follows:
public static class Node{
int id;
public double x;
public double y;
public Node leftChild;
public Node rightChild;
Node(int id, double x, double y){
this.id = id;
this.x = x;
this.y = y;
leftChild = null;
rightChild = null;
}
}
Then I have defined class KDTree in which I want to implement method contains(Node t):
public static class KDTree<T extends Node>{
Node root;
KDTree(Node root){
this.root = root;
}
boolean contains(Node t){
if (t == null)
return false;
if (t.x == root.x && t.y == root.y)
return true;
else
return ...; //something else
}
}
Do I need to use generics in class KDTree? I'm not aware of when it is a good practice to use generics and when it is not? It would be nice if someone can recommend me an article on when we have to use generics and when we don't have to.
As far as I read the declaration public static class KDTree<T extends Node> means "class KDTree contains objects that extends type Node". Is there any way to declare that class KDTree contains only objects of type Node?
Class Node represents a point in the plane, given by its x and y coordinate. Class KDTree is a tree-structure that stores the points in such a way that it is easy to answer queries of the type: give me m nearest points to my point p(x1, y1). This is the method that I use to construct KDTree:
public static Node buildKDTree(List<Node> list, boolean depth, int begin, int end){
int middle = median(list, depth, begin, end);
Node root = list.get(middle);
root.leftChild = buildKDTree(list, !depth, begin, middle-1);
depth = !depth;
root.rightChild = buildKDTree(list, !depth, middle+1, end);
return root;
}
NodeandKDTreeare intended for? \$\endgroup\$