00001
00030 #ifndef tree_util_hh_
00031 #define tree_util_hh_
00032
00033 #include <iostream>
00034 #include "tree.hh"
00035
00036 namespace kptree {
00037
00038 template<class T>
00039 void print_tree_bracketed(const tree<T>& t, std::ostream& str=std::cout);
00040
00041 template<class T>
00042 void print_subtree_bracketed(const tree<T>& t, typename tree<T>::iterator iRoot,
00043 std::ostream& str=std::cout);
00044
00045
00046
00047
00048
00049
00050 template<class T>
00051 void print_tree_bracketed(const tree<T>& t, std::ostream& str)
00052 {
00053 std::cout<<std::endl;
00054 int headCount = t.number_of_siblings(t.begin());
00055 int headNum = 0;
00056 for(typename tree<T>::sibling_iterator iRoots = t.begin(); iRoots != t.end(); ++iRoots, ++headNum) {
00057 print_subtree_bracketed(t,iRoots,str);
00058 if (headNum != headCount) {
00059 str << std::endl;
00060 }
00061 }
00062
00063 std::cout<<std::endl;
00064 }
00065
00066
00067
00068
00069 template<class T>
00070 void print_subtree_bracketed(const tree<T>& t, typename tree<T>::iterator iRoot, std::ostream& str)
00071 {
00072 if(t.empty()) return;
00073 if (t.number_of_children(iRoot) == 0) {
00074 str << *iRoot;
00075 }
00076 else {
00077
00078 str << *iRoot;
00079 str << "(";
00080
00081 int siblingCount = t.number_of_siblings(t.begin(iRoot));
00082 int siblingNum;
00083 typename tree<T>::sibling_iterator iChildren;
00084 for (iChildren = t.begin(iRoot), siblingNum = 0; iChildren != t.end(iRoot); ++iChildren, ++siblingNum) {
00085
00086 print_subtree_bracketed(t,iChildren,str);
00087
00088 if (siblingNum != siblingCount ) {
00089 str << ", ";
00090 }
00091 }
00092 str << ")";
00093 }
00094 }
00095
00096 }
00097
00098 #endif