Блог пользователя Rajveer_100

Автор Rajveer_100, история, 3 года назад, По-английски

Hey Guys! =========

I am really so happy to share my custom Order-Statistic Tree using Red Black trees, with the Select(x, i) and Rank(x)...which allow in finding the order statistic and the rank of the key element in log(n) time...

Now I do know that PBDS in C++ does exist, but as a challenge I built this..with great hardwork..and also took reference from CLRS which helped me in understanding the working of this data structure...

At first I actually encountered this problem in CSES named Josephus Problem, and that could be solved using Circular Doubly Linked Lists (in O(N) time) as well, but even O(NlogN) would suffice so I thought of doing so...A pretty fun experience in building own custom stuff, apart from the STL...

Also wanted the opinion of you guys, on how can I improve my work or further make some new functionalities to it in the future, or even build more such data structures allowing iterators like begin() and end() for the same..As far as the debugging part I have submitted the code as well just to be sure of any errors..

CSES Josephus Problem 2 -> Accepted Code

Thank You So Much!

Have a great day ahead!!!

Here is my Code in C++ :

Spoiler
  • Проголосовать: нравится
  • +79
  • Проголосовать: не нравится

»
3 года назад, # |
  Проголосовать: нравится +8 Проголосовать: не нравится

Auto comment: topic has been updated by Rajveer_100 (previous revision, new revision, compare).

»
3 года назад, # |
  Проголосовать: нравится 0 Проголосовать: не нравится

Nice one :)

»
3 года назад, # |
  Проголосовать: нравится +6 Проголосовать: не нравится

Make better use of spoiler as well

  • »
    »
    3 года назад, # ^ |
      Проголосовать: нравится +9 Проголосовать: не нравится

    Yes, have made the changes, will learn with time, on how to make better blogs in future! Thanks!

»
3 года назад, # |
  Проголосовать: нравится 0 Проголосовать: не нравится

Congrats, nice job.

Just curious — what are the time differences between this implementation and the C++ one?

  • »
    »
    3 года назад, # ^ |
    Rev. 2   Проголосовать: нравится -8 Проголосовать: не нравится

    I have to check it out, mine will definitely be faster, cause STL has many other functions as well with templates and lots of other features, if I add them as well...then they will be close enough... :)

    • »
      »
      »
      3 года назад, # ^ |
        Проголосовать: нравится +6 Проголосовать: не нравится

      For what it's worth, I benchmarked your implementation against PBDS ordered set:

      Results Ran Locally

      Overall, I think this implementation is promising. Some notes for improvement:

      • The osRank method is a bit limited right now, since it only works for values that exist in the tree (unless I'm using it wrong).
      • When a node is not found with the find method, it returns tNil instead of NULL, and since tNil is a private instance variable, I actually can't check if the result of find is equal to tNil, so I had to modify your code to make it public.
»
3 года назад, # |
  Проголосовать: нравится 0 Проголосовать: не нравится

Nice one :)

»
3 года назад, # |
Rev. 3   Проголосовать: нравится 0 Проголосовать: не нравится

I know that your purpose is to implement the RB-tree. But if you just take a step back, you can see that the operations needed for this problem are: building the tree, finding number (base on rank/position), and deletion. There is no insertion, therefore there is no need for self-balancing once the tree is built. The algorithm for building the tree is actually very standard, you can follow the algorithm described here, and this also means that the building part is linear. For deletion, you can just apply deletion on a BST, again there is no need for rebalancing because once the tree is built, the maximum height will always be $$$O(\log n)$$$.

  • »
    »
    3 года назад, # ^ |
      Проголосовать: нравится +10 Проголосовать: не нравится

    Yes you are right buddy...I knew that...but the point was to just do some extra work and learning...and I did it as a challenge...:)

    Thanks a lot for commenting, I am grateful for it..

»
3 года назад, # |
Rev. 2   Проголосовать: нравится +2 Проголосовать: не нравится

If you know rb-tree and how this structure using, why are you not yellow /red yet?