Hi there, I'm attemtping to write my own version of the java "headSet" function which steps through a given tree set and returns all elements that are less than the function argument.
For example:
headSet(30) called for a set such as:
[2, 5, 7, 18, 22, 34, 50, 76]
should return
[2, 5, 7, 18, 22]
Where I'm having trouble is comparing the current node's data with the argument which happens to be an object of the set, here's the code so far:
public SortedSet<E> headSet(E before) {
SortedSet<E> set = new SearchTreeSet<E>();
Node check = root;
if (isEmpty()) {
return null;
}
while (check.right != null) {
check = check.right;
}
while(check.left != null) {
if(check.data > before) {
remove(check.data);
check = check.left;
}
}
return set;
}
This line:
if(check.data > before)
returns an error saying
Bad operand types for binary operator '>'
first type: E
second type: Ewhere E is a type-variable:
E extends Object declared in class SearchTreeSet
Little confused here, how am I supposed to check the current node's data with the E object's data (the argument)?
Thank you for any and all help, if you need more information, let me know!