Exemplo n.º 1
0
  /**
   * Returns the leaf before this node or null if this node is the first leaf in the tree.
   *
   * <p>In this implementation of the <code>MutableNode</code> interface, this operation is very
   * inefficient. In order to determine the previous node, this method first performs a linear
   * search in the parent's child-list in order to find the current node.
   *
   * <p>That implementation makes the operation suitable for short traversals from a known position.
   * But to traverse all of the leaves in the tree, you should use <code>depthFirstEnumeration
   * </code> to enumerate the nodes in the tree and use <code>isLeaf</code> on each node to
   * determine which are leaves.
   *
   * @see #depthFirstEnumeration
   * @see #isLeaf
   * @return returns the leaf before this node
   */
  public DefaultMutableTreeNode getPreviousLeaf() {
    DefaultMutableTreeNode previousSibling;
    DefaultMutableTreeNode myParent = (DefaultMutableTreeNode) getParent();

    if (myParent == null) return null;

    previousSibling = getPreviousSibling(); // linear search

    if (previousSibling != null) return previousSibling.getLastLeaf();

    return myParent.getPreviousLeaf(); // tail recursion
  }
Exemplo n.º 2
0
  /**
   * Returns the node that precedes this node in a preorder traversal of this node's tree. Returns
   * <code>null</code> if this node is the first node of the traversal -- the root of the tree. This
   * is an inefficient way to traverse the entire tree; use an enumeration, instead.
   *
   * @see #preorderEnumeration
   * @return the node that precedes this node in a preorder traversal, or null if this node is the
   *     first
   */
  public DefaultMutableTreeNode getPreviousNode() {
    DefaultMutableTreeNode previousSibling;
    DefaultMutableTreeNode myParent = (DefaultMutableTreeNode) getParent();

    if (myParent == null) {
      return null;
    }

    previousSibling = getPreviousSibling();

    if (previousSibling != null) {
      if (previousSibling.getChildCount() == 0) return previousSibling;
      else return previousSibling.getLastLeaf();
    } else {
      return myParent;
    }
  }