views:

1086

answers:

9

The base class library in .NET has some excellent data structures for collections (List, Queue, Stack, Dictionary), but oddly enough it does not contain any data structures for binary trees. This is a terribly useful structure for certain algorithms, such as those that take advantage of different traversal paths. I'm looking for a correctly written, free implementation.

Am I simply blind, and not finding it... is it buried somewhere in the BCL? If not, can someone recommend a free or open-source C#/.NET library for binary trees? Preferably one that employs generics.

EDIT: To clarify what I'm looking for. I'm not interested in ordered dictionary collections that internally use a tree. I'm actually interested in a binary tree - one that exposes its structure so that you can do things like extract subtrees, or perform post-fix traversal on the nodes. Ideally such a class could be extended to provide the behaviors of specialized trees (ie. Red/Black, AVL, Balanced, etc).

+5  A: 

No, there isn't any "Tree<T>-like" type in the BCL (something that has always puzzled me as well) but here is a good article that will walk you through implementing your own in C#.

I guess you could make the argument that tree-based data structures are less commonly used in the kind of applications that .NET is usually used for (business apps, data-moving apps, etc.). Still, I agree with you, it is strange that the BCL has no implementation at all.

Andrew Hare
A: 

There's a TreeNode you can use. It's not generic and hidden away in windows forms and used with the treeview control, but you can use it elsewhere as well.

Joel Coehoorn
Yeah, but it just has a Tag property of System.Object ype. No Generic <T> parameter
Henk Holterman
I always feel weird about doing stuff like that. It's less visible with your specific example, but if people routinely re-purpose classes from, say, dependencies, this can result in hard-to-explain dependencies and can get you into trouble if the re-purposed class changes in an unexpected way across dependency versions.
Paul Morie
+10  A: 

I believe that SortedDictionary as the log(n) insert, retrieval characteristics that you would expect from a Tree Data Stucture.

http://msdn.microsoft.com/en-us/library/f7fta44c(VS.80).aspx

Sean
+1 Good point! I didn't even think of that :)
Andrew Hare
+8  A: 

SortedDictionary<TKey, TValue> is actually a binary tree. SortedList<T> is also.

If you would like to expose the details in a more tree like fashion, you should be able to create a generic class that contains a SortedList<T> which has the interface you are looking for:

public class Tree<T>
{
   private SortedList<T> _TreeImplementation;

   // Tree interface methods, delegate storage and 
   // retrieval to _TreeImplementation.
}
Matt Brunell
Unfortunately these are simply implementations of ordered maps and lists. Neither is helpful for reuse as a binary tree - these tree structures are simply an implementation detail of the collection. I'm actually looking for a class that exposes the tree structure.
LBushkin
+13  A: 

What would you want from such an implementation?

Binary tree? Red-black? Radix tree? B-tree? R-tree? R*-tree?

A tree is more a pattern than a data structure, and they tend to be used where performance matters (so implementation details probably matter too). If the BCL included some kind of a tree class, you'd only have to roll your own anyway

Alun Harford
Best answer for the "why" part of the question.
Joel Coehoorn
And those are only the search trees. There are also expression trees, decision tress, ...
Henk Holterman
Indeed, the implementation details matter. In my case, I'm looking to implement some algorithms that would perform different traversals (infix, postfix) on an organized set of data as part of a series of transformations. A tree structure is the most elegant way of solving my problem.
LBushkin
+1  A: 

You could define your own:

public class MyTree<K, V> : Dictionary<K, MyTree<K, V>>
{
    public V Value { get; set; }
}

Or unkeyed:

public class MyTree<V> : HashSet<MyTree<V>>
{
    public V Value { get; set; }
}
Jason
A: 

This series of articles was helpful for me when I had to write my own especially part 3 and 4.

An Extensive Examination of Data Structures

Gulzar
+6  A: 

You're right, there's nothing in the BCL. I suspect this is because the choice of whether to use a tree is typically an implementation detail and is otherwise an unconventional way to access data. That is, you don't say, "binary-search-for element #37"; instead, you say, "get me element #37".

But have you taken a look at C5? It's super-handy and they have several tree implementations (1, 2, 3).

John Feminella
C5 supports Red Black trees not B-Tree. They are differences whick people should be aware of. B-Tree is more optimal for a disk based tree or large memory based tree. More nodes are kept in the same locality so you get better processor cache performance and is quicker to read write to disk.
Tony Lambert
A: 

Give a look also to the SortedList<T> class is actually a Binary Search Tree implementation.

There are out there some interesting implementations:

CMS