Skip to main content
deleted 14 characters in body; edited title
Source Link
Jamal
  • 35.2k
  • 13
  • 134
  • 238

Do I need Generics in this casefor these node and tree classes?

I want to implement KD tree. I have defined class nodeNode as follows:

Then I have defined class KDTreeKDTree in which I want to implement method contains(Node t)contains(Node t):

My question is doDo I need to use generics in class KDTreeKDTree? ImI'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.

EDIT: and also asAs far as I read the declaration public static class KDTree<T extends Node> means "class KDTreeKDTree contains objects that extends type Node". Is there any way to declare that class KDTreeKDTree contains only objects of type Node?

EDIT: class NodeClass Node represents a point in the plane, given by its x and y coordinate. Class KDTreeKDTree is a tree-structure that stores the points in such a way that it is easy to answer queries of the type: give me mm nearest points to my point p(x1, y1)p(x1, y1). This is the method that I use to construct KDTreeKDTree:

Do I need Generics in this case?

I want to implement KD tree. I have defined class node as follows:

Then I have defined class KDTree in which I want to implement method contains(Node t):

My question is do I need to use generics in class KDTree? Im 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.

EDIT: and also 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?

EDIT: 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:

Do I need Generics for these node and tree classes?

I want to implement KD tree. I have defined class Node as follows:

Then I have defined class KDTree in which I want to implement method contains(Node t):

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:

edited tags
Link
200_success
  • 145.7k
  • 22
  • 191
  • 481
added 642 characters in body
Source Link
user3371223
  • 861
  • 4
  • 12
  • 22

I want to implement KD tree. I have defibeddefined 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
    }
}

My question is do I need to use generics in class KDTree? Im 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.

EDIT: and also 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?

EDIT: 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;
}

I want to implement KD tree. I have defibed 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
    }
}

My question is do I need to use generics in class KDTree? Im 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.

EDIT: and also 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

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
    }
}

My question is do I need to use generics in class KDTree? Im 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.

EDIT: and also 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?

EDIT: 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;
}
Source Link
user3371223
  • 861
  • 4
  • 12
  • 22
Loading