Find dead nodes from Parent Child Node collection

dev1

I have a collection of custom node that has a parent child relation. Each node can be a composite type (that has other child in it) or a simple type (leaf level node)

I want to write a function that will be give me list of all dead nodes. For example here is the node collection

enter image description here

Based on the above case, p2, p3, p8, p9, p10, p6, c1 are dead nodes (since down their hierarchy they don't have any simple node in them)

I need a function as

private List<NodeEntity> GetDeadNodes(List<NodeEntity> originalList) 

Here is the function for has the 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;
}
polkduran

You can try something like that.

    private List<NodeEntity> GetDeadNodes(List<NodeEntity> originalList)
    {
        var rest = originalList.ToList();

        // Remove simple nodes and their ascendants.
        // The rest will be 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);

        // We have reached the root node.
        if (parent == null)
        {
            return;
        }
        rest.Remove(parent);
        RemoveAscendants(rest, parent);
    }

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

Fastest way to find the index of a child node in parent

XPath: Get parent node from child node

Find all child nodes for any parent node in Oracle 10g

How can I get all the child nodes from a parent node in Firebase?

get child nodes from parent (xml, java)

Find a child of a node in a node collection

Populate QTreeView as parent and child nodes from database

How to access the child nodes within a parent node

Finding parent nodes based on double value of child node in XPath

How to mark parent tree node as selected if all the child nodes are selected

Need to shift the child nodes in json to parent node

Find parent and child from array

Finding a parent node by its child nodes

xml nodes() in tsql - matching parent nodes with child node attributes

Find all checked nodes of parent node in treeview

Select just the child nodes of a parent node, and eliminate (temporarily) from the graph all other nodes in cytoscape.js

how to expand the parent nodes of a selected child node on kendo treeview

PowerShell .NET Retrieve only parent node if all child nodes are selected

Collecting XML child nodes from multiple parent nodes

How to read child nodes from each parent node using vba and write them to excel

How to import only parent node leaving all the child nodes from xml file using powershell?

Insert multiple child nodes with same structure into the given parent node

Get parent collection with hierarchy from a child collection

Extract parent node by filtering on child nodes

Extract all parent and child nodes from xml

How to find all child nodes of a node in puppeteer

How to get a collection of all direct child nodes of a node?

Extract all the child nodes for a parent node from turtle file using SPARQL

How to get child documents from parent collection