38

I am doing a unique form of Huffman encoding, and am constructing a k-ary (in this particular case, 3-ary) tree that is full (every node will have 0 or k children), and I know how many leaves it will have before I construct it. How do I calculate the total number of nodes in the tree in terms of the number of leaves?

I know that in the case of a full binary tree (2-ary), the formula for this is 2L - 1, where L is the number of leaves. I would like to extend this principle to the case of a k-ary tree.

Sandipan Dey
  • 21,482
  • 2
  • 51
  • 63
Andrew
  • 2,425
  • 2
  • 24
  • 35

4 Answers4

49

Think about how to prove the result for a full binary tree, and you'll see how to do it in general. For the full binary tree, say of height h, the number of nodes N is

N = 2^{h+1} - 1

Why? Because the first level has 2^0 nodes, the second level has 2^1 nodes, and, in general, the kth level has 2^{k-1} nodes. Adding these up for a total of h+1 levels (so height h) gives

N = 1 + 2 + 2^2 + 2^3 + ... + 2^h = (2^{h+1} - 1) / (2 - 1) = 2^{h+1} - 1

The total number of leaves L is just the number of nodes at the last level, so L = 2^h. Therefore, by substitution, we get

N = 2*L - 1

For a k-ary tree, nothing changes but the 2. So

N = 1 + k + k^2 + k^3 + ... + k^h = (k^{h+1} - 1) / (k - 1)

L = k^h

and so a bit of algebra can take you the final step to get

N = (k*L - 1) / (k-1)
PengOne
  • 48,188
  • 17
  • 130
  • 149
0

The problem is more general and the result holds for any complete k-ary tree (not necessarily full).

We could prove it with graph theory too. Notice from the following figure that there are only 3 types of nodes in a complete k-ary tree with n = I + L nodes, i.e., with I internal and L leaf nodes:

  1. 1 root node, with degree exactly k
  2. (I-1) internal nodes, each with degree exactly k+1 (since root is also an internal node)
  3. L leaf nodes, each with degree 1

enter image description here

Now, if we use the following two facts from graph theory:

  1. A tree with n nodes has n-1 edges
  2. Sum of the degrees of nodes in any graph is twice the number of edges in the graph

Combining all the above, we get,

Sum of degrees of all the nodes in the tree

= 1.k + (I-1)(k+1) + L.1 = 2(I+L-1)

=> L = (k-1)I + 1

Sandipan Dey
  • 21,482
  • 2
  • 51
  • 63
0

The formula for 2L-1 that you mentioned comes from looking on a full, complete and balanced binary tree: on the last level you have 2^h leafs, and on the other levels: 1+2+4+....+2^(h-1) = 2^h -1 leafs. When you "mess" levels in the tree and create an unbalanced one, then the number of internal nodes that you have doesn't change.

In 3-ary tree its the same logic: on the last level you have 3^h leafs, and on the other levels: 1+3+9+....+3^(h-1)= (3^h -1 )/2, that means that on a 3-ary tree you have 1.5*L - 0.5 leafs (and this make sence- because the degree is larger you need less internal nodes). I thing that also here, when you mess up levels in the tree you will still need the same number of internal nodes.

Hope that it helps you

Bartolinio
  • 780
  • 2
  • 8
  • 17
-2

For any k-ary tree the total number of nodes n = [(k^(h+1))-1]/(h-1) where h is the height of the k-ary tree.

Ex:- For complete binary tree(k=2) total no. of nodes = [(2^(h+1))-1]/(h-1).

So for height 3 the total no. of nodes will be 15.

For complete ternary tree tree(k=3) total no. of nodes = [(3^(h+1))-1]/(h-1).

So for height 3 the total no. of nodes will be 40.

alkber
  • 1,426
  • 2
  • 20
  • 26
Kishan
  • 5
  • 2