4

I have an issue creating a treee from a list of list of string. Here my input data :

    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, its a list of list of string and i want to get a tree from it . here is my object that i want to return in the end

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

and this is 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 anyone point me please to a recursive method that allows me to fill the tree from the list of list please?

Thanks in advance

EDIT : After the last comment i want to clarify that i want to get the object of type Node that i created ... however my Input is the list of list of string

4
  • hi ! Can you accept a non recursive method if it solves your problem? Commented Nov 28, 2016 at 17:19
  • @KarouiHaythem yes if i can get the object as described ofcourse it would be great ! :) Commented Nov 28, 2016 at 17:20
  • I didn't quite get if you want to use your own structure or want to use standard List of lists Commented Nov 28, 2016 at 17:21
  • @t.m. my input, as mentioned, is a list of list, and i want to return an object of type Node that i created. Commented Nov 28, 2016 at 17:24

2 Answers 2

1
    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;
        }
    }

Note that there's some difference between the data in listeDesParcours and the drawn tree, so the resulting tree in root doesn't look exactly like yours.

Sign up to request clarification or add additional context in comments.

Comments

0
  1. Create the rootNode that will be returned.

  2. Write a function like populateRootNode(Node rootNode, IReadOnlyList> input)

    In this function, create nodes for each list, an add crated nodes to children of rootNode. And call addChildrenNodes(see 3) for each List.

  3. Write a function like addChildrenNodes(Node ParentNode, List input).

    In this function, create nodes for each item of given list and add created nodes as children of current node.

  4. Return the rootNode

1 Comment

This is not a recursive method, because the input is not a tree of uniform nodes.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.