1

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 distance step, then:

1st child is N + 1 2nd child is N + 1 + step 3rd child is N + 1 + step + step ...and so on.

Can anyone provide a better explanation for how or why these formulas work?

Community
  • 1
  • 1
  • This is probably a better match for math.stackexchange.com or perhaps cs.stackexchange.com, both of which allow mathjax, unlike this site. – rici Aug 23 '16 at 00:46

1 Answers1

0

For the BFS:

If node N is at depth D and there is a nodes before N at depth D (and b nodes after):

N = K^0 + K^1 + ... + K^(D-1) + a

How many nodes will be labeled before its first child? There is b remaining nodes at depth D and a * K "child" nodes at depth D+1 that will come before. So if C is the label of the first child of N:

C = N + b + a * K + 1
C = K^0 + K^1 + ... + K^(D-1) + a + b + a * K + 1
C = K^0 + K^1 + ... + K^(D-1) + K^D + a * K

Indeed there is K^D nodes at depth D so a + b + 1 = K^D, therefor:

C = 1 + (K^0 + ... + K^(D-2) + K^(D-1) + a )* K
C = 1 + N*k

For the DFS:

To compute the size of the step you have to compute the size of the remaining sub-tree, and like a sub-tree of a perfect K-ary tree is itself a perfect K-ary tree, you can compute its number of nodes.

user1470500
  • 652
  • 5
  • 14
  • Ah, I thought about formulating it terms of #-of-before nodes and #-of-after nodes, but was stuck there. Expanding `N` in terms of `K` was the insight I needed to understand the derivation. I think due to the very abstract nature of the problem, there is probably not a fundamentally simpler explanation than this one for the BFS case, and likewise for my explanation of the DFS case. I will accept this answer. –  Aug 23 '16 at 03:50