structure
Class BTPreorderIterator

java.lang.Object
  |
  +--structure.AbstractIterator
        |
        +--structure.BTPreorderIterator
All Implemented Interfaces:
java.util.Enumeration, java.util.Iterator

class BTPreorderIterator
extends AbstractIterator

This class implements an iterator that traverses a tree in pre-order. Each node is considered before its descendants.

Example usage:

      BinaryTree t = new BinaryTree();
      // ...tree is grown
      Iterator ti = t.preorderIterator();
      while (ti.hasNext())
      {
          System.out.println(ti.next());
      }
      ti.reset();
      while (ti.hasNext())
      { .... }
 


Field Summary
protected  BinaryTree root
          The root of the subtree to be considered by traversal.
protected  Stack todo
          The stack that maintains the state of the iterator.
 
Constructor Summary
BTPreorderIterator(BinaryTree root)
          Constructs a pre-order traversal of subtree rooted at root.
 
Method Summary
 java.lang.Object get()
          Returns the value currently being referenced by iterator.
 boolean hasNext()
          Returns true if some nodes of subtree have yet to be considered.
 java.lang.Object next()
          Returns the current value and increments the iterator.
 void reset()
          Resets 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 subtree to be considered by traversal.


todo

protected Stack todo
The stack that maintains the state of the iterator.

Constructor Detail

BTPreorderIterator

public BTPreorderIterator(BinaryTree root)
Constructs a pre-order traversal of subtree rooted at root.

Parameters:
root - Root of subtree to be traversed.
Method Detail

reset

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

Specified by:
reset in class AbstractIterator

hasNext

public boolean hasNext()
Returns true if some nodes of subtree have yet to be considered.

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

get

public java.lang.Object get()
Returns the value currently being referenced by iterator.

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

next

public java.lang.Object next()
Returns the current value and increments the iterator. Iterator is then incremented.

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