structure
Class BTPostorderIterator
java.lang.Object
|
+--structure.AbstractIterator
|
+--structure.BTPostorderIterator
- All Implemented Interfaces:
- Enumeration, Iterator
- class BTPostorderIterator
- extends AbstractIterator
This class implements a post-order traversal of a binary tree.
This iterator considers every node after its non-trivial descendants.
Example usage:
BinaryTree
t = new BinaryTree()
;
// ...tree is grown
Iterator
ti = t.postorderIterator()
;
while (ti.hasNext()
)
{
System.out.println(ti.next()
);
}
ti.reset()
;
while (ti.hasNext()
)
{ .... }
- See Also:
BinaryTree#PostorderElements
Field Summary |
protected BinaryTree |
root
The root of the tree currently being traversed. |
protected Stack |
todo
The stack the maintains the state of the iterator. |
Method Summary |
Object |
get()
Return the value of the current node. |
boolean |
hasNext()
Return true iff more nodes are to be considered in traversal. |
Object |
next()
Return current value and increment iterator. |
void |
reset()
Reset the iterator to the first node of the traversal. |
Methods inherited from class java.lang.Object |
, clone, equals, finalize, getClass, hashCode, notify, notifyAll, registerNatives, toString, wait, wait, wait |
root
protected BinaryTree root
- The root of the tree currently being traversed.
todo
protected Stack todo
- The stack the maintains the state of the iterator.
Elements of the stack are nodes whose descendants are still being
considered.
BTPostorderIterator
public BTPostorderIterator(BinaryTree root)
- Construct an iterator to traverse subtree rooted at root
in post-order.
- Parameters:
root
- The root of the subtree to be traversed.
reset
public void reset()
- Reset the iterator to the first node of the traversal.
- Overrides:
reset
in class AbstractIterator
- Postcondition:
- Resets the iterator to retraverse
hasNext
public boolean hasNext()
- Return true iff more nodes are to be considered in traversal.
- Overrides:
hasNext
in class AbstractIterator
- Postcondition:
- Returns true iff iterator is not finished
- Returns:
- True iff more nodes are to be considered in traversal.
get
public Object get()
- Return the value of the current node.
- Overrides:
get
in class AbstractIterator
- Precondition:
- hasNext()
- Postcondition:
- returns reference to current value
- Returns:
- The value referenced by current node.
next
public Object next()
- Return current value and increment iterator.
- Overrides:
next
in class AbstractIterator
- Precondition:
- hasNext();
- Postcondition:
- returns current value, increments iterator
- Returns:
- The value currently considered by iterator, increment.