2

I used a Tree control to view some hierarchical items base on a nested (parent child) table .

Every node has a NameValue format that accept either a name and value .

But only Leaves (last nodes) have integer values and values of parents are left blank (just the Names they have) .

I want to summarize values so that every parent hold the sum of it's sub nodes and leaves values .

I think recursion or maybe LINQ is needed to accomplish this task but i don't know how ?

maybe some pseudo code will be helpful for me .

Thanks in advance for the help!

Mostafa Armandi
  • 879
  • 1
  • 11
  • 24

2 Answers2

1

This is untested but i think it might work to set all the values of all nodes:

public void SetNodeValues(Node node)
{
    if (node.Name == String.Empty)
    {
        //If it has no name it is a leaf, which needs no value
        return;
    }
    else
    {
        //Make sure all child-nodes have values
        foreach (var childNode in node.ChildNodes)
        {
            SetNodeValues(childNode);
        }

        //Sum them up and set that as the current node's value
        node.Value = node.ChildNodes.Sum(x => x.Value);
    }
}
H.B.
  • 166,899
  • 29
  • 327
  • 400
0

This will do it for you :

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

    public IEnumerable<Node> GetSubTree()
    {
        return Children.SelectMany(c => c.GetSubTree()).Concat(new[] { this });
    }

    public List<Node> Children { get; set; }
    public string Value { get; set; }
}

class Tree
{
    public Tree()
    {
        Root = new Node();
    }

    public IEnumerable<Node> GetAllNodes()
    {
        return Root.Children.SelectMany(root => root.GetSubTree()); 
    }

    Node Root { get; set; }

    //This is the Property you want:
    public int GetValuesSum
    {
        get
        {
            return GetAllNodes().Where(node => !string.IsNullOrEmpty(node.Value)).Sum(node => Convert.ToInt32(node.Value));
        }
    }
}

Reference : How can I get a List from all nodes in a tree using LINQ?

Community
  • 1
  • 1
Farzin Zaker
  • 3,578
  • 3
  • 25
  • 35
  • That's not a tree, it's a forest -_- – H.B. Apr 17 '11 at 06:37
  • @H.B I changed it to a tree ;) – Farzin Zaker Apr 17 '11 at 06:41
  • Haha, nice, but i still wonder if this works for the OP, the targeted datastructure is a bit non-obvious from the description. Also he/she wants to set the values of the branch nodes, not just get the overall sum. – H.B. Apr 17 '11 at 06:47
  • @H.B. As he said only leaf nodes has value and other nodes leaved blank. so this code as you can see only gets sum of leaf nodes. tested and working – Farzin Zaker Apr 17 '11 at 07:03
  • thanks Farzin for your attention i was checking your solution and seems it works. but may you complete this by a recursion method by witch all parents get filled automatically so i could Bind it to a DataGridView for example ? – Mostafa Armandi Apr 17 '11 at 07:44
  • you do not need any recursion, linq is doing it for you – Farzin Zaker Apr 17 '11 at 08:16
  • You do not seem to understand what he/she wants; the point is *not to return* the overall sum but to *set all the values* of every node to the sum of its children. – H.B. Apr 17 '11 at 09:34