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);
    }
}
gangwerz
  • 435
  • 2
  • 8
  • 18
  • Refer [this](http://stackoverflow.com/questions/20793082/java-comparing-generic-types) question, it is exactly the same issue as yours. – Srini Jun 05 '15 at 00: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`** instead of `TreeNode` – Srini Jun 05 '15 at 00:31

2 Answers2

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.

Thilo
  • 257,207
  • 101
  • 511
  • 656
  • Thanks, this worked! I had tried to implement Comparable 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. – gangwerz Jun 05 '15 at 05:43
  • 1
    That's just a syntax thing. There is no ``. Only `` and `` (its opposite), both of which do not make a distinction between classes and interfaces. – Thilo Jun 05 '15 at 05:45
  • Thanks! This helped a lot. – gangwerz Jun 05 '15 at 05:46
  • for best results use `>` – newacct Jun 05 '15 at 21:43
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);
    }
}
Eranda
  • 1,439
  • 1
  • 17
  • 30