Counting the number of binary heaps created with N elements with duplicite numbers

224 Views Asked by At

A heap is a tree with numbers where a parent of a number must be equal or lower than the number itself. (This.)

I know how to count the number of possible heaps with distinct elements such as {1, 2, 3, 4, 5, 6, 7, 8, 9}. But if I wanted to count the number of heaps looking like this: {1, 2, 4, 4, 5, 6, 7, 8, 9}, where 4 is two times...?

How do I rewrite f(N) = (N−1 C L) * f(L) * f(R) (used to count possibilities from first sample) to be able to work with duplicates?

1

There are 1 best solutions below

3
On

I don't know the general answer and I don't know what your formula means. But in your second case, the only difference is that you can always swap the positions of the two $4$ and you'll get the same heap, whereas before you only could swap $3$ and $4$ when they where not linked, and the heap was then different. So if $N_1$ and $N_2$ denote the number of heaps in the two cases and if $R$ is the number of heaps in the first case where $3$ and $4$ are linked, then you get:

$$ N_2 = R + \frac{N_1-R}{2}.$$