树结构.NET4 [英] Tree structure .NET4
本文介绍了树结构.NET4的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
有人知道c#具有所有net4好东西并经过稳定,快速的snd测试的通用树.或一些Java语言(也许我可以移植它).(也许我会只用google sanswer,但是...)在此先感谢.
Is anybody aware of generic tree for c# that has all of net4 goodies and is stable,fast snd tested. or some for java (maybe I could port it).(probably I''ll just get use a google sanswer , but ...) Thanks in advance.
推荐答案
您不需要树的数据特殊结构.在标准库中具有这种数据类型绝对是多余的,因为树只是某些元素的列表,这些元素具有相同类型的子级"成员.
这是最简单的示例:
You don''t need a data special structure for the tree. Having such data type in a standard library would be absolutely redundant, because a tree is just a list of some elements which has a "children" member of the same type.
Here is the simplest example:
using Tree = System.Collections.Generic.List<TreeNode>;
class TreeNodeData { /*...*/ }
class TreeNode {
public TreeNodeData Data { get; set; }
public Tree Children { get { return fChildren; } }
public void AddChild(TreeNode child) { //lazily
if (fChildren == null)
fChildren = new Tree();
fChildren.Add(child);
} //AddChild
//remove, delete by index, access by index, etc.
// must not be constructed here, to avoid infinite recursion;
// lazy construction required, see above
Tree fChildren;
} //TreeNode
//...
MyTree tree = new Tree();
tree.Data = //...
tree.AddChild(new Tree());
//...
有想法吗?
Got an idea?
^ ]
只是对SAKryukovs的回答,怎么样:
Just a followup on SAKryukovs'' answer, how about:
public class TreeNode< T >
{
public class ChildList : BindingList< TreeNode< T > >
{}
private ChildList children;
private T data;
public TreeNode()
{
children = new ChildList();
}
public TreeNode(T data)
: this()
{
this.data = data;
}
public T Data
{
get
{
return data;
}
set
{
data = value;
}
}
public ChildList Children
{
get
{
return children;
}
}
}
最好的问候
Espen Harlinn
Best regards
Espen Harlinn
这篇关于树结构.NET4的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文