I have written a method that adds non duplicate entries to a bst but now i want to add duplicate nodes to the right child of the original node. I have listed my method on adding non duplicate nodes but i have no clue on how to update my method to add duplicates. Thanks for your help.
private T addEntry(T newEntry) {
BinaryNodeInterface<T> currentNode = getRootNode();
assert currentNode != null;
T result = null;
boolean found = false;
while (!found) {
T currentEntry = currentNode.getData();
int comparison = newEntry.compareTo(currentEntry);
if (comparison == 0) { // newEntry matches currentEntry;
// return and replace currentEntry
found = true;
currentNode.setData(newEntry);
} else if (comparison < 0) {
if (currentNode.hasLeftChild())
currentNode = currentNode.getLeftChild();
else {
found = true;
currentNode.setLeftChild(new BinaryNode<T>(newEntry));
} // end if
} else {
assert comparison > 0;
if (currentNode.hasRightChild())
currentNode = currentNode.getRightChild();
else {
found = true;
currentNode.setRightChild(new BinaryNode<T>(newEntry));
}
}
}
return result;
}