В настоящее время у меня есть 3 класса - DictionaryApp, BSTSet и BSTNode - как BSTSet, так и BSTNode содержат методы.Реализация двоичного дерева поиска - Содержит метод
public class BSTSet <E extends Comparable<E>> extends AbstractSet <E> {
// the root of the supporting binary search tree
private BSTNode<E> root;
// number of elements in the set
private int count = 0;
public boolean isEmpty() {
return count == 0;
}
public boolean contains(E item) throws ClassCastException {
if(root == null) return false;
return root.contains(item);
}
public boolean add(E item) {
if (item == null)
throw new IllegalArgumentException("Null cannot be added to a BSTSet");
if(contains(item))return false;
if(root == null){
root = new BSTNode(item);
count++;
return true;
}else{
root.add(item);
count++;
return true;
}
}
}
public class BSTNode <E extends Comparable<E>> {
private E value;
private BSTNode<E> left;
public BSTNode<E> right;
public BSTNode(E value) {
this.value = value;
}
public E getValue() {
return value;
}
public BSTNode<E> getLeft() {
return left;
}
public BSTNode<E> getRight() {
return right;
}
public boolean contains(E item) {
if(item.compareTo(value) == 0) return true;
if(left != null) left.contains(item);
if(right != null) right.contains(item);
// no matching node was found
return false;
}
public boolean add(E item) {
if(item.compareTo(value) < 0) {left = new BSTNode(item); return true;}
if(item.compareTo(value) > 0) {right = new BSTNode(item); return true;}
return false;
}
}
Мои вопросы, что содержит метод в классе BSTNode никогда не возвращает истину, и я не могу понять, почему. Если вы хотите больше узнать о моем коде или больше информации, пожалуйста, не стесняйтесь спрашивать.
Содержит должны смотреть на больше чем/меньше, чем на результат CompareTo и пройти соответствующее направление – spudone