I have written a method that searches for an entry in a binary search tree. It successfully searches for a value but i don't think it will find all duplicates. The reason i know it doesn't work is that i'm trying to write another method that returns an arraylist with all the duplicates of a search value. That method only returns the first value it finds. here are my two methods. for the arraylist method i think i should write a recursive method to get all the values but i'm not sure how to get that working.
public T getEntry(T entry) {
T result = null;
boolean found = false;
BinaryNodeInterface<T> currentNode = getRootNode();
while (!found && (currentNode != null)) {
T currentEntry = currentNode.getData();
if (entry.equals(currentEntry)) {
result = currentEntry;
found = true;
} else if (entry.compareTo(currentEntry) < 0)
currentNode = currentNode.getLeftChild();
else
currentNode = currentNode.getRightChild();
}
return result;
}
public ArrayList<T> getAllEntries(T searchVal) {
BinaryNodeInterface<T> currentNode = getRootNode();
ArrayList<T> array = new ArrayList<T>();
T value = getEntry(searchVal);
if (value == null)
return array;
else
array.add(getEntry(searchVal));
return array;
}