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.
Show activity on this post. The maximum number of nodes in a binary tree of depth k is 2k−1, k≥1.
How many extra nodes are there in Full K-ary tree than complete K-ary tree? Explanation: Every Full K-ary tree is also a complete K-ary tree. Therefore, both have same number of nodes. 12.
The K-ary tree is a rooted tree, where each node can hold at most k number of children. If the value of k is 2, then this is known as binary tree. The binary tree, or ternary trees are some specialized k-ary trees. So k-ary trees re generalized. Example of K-ary Tree −
The number of leaf nodes is always one more than the number of internal nodes i.e. L = T + 1. The minimum height of a full binary tree is log2(n+1) – 1. The minimum number of nodes in a full binary tree is 2*h-1. The maximum height of a full binary tree is (n+1)/2.
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 k
th 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)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With