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.
№ | Пользователь | Рейтинг |
---|---|---|
1 | tourist | 3993 |
2 | jiangly | 3743 |
3 | orzdevinwang | 3707 |
4 | Radewoosh | 3627 |
5 | jqdai0815 | 3620 |
6 | Benq | 3564 |
7 | Kevin114514 | 3443 |
8 | ksun48 | 3434 |
9 | Rewinding | 3397 |
10 | Um_nik | 3396 |
Страны | Города | Организации | Всё → |
№ | Пользователь | Вклад |
---|---|---|
1 | cry | 167 |
2 | Um_nik | 163 |
3 | maomao90 | 162 |
3 | atcoder_official | 162 |
5 | adamant | 159 |
6 | -is-this-fft- | 158 |
7 | awoo | 155 |
8 | TheScrasse | 154 |
9 | Dominater069 | 153 |
10 | nor | 152 |
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.
Название |
---|
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).