Next/Previous BTree element?

Hi,
Given that a BTree has an order on its elements, I thought it wouldn’t be too hard to get the next and previous elements (where they exist). I can’t see an easy way to do this with std::collections::BTreeMap and std:: collections::BTreeSet. Am I missing something? (I may well be; I was navigating the docs on my phone.) If not, it I wrote some additions, how hard would it be to get them in the standard lib?

Thanks,

Will

Take a look at BTreeMap::range.

The "next item after x" is then .range((Bound::Excluded(x), Bound::Unbounded)).next(), and the one before it is .range((Bound::Unbounded, Bound::Excluded(x))).next_back() (or just .range(..x).next_back()).

3 Likes

Ahh. Thank you. :slight_smile: