Main Page   Modules   Namespace List   Class Hierarchy   Compound List   File List   Compound Members  

NodeIterator


Compounds

class  AllNodeIterator
 This iterator iterates over all nodes that follow a given node (with no respect to the root of node). More...

class  LeaveIterator
 This iterator iterates over all leaves that belong to a given node (have the same root). More...

class  NodeIterator
 This iterator iterates in preorder over all nodes that belong to a given node (have the same root). More...

class  PostOrderNodeIterator
 This iterator iterates over all nodes in post order that belong to a given node (have the same root) and are not fixed. More...

class  VariableNodeIterator

Detailed Description

These classes realize the preorder iteration over trees with the help of the iterator design pattern. Preorder iteration means, that it first visits a node before descending to its children.

A NodeIterator iterates over all nodes of a tree that have the same root. In a simple tree, that does not contain subtrees, it iterates over all nodes. If subtrees are present, the inner nodes of a subtree are not traversed. This way subtrees are treated as complete entities.

The linkage of an iterator with a tree can happen in several ways. The constructor of NodeIterator takes a node as argument which is the start node of the iteration. There also is the method NodeIterator::setStart() to set the start node after construction. The third method is inspired by STL: The methods Node::begin() and Node::end() return a NodeIterator, which points to the first resp. behind the last node. With the method NodeIterator::isEnd() there also can be tested, if the end of a traversion is reached.

  NodeIterator<Node<mytype> >* p_myIter = new NodeIterator<Node<mytype> >();
  p_myIter->setStart(p_startnode);
  myIter = p_startnode.begin();
  for (myIter=p_startnode.begin();myIter!=p_startnode.end();++myIter)
    dosomething(myIter);

The iteration with NodeIterator is done by the method NodeIterator::operator++():

  ++myIter;

The access is done by NodeIterator::operator *(), NodeIterator::operator->() or NodeIterator::operator()().

  nodepointer = myIter();
  node = *myIter;
  myIter->nodemethod();

Since the iterator interface also contains the methods NodeIterator::operator==() and NodeIterator::operator!=() for comparsion it is conform it iterator of STL. Therefore the interesting possibility is given to use the algorithms of the STL library, i.e. for_each().

The NodeIterator has 3 template parameters:

Parameters:
_Node:  node type that is iterated
_Ref:  reference type to node
_Ptr:  pointer type to node
You can choose _Node as Node<mycontenttype> or also make const iterators by choosing const Node<mycontenttype>. _Ref and _Ptr are set automatically. The easiest way to get the right iterator is to use the typedefs in Node:

  Node<mytype>::iterator p_myIter;
or
  Node<mytype>::const_iterator p_myIter;

The NodeIterator class contains the interface that is (at the moment) implemented by the following classes.

AllNodeIterator
Visits all nodes of a tree, also the ones, that are part of subtrees. Since this iterator is as important as the NodeIterator, the Node interface also contains construction methods in Node (Node::beginAll() and Node::endAll()). This iterator is used when a visitor needs information about the whole tree.
VariableNodeIterator
This iterator runs over all variable nodes, which are all nodes, that are not marked as \fixed. Like NodeIterator it is a preorder iterator that does not visit the nodes of a subtree.
LeaveIterator
The LeaveIterator iterates over all leaves of a tree or subtree. The leaves all have the same root.
PostOrderNodeIterator
The PostOrderNodeIterator visits all nodes, but in postorder. Thus it first descends and visits then the node, when it comes back. Note, that the corresponding Traversal class is much more efficient.

nodeiterator.jpg

Hierarchy of NodeIterator classes

Note:
NodeIterator and AllNodeIterator might be renamed in future versions and AllNodeIterator might become the standard iterator.

Generated on Mon Jan 6 12:02:16 2003 for TreeComp by doxygen1.2.17