本文整理汇总了C++中tree::depth方法的典型用法代码示例。如果您正苦于以下问题:C++ tree::depth方法的具体用法?C++ tree::depth怎么用?C++ tree::depth使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类tree
的用法示例。
在下文中一共展示了tree::depth方法的5个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C++代码示例。
示例1: print_tree_post
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;
}
示例2: print_tree
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;
}
示例3: mergeNodes
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
}
示例4: printTree
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
}
示例5: print_tree_rev_post
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;
}