本文介绍了树(定向非循环图)实现的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我需要一个树/定向非循环图实现,如下所示:
I require a tree / directed acyclic graph implementation something like this:
public class TreeNode<K, V> {
private K key; // 'key' for this node, always present
private V value; // 'value' for this node, doesn't have to be set
private TreeNode<K, V> parent;
private Set<TreeNode<K, V>> children;
}
- 没有任何排序。
-
TreeNode
只是一个包围键和一个可能的值(节点不必设置值) / li>
- 我需要链接到父母和孩子。
- There is no sorting of any kind.
- The
TreeNode
is just a wrapper around the key and a possible value (nodes don't have to have values set). - I require links to both the parent and the children.
有没有什么标准API或Commons等将为我做这个?
Is there anything out there in the standard APIs or Commons etc that will do this for me?
我不介意自己写(我肯定是不要求你们)我只是不想重新发明车轮。
I don't mind writing it myself (and I'm certainly not asking you folks to) I just don't want to re-invent the wheel.
推荐答案
似乎没有任何类型的东西上个星期我问过,最终实现了我自己的树。我的实现与你提出的非常相似:
There doesn't seem to be anything of the kind. I asked a similar question last week and ended up implementing my own tree. My implementation was very similar to what you're proposing:
public class TreeNode<T>
{
private LinkedList<TreeNode<T>> children = new LinkedList<TreeNode<T>>();
public T value { get; set; }
public TreeNode(T value)
{
this.value = value;
}
public LinkedList<TreeNode<T>> GetChildren()
{
return children;
}
}
您必须将一个链接添加回父母(s)。
You will have to add a link back to the parent(s).
这篇关于树(定向非循环图)实现的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!