Very often we use map c++ and solve many complex problem easily . But what if i am not allowed to use any stl and i have to implement map::std c++ by myself . Which data structure should i use ? Should i use Red black tree ? Is it really possible to implement map using red black tree ? If there are alternative way then tell me .
std::map is basically the same as a std::set of pairs (key, value) where we compare these pairs only by the key.
So to implement std::map, you'd just need to use a balanced binary search tree (BBST) such as red-black trees (with each node storing the key and the value). I think most STL implementations of std::map use red-black trees.
If you have to do that yourself during contest, treap is what you need, just cause it's (imho) easier to implement than other BBSTs. Also if you learn how to do that you'll also be able to implement an implicit treap which is a really powerful data structure.
nmakeenkov Ow i heard the name for the first time . Do you meant that : https://cp-algorithms.com/data_structures/treap.html ?
Can i implement set::std using treap ?
Yes, just take a look at the
Operations
section.nmakeenkov can i use treap for implementing std::set c++?
As I already said take a look at the
Operations
section.Also for std::set you'll need lower/upper bound, that is also $$$O(logN)$$$ by just trivially going into the tree. Iterators are also easy to implement (if you need that), but probably all you'll need will be the smallest/largest element (i.e left/right-most nodes). But it's the same as with any BBST as mentioned above.
I presume Samsung is gonna visit your campus for hiring and since it does not allow using <bits/stdc++.h> you are asking this question. The tricky part is you can use all the STL in Samsung test for that you should not use .h header files. If you want to use map you must use
I can't see your comment
To see the comment one requires Hustle, Loyalty and Respect my friend.