我正在尝试构建一个原始类型BST
,使用Comparable<T>
. 问题是我的声明做错了什么,因为我在Node
类类型中使用Comparable<type>
并且在BST
类中使用错误
The method setParent(Node<Comparable<Comparable<type>>>) in the type
Node<Comparable<type>> is not applicable for the arguments (Node<Comparable<type>>)
BinarySearchTree.java /lab2/src line 22 Java Problem
Node.java
:
public class Node <type> {
private Comparable<type> key;
private Node <Comparable<type>> parent;
private Node <Comparable<type>> leftChild;
private Node <Comparable<type>> rightChild;
public Node(Comparable<type> key, Node <Comparable<type>> leftChild, Node <Comparable<type>> rightChild) {
this.setKey(key);
this.setLeftChild(leftChild);
this.setRightChild(rightChild);
}
public void setKey(Comparable<type> key) {
this.key = key;
}
public Comparable<type> getKey() {
return key;
}
public void setParent(Node<Comparable<type>> y) {
this.parent = y;
}
public Node <Comparable<type>> getParent() {
return parent;
}
public void setLeftChild(Node <Comparable<type>> leftChild) {
this.leftChild = leftChild;
}
public Node <Comparable<type>> getLeftChild() {
return leftChild;
}
public void setRightChild(Node <Comparable<type>> rightChild) {
this.rightChild = rightChild;
}
public Node <Comparable<type>> getRightChild() {
return rightChild;
}
}
BinarySearchTree.java
:
import java.util.Iterator;
public class BinarySearchTree<type> implements SortedSet<type> {
private Node <Comparable<type>> root;
public void insert(Node <Comparable<type>> z) {
Node <Comparable<type>> y = null;
Node <Comparable<type>> x = root;
while (x != null) {
y = x;
if (z.getKey() < x.getKey()) { // ERROR '<' is undefined for type...
x = x.getLeftChild(); // PARAM TYPE ERROR
} else {
x = x.getRightChild(); // PARAM TYPE ERROR
}
}
z.setParent(y);
if (y == null) {
root = z;
} else if (z.getKey() < y.getKey()) {
y.setLeftChild(z);
} else {
y.setRightChild(z);
}
}