I want to remove values between a certain range in a TreeSet as follows:
TreeSet<Integer> ts = new TreeSet<Integer>();
for(int i = 0; i < (int)1e7; i++){
ts.add((int)(Math.random() * 1e9));
}
System.out.println("ts: " + ts.size());
SortedSet<Integer> s = ts.subSet(500, (int)8e8);
s.clear();
System.out.println("ts: " + ts.size());
What is the runtime of this operation?
Is it O(log(n))
or O(n)
? I found some references stating that the subSet()
operation takes log(n)
. I've also read that the clear()
operation is O(1)
, but wouldn't the individual nodes to be cleared, making it potentially O(n)
?