Source of 25.15.java


  1: // Adds newEntry to the nonempty subtree rooted at rootNode.
  2: private T addEntry(BinaryNode<T> rootNode, T newEntry)
  3: {
  4:    assert rootNode != null;
  5:    T result = null;
  6:    int comparison = newEntry.compareTo(rootNode.getData());
  7: 
  8:    if (comparison == 0)
  9:    {
 10:       result = rootNode.getData();
 11:       rootNode.setData(newEntry);
 12:    }
 13:    else if (comparison < 0)
 14:    {
 15:       if (rootNode.hasLeftChild())
 16:          result = addEntry(rootNode.getLeftChild(), newEntry);
 17:       else
 18:          rootNode.setLeftChild(new BinaryNode<>(newEntry));
 19:    }
 20:    else
 21:    {
 22:       assert comparison > 0;
 23: 
 24:       if (rootNode.hasRightChild())
 25:          result = addEntry(rootNode.getRightChild(), newEntry);
 26:       else
 27:          rootNode.setRightChild(new BinaryNode<>(newEntry));
 28:    } // end if
 29: 
 30:    return result;
 31: } // end addEntry
 32: // Version 4.0