tree_util.h

Go to the documentation of this file.
00001 /*
00002    A collection of miscellaneous utilities that operate on the templated 
00003    tree.hh class.
00004 
00005    (At the moment only a printing utility.
00006    Linda Buisman, linda.buisman@studentmail.newcastle.edu.au)
00007 
00008    This program is free software; you can redistribute it and/or modify
00009    it under the terms of the GNU General Public License as published by
00010    the Free Software Foundation; version 2.
00011    
00012    This program is distributed in the hope that it will be useful,
00013    but WITHOUT ANY WARRANTY; without even the implied warranty of
00014    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00015    GNU General Public License for more details.
00016    
00017    You should have received a copy of the GNU General Public License
00018    along with this program; if not, write to the Free Software
00019    Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
00020 
00021 */
00022 
00023 #ifndef tree_util_hh_
00024 #define tree_util_hh_
00025 
00026 #include <iostream>
00027 #include <string>
00028 #include "tree.h"
00029 
00030 namespace kptree {
00031 
00032 // Iterate over all roots (the head) and print each one on a new line
00033 // by calling printSingleRoot.
00034 
00035 template<class T>
00036 void print_tree_bracketed(const tree<T>& t, std::ostream& str)
00037    {
00038    int headCount = t.number_of_siblings(t.begin());
00039    int headNum = 0;
00040    for(typename tree<T>::sibling_iterator iRoots = t.begin(); iRoots != t.end(); iRoots++) {
00041       print_subtree_bracketed(t,iRoots,str,"");
00042       if (headNum <= headCount - 1) {
00043          str << std::endl;
00044          }
00045       }
00046    }
00047 
00048 
00049 // Print everything under this root in a flat, bracketed structure.
00050 template<class T>
00051 void print_subtree_bracketed(const tree<T>& t, typename tree<T>::iterator iRoot, std::ostream& str, std::string padding = "") 
00052    {
00053    if(t.empty()) return;
00054    if (t.number_of_children(iRoot) == 0) {
00055       str << padding << *iRoot << std::endl;
00056       }
00057    else {
00058       // parent
00059       str << padding << *iRoot << std::endl;
00060       // child1, ..., childn
00061       int siblingNum;
00062       typename tree<T>::sibling_iterator iChildren;
00063       for (iChildren = t.begin(iRoot), siblingNum = 0; iChildren != t.end(iRoot); ++iChildren) {
00064          // recursively print child
00065          print_subtree_bracketed(t,iChildren,str, padding + " ");
00066          // comma after every child except the last one
00067          }
00068       }
00069    }
00070 }
00071 
00072 #endif

Generated on Wed Feb 27 20:31:06 2008 for php.ast.svn.src. by  doxygen 1.5.3