树(有向无环图)实现 [英] Tree (directed acyclic graph) implementation

查看:42
本文介绍了树(有向无环图)实现的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要像这样的树/有向无环图实现:

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 只是键和可能值的包装器(节点不必设置值).
  • 我需要指向父母和孩子的链接.
    • 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;
          }
      }
      

      您必须添加返回父级的链接.

      You will have to add a link back to the parent(s).

      这篇关于树(有向无环图)实现的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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