Note that there are some explanatory texts on larger screens.

plurals
  1. POIdentify subsets of root data in a list of trees
    primarykey
    data
    text
    <p>I have following structure:</p> <pre><code>Node { List&lt;String&gt; rootData; List&lt;Node&gt; Children; } </code></pre> <p>and a collection as </p> <pre><code>List&lt;Node&gt; lstOfTrees </code></pre> <p>the first Structure holds some words on rootData, (List of node is not really important here) and the collection <strong>lstOfTrees</strong> contains the the trees.</p> <p>Problem is: In <strong>lstOfTrees</strong>, there are multiple trees. Some of the trees have subset of rootData of other trees (possibly, not necessarily). I want to keep the tree having super-set of other rootData(s) in lstOfTrees (subset should be ignored). </p> <p>example: assuming, lstOfTrees contain the trees as</p> <pre><code>1: {rootData: A, B, C, D} 2: {rootData: E, F, G} 3: {rootData: G, H} 4: {rootData: J, A, C} 5: {rootData: D, Z} </code></pre> <p>the final answer I need, should be in a new list containing:</p> <pre><code>1: {rootData: A, B, C, D} 2: {rootData: E, F, G} </code></pre> <p>Can this be done using LINQ and TPL (or the more effecient way) ? I want it to be efficient and correct.</p> <p><strong>EDIT:</strong></p> <p>should the following code work correctly in all cases or am I missing something??</p> <pre><code>lstOfTrees.Add(new node()); lstOfTrees[0].rootData = new List&lt;string&gt; {"A", "B", "C", "D"}; lstOfTrees.Add(new node()); lstOfTrees[1].rootData = new List&lt;string&gt; {"E", "F", "G"}; lstOfTrees.Add(new node()); lstOfTrees[2].rootData = new List&lt;string&gt; {"G", "H"}; lstOfTrees.Add(new node()); lstOfTrees[3].rootData = new List&lt;string&gt; {"J", "A", "C"}; lstOfTrees.Add(new node()); lstOfTrees[4].rootData = new List&lt;string&gt; {"D", "Z"}; Dictionary&lt;int,node&gt; dictOfTrees_indexToNode = Enumerable.Range(0, lstOfTrees.Count).ToDictionary(x=&gt;x,x =&gt; lstOfTrees[x]); List&lt;int&gt; notToInclude = new List&lt;int&gt;(); for (int i = 0; i &lt; lstOfTrees.Count; i++) { for (int j = 0; j &lt; lstOfTrees.Count; j++) { if (j != i) { if (!lstOfTrees[j].Equals(lstOfTrees[i])) { if (lstOfTrees[j].rootData.Join(lstOfTrees[i].rootData, root =&gt; root, innerRoot =&gt; innerRoot, (root, innerRoot) =&gt; 1).Any()) { bool test = (lstOfTrees[j].rootData.Count &gt; lstOfTrees[i].rootData.Count); notToInclude.Add(test ? i : j); } } } } } List&lt;node&gt; finalList = new List&lt;node&gt;(); finalList.AddRange(lstOfTrees.Except(notToInclude.Select(s=&gt;dictOfTrees_indexToNode[s]))); </code></pre> <p><strong>Also, Can I improve from this?</strong></p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload