using System.Collections.Generic;
public class Person : INode<Person>
public string Name { get; set; }
public string FullPath { get; set; }
public IList<Person> Children { get; set; } = new List<Person>();
public interface INode<T>
public string Name { get; }
public string FullPath { get; }
public IList<T> Children { get; }
public static class Extensions
public static IEnumerable<T> BuildTrees<T>(
this IEnumerable<string> paths,
Func<string, string, T> nodeFactory,
var nodes = new Dictionary<string, T>();
var roots = new List<T>();
foreach(var path in paths)
foreach(var name in path.Split(delimiter))
fullPath = $"{fullPath}{delimiter}{name}";
if (nodes.ContainsKey(fullPath))
node = nodeFactory(name, fullPath);
nodes.Add(fullPath, node);
parent.Children.Add(node);
public static void Main()
"Parent1/Child1/SuperChild1/SupersuperChild1",
"Parent1/Child1/SuperChild1/SupersuperChild2",
"Parent2/Child2/SuperChild2/SupersuperChild3",
"Parent2/Child3/SuperChild3/SupersuperChild4",
"Parent2/Child3/SuperChild3/SupersuperChild5"
var roots = list.BuildTrees<Person>(
(name, fullPath) => new Person { Name = name, FullPath = fullPath });