Interest Ask! I believe the answer is N factorial!
For a tree structure, there is only one way to populate the key values ββof a binary search tree.
Thus, all we need to do is to calculate a different amount of heap.
Given the heap, consider going around the tree in order.
This corresponds to a permutation of numbers from 1 to N.
Now, given any permutation {1,2 ..., N}, you can build the heap as follows:
Find the position of the largest element. Elements to the left of it of the left subtree and elements to the right of it form the right subtree. These subtrees are recursively formed by finding the largest element and splitting there.
This results in a heap, since we always select the max element and a traversal in the sequence of this heap is the permutation we started with. Thus, we have a clear path from the heap to the permutation and vice versa.
Thus, the required number is N !.
As an example:
5 / \ 3 4 In-order traversal -> 35142 / \ 1 2
Now start with 35142. The highest value is 5, so 3 is the left subtree, and 142 is correct.
5 / \ 3 {142}
In 142, 4 is the largest, and 1 is left, and 2 is right, so we get
5 / \ 3 4 / \ 1 2
The only way to populate binary search keys for this is:
(2,5) / \ (1,3) (4,4) / \ (3,1) (5,2)
For more formal proof:
If H N is the number of heaps per 1 ... N, then we have that
H N = Sum_ {L = 0 to N-1} H L * H N-1-L * (N- 1 select L)
(basically we select max and assign root. Choose the size of the left subtree and choose that many elements and recursion left and right).
Now,
H0 = 1 H1 = 1 H2 = 2 H3 = 6
If H n = n! for 0? n? to
Then H K + 1 = Sum_{L=0 to K} L! * (KL)! * (K!/L!*(KL)!) = (K+1)! Sum_{L=0 to K} L! * (KL)! * (K!/L!*(KL)!) = (K+1)!