C# 为什么 .NET 中没有 Tree<T> 类?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/942053/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Why is there no Tree<T> class in .NET?
提问by LBushkin
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.
.NET 中的基类库有一些优秀的集合数据结构(列表、队列、堆栈、字典),但奇怪的是它不包含任何二叉树的数据结构。对于某些算法来说,这是一个非常有用的结构,例如那些利用不同遍历路径的算法。我正在寻找正确编写的免费实现。
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.
难道我只是瞎了,没有找到它……它是否被埋在 BCL 的某个地方?如果没有,有人可以为二叉树推荐一个免费或开源的 C#/.NET 库吗?最好是使用泛型的。
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).
编辑:澄清我在找什么。我对内部使用树的有序字典集合不感兴趣。我实际上对二叉树感兴趣 - 一种公开其结构的树,以便您可以执行诸如提取子树或在节点上执行修复后遍历之类的操作。理想情况下,这样的类可以扩展为提供专门树的行为(即红/黑、AVL、平衡等)。
采纳答案by John Feminella
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".
你是对的,BCL 中什么都没有。我怀疑这是因为选择是否使用树通常是一个实现细节,否则是一种非常规的数据访问方式。也就是说,您不会说“二进制搜索元素 #37”;相反,你说,“给我元素#37”。
But have you taken a look at C5? It's super-handy and they have several tree implementations (1, 2, 3).
回答by Andrew Hare
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 articlethat will walk you through implementing your own in C#.
不,Tree<T>
BCL 中没有任何“ -like”类型(这也一直困扰着我),但这里有一篇很好的文章,将引导您在 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.
我想您可能会说基于树的数据结构在 .NET 通常用于的应用程序类型(业务应用程序、数据移动应用程序等)中不太常用。不过,我同意你的看法,奇怪的是 BCL 根本没有实现。
回答by Joel Coehoorn
回答by Sean
I believe that SortedDictionary
as the log(n) insert, retrieval characteristics that you would expect from a Tree Data Stucture.
我相信SortedDictionary
随着 log(n) 插入,您期望从树数据结构中获取检索特征。
http://msdn.microsoft.com/en-us/library/f7fta44c(VS.80).aspx
http://msdn.microsoft.com/en-us/library/f7fta44c(VS.80).aspx
回答by Matt Brunell
SortedSet<T>
is implemented as a binary search treeref. SortedDictionary<TKey, TValue>
internally makes use of SortedSet<T>
so it too is a binary search tree ref.
SortedSet<T>
被实现为二叉搜索树ref。SortedDictionary<TKey, TValue>
内部使用SortedSet<T>
所以它也是一个二叉搜索树ref。
回答by Alun Harford
What would you want from such an implementation?
你想从这样的实现中得到什么?
Binary tree? Red-black? Radix tree? B-tree? R-tree? R*-tree?
二叉树?红黑?萝卜树?B树?R树?R*-树?
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
树与其说是数据结构,不如说是一种模式,它们往往用于性能很重要的地方(因此实现细节可能也很重要)。如果 BCL 包含某种树类,则无论如何您只需要推出自己的
回答by Jason
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; }
}
回答by Gulzar Nazim
This series of articles was helpful for me when I had to write my own especially part 3 and 4.
当我不得不自己编写特别是第 3 部分和第 4 部分时,这一系列文章对我很有帮助。