1: // Removes an entry from the tree rooted at a given node.
2: // Parameters:
3: // rootNode A reference to the root of a tree.
4: // anEntry The object to be removed.
5: // oldEntry An object whose data field is null.
6: // Returns: The root node of the resulting tree; if anEntry matches
7: // an entry in the tree, oldEntry's data field is the entry
8: // that was removed from the tree; otherwise it is null.
9: // @author Frank M. Carrano, Timothy M. Henry
10: // @version 5.0
11: private BinaryNode<T> removeEntry(BinaryNode<T> rootNode, T anEntry,
12: ReturnObject oldEntry)
13: {
14: if (rootNode != null)
15: {
16: T rootData = rootNode.getData();
17: int comparison = anEntry.compareTo(rootData);
19: if (comparison == 0) // anEntry == root entry
20: {
21: oldEntry.set(rootData);
22: rootNode = removeFromRoot(rootNode);
23: }
24: else if (comparison < 0) // anEntry < root entry
25: {
26: BinaryNode<T> leftChild = rootNode.getLeftChild();
27: BinaryNode<T> subtreeRoot = removeEntry(leftChild, anEntry, oldEntry);
28: rootNode.setLeftChild(subtreeRoot);
29: }
30: else // anEntry > root entry
31: {
32: BinaryNode<T> rightChild = rootNode.getRightChild();
33: // A different way of coding than for left child:
34: rootNode.setRightChild(removeEntry(rightChild, anEntry, oldEntry));
35: } // end if
36: } // end if
38: return rootNode;
39: } // end removeEntry