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 | 3831 |
3 | Radewoosh | 3646 |
4 | jqdai0815 | 3620 |
4 | Benq | 3620 |
6 | orzdevinwang | 3529 |
7 | ecnerwala | 3446 |
8 | Um_nik | 3396 |
9 | gamegame | 3386 |
10 | ksun48 | 3373 |
# | User | Contrib. |
---|---|---|
1 | cry | 164 |
1 | maomao90 | 164 |
3 | Um_nik | 163 |
4 | atcoder_official | 160 |
5 | -is-this-fft- | 158 |
6 | awoo | 157 |
7 | adamant | 156 |
8 | TheScrasse | 154 |
8 | nor | 154 |
10 | Dominater069 | 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).