I'm implementing the Bentley-Ottman algorithm which requires for the sweep line (SL) a data structure with the following properties:
- maintain a sorted collection of
T
, whereT
isIComparable<T>
, - insertion of elements should be
O(log count)
, and should return whether the element is already inserted, - deletion of elements should be
O(log count)
, - for a given element
e
(whether already in the collection or not) I need the previous and next element of the collection next toe
in the sort order.
SortedList<TKey, TValue>
has an O(count)
at insertion and deletion, since it has to move all consecutive elements in the list. However, I could index it, and therefore get the previous and next elements in O(1)
, once I know the index of e
.
SortedDictionary<TKey, TValue>
and SortedSet<T>
have O(log count)
insertion and deletion, but I cannot find any iterator that gives me the next and previous elements.
Is there any implementation that gives me the full functionality?
And if not, what would be the fastest way to implement it? LinkedList<T>
does not allow a binary search. List<T>
still has the O(count)
insertion/deletion. Do I really have to implement my own balanced tree?
For example the
TreeDictionary
of the c5 collection library and nuget and github has aand a
and should have nearly everything you need.