-3

I'm trying to find out to get count of available trees' number if the number of data are given.. ex) If there are 8 different data, How many tree can be made?

1 Answers1

0

The question is not totally clear. I'll assume you want the number of full binary (resp. ternary) trees with exactly n leaves (if you're rather looking for n nodes in total, you'll be able to find it from my result; if you want the number of trees that can store a given set of n different data, i.e. take into account all the ways to store your data in such a tree, then you should also be able to get it easily).

Let's consider the case of full binary trees. If you want n leaves in it, you can call k the number of leaves in the left subtree, you'll have n-k on the right. These subtrees have N(k) and N(n-k) possibilities respectively, Then you have N(n)=sum(N(k)*N(n-k), k=1..n-1). With N(1)=1.

This is the (a?) definition of Catalan Numbers: https://en.wikipedia.org/wiki/Catalan_number

N(n)=C(n-1)

You can do something similar for ternary trees

gdelab
  • 6,124
  • 2
  • 26
  • 59
  • [Catalan](https://en.wikipedia.org/wiki/Catalan_number) is for full binary trees, e.g. always 2 leaves, [Motzkin](https://en.wikipedia.org/wiki/Motzkin_number) is for binary trees, e.g. 1 or 2 leaves. – Guy Coder Jun 14 '17 at 15:56