All Packages Class Hierarchy This Package Previous Next Index
Class graph.TreeIterator
java.lang.Object
|
+----graph.TreeIterator
- public class TreeIterator
- extends Object
This class represents iterators for class Tree.
Iteration over objects of class Tree is essentially tree traversal.
Class TreeIterator also has methods that enable
changes in traversed trees in the process of traversal.
Traversal of trees is implemented with using a stack of nodes.
Traversals in pre-order, post-order, and traversals with
multiple visits of non-terminal nodes are supported.
- See Also:
- Tree
-
TreeIterator(Tree)
- Constructs a TreeIterator to walk over the specified Tree.
-
allorder()
- Iterates to the next tree node by going through all edges first forward and then backward.
-
current()
- Returns the currently visited node of the traversed Tree.
-
isPost()
- Returns true if the currently visited node is not a leaf node and it is
the last visit of this node.
-
isPre()
- Returns true if the currently visited node is not a leaf node and it is
the first visit of this node.
-
pop()
- Forces to walk to the parent of the currently visited node.
-
postorder()
- Iterates to the next tree node in post-order.
-
preorder()
- Iterates to the next tree node in pre-order.
-
push(int)
- Forces to walk to the child with the specified index from the currently visited node.
-
skip()
- Forces to skip visiting of yet unvisited children of the currently visited node.
-
substitute(Tree)
- Substitutes the specified Tree for the currently visited node
in the traversed tree.
-
substituteCopy(Tree)
- Substitutes a copy of the specified Tree for the currently visited node
in the traversed tree.
-
visit()
- Returns a number specifying the current visit.
TreeIterator
public TreeIterator(Tree p)
- Constructs a TreeIterator to walk over the specified Tree.
- Parameters:
- p - tree to traverse
visit
public int visit()
- Returns a number specifying the current visit.
It returns the ordinal number of the current visit for all-order traversals.
If this is the last visit, lastIndex()+1 is returned for non-terminal nodes.
This method always returns 1 for pre-order.
It returns lastIndex()+1 when walking non-terminal nodes in post-order.
This method is useful when doing all-order traversals visit
non-terminal tree nodes several times.
isPre
public boolean isPre()
- Returns true if the currently visited node is not a leaf node and it is
the first visit of this node.
Returns false otherwise.
isPost
public boolean isPost()
- Returns true if the currently visited node is not a leaf node and it is
the last visit of this node.
Returns false otherwise.
skip
public void skip()
- Forces to skip visiting of yet unvisited children of the currently visited node.
Note that the current node shall not be modified between skip()
and the following iterator invocation. Otherwise, there is no guarantee that
children of this node are skipped.
preorder
public synchronized Tree preorder()
- Iterates to the next tree node in pre-order.
postorder
public synchronized Tree postorder()
- Iterates to the next tree node in post-order.
allorder
public synchronized Tree allorder()
- Iterates to the next tree node by going through all edges first forward and then backward.
Therefore, each node is visited before visiting the first child
and then after visiting each child. Terminal nodes are visited once.
current
public Tree current()
- Returns the currently visited node of the traversed Tree.
pop
public synchronized void pop()
- Forces to walk to the parent of the currently visited node.
push
public synchronized void push(int n)
- Forces to walk to the child with the specified index from the currently visited node.
- Parameters:
- n - index of the child to push onto the traversal stack
substitute
public synchronized void substitute(Tree r)
- Substitutes the specified Tree for the currently visited node
in the traversed tree. The substituted Tree becomes
the latest visited node.
The parameter should not be a subtree unless
it is a subtree of the currently visited node.
- Parameters:
- p - tree that replaces the currently visited node
- Throws: RuntimeException
- the currently visited node is a root,
or an attempt is made to substitute at an intermediate visit (neither first nor last),
or an attempt is made to substitute a subtree which is not a part of the tree rooted at
the currently visited node
substituteCopy
public synchronized void substituteCopy(Tree u)
- Substitutes a copy of the specified Tree for the currently visited node
in the traversed tree. The substituted Tree becomes
the latest visited node.
- Parameters:
- u - tree whose copy replaces the currently visited node
- Throws: RuntimeException
- the currently visited node is a root,
or an attempt is made to substitute at an intermediate visit (neither first nor last)
All Packages Class Hierarchy This Package Previous Next Index