Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is the time complexity of the tailSet operation of java.util.TreeSet?

I was implementing the 2D closest pair algorithm using sweepline, and it says that you need to find the six points above a certain y coordinate. What I did is I put the points in a TreeSet sorted by y-coordinate, and used the tailSet method to get all points above a certain point, and iterate up to 6 times.

I was wondering if the complexity of the tailSet operation is O(log n), and if it is, is iterating over the tailSet at most six times also O(log n)?

Reference: http://people.scs.carleton.ca/~michiel/lecturenotes/ALGGEOM/sweepclosestpair.pdf

like image 340
Alvin John Burgos Avatar asked Sep 15 '25 10:09

Alvin John Burgos


2 Answers

AFAIK Taking the tailSet is O(log n), but iterating over the the last m elements is O(m * log n)

like image 106
Peter Lawrey Avatar answered Sep 16 '25 23:09

Peter Lawrey


Hmm... It’s strange to me. I thought that in terms of big O a process of creating a tailSet inside java.util.TreeSet is O(1).

Small clarification: tailSet(), headSet() and subSet() return very small objects that delegate all the hard work to methods of the underlying set. No new set is constructed. Hence O(1) and pretty insignificant.

a link for discussion

like image 31
acridity Avatar answered Sep 17 '25 01:09

acridity