com.sas.util
Class TreeIterator

com.sas.util.TreeIterator
All Implemented Interfaces:
TreeIteratorInterface, java.util.Enumeration, java.util.Iterator
Direct Known Subclasses:
PreorderTreeIterator

public abstract class TreeIterator
implements TreeIteratorInterface, java.util.Iterator

Defines an abstract base class for different tree traversal algorithms, for example preorder traversal, inorder traversal, and postorder traversal.

Each node in the tree is identified by a zero-based integer index, thus the first node, as defined by the traversal algorithm, is assigned index zero and the last node is assigned an index equal to one less than the total number of nodes in the tree.

Nodes can be visited in succession using the standard enumeration methods hasMoreElements() and nextElement(), or by jumping to a specific index via setNodeIndex(). In either case, the current node is always accessible via getNode().

Note that TreeIterator will not be aware of any changes made to the tree it's traversing, such as new or deleted nodes. Therefore it is the user of TreeIterator's responsibility to reset or reposition the iterator when traversing a dynamic tree.

See Also:
PreorderTreeIterator

Constructor Summary
TreeIterator(com.sas.models.TreeInterface tree)
          Constructor.
 
Method Summary
 boolean equals(java.lang.Object obj)
          Compares this and a given object for equality.
 boolean equals(TreeIterator other)
          Compares this and a given TreeIterator for equality.
 com.sas.models.TreeInterface getTree()
          Returns the tree being traversed.
 boolean hasNext()
          Implementation of java.util.Iterator.
 java.lang.Object next()
          Implementation of java.util.Iterator.
 void remove()
          Implementation of java.util.Iterator.
 void setNode(com.sas.models.SimpleNodeInterface node)
          Sets the current node.
 void setNodeIndex(int newIndex)
          Sets the current node via index.
 
Methods inherited from interface com.sas.util.TreeIteratorInterface
getNode, getNodeIndex, getNodeParents, hasMoreNodes, nextNode, reset
 

Constructor Detail

TreeIterator

public TreeIterator(com.sas.models.TreeInterface tree)
Constructor.

Parameters:
tree - The tree to traverse.
See Also:
getTree()
Method Detail

getTree

public com.sas.models.TreeInterface getTree()
Returns the tree being traversed.

Specified by:
getTree in interface TreeIteratorInterface
Returns:
the tree that was passed to the constructor.

setNode

public void setNode(com.sas.models.SimpleNodeInterface node)
Sets the current node.

Implementation note: the algorithm used is to sequentially search for the node from the current node to the end of the tree continuing as necessary from the beginning of the tree. If the node isn't found, an java.util.NoSuchElementException is thrown. Subclasses that have additional knowledge about the node and/or the tree may be able to implement this more efficiently and should do so.

Specified by:
setNode in interface TreeIteratorInterface
Parameters:
node - the node to position on.
See Also:
TreeIteratorInterface.getNode()

setNodeIndex

public void setNodeIndex(int newIndex)
Sets the current node via index.

Implementation note: the algorithm used is to sequentially advance the node via nextNode() until getNodeIndex() returns newIndex. If getNodeIndex() is initially greater than newIndex, then a reset() is done first. If after searching the whole tree a match wasn't found, then an IndexOutOfBoundsException is thrown. Subclasses that have additional knowledge about the node and/or the tree may be able to implement this more efficiently and should do so.

Specified by:
setNodeIndex in interface TreeIteratorInterface
Parameters:
newIndex - the zero-based index of the node to position on.
See Also:
TreeIteratorInterface.getNodeIndex()

equals

public final boolean equals(java.lang.Object obj)
Compares this and a given object for equality.

Overrides:
equals in class java.lang.Object
Returns:
true if obj is a TreeIterator and equals((TreeIterator)obj) returns true, and false otherwise.

equals

public boolean equals(TreeIterator other)
Compares this and a given TreeIterator for equality.

Returns:
true if this has the same nodeIndex and equivalent tree as other, and false otherwise.

hasNext

public boolean hasNext()
Implementation of java.util.Iterator. Alias for hasMoreElements().

Specified by:
hasNext in interface java.util.Iterator

next

public java.lang.Object next()
Implementation of java.util.Iterator. Alias for next().

Specified by:
next in interface java.util.Iterator

remove

public void remove()
Implementation of java.util.Iterator. This method is is not supported and an UnsupportedOperationException will result if called.

Specified by:
remove in interface java.util.Iterator
Throws:
java.lang.UnsupportedOperationException - remove()



Copyright © 2009 SAS Institute Inc. All Rights Reserved.