I'm searching for a solution to create a dynamic Tree-like structure from a Flat Array.
Sample Input -> [1, 2, 3, 4, 5, 6, 7]
Currently, I can determine the number of columns and rows needed. However, I'm struggling to find a pattern for the position of each element in order to position them with proper spacing.
** During iteration I will receive column and index information
Ex1: Element number 4 will be placed in the 3rd column and 2nd row.
Ex2: If there were 3 elements in the input, the positions would be as follows:
1st element - 1st column and 2nd row
2nd element - 2nd column and 1st row
3rd element - 2nd column and 3rd row
The number of columns and rows are determined by the following method ->
getColumnLength() {
if (this.list.length <= 1) {
return 1;
}
if (this.list.length <= 3) {
return 2;
}
for (let i = 2; i <= this.list.length; i++) {
let columnLength = Math.pow(2, i);
if (columnLength >= this.list.length) {
return i;
}
}
},
getRowLength() {
return Math.pow(2, this.getColumnLength) + 1;
},
https://i.sstatic.net/eh7oh.png
Any suggestions would be greatly appreciated.