/** Delete an element from the tree. * <p>The element is deleted only if there is a node {@code n} * containing exactly the element instance specified, i.e. for which * {@code n.getElement() == element}. This is purposely * <em>different</em> from the specification of the * {@code java.util.Set} {@code remove} method (in fact, * this is the reason why a specific class has been developed).</p> * @param element element to delete (silently ignored if null) * @return true if the element was deleted from the tree */ public boolean delete(final T element) { if (element != null) { for (Node node = getNotSmaller(element); node != null; node = node.getNext()) { // loop over all elements neither smaller nor larger // than the specified one if (node.element == element) { node.delete(); return true; } else if (node.element.compareTo(element) > 0) { // all the remaining elements are known to be larger, // the element is not in the tree return false; } } } return false; }
/** Delete an element from the tree. * <p>The element is deleted only if there is a node {@code n} * containing exactly the element instance specified, i.e. for which * {@code n.getElement() == element}. This is purposely * <em>different</em> from the specification of the * {@code java.util.Set} {@code remove} method (in fact, * this is the reason why a specific class has been developed).</p> * @param element element to delete (silently ignored if null) * @return true if the element was deleted from the tree */ public boolean delete(final T element) { if (element != null) { for (Node node = getNotSmaller(element); node != null; node = node.getNext()) { // loop over all elements neither smaller nor larger // than the specified one if (node.element == element) { node.delete(); return true; } else if (node.element.compareTo(element) > 0) { // all the remaining elements are known to be larger, // the element is not in the tree return false; } } } return false; }
/** Get the node whose element is the smallest one in the tree. * @return the tree node containing the smallest element in the tree * or null if the tree is empty * @see #getLargest * @see #getNotSmaller * @see #getNotLarger * @see Node#getPrevious * @see Node#getNext */ public Node getSmallest() { return (top == null) ? null : top.getSmallest(); }
/** Get the number of elements of the tree. * @return number of elements contained in the tree */ public int size() { return (top == null) ? 0 : top.size(); }
/** Get the node whose element is the largest one in the tree. * @return the tree node containing the largest element in the tree * or null if the tree is empty * @see #getSmallest * @see #getNotSmaller * @see #getNotLarger * @see Node#getPrevious * @see Node#getNext */ public Node getLargest() { return (top == null) ? null : top.getLargest(); }
/** Get the node whose element is the largest one in the tree. * @return the tree node containing the largest element in the tree * or null if the tree is empty * @see #getSmallest * @see #getNotSmaller * @see #getNotLarger * @see Node#getPrevious * @see Node#getNext */ public Node getLargest() { return (top == null) ? null : top.getLargest(); }
/** Get the node whose element is the smallest one in the tree. * @return the tree node containing the smallest element in the tree * or null if the tree is empty * @see #getLargest * @see #getNotSmaller * @see #getNotLarger * @see Node#getPrevious * @see Node#getNext */ public Node getSmallest() { return (top == null) ? null : top.getSmallest(); }
/** Get the number of elements of the tree. * @return number of elements contained in the tree */ public int size() { return (top == null) ? 0 : top.size(); }