如何找到std::map中每个节点的深度?

发布于 2024-09-15 19:09:32 字数 488 浏览 1 评论 0原文

如果我构建自己的二叉树,那么我可以找到每个节点的深度。 示例代码如下

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

青芜 2024-09-22 19:09:32

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. :)

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文