Create tree from C # list list

I have a problem creating a tree from a list of strings. Here is my input:

    IReadOnlyList<IReadOnlyList<string>> listeDesParcours = new List<IReadOnlyList<string>>
    {
        new List<string>
        {
            "Produit","Sinistre","Particulier","Auto","RC"
        },
        new List<string>
        {
            "Produit","Sinistre","Entreprise","Auto","2roues"
        },
        new List<string>
        {
            "Produit","reclamation","Particulier","Moto","PP"
        },
        new List<string>
        {
            "Produit","reclamation","Entreprise","Auto","TT"
        },
        new List<string>
        {
            "Produit","reclamation","Entreprise","Auto","PP"
        },
        new List<string>
        {
            "Produit","souscription","Salarie","Aviation"
        },
        new List<string>
        {
            "Produit","souscription","Salarie","Aviation","Airbus"
        },
        new List<string>
        {
            "Produit","reclamation","Reclamation tout court"
        },
        new List<string>
        {
            "Produit","Produit tout court"
        },
        new List<string>
        {
            "Produit","Sinistre","Entreprise","Auto","5roues"
        }
    };

As you can see, this is a list of a list of strings, and I want to get a tree from it, here is my object that I want to return at the end

 public class Node
        {
            public string Value { get; set; }
            public List<Node> Childs { get; set; }
        }

and that’s exactly how I want to get the structure

                                 RootElement
                                      |
        ___________________________Produit__________________________
       /                             |                              \
__sinistre___________          reclamation_______                 Souscription
|                   \               /            \                     |           
entreprise      particulier      entreprise   particulier            Salarie______________
    |               |                |            |                       |               \
   auto            auto             auto        auto                    Marine             Aviation__
                                                                                              /      \
                                                                                           Airbus  Boing

Can someone point me to a recursive method that allows me to populate a tree from a list of list?

Thanks in advance

EDIT : After the last comment, I want to clarify that I want to get an object of type Node that I created ... however my input is a list of list of strings

+4
source share
2 answers
    var root = new Node() { Value = "RootElement", Childs = new List<Node>() };
    foreach (var route in listeDesParcours)
    {
        var current = root;
        foreach (var value in route)
        {
            var child = current.Childs.Find(x => x.Value == value);
            if (child == null)
            {
                child = new Node() { Value = value, Childs = new List<Node>() };
                current.Childs.Add(child);
            }
            current = child;
        }
    }

, listeDesParcours , root .

+1
  • rootNode, .

  • populateRootNode(Node rootNode, IReadOnlyList> input)

    rootNode. addChildrenNodes (. 3) .

  • addChildrenNodes(Node ParentNode, List input).

    node.

  • rootNode

0

Source: https://habr.com/ru/post/1662170/


All Articles