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 | 4009 |
2 | jiangly | 3823 |
3 | Benq | 3738 |
4 | Radewoosh | 3633 |
5 | jqdai0815 | 3620 |
6 | orzdevinwang | 3529 |
7 | ecnerwala | 3446 |
8 | Um_nik | 3396 |
9 | ksun48 | 3390 |
10 | gamegame | 3386 |
# | User | Contrib. |
---|---|---|
1 | cry | 167 |
2 | Um_nik | 163 |
3 | maomao90 | 162 |
4 | atcoder_official | 161 |
5 | adamant | 159 |
6 | -is-this-fft- | 158 |
7 | awoo | 157 |
8 | TheScrasse | 154 |
9 | Dominater069 | 153 |
9 | nor | 153 |
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).