void print_tree_post(const tree<Type>& tr, typename tree<Type>::post_order_iterator it, typename tree<Type>::post_order_iterator end) { int rootdepth=tr.depth(it); std::cout << "-----" << std::endl; while(it!=end) { for(int i=0; i<tr.depth(it)-rootdepth; ++i) std::cout << " "; std::cout << (*it) << std::endl << std::flush; ++it; } std::cout << "-----" << std::endl; }
void print_tree(const tree<Type>& tr, typename tree<Type>::pre_order_iterator it, typename tree<Type>::pre_order_iterator end) { if(!tr.is_valid(it)) return; int rootdepth=tr.depth(it); std::cout << "-----" << std::endl; while(it!=end) { for(int i=0; i<tr.depth(it)-rootdepth; ++i) std::cout << " "; std::cout << (*it) << std::endl << std::flush; ++it; } std::cout << "-----" << std::endl; }
void mergeNodes(tree<T> &intree) { // First, determine which top node has the shallowest depth int minimal_d(1000); for(typename tree<T>::leaf_iterator c = intree.begin_leaf();c != intree.end_leaf();++c){ int this_depth(intree.depth(c)); if(this_depth < minimal_d) minimal_d = this_depth; } // End c if(minimal_d == -1) return; typedef std::map<T, typename tree<T>::iterator> Info; auto now(intree.begin()); //const int max_depth(intree.max_depth()-1); const int max_depth(minimal_d); for(int depth = 0;depth < max_depth;++depth){ typename tree<T>::fixed_depth_iterator itr_begin(intree.begin_fixed(intree.begin(), depth)); Info siblings; while(intree.is_valid(itr_begin)){ int counts(siblings.count(*itr_begin)); typename tree<T>::fixed_depth_iterator itr_next(intree.next_at_same_depth(itr_begin)); // If current node and the next node are same, combine them if(!counts){ typename tree<T>::iterator current(itr_begin); siblings.insert(typename Info::value_type(*itr_begin, current)); } // End if else{ typename tree<T>::iterator current(itr_begin); typename tree<T>::iterator next(siblings[*itr_begin]); intree.merge(intree.begin(next), intree.end(next), intree.begin(current), intree.end(current)); #ifdef _DEBUG_TREE std::cout << "C: " << *current << " == N: " << *next << std::endl; #endif typename tree<T>::iterator c_back(itr_begin); const int mydepth(intree.depth(c_back)); c_back -= mydepth; intree.erase(c_back); // Erase the redundunt nodes } // End else itr_begin = itr_next; } // End while } // End depth }
void printTree(tree<T> const &intree, std::ostream& str=std::cout) { int nhead(0); auto now(intree.begin()); while(intree.is_valid(now)){ str << " " << *now << boost::format(" <-- [Head](%3d)") % nhead++ << std::endl; for(typename tree<T>::iterator sib = intree.begin(now);sib != intree.end(now);++sib){ for(int i = 0;i < intree.depth(sib);++i) str << "---"; str << "> "; str << *sib << std::endl; } // End sib now = intree.next_at_same_depth(now); } // End while }
void print_tree_rev_post(const tree<Type>& tr, typename tree<Type>::post_order_iterator it, typename tree<Type>::post_order_iterator end) { --it; int rootdepth=0;//tr.depth(it); std::cout << "-----" << std::endl; while(1==1) { for(int i=0; i<tr.depth(it)-rootdepth; ++i) std::cout << " "; std::cout << (*it) << std::endl; if(it==end) break; --it; } std::cout << "-----" << std::endl; }