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}]
}]
}]
}]
},
]
}
]
}
]
}
]
}
]
}
]};
javascript arrays algorithm ecmascript-6 graph-algorithm
New contributor
add a comment |
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}]
}]
}]
}]
},
]
}
]
}
]
}
]
}
]
}
]};
javascript arrays algorithm ecmascript-6 graph-algorithm
New contributor
2
please add the code, you tried and the wanted result as well. what do you mean with " minimum value of theval
"?
– Nina Scholz
yesterday
add a comment |
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}]
}]
}]
}]
},
]
}
]
}
]
}
]
}
]
}
]};
javascript arrays algorithm ecmascript-6 graph-algorithm
New contributor
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
javascript arrays algorithm ecmascript-6 graph-algorithm
New contributor
New contributor
New contributor
asked yesterday
Ivan
31
31
New contributor
New contributor
2
please add the code, you tried and the wanted result as well. what do you mean with " minimum value of theval
"?
– Nina Scholz
yesterday
add a comment |
2
please add the code, you tried and the wanted result as well. what do you mean with " minimum value of theval
"?
– 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
add a comment |
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)
add a comment |
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)
add a comment |
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)
add a comment |
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)
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)
answered yesterday
Nenad Vracar
68.4k115577
68.4k115577
add a comment |
add a comment |
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.
Ivan is a new contributor. Be nice, and check out our Code of Conduct.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
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
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
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
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