1: private T addEntry(T newEntry)
2: {
3: BinaryNode<T> currentNode = getRootNode();
4: assert currentNode != null;
5: T result = null;
6: boolean found = false;
7:
8: while (!found)
9: {
10: T currentEntry = currentNode.getData();
11: int comparison = newEntry.compareTo(currentEntry);
12:
13: if (comparison == 0)
14: { // newEntry matches currentEntry;
15: // return and replace currentEntry
16: found = true;
17: result = currentEntry;
18: currentNode.setData(newEntry);
19: }
20: else if (comparison < 0)
21: {
22: if (currentNode.hasLeftChild())
23: currentNode = currentNode.getLeftChild();
24: else
25: {
26: found = true;
27: currentNode.setLeftChild(new BinaryNode<>(newEntry));
28: } // end if
29: }
30: else
31: {
32: assert comparison > 0;
33:
34: if (currentNode.hasRightChild())
35: currentNode = currentNode.getRightChild();
36: else
37: {
38: found = true;
39: currentNode.setRightChild(new BinaryNode<>(newEntry));
40: } // end if
41: } // end if
42: } // end while
43:
44: return result;
45: } // end addEntry
46: // Version 4.0