I was making a problem in which I have assumed that the tree is balanced (height of the tree is $$$O(logN)$$$). How to generate a random balanced tree $$$?$$$
| # | User | Rating |
|---|---|---|
| 1 | Benq | 3792 |
| 2 | VivaciousAubergine | 3647 |
| 3 | Kevin114514 | 3611 |
| 4 | jiangly | 3583 |
| 5 | strapple | 3515 |
| 6 | tourist | 3470 |
| 7 | Radewoosh | 3415 |
| 8 | Um_nik | 3376 |
| 9 | maroonrk | 3361 |
| 10 | XVIII | 3345 |
| # | User | Contrib. |
|---|---|---|
| 1 | Qingyu | 162 |
| 2 | adamant | 148 |
| 3 | Um_nik | 146 |
| 4 | Dominater069 | 143 |
| 5 | errorgorn | 141 |
| 6 | cry | 138 |
| 7 | Proof_by_QED | 136 |
| 8 | YuukiS | 135 |
| 9 | chromate00 | 134 |
| 10 | soullless | 133 |
I was making a problem in which I have assumed that the tree is balanced (height of the tree is $$$O(logN)$$$). How to generate a random balanced tree $$$?$$$
| Name |
|---|



Here is an idea (that may not work).
Create an AVL — or any data structure that is backed by a self balancing binary tree.
Generate n random numbers and insert them one by one into said tree.
This will result in a binary tree that should be random enough for most uses.
I guess you can simply generate an array of ancestors as follows:
$$$p_k = \text{rng()} \mod k$$$, where $$$k \in [1, n)$$$
Then build a zero indexed tree with edges $$$(i, p_i)$$$. It can be proved that height of the tree is $$$O(\log(n))$$$. You can additionally shuffle vertices to get random enumeration of them as well.