gpt4 book ai didi

Why is there no Tree class in .NET?(为什么.NET中没有Tree类?)

转载 作者:bug小助手 更新时间:2023-10-28 13:55:49 26 4
gpt4 key购买 nike



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.

我是不是就是瞎了,找不到它...它是不是埋在集装箱里的某个地方?如果没有,有没有人可以推荐一个免费的或开源的二叉树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、平衡等)。


更多回答

Agreed. I occasionally have the need to find (in O(Log N) time) the two nodes which bound a value (when the value is not found in the collection). For example the collection (tree) contains 13 and 17 (among others) and I am looking for the greatest less than and least greater than 16. A tree could do this, but Dictionaries, sorted lists, and hash tables take O(N).

同意。我偶尔需要(在O(Log N)时间内)找到绑定一个值的两个节点(当在集合中找不到该值时)。例如,集合(树)包含13和17(以及其他),我正在寻找最大的小于和最小的大于16。树可以做到这一点,但字典、排序列表和哈希表需要O(N)。

优秀答案推荐

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; }
}


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包含某种类型的树类,那么无论如何您都只需要编写自己的类



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

你说得对,集装箱里什么都没有。我怀疑这是因为是否使用树的选择通常是一个实现细节,否则就是一种非常规的数据访问方式。也就是说,您不会说“对37号元素进行二进制搜索”;相反,您需要说“给我找37号元素”。



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

但你有没有看过C5?它非常方便,而且他们有几个树实现(1、2、3)。



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



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 被实现为二进制搜索树ref。SortedDictionary<TKey,TValue>在内部使用了SortedSet ,所以它也是一个二叉搜索树ref。



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

不,BCL中没有任何“Tree ”类型(这也是我一直感到困惑的),但这里有一篇很好的文章,它将指导您在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根本没有实现。



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

当我不得不写自己的文章,特别是第3部分和第4部分时,这一系列文章对我很有帮助。



An Extensive Examination of Data Structures

对数据结构的广泛研究



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.

您可以使用TreeNode。它不是通用的,隐藏在Windows窗体中,并与TreeView控件一起使用,但您也可以在其他地方使用它。


更多回答

This however requires you to know how many tree levels you will handle at compile time, am I wrong ?

然而,这要求您知道在编译时将处理多少个树级别,我说错了吗?

No, the C# compiler supports this syntax.

不支持,C#编译器支持此语法。

Beware that elements are un-ordered this way

请注意,元素是以这种方式取消排序的

Im curious. How do you use this? Practically? Any sample?

我很好奇。你怎么用这个?真的吗?有样品吗?

To make it an ordered tree inherit from System.Collections.Specialized.OrderedDictionary

要使其成为从System.Collections.Specialized.OrderedDictionary继承的有序树

Best answer for the "why" part of the question.

这个问题的“为什么”部分是最好的答案。

And those are only the search trees. There are also expression trees, decision tress, ...

这些只是搜索树。也有表达式树,决策树,...

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.

事实上,实施细节很重要。在我的例子中,我希望实现一些算法,作为一系列转换的一部分,对一组有组织的数据执行不同的遍历(中缀、后缀)。树形结构是解决我的问题的最优雅的方式。

In a parallel universe someone is asking the question: "Why are there no list types in .Net?", and they get the answer: "What would you want from such an implementation? An array? A linked list? A queue? A dictionary?" I think this is a non sequitur answer.

在一个平行宇宙中,有人会问这样一个问题:“为什么.NET中没有列表类型?”他们得到的答案是:“你想从这样的实现中得到什么?一个数组?一个链表?一个队列?一个词典?”我认为这是一个非必然的答案。

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.

C5支持红黑树,而不是B树。这些都是人们应该意识到的差异。B-Tree更适合基于磁盘的树或基于大内存的树。更多的节点保留在相同的位置,因此您可以获得更好的处理器缓存性能,并且读写磁盘的速度更快。

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.

不幸的是,这些只是有序映射和列表的简单实现。这两种方法都不利于作为二叉树进行重用--这些树结构只是集合的实现细节。我实际上正在寻找一个公开树结构的类。

Yeah, but it just has a Tag property of System.Object ype. No Generic <T> parameter

是的,但它只有一个标记属性System.Object类型。没有泛型参数

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.

我总是觉得做那样的事很奇怪。这在您的特定示例中不太明显,但如果人们例行公事地从依赖项中重新调整类的用途,这可能会导致难以解释的依赖项,并且如果重新调整用途的类在依赖项版本之间以意外的方式发生变化,可能会给您带来麻烦。

What would be the profit of using it? A tree node does usually not have any relevant functionality in it. It just holds references to childs and eventually a parent. No reason to misuse a System.Windows.Forms class for it! Just write it yourself - in a minute or less.

使用它会有什么好处?树节点通常没有任何相关功能。它只包含对孩子的引用,并最终引用父母。没有理由为它滥用System.Windows.Forms类!你自己写就行了--马上写或者更快写。

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com