即時嘗試創建一個方法,它允許我檢查一個BST是否包含一個項目。這是我到目前爲止有:比較器和BST
public boolean contains(Object item) {
// YOUR CODE HERE
//changes item to E so it can be used in the comparator
E value1 = (E) item;
if (root.value.equals(item)){
return true;
}
comp.compare(value1,root.value);
if(value1<root.value){
if (left == null)
return null;
else
return left.contains(item);
}
else if(item >= value){
if (right == null)
return null;
else
return right.contains(item);
}
}
,這些都是我的領域:
// Data fields
private BSTNode root;
private int count = 0;
private Comparator<E> comp; // default comparator
/** Private class for the nodes.
* Has public fields so methods in BSTSet can access fields directly.
*/
private class BSTNode {
// Data fields
public E value;
public BSTNode left = null;
public BSTNode right = null;
// Constructor
public BSTNode(E v) {
value = v;
}
}
public BSTSet() {
comp = new ComparableComparator(); // Declared below
}
public BSTSet(Comparator <E> c) {
comp = c;
}
我的問題是如何解決我的contains方法使其作品。到目前爲止,它到達comp.compare(value1.root.value)下的行,並且說'<'不能用於E類型的兩個元素。我如何解決這個問題,以便我可以繼續運行比較器?