Source of 26.15.java


  1: // @author Frank M. Carrano, Timothy M. Henry
  2: // @version 5.0

  4: // Adds anEntry to the nonempty subtree rooted at rootNode.
  5: private T addEntry(BinaryNode<T> rootNode, T anEntry)
  6: {
  7:    // Assertion: rootNode != null
  8:    T result = null;
  9:    int comparison = anEntry.compareTo(rootNode.getData());

 11:    if (comparison == 0)
 12:    {
 13:       result = rootNode.getData();
 14:       rootNode.setData(anEntry);
 15:    }
 16:    else if (comparison < 0)
 17:    {
 18:       if (rootNode.hasLeftChild())
 19:          result = addEntry(rootNode.getLeftChild(), anEntry);
 20:       else
 21:          rootNode.setLeftChild(new BinaryNode<>(anEntry));
 22:    }
 23:    else
 24:    {
 25:       // Assertion: comparison > 0

 27:       if (rootNode.hasRightChild())
 28:          result = addEntry(rootNode.getRightChild(), anEntry);
 29:       else
 30:          rootNode.setRightChild(new BinaryNode<>(anEntry));
 31:    } // end if

 33:    return result;
 34: } // end addEntry