我想要使用binary search tree储存节点node,在node中我加入了compareToNode来比较两个node的key,从而可以在binary search tree中判断是向左寻找还是向右寻找。但是当我在binary search tree中调用compareToNode时,会出现cannot resolve method报错,请问这是为什么?
package java.implement;
public class Node<K extends Comparable<K>,V> {
K key; // store the key ...
V value; // ... and the value ...
long insertionTime; // ... and the time this entry was inserted
// constructor
Node(K key, V value) {
this.key = key;
this.value = value;
// this gives a measure of time, in nanoseconds
insertionTime = System.nanoTime();
}
public K getKey(){ return key; }
public V getValue(){ return value; }
public Boolean compareToNode(Node n1, Node n2){
if (n1.key.compareTo(n2.key)>0){
return true;
}else{
return false;
}
}
}
在find函数中出现bug
package java.implement;
import java.Interface.IBinarySearchTree;
import java.Interface.IPosition;
import java.util.logging.Logger;
public class BinarySearchTree<Node> extends ProperLinkedBinaryTree<Node> implements IBinarySearchTree<Node> {
private static Logger logger = Logger.getLogger(BinarySearchTree.class.getName());
public BinarySearchTree() {
}
public IPosition<Node> find(IPosition<Node> start, Node node) {
if (this.isExternal(start)) {
return start;
} else {
int result = node.compareToNode(start.element());
if (result < 0) {
return this.find(this.left(start), node);
} else {
return result > 0 ? this.find(this.right(start), node) : start;
}
}
}
public void insert(Node value) {
IPosition<Node> position = this.find(this.root(), value);
if (this.isExternal(position)) {
this.expandExternal(position, value);
}
}
public void remove(Node value) {
IPosition<Node> position = this.find(this.root(), value);
if (this.isInternal(position)) {
if (this.isInternal(this.left(position)) && this.isInternal(this.right(position))) {
IPosition current;
for(current = this.right(position); this.isInternal(this.left(current)); current = this.left(current)) {
}
this.replace(position, (Node) current.element());
this.remove(current);
} else {
this.remove(position);
}
}
}
public boolean contains(Node value) {
IPosition<Node> position = this.find(this.root(), value);
return this.isInternal(position);
}
}