1

I define the following class Node in order to get the relation parent/child

class Node
{
        public int Id { get; set; }
        public int? ParentId { get; set; }
        public string Operator { get; set; }
        public string Sign { get; set; }
        public Node Parent { get; set; }
        public IList<Node> Children { get; set; }

        public Node()
        {
            Children = new List<Node>();
        }

        public override string ToString()
        {
            //return "Node: " + Operator + " " + Id + " " + string.Join(",", Children.Select(x => x.Id));
            return "Node: " + Operator + " " + Id + " "
            + string.Join(",", Children.Select(x => string.Format("({0}, {1})", x.Sign, x.Id)));
        }
}

I have the following table as example

PNLId   PNLCode PNLParentId Operator    Sign
0      
49  C   51  +   NULL
50  Z   51  +   NULL
51  Y   107 /   NULL
52  B   107 /   NULL
53  B   108 +   NULL

I wrote the following script in the main :

var map = new Dictionary<int, Node>();
var rootNodes = new List<Node>();

foreach (DataRow row in dt.Rows)
{
    int id = Convert.ToInt32(row["PNLId"]);
    int? parentId = null;
    if (!row.IsNull("PNLParentId"))
    {
        parentId = Convert.ToInt32(row["PNLParentId"]);
    }
    string op = Convert.ToString(row["Operator"]);
    string sign = Convert.ToString(row["Sign"]);
    map[id] = new Node
    {
        Id = id,
        ParentId = parentId,
        Operator = op,
        Sign=sign

    };
}

foreach (var pair in map)
{
    if (pair.Value.ParentId.HasValue)
    {
        var parent = map[pair.Value.ParentId.Value];
        pair.Value.Parent = parent;
        parent.Children.Add(pair.Value);
        parent.Operator = pair.Value.Operator;
    }
    else
    {
        rootNodes.Add(pair.Value);

    }
}

In this case it will return ,for the PNLParentId 107

[107 Node: + 107 (, 51),(, 52)]  

which is wrong it should be

[107 Node: / 107 (, 51),(, 52)]

How can I modify the code above to Build node tree first. Then start at the root nodes copying the operator from the children. Progress to the children and copy their children's operator ?

1 Answer 1

1

I have fixed the code you have written to build the tree. Here is some test data to work with (I don't have the database tables you have):

var map = new Node[] {
    new Node() { Id=0, ParentId=0, Operator="", Sign=null}
    ,new Node() { Id=1, ParentId=null, Operator="+", Sign=null}
    ,new Node() { Id=2, ParentId=3, Operator="+", Sign=null}
    ,new Node() { Id=3, ParentId=4, Operator="/", Sign=null} 
    ,new Node() { Id=4, ParentId=4, Operator="/", Sign=null} 
    ,new Node() { Id=5, ParentId=1, Operator="+", Sign=null} 
};

And here is the fixed code:

// remove Value
var rootNodes = new List<Node>();
foreach (var pair in map)
{
    if (pair.ParentId.HasValue) 
    {
        var parent = map[pair.ParentId.Value]; 
        pair.Parent = parent; 
        parent.Children.Add(pair);
        parent.Operator = pair.Operator;
    }
    else
    {
        rootNodes.Add(pair);    
    }
}

Changes are:

  • Added rootNodes
  • Removed .Value

After you have run it, the rootNodes collection will contain 1 element (the one with Id=1) and the map array will contain correct object references to its parents.

Explanation:

The pair variable belongs to the foreach loop and contains elements of map. The variable map is an array of Node objects, which is a class in which you have declared the property Parent - which is also of type Node.

Variables of type Node are all object types, not value types and hence they don't own the .Value property. But the ParentId, which you have declared, is a nullable value type, because of the declaration int? - and all nullable value types (note the ? suffix) have the .Value property: So for ParentId, you can obtain the value using ParentId.Value (and .HasValue to check for null), but not for Parent, which is a Node object. Node object's properties need to be accessed directly by their property names.

3
  • Why to remove .Value ? I didn't get it . May 8, 2014 at 8:56
  • @user3548593: I have edited the answer of user3615347 and added an explanation. Hope that helps.
    – Matt
    May 8, 2014 at 10:37
  • @user3548593: You can test the code from this answer in LinqPad (a tool which is free) - just put it into the main() method, add rootNodes.Dump(); map.Dump(); at the end, then add the Node class from your question. If you run the example you see the recursive structure being built in the output window: One of the benefits of the .Dump() method LinqPad provides!
    – Matt
    May 8, 2014 at 11:17

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.