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 | jiangly | 3976 |
2 | tourist | 3815 |
3 | jqdai0815 | 3682 |
4 | ksun48 | 3614 |
5 | orzdevinwang | 3526 |
6 | ecnerwala | 3514 |
7 | Benq | 3482 |
8 | hos.lyric | 3382 |
9 | gamegame | 3374 |
10 | heuristica | 3357 |
# | User | Contrib. |
---|---|---|
1 | cry | 169 |
2 | -is-this-fft- | 162 |
3 | Um_nik | 161 |
4 | atcoder_official | 160 |
5 | djm03178 | 157 |
6 | Dominater069 | 156 |
7 | adamant | 154 |
8 | luogu_official | 152 |
9 | awoo | 151 |
10 | TheScrasse | 148 |
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).