Need to implement an algorithm to obtain the average value of the fields of the graph structure( graph tree)











up vote
-5
down vote

favorite












I have such a task, please help with the solution!
There are graph nodes which have objects.
Please help me find the average value of the 'val' fields and the node with the minimum value of the 'val' field



Example graph structure(graph tree) Also i need implement this on JavaScript. Thank You very Match



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}]
}]
}]
}]
},
]
}
]
}
]
}
]
}
]
}
]};









share|improve this question







New contributor




Ivan is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.
















  • 2




    please add the code, you tried and the wanted result as well. what do you mean with " minimum value of the val"?
    – Nina Scholz
    yesterday

















up vote
-5
down vote

favorite












I have such a task, please help with the solution!
There are graph nodes which have objects.
Please help me find the average value of the 'val' fields and the node with the minimum value of the 'val' field



Example graph structure(graph tree) Also i need implement this on JavaScript. Thank You very Match



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}]
}]
}]
}]
},
]
}
]
}
]
}
]
}
]
}
]};









share|improve this question







New contributor




Ivan is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.
















  • 2




    please add the code, you tried and the wanted result as well. what do you mean with " minimum value of the val"?
    – Nina Scholz
    yesterday















up vote
-5
down vote

favorite









up vote
-5
down vote

favorite











I have such a task, please help with the solution!
There are graph nodes which have objects.
Please help me find the average value of the 'val' fields and the node with the minimum value of the 'val' field



Example graph structure(graph tree) Also i need implement this on JavaScript. Thank You very Match



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}]
}]
}]
}]
},
]
}
]
}
]
}
]
}
]
}
]};









share|improve this question







New contributor




Ivan is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.











I have such a task, please help with the solution!
There are graph nodes which have objects.
Please help me find the average value of the 'val' fields and the node with the minimum value of the 'val' field



Example graph structure(graph tree) Also i need implement this on JavaScript. Thank You very Match



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}]
}]
}]
}]
},
]
}
]
}
]
}
]
}
]
}
]};






javascript arrays algorithm ecmascript-6 graph-algorithm






share|improve this question







New contributor




Ivan is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.











share|improve this question







New contributor




Ivan is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.









share|improve this question




share|improve this question






New contributor




Ivan is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.









asked yesterday









Ivan

31




31




New contributor




Ivan is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.





New contributor





Ivan is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.






Ivan is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.








  • 2




    please add the code, you tried and the wanted result as well. what do you mean with " minimum value of the val"?
    – Nina Scholz
    yesterday
















  • 2




    please add the code, you tried and the wanted result as well. what do you mean with " minimum value of the val"?
    – Nina Scholz
    yesterday










2




2




please add the code, you tried and the wanted result as well. what do you mean with " minimum value of the val"?
– Nina Scholz
yesterday






please add the code, you tried and the wanted result as well. what do you mean with " minimum value of the val"?
– Nina Scholz
yesterday














1 Answer
1






active

oldest

votes

















up vote
0
down vote



accepted










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)








share|improve this answer





















    Your Answer






    StackExchange.ifUsing("editor", function () {
    StackExchange.using("externalEditor", function () {
    StackExchange.using("snippets", function () {
    StackExchange.snippets.init();
    });
    });
    }, "code-snippets");

    StackExchange.ready(function() {
    var channelOptions = {
    tags: "".split(" "),
    id: "1"
    };
    initTagRenderer("".split(" "), "".split(" "), channelOptions);

    StackExchange.using("externalEditor", function() {
    // Have to fire editor after snippets, if snippets enabled
    if (StackExchange.settings.snippets.snippetsEnabled) {
    StackExchange.using("snippets", function() {
    createEditor();
    });
    }
    else {
    createEditor();
    }
    });

    function createEditor() {
    StackExchange.prepareEditor({
    heartbeatType: 'answer',
    convertImagesToLinks: true,
    noModals: true,
    showLowRepImageUploadWarning: true,
    reputationToPostImages: 10,
    bindNavPrevention: true,
    postfix: "",
    imageUploader: {
    brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
    contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
    allowUrls: true
    },
    onDemand: true,
    discardSelector: ".discard-answer"
    ,immediatelyShowMarkdownHelp:true
    });


    }
    });






    Ivan is a new contributor. Be nice, and check out our Code of Conduct.










     

    draft saved


    draft discarded


















    StackExchange.ready(
    function () {
    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53372346%2fneed-to-implement-an-algorithm-to-obtain-the-average-value-of-the-fields-of-the%23new-answer', 'question_page');
    }
    );

    Post as a guest















    Required, but never shown

























    1 Answer
    1






    active

    oldest

    votes








    1 Answer
    1






    active

    oldest

    votes









    active

    oldest

    votes






    active

    oldest

    votes








    up vote
    0
    down vote



    accepted










    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)








    share|improve this answer

























      up vote
      0
      down vote



      accepted










      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)








      share|improve this answer























        up vote
        0
        down vote



        accepted







        up vote
        0
        down vote



        accepted






        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)








        share|improve this answer












        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)








        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)





        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)






        share|improve this answer












        share|improve this answer



        share|improve this answer










        answered yesterday









        Nenad Vracar

        68.4k115577




        68.4k115577






















            Ivan is a new contributor. Be nice, and check out our Code of Conduct.










             

            draft saved


            draft discarded


















            Ivan is a new contributor. Be nice, and check out our Code of Conduct.













            Ivan is a new contributor. Be nice, and check out our Code of Conduct.












            Ivan is a new contributor. Be nice, and check out our Code of Conduct.















             


            draft saved


            draft discarded














            StackExchange.ready(
            function () {
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53372346%2fneed-to-implement-an-algorithm-to-obtain-the-average-value-of-the-fields-of-the%23new-answer', 'question_page');
            }
            );

            Post as a guest















            Required, but never shown





















































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown

































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown







            Popular posts from this blog

            'app-layout' is not a known element: how to share Component with different Modules

            android studio warns about leanback feature tag usage required on manifest while using Unity exported app?

            WPF add header to Image with URL pettitions [duplicate]