0

I have a tree of objects T, declared as

 public class Tree<T> {

    private Node<T> root;

    public Tree(T rootData) {
        root = new Node<T>();
        root.data = rootData;
        root.children = new ArrayList<Node<T>>();
    }

    public static class Node<T> {
        private T data;
        private Node<T> parent;
        private List<Node<T>> children;
    }
}

and want to add a function keepBranch, which reduces the tree to one of its branch.

But I need keepBranch to take an object T as a parameter, to select the branch.
Something like:

public void keepBranch(<T> param) {

        for (Node<T> node : this.root.children) {
            if (param.equals(node.data)) {
                this.root = node;
            }
        }
}

Is there a way to do this? Or am I doing it wrong?

5
  • <T> is used to define a type variable in a certain context. But once defined, you can use T as you would use any other class name. (Syntactically, that is. You can't do everything you could do with a concrete class name.) Commented Feb 9, 2017 at 14:21
  • 1
    You already have an example for the correct usage in your constructor public Tree(T rootData), so I wonder why you've thought that you need to use <T>. Commented Feb 9, 2017 at 14:26
  • 1
    @Tom Took the tree code from another thread, as I'm beginning with generics and trying to understand it. But yeah you're right I should have noticed! Commented Feb 9, 2017 at 14:28
  • data in our code should be unique for whole tree. if it isn't so you can get wrong branch by data comparison. Commented Feb 9, 2017 at 14:29
  • @VladBochenin You're right, I will be carefull when using it, thanks! Commented Feb 9, 2017 at 14:37

2 Answers 2

4

Change the parameter type to T. But your comparison is wrong, you compare a value to a node. Change is so that the node's value is being compared to param:

public void keepBranch(T param) {
    for (Node<T> node : this.root.children) {
        if (param.equals(node.data)) {
            this.root = node;
        }
    }
}
Sign up to request clarification or add additional context in comments.

2 Comments

OP maybe wants to compare by reference. For example some nodes could be equals but placed on different places in tree
@VladBochenin It isn't the reference comparison that is the biggest problem, it's comparing a Node instance (node) to something that is definitely not a Node instance (param).
2

It should be T param instead of <T> param :

public void keepBranch(T param) {

        for (Node<T> node : this.root.children) {
            if (param.equals(node.data)) {
                this.root = node;
            }
        }
}

EDIT

As said in comments, it should be param.equals(node.data) instead of node == param

Documentation : Lesson: Generics (Updated)

4 Comments

node == param is comparing Node<T> == T. is it correct?
@jackjay no, it's not. It has to be compared to node.data.
Well I just took this comparison for the example, I can edit it if it bothers you though.
@jackjay No, I just thought of passing a parameter of type T, to compare it to the node data. Should I pass a Node<T> type instead?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.