views:

124

answers:

1

Given my recent (somewhat successful) question:

http://stackoverflow.com/questions/3387274/algorithmic-issue-determining-user-sessions

I'm pretty sure that the way to solve it cleanly is to use a balanced binary tree (which would give an n log m solution to the problem, where thankfully the m is going to be quite small, even tiny, compared to the n), like hinted by one of the answer (answer which comes with some pseudo-code).

My question is simple: does the default Java API have a self-balancing tree?

If not, do you know of any implementation of such a tree (Apache, Google collections, anything)?

If nothing looks suitable, what would be the best tree to start with to implement such a balanced binary tree?

+4  A: 

java.util.TreeSet is a balanced tree implementation. It guarantees O(logn) access time by modifying tree structure when necessary, so it won't degenerate into a list.

The main question is: what operations you need from that tree and if TreeSet supports them.

Nikita Rybak