ComSDK
 Указатель Классы Пространства имен Функции Переменные Определения типов Перечисления Элементы перечислений Друзья Группы Страницы
tree_util.h
1 /*
2 
3  A collection of miscellaneous utilities that operate on the templated
4  tree.hh class.
5 
6 
7  Copyright (C) 2001-2009 Kasper Peeters <kasper.peeters@aei.mpg.de>
8 
9  (At the moment this only contains a printing utility, thanks to Linda
10  Buisman <linda.buisman@studentmail.newcastle.edu.au>)
11 
12  This program is free software: you can redistribute it and/or
13  modify it under the terms of the GNU General Public License as
14  published by the Free Software Foundation, either version 3 of the
15  License, or (at your option) any later version.
16 
17  This program is distributed in the hope that it will be useful,
18  but WITHOUT ANY WARRANTY; without even the implied warranty of
19  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
20  General Public License for more details.
21 
22  You should have received a copy of the GNU General Public License
23  along with this program. If not, see <http://www.gnu.org/licenses/>.
24 
25 */
26 
27 #ifndef tree_util_hh_
28 #define tree_util_hh_
29 
30 #include <iostream>
31 #include "tree.hh"
32 
33 namespace kptree {
34 
35 template<class T>
36 void print_tree_bracketed(const tree<T>& t, std::ostream& str=std::cout);
37 
38 template<class T>
39 void print_subtree_bracketed(const tree<T>& t, typename tree<T>::iterator iRoot,
40  std::ostream& str=std::cout);
41 
42 
43 
44 // Iterate over all roots (the head) and print each one on a new line
45 // by calling printSingleRoot.
46 
47 template<class T>
48 void print_tree_bracketed(const tree<T>& t, std::ostream& str)
49  {
50  int headCount = t.number_of_siblings(t.begin());
51  int headNum = 0;
52  for(typename tree<T>::sibling_iterator iRoots = t.begin(); iRoots != t.end(); ++iRoots, ++headNum) {
53  print_subtree_bracketed(t,iRoots,str);
54  if (headNum != headCount) {
55  str << std::endl;
56  }
57  }
58  }
59 
60 
61 // Print everything under this root in a flat, bracketed structure.
62 
63 template<class T>
64 void print_subtree_bracketed(const tree<T>& t, typename tree<T>::iterator iRoot, std::ostream& str)
65  {
66  if(t.empty()) return;
67  if (t.number_of_children(iRoot) == 0) {
68  str << *iRoot;
69  }
70  else {
71  // parent
72  str << *iRoot;
73  str << "(";
74  // child1, ..., childn
75  int siblingCount = t.number_of_siblings(t.begin(iRoot));
76  int siblingNum;
77  typename tree<T>::sibling_iterator iChildren;
78  for (iChildren = t.begin(iRoot), siblingNum = 0; iChildren != t.end(iRoot); ++iChildren, ++siblingNum) {
79  // recursively print child
80  print_subtree_bracketed(t,iChildren,str);
81  // comma after every child except the last one
82  if (siblingNum != siblingCount ) {
83  str << ", ";
84  }
85  }
86  str << ")";
87  }
88  }
89 
90 };
91 
92 #endif
static unsigned int number_of_children(const iterator_base &)
Count the number of children of node at position.
Definition: tree.h:1697
bool empty() const
Check if tree is empty.
Definition: tree.h:1625
Iterator which traverses only the nodes which are siblings of each other.
Definition: tree.h:207
pre_order_iterator end() const
Return iterator to the end of the tree.
Definition: tree.h:619
pre_order_iterator begin() const
Return iterator to the beginning of the tree.
Definition: tree.h:613
unsigned int number_of_siblings(const iterator_base &) const
Count the number of siblings (left and right) of node at iterator. Total nodes at this level is +1...
Definition: tree.h:1713
Definition: tree.h:71
Depth-first iterator, first accessing the node, then its children.
Definition: tree.h:126