structure
Class BTPostorderIterator

java.lang.Object
  |
  +--structure.AbstractIterator
        |
        +--structure.BTPostorderIterator
All Implemented Interfaces:
java.util.Enumeration, java.util.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.
 
Constructor Summary
BTPostorderIterator(BinaryTree root)
          Construct an iterator to traverse subtree rooted at root in post-order.
 
Method Summary
 java.lang.Object get()
          Return the value of the current node.
 boolean hasNext()
          Return true iff more nodes are to be considered in traversal.
 java.lang.Object next()
          Return current value and increment iterator.
 void reset()
          Reset the iterator to the first node of the traversal.
 
Methods inherited from class structure.AbstractIterator
hasMoreElements, nextElement, remove, value
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

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.

Constructor Detail

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.
Method Detail

reset

public void reset()
Reset the iterator to the first node of the traversal.

Specified by:
reset in class AbstractIterator

hasNext

public boolean hasNext()
Return true iff more nodes are to be considered in traversal.

Specified by:
hasNext in interface java.util.Iterator
Specified by:
hasNext in class AbstractIterator
Returns:
True iff more nodes are to be considered in traversal.
See Also:
AbstractIterator.hasMoreElements()

get

public java.lang.Object get()
Return the value of the current node.

Specified by:
get in class AbstractIterator
Returns:
The value referenced by current node.

next

public java.lang.Object next()
Return current value and increment iterator.

Specified by:
next in interface java.util.Iterator
Specified by:
next in class AbstractIterator
Returns:
The value currently considered by iterator, increment.
See Also:
AbstractIterator.hasMoreElements(), AbstractIterator.value()