c# - Find dead nodes from Parent Child Node collection -




i have collection of custom node has parent child relation. each node can composite type (that has other child in it) or simple type (leaf level node)

i want write function give me list of dead nodes. example here node collection

enter image description here

based on above case, p2, p3, p8, p9, p10, p6, c1 dead nodes (since down hierarchy don't have simple node in them)

i need function as

private list<nodeentity> getdeadnodes(list<nodeentity> originallist)  

here function has original list

private list<nodeentity> getoriginallist() {     var list = new list<nodeentity>()     {         new nodeentity() {code = "p1", parentcode = "001", type = nodetype.composite},         new nodeentity() {code = "c1", parentcode = "001", type = nodetype.composite},         new nodeentity() {code = "p2", parentcode = "p1", type = nodetype.composite},         new nodeentity() {code = "p3", parentcode = "p2", type = nodetype.composite},         new nodeentity() {code = "p8", parentcode = "p3", type = nodetype.composite},         new nodeentity() {code = "p9", parentcode = "p3", type = nodetype.composite},         new nodeentity() {code = "p4", parentcode = "p1", type = nodetype.composite},         new nodeentity() {code = "l3", parentcode = "p1",  type = nodetype.simple},         new nodeentity() {code = "p6", parentcode = "p1",  type = nodetype.composite},         new nodeentity() {code = "p10", parentcode = "p4",  type = nodetype.composite},         new nodeentity() {code = "l2", parentcode = "p4",  type = nodetype.simple},         new nodeentity() {code = "p5", parentcode = "p4",  type = nodetype.composite},         new nodeentity() {code = "l1", parentcode = "p5",  type = nodetype.simple}     };     return list; } 

you can try that.

    private list<nodeentity> getdeadnodes(list<nodeentity> originallist)     {         var rest = originallist.tolist();          // remove simple nodes , ascendants.         // rest dead nodes.         var simplenodes = originallist.where(n => n.type == nodetype.simple);         foreach (var simplenode in simplenodes)         {             rest.remove(simplenode);             removeascendants(rest, simplenode);         }          return rest;     }      private void removeascendants(list<nodeentity> rest, nodeentity node)     {         var parent = rest.singleordefault(n => n.code == node.parentcode);          // have reached root node.         if (parent == null)         {             return;         }         rest.remove(parent);         removeascendants(rest, parent);     } 




wiki

Comments

Popular posts from this blog

elasticsearch - what is the equivalent data type for geo_point in hibernate search? -

firebase - How to wait value in Ionic 2 -

Jenkins: find build number for git commit -