如何找到std::map中每个节点的深度?
如果我构建自己的二叉树,那么我可以找到每个节点的深度。 示例代码如下
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);
}
但是想知道,既然map是b树,是否可以为std::map
编写类似的东西?
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
?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
std::map
不保证是 B 树,只是保证至少具有同样好的运行时复杂性。为了为其他潜在的实现打开大门,该接口不包含用于检查此类实现细节的函数。 :)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. :)