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
Create the rootNode that will be returned.
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.
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.
Return the rootNode
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.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.