Here is 100 numbers, 10 per row.
[1, 2, 3, 4, 5, 6, 7, 8, 9, 0,
 1, 2, 3, 4, 5, 6, 7, 8, 9, 0,
 1, 2, 3, 4, 5, 6, 7, 8, 9, 0,
 1, 2, 3, 4, 5, 6, 7, 8, 9, 0,
 1, 2, 3, 4, 5, 6, 7, 8, 9, 0,
 1, 2, 3, 4, 5, 6, 7, 8, 9, 0,
 1, 2, 3, 4, 5, 6, 7, 8, 9, 0,
 1, 2, 3, 4, 5, 6, 7, 8, 9, 0,
 1, 2, 3, 4, 5, 6, 7, 8, 9, 0,
 1, 2, 3, 4, 5, 6, 7, 8, 9, 0]
I would like to arrange these numbers into a tree sort of thing, where each node has max 5 elements. Something like this:
[                                                                                       ]
 [                   ],[                   ],[                   ],[                   ]
  [ ],[ ],[ ],[ ],[ ]   [ ],[ ],[ ],[ ],[ ]   [ ],[ ],[ ],[ ],[ ]   [ ],[ ],[ ],[ ],[ ]
   1   6   1   6   1     6   1   6   1   6     1   6   1   6   1     6   1   6   1   6
   2   7   2   7   2     7   2   7   2   7     2   7   2   7   2     7   2   7   2   7
   3   8   3   8   3     8   3   8   3   8     3   8   3   8   3     8   3   8   3   8
   4   9   4   9   4     9   4   9   4   9     4   9   4   9   4     9   4   9   4   9
   5   0   5   0   5     0   5   0   5   0     5   0   5   0   5     0   5   0   5   0
So we have 4 "layers" in the tree:
- In layer 1 (top layer), we have 4 children (4 arrays of arrays of numbers).
- In layer 2, we have 5 children (5 arrays of numbers).
- In layer 3, we have 5 children (5 numbers).
- Layer 4 are the numbers.
How does one write a JavaScript algorithm to generate a tree like this? The rules are, max 5 per block. Or more generally, max n per block.
This is somewhat similar to an array chunking algorithm, but at the same time way more complicated it seems.
I have been stumped on this for a few days, but it will help in solving this problem: How to divide an array into a tree of buckets sized by powers of two?
Basically, as the array gets longer, the nesting will get bigger and bigger.
Another simpler example is this, 13-item-array:
[1, 2, 3, 4, 5, 6, 7, 8, 9, 0,
 1, 2, 3]
Which is converted into this tree:
[           ]
 [ ],[ ],[ ]
  1   6   1
  2   7   2
  3   8   3
  4   9   
  5   0   
 
    