treeToMatrix = (tree) => {
const convert = (node, prev, i) => {
if(isLeaf(node)) {
return [[...prev, node.name],1];
}
const curr = [...prev, `i-${i}`];
let maxIndex = i;
const children = [];
node.children.forEach((child) => {
const [c, k] = convert(child, curr, maxIndex);
children.push(c);
maxIndex += k;
})
let result = [];
children.forEach((child) => {
if(Array.isArray(child) && Array.isArray(child[0])) {
result = [...result, ...child];
} else {
result.push(child);
}
})
return [result, maxIndex];
};
return convert(tree, [], 0)[0];
}