Assume a binary tree of height N. All nodes have exactly 2 children and all leaves have height N. For example, the following tree has N=3:
3
/ \
/ \
/ \
2 2
/ \ / \
/ \ / \
1 1 1 1
/ \ / \ / \ / \
0 0 0 0 0 0 0 0
When we do a depth-first traversal and note the height of each node, we get the following array (for any N>3):
[0, 0, 1, 0, 0, 1, 2, 0, 0, 1, 0, 0, 1, 2, 3, 0, 0, 1, ...]
Note how the head of the array isn't dependent on N and can go infinitely.
I'm looking for a function F that, for any index in that array, would give the corresponding value. For example:
F(2) = 1
F(4) = 0
F(6) = 2
If possible, the function shouldn't be recursive. I do no want to have to iterate through millions of elements to calculate a height at an index far in.