总结树上的值[英] summarize values on a tree

本文是小编为大家收集整理的关于总结树上的值的处理/解决方法,可以参考本文帮助大家快速定位并解决问题,中文翻译不准确的可切换到English标签页查看源文。

问题描述

我使用树控件来查看嵌套(父母)表上的一些层次结构项目.

每个节点都有一个名称值格式,可以接受名称和值.

,但只有叶子(最后一个节点)具有整数值和父母的价值.

我想总结值,以便每个父持有其子节点的总和留下值.

我认为递归或可能需要Linq来完成这项任务,但我不知道如何?

也许某些伪代码对我有帮助.

事先感谢您的帮助!

推荐答案

这是未经测试的,但我认为设置所有节点的所有值可能有效:

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);
    }
}

其他推荐答案

这将为您完成:

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));
        }
    }
}

参考:如何使用linq从树上的所有节点获取列表?

本文地址:https://www.itbaoku.cn/post/1556851.html

问题描述

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!

推荐答案

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);
    }
}

其他推荐答案

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?

相关标签/搜索