© 1998-2002 McGraw-Hill

structure
Class BTPreorderIterator

java.lang.Object
  |
  +--structure.AbstractIterator
        |
        +--structure.BTPreorderIterator
All Implemented Interfaces:
Enumeration, 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
 Object get()
          Returns the value currently being referenced by iterator.
 boolean hasNext()
          Returns true if some nodes of subtree have yet to be considered.
 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, registerNatives, 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.
Overrides:
reset in class AbstractIterator
Postcondition:
resets the iterator to retraverse

hasNext

public boolean hasNext()
Returns true if some nodes of subtree have yet to be considered.
Overrides:
hasNext in class AbstractIterator
Postcondition:
returns true iff iterator is not finished
Returns:
True iff more nodes to be considered in traversal.

get

public Object get()
Returns the value currently being referenced by iterator.
Overrides:
get in class AbstractIterator
Precondition:
hasNext()
Postcondition:
returns reference to current value
Returns:
The current value.

next

public Object next()
Returns the current value and increments the iterator. Iterator is then incremented.
Overrides:
next in class AbstractIterator
Precondition:
hasNext();
Postcondition:
returns current value, increments iterator
Returns:
The value currently being considered.

© 1998-2002 McGraw-Hill