In many compilers, Standard data structures like Set, Map and Multimap use Red-Black-Trees in behind, and a multimap stores multiple and duplicated keys.
I have a question about below quote:
“A red-black-tree stores keys uniquely and binds just one DataValue to
each key”
- Is above statement true?
- If that is true, How we can use a red-black-tree to implement a
multimap(as C++ STL did)?
1) Nope, not true.
2) Modifying a single mapping red black tree to map keys to multiple values would be trivial. It would just require using a second data structure and mapping key -> collection.
For example, instead of mapping from a string to and int, you could map from a string to a vector of ints. Or a string to a linked list of ints. Or a string to a single-mapping RBT. So on :).
Revisiting #1: Technically that would still be mapping a key to a single value, just the value wouldn’t be the directly mapped type. Depending on what you consider a “DataValue”, then yes, the statement is true.
Also, the auxiliary data structure isn’t actually necessary; it just simplifies traversal. Basically to accommodate duplicates, instead of a strict less than/greater than relation between parent/left and parent/right, you have one of the sides also include equal.
For example: