树(定向非循环图)实现

编程入门 行业动态 更新时间:2024-10-23 11:29:52
本文介绍了树(定向非循环图)实现的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我需要一个树/定向非循环图实现,如下所示:

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

更多推荐

树(定向非循环图)实现

本文发布于:2023-11-30 20:13:04,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1651342.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!