Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Computational complexity of TreeSet operations in Java?

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. :-)

like image 201
Andreas K. Avatar asked Aug 02 '10 18:08

Andreas K.


People also ask

What is the time complexity of the following code TreeSet?

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)).

What is the time complexity of set in Java?

the elements in a set are sorted, but the add, remove, and contains methods has time complexity of o(log (n)).

What sorting algorithm does TreeSet use?

According to the API, TreeSet uses a TreeMap internally, which is based on a "Red-Black tree" algorithm.

What is the benefit of using a TreeSet?

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.


3 Answers

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.

like image 89
bshields Avatar answered Oct 02 '22 18:10

bshields


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

like image 26
Kiersten Arnold Avatar answered Oct 02 '22 19:10

Kiersten Arnold


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.

like image 39
Karel Petranek Avatar answered Oct 02 '22 18:10

Karel Petranek