Is SortedDictionary a red-black tree?

后端 未结 5 2010
南笙
南笙 2020-12-17 16:13

I saw several quotes about this on the Internet but no official documentation? Can anyone tell me where I can get information about this?

相关标签:
5条回答
  • 2020-12-17 16:31

    Documentation does seem to guarantee O(log n) for retrieval from a BST. If they are reporting "on average" with respective to possible trees then even non-balancing implementations can claim that. Even if it were a worse case guarantee, this together with being a BST isn't enough to say whether it is or isn't implemented as a red-black trees without resorting to decompiling. It could also be an AVL, splay, or some other balancing variety.

    I pulled out dot peek. On The 4.0.0.0 System Assembly. OrderedDictionary uses Treeset which subclasses SortedSet. This does appear likely to be a red-black tree. However, it is not the typical example similar to many examples on the web these implement balancing after insertion or deletion. The implementation is primarily iterative, and the insert appears to fix the colors on the way down instead of after the insertion (top-down - there are a couple papers on this sort of approach). Something similar was up with the removal, but don't have time to verify it. Certainly not something directly comparable.

    At the very least, my guess is it should have a similar kind of run-time characteristic. By the time it gets to the insert/deletion point there isn't much it does since it was all done on the way down.

    0 讨论(0)
  • 2020-12-17 16:34

    This isn’t supposed to be documented since it’s an implementation detail.

    For instance, there is more than one implementation of SortedDictionary: there’s Microsoft’s and there’s the Mono implementation.

    And the Mono implementation does, in fact, use a red-black tree in its current version (2.10.9). So does the current .NET version (you can find that out by decompiling the code, for instance by using Reflector, ildasm.exe or the built-in IL viewer in MonoDevelop).

    However, this is probably going to change in the future since there are actually more efficient implementations (as B trees).

    So, again: this information isn’t useful, it’s an implementation detail, and it is going to change with high probability.

    0 讨论(0)
  • 2020-12-17 16:34

    You can decompile it (for example with Reflector)... BUT since that is an "implementation detail" I would not rely on it, it could be changed anytime with any update.

    Not sure how relevant such an implementation detail is but if you really need a RedBlack tree THEN implement it explicitly... anything else would be "technical debt" / "desaster waiting to happen" IMHO.

    0 讨论(0)
  • 2020-12-17 16:37

    From its MSDN page:

    The SortedDictionary generic class is a binary search tree with O(log n) retrieval, where n is the number of elements in the dictionary

    0 讨论(0)
  • 2020-12-17 16:49

    This is the official documentation from MSDN page;

    The SortedDictionary generic class is a binary search tree with O(log n) retrieval, where n is the number of elements in the dictionary


    Is SortedDictionery a red-black tree?

    Well, I'm not too much fimiliar with red-black tree but I just decompiled SortedDictionary class with dotPeek (which is free) but red-black tree's deletion algorithm and SortedDictionary's Remove() method's code doesn't seems similar. So, my money is for No.

    SortedDictionary keeps its keys always sorted. It allows you to avoid sorting the keys on your own. Its lookup performance is slower than Dictionary. It has advantages if you require a sorted lookup table in memory.

    enter image description here

    Dictionary lookup time:       Close to O(1)
    SortedDictionary lookup time: O(log n) 
    

    Check out more details from here.

    0 讨论(0)
提交回复
热议问题