什么集合存储树结构? [英] What collection to store a tree structure?

查看:95
本文介绍了什么集合存储树结构?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在集合中存储组织结构图。我认为树数据结构将最适合我的需要,因为我需要添加多个节点到一个节点。



LinkedList 只提供一个节点添加到另一个节点,如果我理解它正确。

我已经看过C5 treeset 集合,但它似乎没有



我也看过 Treeview

code>类从Windows窗体库,但我不想添加Windows窗体 dll 到我的项目,因为我正在构建一个服务层应用程序。 (或者很好?)



我不想写自己的树集合类,如果已经有第三方提供的树集合类?



有任何建议吗?



感谢

解决方案>

这样的事情可以是一个起点。通过使用泛型,这个可以容纳任何

 类树TreeNode< T> 
{
List< TreeNode< T>儿童;

T Item {get; set;}

public TreeNode(T item)
{
Item = item;
}

public TreeNode< T> AddChild(T item)
{
TreeNode< T> nodeItem = new TreeNode< T>(item);
Children.Add(nodeItem);
return nodeItem;
}
}

包含字符串树的示例

  string root =root; 
TreeNode< string> myTreeRoot = new TreeNode< string>(root);
var first = myTreeRoot.AddChild(first child);
var second = myTreeRoot.AddChild(second child);
var grandChild = first.AddChild(first child's child);


I want to store an organisation chart in a collection. I think a tree data structure will be best suited to my needs, as I need to add multiple nodes to one node.

LinkedList only provides adding one node to another node, if I understand it correctly.

I have looked at C5 treeset collection, but it doesn't seem to have Add() method to add more than 2 nodes to one node.

I have also looked at Treeview class from Windows Forms library, but I do not want to add Windows forms dll to my project, since I am building a service layer application. (or is it fine?)

I do not want to write my own tree collection class, if there is already one provided by 3rd party?

Any suggestion please?

Thanks

解决方案

Something like this can be a starting point. By using generics this one can hold a tree of anything

class TreeNode<T>
{
    List<TreeNode<T>> Children;

    T Item {get;set;}

    public TreeNode (T item)
    {
        Item = item;
    }

    public TreeNode<T> AddChild(T item)
    {
        TreeNode<T> nodeItem = new TreeNode<T>(item);
        Children.Add(nodeItem);
        return nodeItem;
    }
}

A sample which holds a tree of strings

string root = "root";
TreeNode<string> myTreeRoot = new TreeNode<string>(root);
var first = myTreeRoot.AddChild("first child");
var second = myTreeRoot.AddChild("second child");
var grandChild = first.AddChild("first child's child");

这篇关于什么集合存储树结构?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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