问题
If I construct, my own binary tree, then I can find the depth of each node. The sample code is as follows
template<class datatype>
void binary_node<datatype>::printNodeWithDepth(int currentNodeDepth)
{
if ( left )
left->printNodeWithDepth(currentNodeDepth+1);
std::cout << value << " and the depth is " << currentNodeDepth << std::endl;
if ( right)
right->printNodeWithDepth(currentNodeDepth+1);
}
But wondering, since map is a b-tree, is it possible to write something similar to this for a std::map
?
回答1:
std::map
is not guaranteed to be a b-tree, it is just guaranteed to have at least as good runtime complexity. To open the door for other potential implementations, the interface does not include functions for inspecting this kind of implementation details. :)
来源:https://stackoverflow.com/questions/3549255/how-to-find-the-depth-of-each-node-in-stdmap