Given two strings a and b, where a is lexicographically < b, I\'d like to return a string c such that a < c < b. The use case is inserting a node in a database sort
This is a very simple way to achieve this and probably far from optimal (depending on what you call optimal of course).
I use only a and b. I suppose you could generalise this to use more letters.
Two simple observations:
abba < abbab.x is only always guaranteed to be possible if x ends with b. Now, replace that b by an a and append one or more letters. E.g., abbab > abbaab.The algorithm is now very simple. Start with a and b as sentinels. Inserting a new key between two existing keys x and y:
x is a prefix of y: the new key is y with the ending b replaced by ab.x is not a prefix of y: the new key is x with a b appended.Example run:
a, b
a, ab*, b
a, aab*, ab, b
a, aab, ab, abb*, b
a, aab, ab, abab*, abb, b
a, aaab*, aab, ab, abab, abb, b