At a high level, what I am trying to do is build up a "tree array" structure, exactly like in the tree array structure in this answer, with one additional constraint: every array can only be 1, 2, 4, 8, 16, or 32 items/arrays in length.
It acts like an array from the outside (has all the regular array methods), but it is constructed of a tree. With the added constraint that every node in the tree can only have 1, 2, 4, 8, 16, or 32 nodes (powers of 2). The nodes can be internal ("container") nodes, the base, or the leaves. In order to achieve these numbers 1, 2, 4, 8, 16 and 32, for items you add a null placeholder in the trailing spots after where you added an item, and for the containers (arrays), you simply add extra arrays to give you to that level.
I will demonstrate what the data structure looks like at different key points in its development now.
So here is how the first 32 items get laid out:
[]
[1]
[1,2]
[1,2,3,-]
[1,2,3,4]
[1,2,3,4,5,-,-,-]
[1,2,3,4,5,6,-,-]
[1,2,3,4,5,6,7,-]
[1,2,3,4,5,6,7,8]
[1,2,3,4,5,6,7,8,9,- ,- ,- ,- ,- ,- ,- ]
[1,2,3,4,5,6,7,8,9,10,- ,- ,- ,- ,- ,- ]
[1,2,3,4,5,6,7,8,9,10,11,- ,- ,- ,- ,- ]
[1,2,3,4,5,6,7,8,9,10,11,12,- ,- ,- ,- ]
[1,2,3,4,5,6,7,8,9,10,11,12,13,- ,- ,- ]
[1,2,3,4,5,6,7,8,9,10,11,12,13,14,- ,- ]
[1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,- ]
[1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16]
[1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,- ,- ,- ,- ,- ,- ,- ,- ,- ,- ,- ,- ,- ,- ,- ]
[1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,- ,- ,- ,- ,- ,- ,- ,- ,- ,- ,- ,- ,- ,- ]
[1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,- ,- ,- ,- ,- ,- ,- ,- ,- ,- ,- ,- ,- ]
[1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,- ,- ,- ,- ,- ,- ,- ,- ,- ,- ,- ,- ]
[1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,- ,- ,- ,- ,- ,- ,- ,- ,- ,- ,- ]
[1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,- ,- ,- ,- ,- ,- ,- ,- ,- ,- ]
...
[1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,...,32]
Once it gets to 32, it nests, exactly like this question/answer. So then it starts to build like this:
[[1,2,...,32],[1]]
[[1,2,...,32],[1,2]]
[[1,2,...,32],[1,2,3,-]]
[[1,2,...,32],[1,2,3,4]]
[[1,2,...,32],[1,2,3,4,5,-,-,-]]
...
[[1,2,...,32],[1,2,3,4,5,...,32]]
Then the 3rd one at this nesting level creates an extra blank array:
[[1,2,...,32],[1,2,..,32],[1],[]]
[[1,2,...,32],[1,2,..,32],[1,2],[]]
[[1,2,...,32],[1,2,..,32],[1,2,3,-],[]]
[[1,2,...,32],[1,2,..,32],[1,2,3,4],[]]
[[1,2,...,32],[1,2,..,32],[1,2,3,4,5,-,-,-],[]]
...
[[1,2,...,32],[1,2,..,32],[1,2,3,4,5,...,32],[]]
The reason for the extra []
array at the end is so that the top-level array has 4 children. I guess it could also be null
(-
), either one would work, whatever is easier, so it could be this too.
[[1,2,...,32],[1,2,..,32],[1,2,3,4,5,...,32],-]
So now we fill up the second layer of arrays, all with 32 items each:
[[1,2,...,32],[1,2,..,32],...,[1,2,...,32]]
What happens next is it nests again!
[[[1,2,...,32],[1,2,..,32],...,[1,2,...,32]],[[1]]]
[[[1,2,...,32],[1,2,..,32],...,[1,2,...,32]],[[1,2]]]
[[[1,2,...,32],[1,2,..,32],...,[1,2,...,32]],[[1,2,3,-]]]
[[[1,2,...,32],[1,2,..,32],...,[1,2,...,32]],[[1,2,3,4]]]
[[[1,2,...,32],[1,2,..,32],...,[1,2,...,32]],[[1,2,3,4,5,-,-,-]]]
[[[1,2,...,32],[1,2,..,32],...,[1,2,...,32]],[[1,2,3,4,5,...,32]]]
[[[1,2,...,32],[1,2,..,32],...,[1,2,...,32]],[[1,2,...,32]],[[1]],[[]]]
[[[1,2,...,32],[1,2,..,32],...,[1,2,...,32]],[[1,2,...,32]],[[1,2]],[[]]]
[[[1,2,...,32],[1,2,..,32],...,[1,2,...,32]],[[1,2,...,32]],[[1,2,3,-]],[[]]]
...
What this means is that every "object" in the array tree (every item, which can be anything, even arrays!) is at the same level in the tree (at the same depth), exactly like the linked simplified MVP question/answer.
I have tried to do it but I quickly get lost. I would love to see how this is done in an iterative fashion (i.e. instead of recursion), but recursion would be a nice touch too if you wanted to add it as well. (Some helpful methods can be found at the bottom of that gist too.)
NOTE: The numbers I used in the arrays are not what the actual values would be. The actual values will be any JavaScript objects (arrays, objects, numbers, strings, dates, etc.). I just used numbers to show the positions and how they relate to each other in a concise way. ??
It should work with a single method like in the linked question: tree.add(object)
, or tree.push(object)
, or push(tree, object)
, etc.
Also note, I am asking for this in JavaScript for simplicity's sake. I get that JavaScript arrays are dynamically sized, but pretend they are not. I am going to use this in a custom programming language which has fixed size arrays like C does, so I would like to know how it would work when you have to recreate the arrays as they are required to grow in size.
I modified the linked answer here to get pretty close to what I'm imagining, but still not there yet.
See Question&Answers more detail:
os