1

I have a Generic Binary Tree that will add objects lessThan or equalTo to the left, and objects greater than to the right. My problem is with the comparing the Generics, I know the data value will be a object-wrapped primitive or a String, so they are comparable. But, I do not know how to implement this in the code.

The code is a work in progress, I know the add methods don't add correctly yet, but I am working on that. Thanks

Here is the TreeNode:

public class TreeNode<T>
{
    //Instance Variables
    TreeNode leftChild;
    TreeNode rightChild;
    int childCount;
    int depth;
    T data;


    public TreeNode(T data, int parentDepth)
    {
        leftChild = null;
        rightChild = null;
        childCount = 0;
        depth = parentDepth + 1;
        this.data = data;
    }

    public TreeNode(int parentDepth)
    {
        leftChild = null;
        rightChild = null;
        childCount = 0;
        depth = parentDepth + 1;
        data = null;
    }


    public void add(T data)
    {
        if (this.data.compareTo(data) <= 0)
        {
            addLeft(data);
        } else if (this.data.compareTo(data) > 0)
        {
            addRight(data);
        } else
        {
            System.out.println("ERROR IN TREENODE.ADD");
        }
    }


    public void addLeft(T data)
    {
        leftChild = new TreeNode(data, depth);
    }

    public void addLeft()
    {
        leftChild = new TreeNode(depth);
    }


    public void addRight(T data)
    {
        rightChild = new TreeNode(data, depth);
    }

    public void addRight() {
        rightChild = new TreeNode(depth);
    }
}
2
  • Refer this question, it is exactly the same issue as yours. Commented Jun 5, 2015 at 0:27
  • 1
    On a side note, I'm not sure if this makes a difference but I'd make your leftChild and rightChild of type TreeNode<T> instead of TreeNode Commented Jun 5, 2015 at 0:31

2 Answers 2

6

I know the data value will be a object-wrapped primitive or a String, so they are comparable.

Then you can tell the compiler about it:

public class TreeNode<T extends Comparable<T>>

If you do that, you will get access to the compareTo method defined in Comparable.

Sign up to request clarification or add additional context in comments.

3 Comments

Thanks, this worked! I had tried to implement Comparable<T> and that did not work, but extending did. If you could explain why, that would help me learn, but this worked for my immediate problem.
That's just a syntax thing. There is no <T implements X>. Only <T extends X> and <T super X> (its opposite), both of which do not make a distinction between classes and interfaces.
for best results use <T extends Comparable<? super T>>
1

Your T should be implementing Comparable interface in order to compare.

public class TreeNode<T extends Comparable<T>>
{
    //Instance Variables
    TreeNode leftChild;
    TreeNode rightChild;
    int childCount;
    int depth;
    T data;


    public TreeNode(T data, int parentDepth)
    {
        leftChild = null;
        rightChild = null;
        childCount = 0;
        depth = parentDepth + 1;
        this.data = data;
    }

    public TreeNode(int parentDepth)
    {
        leftChild = null;
        rightChild = null;
        childCount = 0;
        depth = parentDepth + 1;
        data = null;
    }


    public void add(T data)
    {
        if (this.data.compareTo(data) <= 0)
        {
            addLeft(data);
        } else if (this.data.compareTo(data) > 0)
        {
            addRight(data);
        } else
        {
            System.out.println("ERROR IN TREENODE.ADD");
        }
    }


    public void addLeft(T data)
    {
        leftChild = new TreeNode(data, depth);
    }

    public void addLeft()
    {
        leftChild = new TreeNode(depth);
    }


    public void addRight(T data)
    {
        rightChild = new TreeNode(data, depth);
    }

    public void addRight() {
        rightChild = new TreeNode(depth);
    }
}

1 Comment

for best results use <T extends Comparable<? super T>>

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.