I'm trying to implement binary search tree in java. It can take any object as the data of a node in the tree, as long as that object implements the Comparable interface. This is needed because while putting a new node in the tree, we need to decide, whether the new node is of lesser or greater value compared to its parent. My Node class looks something like the following.
package com.java.ds.bst;
public class Node<T extends Comparable<T>> implements Comparable<T> {
private T data;
private Node<T> left = null;
private Node<T> right = null;
public Node() {
this.data = null;
}
public Node(T data) {
this.data = data;
}
public T getValue() {
return this.data;
}
public Node<T> getLeft() {
return this.left;
}
public Node<T> getRight() {
return this.right;
}
public void setLeft(Node<T> left) {
this.left = left;
}
public void setRight(Node<T> right) {
this.right = right;
}
@Override
public int compareTo(T other) {
return this.data.compareTo(other);
}
}
What I don't understand is, in the class name declaration, why do I need T extends Comparable<T>
instead of T implements Comparable<T>
?