3 #include "typedefs.hpp" 4 #include "tree_bones.hpp" 5 #include "treeiterator_bones.hpp" 6 #include "treeiterator_meat.hpp" 10 #ifndef H_PRUNER_TREE_MEAT 11 #define H_PRUNER_TREE_MEAT 16 template <
typename Data_Type>
20 if (this->POSTORDER.size() == 0u)
21 POSTORDER.reserve(this->N_NODES);
25 POSTORDER.shrink_to_fit();
27 this->reset_visited();
32 template <
typename Data_Type>
36 this->visited[i] =
true;
39 for (uint j = 0u; j < this->offspring[i].size(); ++j) {
42 if (this->visited[this->offspring[i][j]])
45 postorder_(this->offspring[i][j]);
51 POSTORDER.push_back(i);
52 for (uint j = 0u; j < this->parents[i].size(); ++j) {
55 if (this->visited[this->parents[i][j]])
58 postorder_(this->parents[i][j]);
66 template <
typename Data_Type>
73 res[0u].reserve(this->N_EDGES);
74 res[1u].reserve(this->N_EDGES);
76 for (uint i = 0u; i < this->N_NODES; ++i) {
77 for (uint j = 0u; j < this->offspring[i].size(); ++j) {
79 res[1u].push_back(this->offspring[i][j]);
87 template <
typename Data_Type>
92 "tree structure with %i nodes and %i edges.\n",
93 this->n_nodes(), this->n_edges()
102 printf(
"List of offspring:\n");
103 for (uint i = 0u; i < this->offspring.size(); ++i) {
107 if (this->offspring[i].size() == 0u)
110 v_uint::const_iterator iter;
111 for (iter = this->offspring[i].begin(); iter != this->offspring[i].end(); ++iter)
112 printf(
" %i", *iter);
120 printf(
"List of parents:\n");
121 for (uint i = 0u; i < this->parents.size(); ++i) {
125 if (this->parents[i].size() == 0u)
128 v_uint::const_iterator iter;
129 for (iter = this->parents[i].begin(); iter != this->parents[i].end(); ++iter)
130 printf(
" %i", *iter);
147 template <
typename Data_Type>
151 if (parents_.size() != offspring_.size()) {
157 uint maxid = 0u, m = parents_.size();
158 for (uint i = 0u; i < m; ++i) {
159 if ((parents_[i] > MAX_TREE_SIZE) || (offspring_[i] > MAX_TREE_SIZE)) {
164 if (maxid < parents_[i])
166 if (maxid < offspring_[i])
167 maxid = offspring_[i];
171 this->parents.resize(maxid + 1u);
172 this->offspring.resize(maxid + 1u);
174 this->visited.resize(maxid + 1u,
false);
175 this->visit_counts.resize(maxid + 1u, 0u);
178 for (uint i = 0u; i < m; ++i) {
179 this->offspring[parents_[i]].push_back(offspring_[i]);
180 this->parents[offspring_[i]].push_back(parents_[i]);
185 this->N_NODES = (uint) maxid + 1u;
195 if (!this->is_connected()) {
200 }
else if (!this->is_dag()) {
210 this->TIPS.reserve(this->n_tips());
211 while ( status == 0 ) {
212 if (this->iter.is_tip())
213 this->TIPS.push_back(*this->iter);
214 status = this->iter.up();
225 typedef v_uint::const_iterator v_uint_iter;
227 template <
typename Data_Type>
230 bool res = this->is_dag_();
231 this->reset_visited();
237 template <
typename Data_Type>
245 if (this->visited[i])
247 this->visited[i] =
true;
250 for (v_uint_iter n = this->parents[i].begin(); n != this->parents[i].end(); ++n) {
254 "Tree<Data_Type>::is_dag() @ parents (i, caller, *n, up_search): (%i, %i, %i, %i)\n",
255 i, caller, *n, up_search
260 if ((
int) *n == caller) {
262 std::printf(
"\tChecking 1:1 cycles.\n");
264 if (up_search)
return false;
268 if (!(this->is_dag_((
int) *n, i,
true)))
273 for (v_uint_iter n = this->offspring[i].begin(); n != this->offspring[i].end(); ++n) {
277 "Tree<Data_Type>::is_dag() @ offspring (i, caller, *n, up_search): (%i, %i, %i, %i)\n",
278 i, caller, *n, up_search
283 if ((
int) *n == caller) {
285 std::printf(
"\tChecking 1:1 cycles.\n");
287 if (!up_search)
return false;
291 if (!(this->is_dag_((
int) *n, i,
false)))
300 template <
typename Data_Type>
303 v_uint res = this->get_postorder();
304 std::reverse(res.begin(), res.end());
309 template <
typename Data_Type>
313 if (parents[i].size() == 0u)
320 v_uint counts(parents[i].size());
322 for (
auto iter = parents[i].begin(); iter != parents[i].end(); ++iter) {
325 counts[j] = get_dist_tip2root_(*iter, count);
329 if (counts[j++] == count)
336 for (
auto iter = counts.begin() + 1; iter != counts.end(); ++iter)
343 template <
typename Data_Type>
346 if (this->DIST_TIPS2ROOT.size() == 0u) {
349 this->DIST_TIPS2ROOT.resize(this->n_tips());
351 for (uint i = 0u; i < TIPS.size(); ++i)
352 DIST_TIPS2ROOT[i] = get_dist_tip2root_(TIPS[i], 0u);
356 return this->DIST_TIPS2ROOT;
360 #define TOTAL(a) (a)->offspring.size() 362 template <
typename Data_Type>
367 uint min_idx = 999999u, max_idx = 0u;
368 for (
auto i = POSTORDER_.begin(); i != POSTORDER_.end(); ++i) {
370 if (*i > max_idx) max_idx = *i;
371 if (*i < min_idx) min_idx = *i;
375 if ((min_idx > max_idx) | (min_idx > TOTAL(
this)) | (max_idx > TOTAL(
this)))
379 this->POSTORDER = POSTORDER_;
384 template <
typename Data_Type>
390 while (status == 0) {
393 status = this->iter.up();
401 template <
typename Data_Type>
405 v_uint OLDPOSTORDER = POSTORDER;
411 while (status == 0) {
414 status = this->iter.up();
419 POSTORDER.swap(OLDPOSTORDER);
425 template <
typename Data_Type>
431 while (status == 0) {
434 status = this->iter.down();
442 template <
typename Data_Type>
446 v_uint OLDPOSTORDER = POSTORDER;
452 while (status == 0) {
455 status = this->iter.down();
460 POSTORDER.swap(OLDPOSTORDER);
466 template <
typename Data_Type>
470 for (
auto i = this->offspring.begin(); i != offspring.end(); ++i)
478 template <
typename Data_Type>
481 if (i < this->offspring.size()) {
482 return this->offspring.at(i).size();
487 template <
typename Data_Type>
490 if (i < this->parents.size()) {
491 return this->parents.at(i).size();
void prune_postorder()
Do the tree-traversal using the postorder.
Definition: tree_meat.hpp:385
uint n_tips() const
Return the number of tips defined as nodes with no offspring.
Definition: tree_meat.hpp:467
int n_parents(uint i) const
Return the number of parents a given node has.
Definition: tree_meat.hpp:488
void prune_preorder()
Do the tree-traversal using the preorder.
Definition: tree_meat.hpp:426
Tree class.
Definition: tree_bones.hpp:36
int n_offspring(uint i) const
Return the number of offsprings a given node has.
Definition: tree_meat.hpp:479
Definition: treeiterator_bones.hpp:12
v_uint get_dist_tip2root()
Distance of tips to the closest root.
Definition: tree_meat.hpp:344