树数据结构在C#

树数据结构在C#

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

问题描述

我正在C#中找一棵树或图形数据结构,但我猜没有一个提供。 解释了一些原因。是否有通常用于提供此功能的方便库?也许通过策略模式解决文章中提出的问题。



我觉得有点愚蠢的实现我自己的树,就像我将实现我自己的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#的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-20 03:26