In my answer to this question, I used two formulas that I arrived at by ad-hoc means, and I am at a loss for a simple explanation for why these formulas work. Here is the problem in full:
Consider a perfect or complete K-ary tree of height H where every node is labeled by their rank in a breadth-first traversal, and the its dual where every node is labeled in depth-first order. Here is an example with K=2, H=2:
_ 0 _ _ 0 _
/ \ / \
1 2 1 4
/ \ / \ / \ / \
3 4 5 6 2 3 5 6
For the BF-ordered tree, the i-th child of a node N at depth D is given by:
K*N + 1 + i
For the DF-ordered tree, the i-th child of a node N at depth D is given by:
N + 1 + i*step, where step = (K^(H - D) - 1) / (K - 1)
What is an intuitive explanation for these formulas?
The BF-ordered formula makes sense to me when looking at any hand-drawn example, but I can't put into words why it works. In the DF-ordered case, the best I can come up with is this:
For a node N at depth D in a DFS-numbered K-ary tree of height H, its first child is simply N+1 because it is the next node to be visited in a depth-first traversal. The second child of N will be visited directly after visiting the entire sub-tree rooted at the first child (N+1), which is itself a complete K-ary tree of height
H - (D + 1)
. The size of any complete, K-ary tree is given by the sum of a finite geometric series as explained here. The size of said sub-tree is the distance between the first and second children, and, in fact, it is the same distance between all siblings since each of their sub-trees are the same size. If we call this distancestep
, then:1st child is
N + 1
2nd child isN + 1 + step
3rd child isN + 1 + step + step
...and so on.
Can anyone provide a better explanation for how or why these formulas work?