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

nodetraversal.h

Go to the documentation of this file.
00001 /***************************************************************************
00002     @file       nodetraversal.h  -  description
00003                          -------------------
00004     @date       Fri Feb 15 2002
00005     copyright            : (C) 2002 by Eva Brucherseifer
00006     email                : eva@rt.e-technik.tu-darmstadt.de
00007  ***************************************************************************/
00008 
00009 /***************************************************************************
00010  *                                                                         *
00011  *   This program is free software; you can redistribute it and/or modify  *
00012  *   it under the terms of the GNU General Library Public License as       *
00013  *   published by the Free Software Foundation; either version 2 of the    *
00014  *   License, or (at your option) any later version.                       *
00015  *                                                                         *
00016  ***************************************************************************/
00017 
00018 #ifndef NODETRAVERSAL_H
00019 #define NODETRAVERSAL_H
00020 
00021 #include <vector>
00022 #include "node.h"
00023 
00024 namespace treecomp
00025 {
00026 
00027 /**
00028   * @interface NodeTraversal
00029   * @author Eva Brucherseifer
00030   * @ingroup nodetraversal
00031   *
00032   * @brief Base class for Traversal classes, that walks through a tree
00033   * and returns a container with all visited nodes.
00034   *
00035   * @param _Node: nodes type that are traversed
00036   * @param _Container: container type that is used to collect the nodes. Default is vector<_Node*>.
00037   * The container class must support the function push_back(node) and must be assignable.
00038   *
00039   * \par Usage:
00040     \code
00041     vector<Node<string> > traversal = PreNodeTraversal()(pointer_to_tree);
00042     \endcode
00043     or
00044     \code
00045     PreNodeTraversal pretraversal;
00046     vector<Node<string> > nodevector = pretraversal(pointer_to_tree);
00047     \endcode
00048   *
00049   * To create a new traversal type, reimplement the protected walk() method.
00050   */
00051 template<class _Node, class _Container=std::vector<_Node*> >
00052 class NodeTraversal : public NonCopyable
00053 {
00054 public: 
00055     virtual ~NodeTraversal(){}
00056     /** start function */
00057     _Container& operator()(_Node*);
00058 //  const _Container getTraversal() const {m_traversal;}
00059     
00060 protected:
00061     virtual void walk(_Node*)=0;
00062     
00063     _Container m_traversal;
00064 };
00065 
00066 
00067 /**
00068   * @class IterNodeTraversal
00069   * @ingroup nodetraversal
00070   * @brief Produces a traversal using the given Iterator.
00071   *
00072   * @note For pre and postorder iterators the
00073   * corresponding traversal classes are more
00074   * specialised and efficient
00075   * @author Eva Brucherseifer
00076   */
00077 template<class _Node, class _TIterator, class _Container=std::vector<_Node*> >
00078 class IterNodeTraversal : public NodeTraversal<_Node,_Container>
00079 {
00080 protected:
00081     virtual void walk(_Node*);
00082 };
00083 
00084 
00085 
00086 
00087 /**
00088   * @class PreOrderNodeTraversal
00089   * @author Eva Brucherseifer
00090   * @ingroup nodetraversal
00091   *
00092   * @brief Produces a traversal in preorder.
00093   * Visits node first and descends then recursively.
00094   */
00095 template<class _Node, class _Container=std::vector<_Node*> >
00096 class PreOrderNodeTraversal : public NodeTraversal<_Node,_Container>
00097 {
00098 protected:
00099     virtual void walk(_Node*);
00100 };
00101 
00102 
00103 /**
00104   * @class PostOrderNodeTraversal
00105   * @author Eva Brucherseifer
00106   * @ingroup nodetraversal
00107   *
00108   * @brief Produces a traversal in postorder.
00109   * Descends first recursively and visits node then.
00110   */
00111 template<class _Node, class _Container=std::vector<_Node*> >
00112 class PostOrderNodeTraversal : public NodeTraversal<_Node,_Container>
00113 {
00114 protected:
00115     virtual void walk(_Node*);
00116 };
00117 
00118 
00119 /*
00120 class MultipleOrderNodeTraversal : public NodeTraversal
00121 {
00122 };
00123 */
00124 
00125 
00126 // =================== Template definition =================================
00127 
00128 // =====================
00129 // === NodeTraversal ===
00130 // =====================
00131 
00132 template<class _Node, class _Container>
00133 _Container& NodeTraversal<_Node,_Container>::operator()(_Node* startnode)
00134 {
00135     m_traversal.clear();    
00136     walk(startnode);
00137     return m_traversal;
00138 }
00139 
00140 // ==============================
00141 // === IterNodeTraversal ===
00142 // ==============================
00143 
00144 template<class _Node, class _TIterator, class _Container>
00145 void IterNodeTraversal<_Node,_TIterator,_Container>::walk(_Node* node)
00146 {
00147     assert(node!=0);
00148     _TIterator niter;
00149     niter.setStart(node);
00150 
00151     while(!niter.isEnd())
00152     {
00153         m_traversal.push_back(niter());
00154         ++niter;
00155     }
00156 }
00157 
00158 // ==============================
00159 // === PostOrderNodeTraversal ===
00160 // ==============================
00161 
00162 template<class _Node, class _Container>
00163 void PostOrderNodeTraversal<_Node,_Container>::walk(_Node* node)
00164 {
00165     assert(node!=0);
00166     typename std::list<typename NodeTraits<_Node>::_NonConstNode*>::const_iterator nlIter;
00167     for (nlIter = node->getChildren()->begin(); nlIter != node->getChildren()->end(); ++nlIter)
00168         walk(*nlIter);
00169     m_traversal.push_back(node);    
00170 }
00171 
00172 
00173 // ==============================
00174 // === PostOrderNodeTraversal ===
00175 // ==============================
00176 
00177 template<class _Node, class _Container>
00178 void PreOrderNodeTraversal<_Node,_Container>::walk(_Node* node)
00179 {
00180     assert(node!=0);
00181     m_traversal.push_back(node);    
00182     typename std::list<typename NodeTraits<_Node>::_NonConstNode*>::const_iterator nlIter;
00183     for (nlIter = node->getChildren()->begin(); nlIter != node->getChildren()->end(); ++nlIter)
00184         walk(*nlIter);
00185 }
00186 
00187 
00188 } // namespace treecomp
00189 
00190 #endif

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