Here is what I am trying to do I am trying to compare to values that are set up as generic values and return whether one is greater than the other and it is telling me that (E, E) cannot be applied and I don't know why. I would appreciate your help.

class TreeNode<E>
{
    TreeNode<E> leftNode;
    E data;
    TreeNode<E> rightNode;

    public TreeNode(E nodeData)
    {
    data = nodeData;
    leftNode=rightNode=null;
    }
    public static <E extends Comparable< E > > int comp(E x, E y)
    {
    E val;
    if(x.compareTo(y)>0){
    return -1; 
    }
    }
    public void insert(E insertValue)
    {
    if(comp(insertValue,data)==-1)
        {
        if(leftNode == null)
            leftNode = new TreeNode(insertValue);
        else
            leftNode.insert(insertValue);
    }
    else if(insertValue > data)
    {
        if(rightNode == null)
            rightNode = new TreeNode(insertValue);
        else
            rightNode.insert(insertValue);
    }
}
}               

public class Tree< E >
{
    private TreeNode root;
    public Tree()
    {
    root=null;
    }
    public void insertNode(E insertValue)
    {
    if(root == null)
        root = new TreeNode(insertValue);
    else
        root.insert(insertValue);
     }

I am going off of the book and the notes from the class what do you mean by your class does not implement Comparable? I followed examples in book.

Okay I follow the implements part, but I am confused on the compareTo part

public int compareTo(E other){
  // return negative int if this object less than "other"
  // etc... 
}

The generics pretty much just alleviate the need for you to cast "other" to your class type before comparing. "other" is already an object of that type and can be used directly.

This is what I have changed please help me I am stuck and the book/notes are not helpful.

import java.util.*;
class TreeNode<E> 
{
    TreeNode<E> leftNode;
    E data;
    TreeNode<E> rightNode;

    public TreeNode(E nodeData)
    {
    data = nodeData;
    leftNode=rightNode=null;
    }
    public int compareTo(E other)
    {
    if(*this > other)
    return 1; 
     else
    return -1; 
    }
    public void insert(E insertValue)
    {
    if(compareTo(data)==-1)
        {
        if(leftNode == null)
            leftNode = new TreeNode(insertValue);
        else
            leftNode.insert(insertValue);
    }
    else if(insertValue > data)
    {
        if(rightNode == null)
            rightNode = new TreeNode(insertValue);
        else
            rightNode.insert(insertValue);
    }
}
}               

public class Tree<E> implements Comparable<E>
{
    private TreeNode root;
    public Tree()
    {
    root=null;
    }
    public void insertNode(E insertValue)
    {
    if(root == null)
        root = new TreeNode(insertValue);
    else
        root.insert(insertValue);
     }

}

Hello, skiing.
As Ezzaral said, for comparing objects of self-created data types, you need to add realization Comparable interface in that class. In other words - you're kinda saying "The objects of my class, called 'TreeNode" can be compared with each other using CompareTo method." Let's look at example:
Suppose we have class Apple (with some variables and methods):

public class Apple
{
    //indicates, how green apple is
    private int _greenLevel;
    //size of the apple
    private int _size;
    //sort of the apple
    private String _sortOfApple;

    public int getAppleSize() { return _size; }

    public Apple(int greenLevel, int size, String sortOfApple )
    {
        _greenLevel = greenLevel;
        _size = size;
        _sortOfApple = sortOfApple;
    }   
  etc.
}

And imagine, that I need to compare the objects of my Apple class e.g. by the level of the green. Then my class needs small changes:

public class Apple implements Comparable

and now I need to realize the "CompareTo" method (as Comparble demand from me):

public int compareTo(Object o) {
       if (this._greenLevel > ((Apple)o)._greenLevel)           
           //this object is greater than given
           return 1;
       else if (this._greenLevel < ((Apple)o)._greenLevel)
            //this object is less than given
            return -1;
        else
           //objects are equal
           return 0;
    }

Now we can freely test that in main method:

public class Test
{
    public static void main( String args[] )
    {
       Apple app1 = new Apple(2, 4, "Asort");
       Apple app2 = new Apple(5, 8, "Asort");
       System.out.println(app1.compareTo(app2));
    }
}

That will give us "-1" in output (as expected, because the green level of the app1 is '2', which is less than green level of the app2, which is '5').

Now knowing, how to compare the objects, we can take a bit more complicated task: compare by 2 fields. Let's take '_size' and '_sortOfApple' fields for that purposes. For bringing in some order, let's say, that the '_sortOfApple' is main comparing factor (I mean, at first, we're comparing by Sort, if the sorts are equal - we're comparing by Size). For that we should make some changes in the CompareTo method:

public int compareTo(Object o) {
    //if we have equal sort's of apples
    if(this._sortOfApple.compareTo(((Apple)o)._sortOfApple) == 0)
    {
        if (this._size > ((Apple)o)._size)
            return 1;
        else if (this._size < ((Apple)o)._size)
            return -1;
        else
            return 0;
    }
    else
        return this._sortOfApple.compareTo(((Apple)o)._sortOfApple);
}

Now, let's a bit widen our main method to test this changes:

public static void main( String args[] )
    {
       Apple app1 = new Apple(2, 4, "Bsort");
       Apple app2 = new Apple(5, 8, "Asort");
       
       Apple app3 = new Apple(2, 4, "Bsort");
       Apple app4 = new Apple(5, 8, "Bsort");

       Apple app5 = new Apple(2, 8, "Bsort");
       Apple app6 = new Apple(5, 8, "Bsort");

       System.out.println("Comparing app1 & app2: " + app1.compareTo(app2));
       System.out.println("Comparing app3 & app4: " + app3.compareTo(app4));
       System.out.println("Comparing app5 & app6: " + app5.compareTo(app6));
    }

here's output:

Comparing app1 & app2: 1
Comparing app3 & app4: -1
Comparing app5 & app6: 0

Seems, works fine :P

Hope this'll help you

Thanks that helped alot I am kinda being lead blindly I was not finding any good examples in book or my notes or the class slides I appreciate the help alot.

:) good luck

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.