views:

81

answers:

4

Is there one type of set-like data structure supporting merging in O(logn) time and k-th element search in O(logn) time? n is the size of this set.

+2  A: 

You might try a Fibonacci heap which does merge in constant amortized time and decrease key in constant amortized time. Most of the time, such a heap is used for operations where you are repeatedly pulling the minimum value, so a check-for-membership function isn't implemented. However, it is simple enough to add one using the decrease key logic, and simply removing the decrease portion.

Conspicuous Compiler
+2  A: 

If k is a constant, then any meldable heap will do this, including leftist heaps, skew heaps, pairing heaps and Fibonacci heaps. Both merging and getting the first element in these structures typically take O(1) or O(lg n) amortized time, so O( k lg n) maximum.

Note, however, that getting to the k'th element may be destructive in the sense that the first k-1 items may have to be removed from the heap.

larsmans
A: 

If you're willing to accept amortization, you could achieve the desired bounds of O(lg n) time for both meld and search by using a binary search tree to represent each set. Melding two trees of size m and n together requires time O(m log(n / m)) where m < n. If you use amortized analysis and charge the cost of the merge to the elements of the smaller set, at most O(lg n) is charged to each element over the course of all of the operations. Selecting the kth element of each set takes O(lg n) time as well.

I think you could also use a collection of sorted arrays to represent each set, but the amortization argument is a little trickier.

As stated in the other answers, you can use heaps, but getting O(lg n) for both meld and select requires some work.

jonderry
A: 

Finger trees can do this and some more operations:

http://en.wikipedia.org/wiki/Finger_tree

There may be something even better if you are not restricted to purely functional data structures (i.e. aka "persistent", where by this is meant not "backed up on non-volatile disk storage", but "all previous 'versions' of the data structure are available even after 'adding' additional elements").

andy_fingerhut