Maybe I am not using the right data structure. I need to use a set, but also want to efficiently return the k-th smallest element. Can TreeSet
in Java do this?
[Below, I abbreviate "kth smallest element search operation" as "Kth op."]
You need to give more details. Which operations will your data structure provide? is K in Kth operation very small compared to N, or can it be anything? How often will you have insertions & deletions compared to look ups? How often will you have Kth smallest element search compared to look ups? Are you looking for a quick solution of couple of lines within Java library, or are you willing to spend some effort to build a custom data structure?
The operations to provide could be any subset of:
LookUp (find an element by its key; where key is comparable and can be anything)
Insert
Delete
Kth
Here are some possibilities:
If there will be no/very few insertions&deletions, you can just sort the elements and use an array, with O(Log(N)) look up time and O(1) for Kth.
If O(Log(N)) for LookUp, Insert, Delete and O(k) for Kth op. is good enough, probably the easiest implementation would be Skip Lists. (Wikipedia article is very good if you need more detail)
If K is small enough, or Kth operations will only come after "insertions&deletions phase" you can keep the smallest K elements in a heap, sorting after the insertions&deletions for O(N + k Log k) time. (You will also need a seperate Hash for LookUp)
If K is arbitrary and O(N) is good enough for Kth operation, you can use a Hash for O(1) time lookup, and use a "one-sided-QuickSort" algorithm for Kth operations (the basic idea is do a quick sort but on every binary divide recurse only on the side you really need; which would give (this is a gross simplification) N (1/2 + 1/4 + 1/8 + ... ) = O(N) expected time)
You can build an Augmented "simple" Interval Tree structure with each node keeping the number of his children, so that LookUp, Insert, Delete, Kth all compute in O(Log N) time as long as the tree is balanced but perhaps it would not be difficult to implement if you are a novice.
etc. etc. The set of alternatives is infinite as the possible interpretations of your question.