The question is, how many ways are there to build a perfectly balanced binary tree with 15 elements?
One possibility would bei 8-4-12-2-6-10-14-1-3-5-7-9-11-13-15..
My idea was to write some code that generates every possible permutation (which would be like.. 15!) and then remove the ones that are incorrect.
Correct ones have the 8 as the first element, 4 always comes before 2 and 6, 2 always comes before 1 and 3, 6 always comes before 5 and 7 and so on.
But something like perms2 = list(itertools.permutations([1,2,3,4,5,6,7,8,9,10,11,12,13,14,15])) causes a memory error.
Is there a way to generate permutations with the rules as above?
Or is there even a more simple way to solve my problem?
Btw..
- if the amount of elements is 1, there is 1 way
- with 3 elements there are 2 ways (2-1-3 or 2-3-1)
- with 7 elements there are 80 ways (according to my code and doing it manually)
but that didn’t help me to get some kind of formula..
Edit: this is the tree i am talking about: http://666kb.com/i/bz9znnpdj7etw0fo9.gif
The correct number is 21964800. This is the appropriate integer sequence:
http://oeis.org/A076615
Basically you recursively multiply the possibilities:
On the lowest level you can choose between two possibilities, e.g: 2-1-3 and 2-3-1. On the level above that you can entangle the chosen order of both lower layers in (6 over 3)
ways and so on.