Try   HackMD

Complete
k
-ary tree with exactly
n
leaves

A complete

k-ary tree has all levels, except possibly the last, completely filled.

                    - - - - [.] - - - -
                  /                     \
                 /                       \
                /                         \
               /                           \
             (*)                           (*)
           /     \                       /     \
          /       \                     /       \
         /         \                   /         \
        /           \                 /           \
      ()             ()             ()             ()
     /  \           /  \           /  \           /  \
    /    \         /    \         /    \         /    \
   *      *       *      *       *     11       12    13
  / \    / \     / \    / \     / \     
 1   2  3   4   5   6  7   8   9  10

Specifically, levels are numbered from

0 to
h
, with the root being at level 0 and the leaves at level
h
. A level
i[0,h]
has exactly
ki
nodes on it.

A leaf is defined as a node in the tree that has 0 children.

In a complete tree, leafs can be either on level

h or
h+1
or both.

How to fit exactly
n
leaves?

Sometimes, we want our a complete

k-ary tree to have exactly
n
leaves. This is easy when
n=kh
for some
h
: just create tree with
h+1
levels, and put all leaves on level
h
. Done.

But when

kh<n<kh+1, we are in trouble: we will need to put some of the leaves on the 2nd-to-last level
h
and the others on the last level
h+1
.

But how do we know how to split the leaves across these levels?

Attempt 1: Works for some
n
or for any
n=n±ε,ε[1,k1]

Assume the 2nd-to-last level

h will store
R
leaves, which means that the last level
h+1
could store exactly
L=(khR)k
leaves. (This won't work for any
n
though, but we'll fix it.)

Overall, we would need these two levels together to store all

n leafs, so:
n=L+R=(khR)k+R

Now, we can solve for
R
, to figure out how many leaves we'll put on level
h
:
n=(khR)k+Rn=kh+1Rk+Rn=kh+1R(k1)R=kh+1nk1

The problem we'll run into here is that the numerator might not be divisible by the numerator.

One thing we can do is change

n into
n=n±ε,ε[1,k1]
, which will be divisible for some
ε
guaranteed.

That's hacky though.

Attempt 2: Works for almost every
n

We'll instead allow level

h+1 to store
L=(khR1)k+ε
leaves, by having the level
h
parent of the last leaf from level
h+1
have
1εk
children.

The new formula for

n becomes:
n=(khR1)k+R+εn=kh+1Rk+Rk+εn=kh+1R(k1)k+εR=kh+1n(kε)k1,for 1εk

So, all we need to do is find the

(kε)[0,k1] for which the numerator is divisible.

Unfortunately, this will never allow for the case where

R=0 and
L=n
. For example, consider
n=3
in a binary tree: all three leaves go on the last level, with the last leaf's parent having only two children:

       *
     /   \
    /     \
   *       *
  / \     /
 1   2   3

In other words, our formulas above implicitly assume that

R>0.

Attempt 3: Works for every
n

We can deal with the problematic case above by identifying it via:

kh+1n<k

If so, we let

L=n and
R=0
. Otherwise, we use the formula from "Attempt #2."