Add to SortedSet<T> and its complexity

旧城冷巷雨未停 提交于 2019-12-20 17:42:18

问题


MSDN states the following SortedSet(T).Add Method :

If Count is less than the capacity of the internal array, this method is an O(1) operation.

Could someone please explain "how so"? I mean when adding new value we need to find a correct place to add a value (comparing it with another values) and internal implementation looks like a "Red-Black tree" with O (log N) insertion complexity.


回答1:


The comment is simply wrong. Yes, it is a red-black tree, O(log(n)) for inserts. Taking a look with Reflector bears this out, the private AddIfNotPresent() method contains a while() loop to find the insertion point, using normal red-black node traversal.

This doc bug has already been submitted by you-know-who.



来源:https://stackoverflow.com/questions/2533007/add-to-sortedsett-and-its-complexity

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