树数据结构在C#

编程入门 行业动态 更新时间:2024-10-07 08:24:11
本文介绍了树数据结构在C#的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述 我正在C#中找一棵树或图形数据结构,但我猜没有一个提供。 使用C#2.0进行的数据结构的广泛检查解释了一些原因。是否有通常用于提供此功能的方便库?也许通过策略模式解决文章中提出的问题。

我觉得有点愚蠢的实现我自己的树,就像我将实现我自己的ArrayList一样。

我只想要一个可以不平衡的通用树。想一下目录树。 C5看起来很漂亮,但是它们的树结构似乎被实现为比表示节点层次更适合搜索的平衡红黑树。

解决方案

我最好的建议是没有标准的树数据结构,因为有很多方法可以实现它将无法用一个解决方案来覆盖所有的基础。更具体的解决方案,它对于任何给定的问题都不太适用。我甚至对LinkedList感到恼火 - 如果我想要一个循环链接列表,怎么办?

您需要实现的基本结构将是一个节点集合,这里是一些选项让你开始。我们假设类Node是整个解决方案的基类。

如果你只需要在树下导航,那么一个Node类需要一个List的List。

如果您需要向上浏览树,那么Node类需要链接到其父节点。

构建一个AddChild方法来处理这两点的所有细节和必须实现的任何其他业务逻辑(子限制,排序子项等)

I was looking for a tree or graph data structure in C# but I guess there isn't one provided. An Extensive Examination of Data Structures Using C# 2.0 explains a bit about why. Is there a convenient library which is commonly used to provide this functionality? Perhaps through a strategy pattern to solve the issues presented in the article.

I feel a bit silly implementing my own tree, just as I would implementing my own ArrayList.

I just want a generic tree which can be unbalanced. Think of a directory tree. C5 looks nifty, but their tree structures seem to be implemented as balanced red-black trees better suited to search than representing a hierarchy of nodes.

解决方案

My best advice would be that there is no standard tree data structure because there are so many ways you could implement it that it would be impossible to cover all bases with one solution. The more specific a solution, the less likely it is applicable to any given problem. I even get annoyed with LinkedList - what if I want a circular linked list?

The basic structure you'll need to implement will be a collection of nodes, and here are some options to get you started. Let's assume that the class Node is the base class of the entire solution.

If you need to only navigate down the tree, then a Node class needs a List of children.

If you need to navigate up the tree, then the Node class needs a link to its parent node.

Build an AddChild method that takes care of all the minutia of these two points and any other business logic that must be implemented (child limits, sorting the children, etc.)

更多推荐

树数据结构在C#

本文发布于:2023-11-30 06:42:13,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1649054.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:数据结构

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!