tree_util.h
Go to the documentation of this file.
1 
30 #ifndef tree_util_hh_
31 #define tree_util_hh_
32 
33 #include <iostream>
34 #include "tree.h"
35 
36 namespace kptree {
37 
38 template<class T>
39 void print_tree_bracketed(const tree<T>& t, std::ostream& str=std::cout);
40 
41 template<class T>
42 void print_subtree_bracketed(const tree<T>& t, typename tree<T>::iterator iRoot,
43  std::ostream& str=std::cout);
44 
45 
46 
47 // Iterate over all roots (the head) and print each one on a new line
48 // by calling printSingleRoot.
49 
50 template<class T>
51 void print_tree_bracketed(const tree<T>& t, std::ostream& str)
52  {
53  std::cout<<std::endl;
54  int headCount = t.number_of_siblings(t.begin());
55  int headNum = 0;
56  for(typename tree<T>::sibling_iterator iRoots = t.begin(); iRoots != t.end(); ++iRoots, ++headNum) {
57  print_subtree_bracketed(t,iRoots,str);
58  if (headNum != headCount) {
59  str << std::endl;
60  }
61  }
62 
63  std::cout<<std::endl;
64  }
65 
66 
67 // Print everything under this root in a flat, bracketed structure.
68 
69 template<class T>
70 void print_subtree_bracketed(const tree<T>& t, typename tree<T>::iterator iRoot, std::ostream& str)
71  {
72  if(t.empty()) return;
73  if (t.number_of_children(iRoot) == 0) {
74  str << *iRoot;
75  }
76  else {
77  // parent
78  str << *iRoot;
79  str << "(";
80  // child1, ..., childn
81  int siblingCount = t.number_of_siblings(t.begin(iRoot));
82  int siblingNum;
83  typename tree<T>::sibling_iterator iChildren;
84  for (iChildren = t.begin(iRoot), siblingNum = 0; iChildren != t.end(iRoot); ++iChildren, ++siblingNum) {
85  // recursively print child
86  print_subtree_bracketed(t,iChildren,str);
87  // comma after every child except the last one
88  if (siblingNum != siblingCount ) {
89  str << ", ";
90  }
91  }
92  str << ")";
93  }
94  }
95 
96 }
97 
98 #endif
void print_tree_bracketed(const tree< T > &t, std::ostream &str=std::cout)
Definition: tree_util.h:51
static unsigned int number_of_children(const iterator_base &)
Count the number of children of node at position.
Definition: tree.h:1810
Source code for th tree class.
bool empty() const
Check if tree is empty.
Definition: tree.h:1738
Iterator which traverses only the nodes which are siblings of each other.
Definition: tree.h:209
pre_order_iterator end() const
Return iterator to the end of the tree.
Definition: tree.h:663
pre_order_iterator begin() const
Return iterator to the beginning of the tree.
Definition: tree.h:657
Timer t
Definition: k_best.cpp:34
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:1826
void print_subtree_bracketed(const tree< T > &t, typename tree< T >::iterator iRoot, std::ostream &str=std::cout)
Definition: tree_util.h:70
Definition: tree.h:71
Depth-first iterator, first accessing the node, then its children.
Definition: tree.h:128


mtt
Author(s): Jorge Almeida
autogenerated on Mon Mar 2 2015 01:32:18