I did the following binary search tree but it doesn't work... need help
class Tree<ContentType extends Comparable<ContentType>>{
Node node;
public Tree(){
node = null;
}
public Tree(ContentType pContent){
node = new Node<ContentType>(pContent);
}
public Tree(ContentType pContent, Tree pLeftTree, Tree pRightTree){
node = new Node(pContent);
node.setLeftTree(pLeftTree);
node.setRightTree(pRightTree);
}
public boolean isEmpty(){
...
}
public void insert(ContentType pContent){
...
}
public ContentType search(ContentType pContent){
...
}
public void delete(ContentType pContent){
...
}
public ContentType getContent(){
...
}
...
private class Node<ContentType>{
Tree left = null;
Tree right = null;
ContentType content = null;
public Node(ContentType pContent){
content=pContent;
left = new Tree();
right = new Tree();
}
...
}
}
Now it tells me
type-variable Content-Type#1 is not within bounds of type-variable Content-Type#2.
Thanks for all help
Here's the fully fixed class file - make sure to specify the generic types when declaring all instances of a type!
class Tree<ContentType extends Comparable<ContentType>>{
// here
Node<ContentType> node;
public Tree(){
node = null;
}
public Tree(ContentType pContent){
node = new Node<ContentType>(pContent);
}
// each time Tree is used, it should be typed!
public Tree(ContentType pContent, Tree<ContentType> pLeftTree, Tree<ContentType> pRightTree){
// same with Node!
node = new Node<ContentType>(pContent);
node.setLeftTree(pLeftTree);
node.setRightTree(pRightTree);
}
//Node needs to be typed with the exact same type as Tree!
private class Node<ContentType extends Comparable<ContentType>>{
// Typed Tree!
Tree<ContentType> left = null;
Tree<ContentType> right = null;
ContentType content = null;
public Node(ContentType pContent){
content=pContent;
left = new Tree<ContentType>();
right = new Tree<ContentType>();
}
public void setLeftTree(Tree<ContentType> tree) {
left = tree;
}
public void setRightTree(Tree<ContentType> tree) {
right = tree;
}
}
}
And to compile w/o warnings:
~ bfung$ javac -version
javac 1.8.0_40
~ bfung$ javac Tree.java
~ bfung$
Collected from the Internet
Please contact [email protected] to delete if infringement.
Comments