001 /*
002 * This file is part of the Echo Point Project. This project is a
003 * collection of Components that have extended the Echo Web Application
004 * Framework Version 3.
005 *
006 * Version: MPL 1.1
007 *
008 * The contents of this file are subject to the Mozilla Public License Version
009 * 1.1 (the "License"); you may not use this file except in compliance with
010 * the License. You may obtain a copy of the License at
011 * http://www.mozilla.org/MPL/
012 *
013 * Software distributed under the License is distributed on an "AS IS" basis,
014 * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
015 * for the specific language governing rights and limitations under the
016 * License.
017 */
018
019 package echopoint.tree;
020
021 import java.util.Enumeration;
022
023 /**
024 * An enumeration that traverses the subtree rooted at a node in postorder.
025 * The first node returned by the enumeration's {@link #nextElement} method is
026 * the leftmost leaf. This is the same as a depth-first traversal.
027 *
028 * @author Brad Baker, Rakesh Vidyadharan 2009-05-29
029 * @version $Id: PostorderEnumeration.java 213 2009-06-03 16:34:13Z sptrakesh $
030 * @since 3.0.0b3
031 */
032 public final class PostorderEnumeration implements Enumeration<TreeNode>
033 {
034 protected TreeNode root;
035
036 protected Enumeration<TreeNode> children;
037
038 protected Enumeration<TreeNode> subtree;
039
040 /**
041 * Create a new enumeration for the specified node.
042 *
043 * @param rootNode The node for which the enumeration is to be created.
044 */
045 public PostorderEnumeration( final TreeNode rootNode )
046 {
047 super();
048 root = rootNode;
049 children = root.children();
050 subtree = DefaultMutableTreeNode.EMPTY_ENUMERATION;
051 }
052
053 /** {@inheritDoc} */
054 public boolean hasMoreElements()
055 {
056 return root != null;
057 }
058
059 /** {@inheritDoc} */
060 public TreeNode nextElement()
061 {
062 TreeNode retval;
063
064 if ( subtree.hasMoreElements() )
065 {
066 retval = subtree.nextElement();
067 }
068 else if ( children.hasMoreElements() )
069 {
070 subtree = new PostorderEnumeration( children.nextElement() );
071 retval = subtree.nextElement();
072 }
073 else
074 {
075 retval = root;
076 root = null;
077 }
078
079 return retval;
080 }
081 }