Rating changes for last rounds are temporarily rolled back. They will be returned soon. ×

Rajveer_100's blog

By Rajveer_100, history, 3 years ago, In English

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
  • Vote: I like it
  • +79
  • Vote: I do not like it

| Write comment?
»
3 years ago, # |
  Vote: I like it +8 Vote: I do not like it

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

»
3 years ago, # |
  Vote: I like it 0 Vote: I do not like it

Nice one :)

»
3 years ago, # |
  Vote: I like it +6 Vote: I do not like it

Make better use of spoiler as well

  • »
    »
    3 years ago, # ^ |
      Vote: I like it +9 Vote: I do not like it

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

»
3 years ago, # |
  Vote: I like it 0 Vote: I do not like it

Congrats, nice job.

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

  • »
    »
    3 years ago, # ^ |
    Rev. 2   Vote: I like it -8 Vote: I do not like it

    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 years ago, # ^ |
        Vote: I like it +6 Vote: I do not like it

      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 years ago, # |
  Vote: I like it 0 Vote: I do not like it

Nice one :)

»
3 years ago, # |
Rev. 3   Vote: I like it 0 Vote: I do not like it

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 years ago, # ^ |
      Vote: I like it +10 Vote: I do not like it

    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 years ago, # |
Rev. 2   Vote: I like it +2 Vote: I do not like it

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