How to form a k-nary tree with x leaf nodes.
Is there any way to find whether a k-nary tree(every node has either k child or it is a leaf node) which have x leaf nodes exists or not. If exits then how to find it.
# | User | Rating |
---|---|---|
1 | tourist | 3985 |
2 | jiangly | 3814 |
3 | jqdai0815 | 3682 |
4 | Benq | 3529 |
5 | orzdevinwang | 3526 |
6 | ksun48 | 3517 |
7 | Radewoosh | 3410 |
8 | hos.lyric | 3399 |
9 | ecnerwala | 3392 |
9 | Um_nik | 3392 |
# | User | Contrib. |
---|---|---|
1 | cry | 169 |
2 | maomao90 | 162 |
2 | Um_nik | 162 |
4 | atcoder_official | 161 |
5 | djm03178 | 158 |
6 | -is-this-fft- | 157 |
7 | adamant | 155 |
8 | awoo | 154 |
8 | Dominater069 | 154 |
10 | luogu_official | 151 |
How to form a k-nary tree with x leaf nodes.
Is there any way to find whether a k-nary tree(every node has either k child or it is a leaf node) which have x leaf nodes exists or not. If exits then how to find it.
Name |
---|
Unless I’m missing something, you can make an arbitrary k-ary tree with any number of nodes.
Work through a few small examples and look for a pattern that can be generalized.
I asked the question after trying. Nevertheless, I will try again.
Let’s walk through some examples together.
For any k, we can start with a k-ary tree of 1 node. Each time we want to go from a k-ary tree of a certain size to the one of the next smallest possible size, we will choose a leaf node and make it an internal node with k children (which are leaves).
(0 internal nodes, 1 leaf node) -> (1 int, 2 leaves) -> (2 int, 3 leaves) -> (3 int, 4 leaves) -> ...
(0 internal nodes, 1 leaf node) -> (1 int, 3 leaves) -> (2 int, 5 leaves) -> (3 int, 7 leaves) -> ...
(0 internal nodes, 1 leaf node) -> (1 int, 11 leaves) -> (2 int, 21 leaves) -> (3 int, 31 leaves) -> ...
...
When you have a k-ary tree with x internal and y leaf nodes, then the next smallest one will have x+1 internal nodes and y-1+k leaf nodes. The one after that will have x+2 internal nodes and y-1+k-1+k = y+2*(k-1) leaf nodes. And so on.
Initially x=0 and y=1. Then you know you can form a k-ary tree with y leaf nodes if y is of the form 1+x*(k-1).