问题
let the number of nodes be 3.
If a,b,c.. are in order c>a>b then possible avl trees are:
n=1 gives 1,n=2 gives 2..(look image)
As we know for a BST it is 2n C n/ (n+1).
Have anyone tried to deduce a formula that can find the number of avl trees when the number of nodes are given.
example question:what is the number of possible avl trees with 11 nodes?
回答1:
I doubt that simple formula exists. But you can find number of possible AVL trees with dynamic programming, filling 2D table, where n is number of nodes, h is tree height, then sum all non-zero n-nodes entries:
F(n, h) = Sum[by all possible i]{F(i,h-1)*F(n-1-i,h-1)} +
Sum[by all possible j]{F(j,h-1)*F(n-1-j,h-2)} +
Sum[by all possible k]{F(k,h-2)*F(n-1-k,h-1)}
Explanation: we can make n-nodes h-height AVL tree, connecting root node with two valid trees of equal height (h-1), or with (h-1) and (h-2) trees, or with (h-2) and (h-1) trees.
来源:https://stackoverflow.com/questions/33195634/formula-for-finding-number-of-possible-avl-trees-with-n-nodes