0
特殊なツリーを作成するためのインターフェイスが1つあります(BSTNode
)。クラスを作成するには、汎用クラスを実装するのですか?
public interface BSTNode<K extends Comparable<K>, V> {
/**
* Recovers the value stored in the node
* @return the value stored in the node
*/
public V getValue();
/**
* Sets the value stored in the node
* @param value the value to store in the node
*/
public void setValue(V value);
/**
* Recovers the key stored in the node
* @return the key stored in the node
*/
public K getKey();
/**
* Sets the key stored in the node
* @param key the key to store in the node
*/
public void setKey(K key);
/**
* Recover the parent stored of the current node
* @return the parent of the current node
*/
public BSTNode<K, V> getParent();
/**
* Set the parent of the current node
* @param parent to set for the current node
*/
public void setParent(BSTNode<K, V> parent);
}
しかし、私はいくつかの問題に遭遇し、このインタフェースを実装する場合:それは
は、誰が私にこのインタフェースを実装するための正しい方法を伝えることができるV cannot be resolved to a type - K cannot be resolved to a type
ことを私に思い出させ続け
public abstract class BinarySearchTreeNode implements BSTNode {
private Object value;
private Object key;
private BinarySearchTreeNode parent;
private BinarySearchTreeNode left;
private BinarySearchTreeNode right;
public BinarySearchTreeNode(){
this.value=null;
this.key=null;
this.parent=null;
this.left=null;
this.right=null;
}
public BinarySearchTreeNode(Object value, Object key, BinarySearchTreeNode parent, BinarySearchTreeNode left, BinarySearchTreeNode right){
this.value=value;
this.key=key;
this.parent=parent;
this.left=left;
this.right=right;
}
public Object getValue() {
return this.value;
}
public void setValue(Object value) {
}
public Comparable getKey() {
return null;
}
public void setKey(Comparable key) {
}
public BSTNode<K, V> getParent() {
}
public void setParent(BSTNode<K, V> parent);
?
詳細な回答ありがとうございます。私は 'K'を置き換えるためにどのタイプを使うべきですか、私はコンパレータを試しましたが、全く動作しません。 –
あなたはインターフェイス契約を定義した人なので、それを知っておく必要があります。 Kはノードに格納されているキーを意味し、Vはツリーノードに格納されている値を意味します。これらの構造を前もって定義しておくべきです。難しいものではないJavaの型パラメータとジェネリックスについての記事をご覧ください。 –