C#中的树状数据结构 [英] Tree data structure in C#

查看:66
本文介绍了C#中的树状数据结构的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在 C# 中寻找树或图形数据结构,但我想没有提供.使用 C# 2.0 对数据结构进行广泛检查 稍微解释一下原因.是否有常用的方便的库来提供此功能?也许通过策略模式来解决文章中提出的问题.

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

我觉得实现自己的树有点傻,就像实现自己的 ArrayList 一样.

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

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

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.

推荐答案

我最好的建议是没有标准的树数据结构,因为实现它的方法太多,不可能涵盖所有基础一种解决方案.解决方案越具体,它就越不可能适用于任何给定的问题.我什至对 LinkedList 感到恼火 - 如果我想要一个循环链表怎么办?

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?

您需要实施的基本结构是节点的集合,这里有一些选项可以帮助您入门.让我们假设类 Node 是整个解决方案的基类.

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.

如果你只需要沿着树向下导航,那么 Node 类需要一个孩子列表.

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

如果您需要向上导航树,那么 Node 类需要一个指向其父节点的链接.

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

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

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#中的树状数据结构的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆