2

所以我有一个像下面这样的树结构,我在叶子节点中有一个计数属性。我想总结计数并将计数总和与其父级相比较。比这样 parent1 和 parent2 有他们的孩子的总和。然后从那里继续,所以grantparent 有parent1 和parent2 的总和。我也有遍历树的功能。但是得到计数我无法得到。

有任何想法吗?

javascript

 function transverse(element, result, isSegmentData) {
        if (element instanceof Array){
            element.forEach(function (item) {
                { transverse(item, result, isSegmentData); }
            });
        }
        else if (element instanceof Object) {
            if (element.hasOwnProperty("count")) {

               // sum the count and provide to parent
            }
            if (element.hasOwnProperty("childNodes")) {
                transverse(element.childNodes, result, isSegmentData);
            }
        }
    }

树状结构

    [
  {
    "nodeId": 66318,
    "nodeName": "grand parent",
    "childNodes": [
      {
        "nodeId": 66323,
        "nodeName": "parent1",
        "childNodes": [
          {
            "nodeId": 66324,
            "nodeName": "child1",
            "childNodes": [],
            "count": 25
          },
          {
            "nodeId": 66334,
            "nodeName": "child2",
            "childNodes": [],
            "count": 85
          },
          {
            "nodeId": 66439,
            "nodeName": "child3",
            "childNodes": [],
            "count": 65
          },
          {
            "nodeId": 66462,
            "nodeName": "child4",
            "childNodes": [],
            "count": 954
          }
        ]
      },
      {
        "nodeId": 66323,
        "nodeName": "parent2",
        "childNodes": [
          {
            "nodeId": 66324,
            "nodeName": "child1",
            "childNodes": [],
            "count": 225
          },
          {
            "nodeId": 66334,
            "nodeName": "child2",
            "childNodes": [],
            "count": 815
          }
        ]
      }
    ]
  }
]
4

3 回答 3

2

您可以使用命名函数并再次为数组调用它。然后分配计数,如果没有给出,则从孩子那里获得计数。

var data = [{ "nodeId": 66318, "nodeName": "grand parent", "childNodes": [{ "nodeId": 66323, "nodeName": "parent1", "childNodes": [{ "nodeId": 66324, "nodeName": "child1", "childNodes": [], "count": 25 }, { "nodeId": 66334, "nodeName": "child2", "childNodes": [], "count": 85 }, { "nodeId": 66439, "nodeName": "child3", "childNodes": [], "count": 65 }, { "nodeId": 66462, "nodeName": "child4", "childNodes": [], "count": 954 }] }, { "nodeId": 66323, "nodeName": "parent2", "childNodes": [{ "nodeId": 66324, "nodeName": "child1", "childNodes": [], "count": 225 }, { "nodeId": 66334, "nodeName": "child2", "childNodes": [], "count": 815 }] }] }];

data.reduce(function x(r, a) {
    a.count = a.count || Array.isArray(a.childNodes) && a.childNodes.reduce(x, 0) || 0;
    return r + a.count;
}, 0);

document.write('<pre>' + JSON.stringify(data, 0, 4) + '</pre>');
console.log(data);
.as-console-wrapper { max-height: 100% !important; top: 0; }

于 2016-09-07T17:19:57.157 回答
1

如果我正确理解了您的问题,您想遍历树并协调所有节点的计数,以使每个节点的计数等于其所有后代的计数(如果有的话)。如果是这种情况,试试这个函数(注意,我没有测试它,但它应该做你想要的):

function reconcileCount(node) {
    var summedCountOfChildren = node.childNodes.reduce(function(_summedCount, childNode) {
        var childCount = childNode.childNodes && childNode.childNodes.length > 0 ? reconcileCount(childNode) : childNode.count;
        return _summedCount + (childCount || 0);
    }, 0);

    node.count = summedCountOfChildren;

    return node.count;
}

基本上,它需要一个节点,并将协调传递的节点以及作为传递节点的后代的所有节点(即,您可以传入您的树,假设树有一个父节点,它将协调您的整个树)。

于 2016-09-07T17:18:59.107 回答
1

递归函数

您必须通过调用相同的函数对元素进行递归循环并对子元素求和,这里是:

function sumNodes(nodeList) {
    var sumChildren = function(node) {
        var sum = 0;
        for (var i = 0; i < node.childNodes.length && node.childNodes != null; i++) {
            sum += sumChildren(node.childNodes[i]);
        }
        node.sum = sum;
        return node.count == undefined ? sum : node.count;
    }
    for(var i=0; i<nodeList.length; i++){
        sumChildren(nodeList[i]);
    }

    return nodeList;
}

sumNodes使用您的树作为参数调用该函数。

var treeWithSum = sumNodes(tree);
console.log(treeWithSum); // to see the results in the log

完整示例:

var data = [{
    "nodeId": 66318,
    "nodeName": "grand parent",
    "childNodes": [{
        "nodeId": 66323,
        "nodeName": "parent1",
        "childNodes": [{
            "nodeId": 66324,
            "nodeName": "child1",
            "childNodes": [{
                "nodeId": 66324,
                "nodeName": "child1",
                "childNodes": [],
                "count": 25
            }, {
                "nodeId": 66324,
                "nodeName": "child1",
                "childNodes": [{
                    "nodeId": 66324,
                    "nodeName": "child1",
                    "childNodes": [],
                    "count": 25
                }, {
                    "nodeId": 66324,
                    "nodeName": "child1",
                    "childNodes": [{
                        "nodeId": 66324,
                        "nodeName": "child1",
                        "childNodes": [],
                        "count": 25
                    }, {
                        "nodeId": 66324,
                        "nodeName": "child1",
                        "childNodes": [],
                        "count": 25
                    }]
                }]
            }]
        }, {
            "nodeId": 66334,
            "nodeName": "child2",
            "childNodes": [],
            "count": 85
        }, {
            "nodeId": 66439,
            "nodeName": "child3",
            "childNodes": [],
            "count": 65
        }, {
            "nodeId": 66462,
            "nodeName": "child4",
            "childNodes": [],
            "count": 954
        }]
    }, {
        "nodeId": 66323,
        "nodeName": "parent2",
        "childNodes": [{
            "nodeId": 66324,
            "nodeName": "child1",
            "childNodes": [],
            "count": 225
        }, {
            "nodeId": 66334,
            "nodeName": "child2",
            "childNodes": [],
            "count": 815
        }]
    }]
}]


function sumNodes(nodeList) {
    var sumChildren = function(node) {
        var sum = 0;
        for (var i = 0; i < node.childNodes.length && node.childNodes != null; i++) {
            sum += sumChildren(node.childNodes[i]);
        }
        node.sum = sum;
        return node.count == undefined ? sum : node.count;
    }
    for(var i=0; i<nodeList.length; i++){
		sumChildren(nodeList[i]);
	}

	return nodeList;
}

$("#results").html(JSON.stringify(sumNodes(data), null, 4));
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
<pre id="results"></pre>

于 2016-09-07T17:20:27.473 回答