views:

98

answers:

4

I'm looking for a built-in Binary Search Tree implementation in .NET 4. Is there one?

+6  A: 

The SortedDictionary<K,V> class uses a Tree, is that what you're after?

See this SO answer for a discussion.

Henk Holterman
+4  A: 

You could use SortedDictionary<TKey, TValue>

Steve Townsend
+1  A: 

Another option is to use a List and sort it. Then you can use the BinarySearch method to find items. To maintain the sorted list you can use the index returned by the BinarySearch to insert at. If the returned index is negative use the complement (~ operator) as your insert location, if the returned index is positive you can insert at that location (unless you want set like behavior in which case don't insert at all).

pstrjds
This provides the same search semantics, but the underlying structure is still a plain old List, not a BST.
Steve Townsend
Good call, didn't think it through when I posted that (only 1 cup of coffee in at the time). I use the List with the BinarySearch and complement index insert to get the BST search semantics. I should read more carefully :)
pstrjds
+1  A: 

C5 library:

Class TreeDictionary implements interface ISortedDictionary and represents a dictionary of (key,value) pairs, or entries, using an ordered balanced redblack binary tree. Entry access, entry deletion, and entry insertion take time O(logn). Enumeration of the keys, values or entries of a tree dictionary follow the key order, as determined by the key comparer.

QrystaL