I am trying to clear up some things regarding complexity in some of the operations of TreeSet. On the javadoc it says:
"This implementation provides guaranteed log(n) time cost for the basic operations (add, remove and contains)."
So far so good. My question is what happens on addAll(), removeAll() etc. Here the javadoc for Set says:
"If the specified collection is also a set, the addAll operation effectively modifies this set so that its value is the union of the two sets."
Is it just explaining the logical outcome of the operation or is it giving a hint about the complexity? I mean, if the two sets are represented by e.g. red-black trees it would be better to somehow join the trees than to "add" each element of one to the other.
In any case, is there a way to combine two TreeSets into one with O(logn) complexity?
Thank you in advance. :-)
From TreeSet : This implementation provides guaranteed log(n) time cost for the basic operations ( add , remove and contains ). You are performing a ~log(n) operation n times, hence the complexity is indeed O(n log(n)).
the elements in a set are sorted, but the add, remove, and contains methods has time complexity of o(log (n)).
According to the API, TreeSet uses a TreeMap internally, which is based on a "Red-Black tree" algorithm.
TreeSet maintains objects in Sorted order defined by either Comparable or Comparator method in Java. TreeSet elements are sorted in ascending order by default. It offers several methods to deal with the ordered set like first(), last(), headSet(), tailSet(), etc.
You could imagine how it would be possible to optimize special cases to O(log n)
, but the worst case has got to be O(m log n)
where m
and n
are the number of elements in each tree.
Edit:
http://net.pku.edu.cn/~course/cs101/resource/Intro2Algorithm/book6/chap14.htm
Describes a special case algorithm that can join trees in O(log(m + n))
but note the restriction: all members of S1
must be less than all members of S2
. This is what I meant that there are special optimizations for special cases.
Looking at the java source for TreeSet, it looks like it if the passed in collection is a SortedSet then it uses a O(n) time algorithm. Otherwise it calls super.addAll, which I'm guessing will result in O(n logn).
EDIT - guess I read the code too fast, TreeSet can only use the O(n) algorithm if it's backing map is empty
According to this blog post:
http://rgrig.blogspot.com/2008/06/java-api-complexity-guarantees.html
it's O(n log n). Because the documentation gives no hints about the complexity, you might want to write your own algorithm if the performance is critical for you.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With