Is there a data structure for a sorted set allows quick lookup of the n-th (i.e. the least but n-th) item? That is, something like a a hybrid between a rope and a red-black tree.
Seems like it should be possible to either keep track of the size of the left subtree and update it through rotations or do something else clever and I’m hoping someone smart has already worked this out.
Seems like it should be possible to either keep track of the size of the left subtree and update it through rotations […]
Yes, this is quite possible; but instead of keeping track of the size of the left subtree, it’s a bit simpler to keep track of the size of the complete subtree rooted at a given node. (You can then get the size of its left subtree by examining its left-child’s size.) It’s not as tricky as you might think, because you can always re-calculate a node’s size as long as its children are up-to-date, so you don’t need any extra bookkeeping beyond making sure that you recalculate sizes by working your way up the tree.
Note that, in most mutable red-black tree implementations, ‘put’ and ‘delete’ stop walking back up the tree once they’ve restored the invariants, whereas with this approach you need to walk all the way back up the tree in all cases. That’ll be a small performance hit, but at least it’s not hard to implement. (In purely functional red-black tree implementations, even that isn’t a problem, because those always have to walk the full path back up to create the new parent nodes. So you can just put the size-calculation in the constructor — very simple.)