Is SortedDictionary a red-black tree?

*爱你&永不变心* 提交于 2019-12-18 04:01:07

问题


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


回答1:


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.




回答2:


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.

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

Check out more details from here.




回答3:


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.




回答4:


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




回答5:


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.



来源:https://stackoverflow.com/questions/14909853/is-sorteddictionary-a-red-black-tree

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!