You could create recursive function with for...in
loop and first calculate total sum and total number of nodes and then use those 2 values to calculate average.
var graph_structure = {"val":74,"child":[{"val":17,"child":[{"val":34,"child":[{"val":34,"child":[{"val":65,"child":[{"val":28,"child":[{"val":85},{"val":30,"child":[{"val":68},{"val":10,"child":[{"val":100,"child":[{"val":21,"child":[{"val":21},{"val":64}]},{"val":86}]}]}]}]}]},{"val":22,"child":[{"val":17,"child":[{"val":65}]}]}]},{"val":53,"child":[{"val":3,"child":[{"val":98,"child":[{"val":90,"child":[{"val":76,"child":[{"val":87,"child":[{"val":52,"child":[{"val":56}]}]},{"val":47},{"val":40,"child":[{"val":80,"child":[{"val":34}]},{"val":23,"child":[{"val":47},{"val":92}]},{"val":98,"child":[{"val":89},{"val":16},{"val":10}]}]}]}]},{"val":35,"child":[{"val":89,"child":[{"val":76,"child":[{"val":50,"child":[{"val":51},{"val":90}]},{"val":69,"child":[{"val":93},{"val":98},{"val":62}]}]}]}]}]}]}]}]}]},{"val":98,"child":[{"val":85},{"val":85,"child":[{"val":58,"child":[{"val":81,"child":[{"val":36,"child":[{"val":45,"child":[{"val":96,"child":[{"val":15,"child":[{"val":11,"child":[{"val":96}]}]},{"val":48,"child":[{"val":4,"child":[{"val":74},{"val":1}]},{"val":7}]}]},{"val":84,"child":[{"val":9},{"val":81,"child":[{"val":10,"child":[{"val":67}]}]}]}]},{"val":85,"child":[{"val":53},{"val":7,"child":[{"val":47,"child":[{"val":74,"child":[{"val":30},{"val":7},{"val":12}]},{"val":22}]},{"val":56,"child":[{"val":51,"child":[{"val":45}]},{"val":54,"child":[{"val":20},{"val":62}]}]}]}]}]}]}]},{"val":62,"child":[{"val":36,"child":[{"val":39,"child":[{"val":20}]},{"val":10,"child":[{"val":91,"child":[{"val":81,"child":[{"val":59,"child":[{"val":19,"child":[{"val":59},{"val":16}]},{"val":35,"child":[{"val":30}]},{"val":6,"child":[{"val":27}]}]},{"val":89,"child":[{"val":60,"child":[{"val":59}]}]}]}]}]}]}]}]}]},{"val":8,"child":[{"val":56,"child":[{"val":55,"child":[{"val":41,"child":[{"val":17,"child":[{"val":15,"child":[{"val":40,"child":[{"val":55,"child":[{"val":50},{"val":99,"child":[{"val":86},{"val":90}]}]}]},{"val":85,"child":[{"val":36,"child":[{"val":39,"child":[{"val":45}]}]}]},{"val":78,"child":[{"val":24,"child":[{"val":93,"child":[{"val":8}]},{"val":26,"child":[{"val":5}]}]},{"val":36}]}]},{"val":13}]}]},{"val":10,"child":[{"val":0,"child":[{"val":77,"child":[{"val":46,"child":[{"val":72,"child":[{"val":17,"child":[{"val":10},{"val":67}]},{"val":48},{"val":60}]},{"val":98,"child":[{"val":12,"child":[{"val":61},{"val":27}]}]}]}]}]}]}]}]}]}]}
function calc(data) {
return (function repeat(data, res) {
for(let i in data) {
if(data.val) {
res.nodes++;
res.total += data.val
if(!res.min) res.min = data
else if(res.min.val > data.val) res.min = data
}
if(typeof data[i] == 'object') repeat(data[i], res)
}
return res
})(data, {min: null, nodes: 0, total: 0})
}
let res = calc(graph_structure);
res.avg = res.total / res.nodes;
console.log(res)
0
solved Need to implement an algorithm to obtain the average value of the fields of the graph structure( graph tree)