I'm looking for an implementation of aRed-Black Tree in C#, with the following features:
- Search, Insert and Delete in O(log n).
- Members type should be generic.
- Support inComparer(T), for sorting
Tby different fields in it. - Searching in the tree should be with the specific field, so it won't accept
T, but it'll accept the field type sorting it. - Searching shouldn't be only exact value. Should support searching the lower/higher one.
Thank you.
- 1Answering to your other question, named "Book or Teacher", thereally best way to learn programming is towrite programs. Write this one on your own and then you'll learn something.P Shved– P Shved2009-11-09 19:47:57 +00:00CommentedNov 9, 2009 at 19:47
- 1@Pavel: I could write this, but I'm looking for something ready, so I can continue develop the main sides of my program, and speed up development.Alon Gubkin– Alon Gubkin2009-11-09 19:54:42 +00:00CommentedNov 9, 2009 at 19:54
4 Answers4
You mostly just describedSortedDictionary<T, U>, except for the next-lowest/next-highest value binary search, which you could implement on your own without much difficulty.
Are there specific reasons thatSortedDictionary is insufficient for you?
3 Comments
SortedSet<T>, which is implemented as a red/black tree. I'm not sure where I heard about it -- I feel like an old version of the MSDN documentation went into more detail about the implementation ofSortedDictionary in contrast toSortedList. Also, umm, I'm not exactly sure why I thought he could easily extend it. It isn't clear that he could. Oh well.Rip the TreeSet from C5 collection libs.
Comments
This is exactly the OrderedDictionary in PowerCollections. It's pretty much identical to SortedDictionary (red black tree with generics) with the addition of the ability to set a start key/end key and scan all values in that range.
SortedDicionary only allows exposes a GetEnumerator() function that starts at the beginning of the collection and only allows a MoveNext() call, so even if you use LINQ there is nothing magic happening: it starts at the beginning and runs your expression on every single node, in order, until it finds those matching your LINQ expression.
OrderedDictionary has a function that gets an enumerator at or before a particular key and that does the lookup in O(log n).
A word of caution though: the enumerator in the PowerCollections OrderedDictionary is implemented using "yield" and the memory usage and enumeration performance is at least O(n^2)... you can change the implementation yourself to make it implement a traditional enumerator and both of these problems go away. I'll submit that patch to Codeplex if I can ever find the time.
Comments
System.Collections.Generic.SortedSet is implemented as a red-black tree.
Comments
Explore related questions
See similar questions with these tags.


