LCA of two map::const_iterators

J

John

The lowest common ancestor of two nodes in a red black tree can be
computed
in O(log n) time. Is there an implementation of LCA for two map
iterators? If not,
does anyone know how to implement this on std::map? Has anyone else
done
something similar?

Thanks,
--j
 
?

=?ISO-8859-1?Q?Erik_Wikstr=F6m?=

The lowest common ancestor of two nodes in a red black tree can be
computed in O(log n) time. Is there an implementation of LCA for two
map iterators? If not, does anyone know how to implement this on
std::map? Has anyone else done something similar?

Do you have any guarantee that std::map will use a red-black tree? Sure,
it probably is but I don't think that there is anything in the
specification saying that it should be. So if such an implementation
existed it would have to be purely platform-specific. You might have
better luck in a forum specific for you platform.

Out of curiosity, why would you want such a thing? It seems to me you
are misusing std::map, if you want a tree-structure you have to make one
your self, std::map is an associative container, not a tree-structure.
 
M

Mark P

John said:
The lowest common ancestor of two nodes in a red black tree can be
computed
in O(log n) time. Is there an implementation of LCA for two map
iterators? If not,
does anyone know how to implement this on std::map? Has anyone else
done
something similar?

Thanks,
--j

This is all implementation dependent and therefore there's no portable
solution. While it's typically the case, there's no requirement that a
map be implemented as a red-black tree, and in any event the tree
structure is not visible from the map interface.
 
M

Michiel.Salters

Erik Wikström schreef:
Do you have any guarantee that std::map will use a red-black tree? Sure,
it probably is but I don't think that there is anything in the
specification saying that it should be.

There isn't. The only requirement is on the complexity of each member.

Now, I'm fairly certain that it's possible to put more than one map
element
in each node of a tree, e.g. 1 to 4 elements (this adds only O(1)
complexity,
due to the finite upper bound) in which case the 'ancestor' really
makes no
sense. Would that make sense? Perhaps for a std::map with 4 byte keys,
to improve locality.

Michiel Salters
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,774
Messages
2,569,599
Members
45,178
Latest member
Crypto Tax Software
Top