I need some help with code in JavaScript. My goal is to write a program which will allow generating a huge binary tree.
Before compilation I specify how many levels of the tree I want, assuming that every object has two children (except the last level).
Simple visualization for levels=3:

Every object has:
-Name - The first field from the shema. Also it should include information about object number,
-Type - The second field from the shema. Information about the depth of the object,
-Properties - Some static information about an object. In my situation it is the same and it is presented below,
-Children.
I wrote some code which accurately reproduces the tree form the schema:
var levels = 3; //2^0, 2^1, 2^2
var level_description = [];
for (var i = 0; i < levels; i++) {
obj = {};
obj.level = i;
obj.amount = Math.pow(2, i);
obj.indexes = [];
if (i === 0) {
obj.indexes[0] = 0;
level_description.push(obj);
} else {
for (var j = 0; j < obj.amount; j++) {
obj.indexes[j] = obj.amount + j - 1;
}
level_description.push(obj);
}
}
console.log(level_description);
var properties = [{
"name": "trend",
"value": "true"
}, {
"name": "unit",
"value": "g"
}];
var jsonString = JSON.stringify([{
"name": "Object_" + level_description[0].indexes[0].toString(), //Object_0
"type": "level_" + level_description[0].level.toString(), //level_0,
"properties": properties,
"children": [{
"name": "Object_" + level_description[1].indexes[0].toString(), //Object_1
"type": "level_" + level_description[1].level.toString(), //level_1,
"properties": properties,
"children": [{
"name": "Object_" + level_description[2].indexes[0].toString(), //Object_3
"type": "level_" + level_description[2].level.toString(), //level_2,
"properties": properties,
"children": []
}, {
"name": "Object_" + level_description[2].indexes[1].toString(), //Object_4
"type": "level_" + level_description[2].level.toString(), //level_2,
"properties": properties,
"children": []
}]
}, {
"name": "Object_" + level_description[1].indexes[1].toString(), //Object_2
"type": "level_" + level_description[1].level.toString(), //level_1,
"properties": properties,
"children": [{
"name": "Object_" + level_description[2].indexes[2].toString(), //Object_5
"type": "level_" + level_description[2].level.toString(), //level_2,
"properties": properties,
"children": []
}, {
"name": "Object_" + level_description[2].indexes[3].toString(), //Object_6
"type": "level_" + level_description[2].level.toString(), //level_2,
"properties": properties,
"children": []
}]
}]
}]);
pm.globals.set('jsonString', jsonString);
But now I am stuck. I have trouble with making it recursive and flexible.
I uploaded an output (json tree) on my google disc: https://drive.google.com/drive/folders/1__nR-AXK7uKRBT4hZtiSWetyaobk72CX?usp=sharing
Thanks for any kind of help.