Formula for finding number of possible AVL trees with n nodes

℡╲_俬逩灬. 提交于 2019-12-11 11:56:54

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!