tags:

views:

65

answers:

1

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?

+7  A: 

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

Magnus Hoff
Thanks. Wondering, like how people jail breakiPhone, if any of you have done some tweaking to std::map?
@nsivakr: You might want to inspect an `std::map` in runtime by using a (visual) debugger.
Magnus Hoff
@nsivakr: You are not asking how a particular STL implementation can be broken, but rather in general how all implementations can be broken. The simile would be not breaking the iPhone, but rather all mobiles from all manufacturers. The STL is implemented in headers... if you really want to achieve this, just read how your particular implementation is done.
David Rodríguez - dribeas
Thanks David. Makes a lot of sense now.
I have looked through gcc's implementation of std::map and I can tell you that at the time (3.4 maybe?) they were using red-black trees.
Niki Yoshiuchi
Thanks Mr.Niki. Appreciate your response.