Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why is there no Tree<T> class in .NET?

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).

like image 904
LBushkin Avatar asked Jun 02 '09 21:06

LBushkin


People also ask

Does C# have a tree class?

In the C# language a tree can be implemented with classes (nodes) that point to further nodes. In this simple implementation, we use a Dictionary. AddWord This method takes a string argument and adds it to the tree by creating (or using existing) nodes (one per character).

Is there a binary tree in C#?

We can implement a single node of a binary tree as a data structure and use it to store data. The implementation is simple, like the implementation of a linked list cell.

Is there a tree class in Python?

Python TreeNode class A TreeNode is a data structure that represents one entry of a tree, which is composed of multiple of such nodes. The topmost node of a tree is called the “root”, and each node (with the exception of the root node) is associated with one parent node.

What is T-tree in data structure?

A T-tree is a balanced index tree data structure optimized for cases where both the index and the actual data are fully kept in memory, just as a B-tree is an index structure optimized for storage on block oriented secondary storage devices like hard disks.


2 Answers

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; } } 
like image 54
Jason Avatar answered Sep 25 '22 02:09

Jason


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

like image 34
Alun Harford Avatar answered Sep 24 '22 02:09

Alun Harford