平面集合中的分层树构建

Hierachial Tree buiding from flat collection

我需要从用点 (.) 分隔的分层平面集合构建一棵树,例如 C# 中的 namespaces。以下是一些条目值作为集合(有序):

A0.A0.A0
A1
A1.A2
A2.A3.A3.A2
A3.A2
A3.A4.A5.A3
A3.A4.A5.A4
B0.B1.B0
B1.B2
B1.B2.B3
B1.B2.B4

这个集合看起来像 C# 中的 namespaces。所以让我们假设它们是 namespaces(正如你所理解的 A.A.A.A namespace 是真正合法的)。

我需要什么?

我需要像这样的这个集合中的父子树(注意,我们保存一些 space 将一些名称连接成一个):

A0.A0.A0
A1
   A2
A2.A3.A3.A2
A3
   A2
   A4.A5        
      A3
      A4
B0.B1.B0
B1.B2
     B3
     B4

在这种情况下,我们将只有 6 个根对象。

这是我们算法的界面:

    public interface IParentChild
    {
        IEnumerable<IParentChild> Children { get; set; }
        string FullName { get; set; }
        string Name { get; set; }
    }

有什么建议吗?

  1. 使用此答案中给出的 Trie 数据结构 Parsing one terabyte of text and efficiently counting the number of occurrences of each word

  2. 修改#1中的Trie生成代码来存储命名空间

  3. 遍历 Trie 并将内容输出到控制台。

如果您需要更多详细信息,请对此发表评论,我可以快速编码。