Formula for finding number of possible AVL trees with n nodes - data-structures

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?

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.

Related

How many binary trees possible with N nodes using Catalan Number concept

So I was understanding Catalan Number Concept and try to implement it in code using topDown DP approach, so the recurrence relation is from what I have learned,
f(N) signifies total count of binary search trees possible by N number of nodes
f(N) = f(i-1).f(N-i)
where,
i = 1.....N (i signifies root node and traverse through all the nodes),
N is total number of nodes present in the binary tree,
f(i-1) signifies number of left subtrees possible when i is a root node,
f(N-i) signifies number of right subtrees possible when i is a root node.
there's a mathematical formulae by which we can find f(N) value,
2NcN/(N+1)
.
And, total count of binary trees possible by N number of nodes,
(factorial of N) * f(N)
My Doubt:
what's the difference between binary trees and binary search trees? I feel like both are defines same meaning but there's a difference that I'm not aware of so help me.
What's the difference between binary trees and binary search trees?
There is no difference in the shape of such trees, but binary search trees put a constraint on the values that its nodes have, which doesn't exist for (just) binary trees:
The value of a node must not be less than any of the values in its left subtree
The value of a node must not be greater than any of the values in its right subtree
Another way to express this constraint is:
The in-order traversal of the values in the tree must be non-decreasing.
Practically this means that when you have been given:
A shape of a binary tree with 𝑛 nodes
A set of 𝑛 unique values that are present in the tree
...then there is only one possibly way to associate those values with the tree's nodes for it to be a binary search tree. If it does not have to be a binary search tree, then there are 𝑛! ways to make that association.
Conclusions:
Given 𝑛, then 𝐢𝑛 (the 𝑛th Catalan number) represents:
the number of binary trees with 𝑛 nodes without values (i.e. number of shapes of binary trees)
the number of binary search trees with 𝑛 distinct values
The number of binary trees with 𝑛 distinct values is 𝑛!𝐢𝑛

Minimum number of vertices in a binary tree of height 5

What is the Formula to Find the minimum number of vertices required to make a binary tree (not a complete binary tree) of height 5 ?
A binary tree's height cannot be bigger than the number of nodes or vertices in the tree. So yes, the minimum number of vertices required for a binary tree of height 5 will be 5. Also, there must be n-1 edges between them. You can imagine a single series of connected nodes, and that is basically what you get.
Alternately, a full binary tree is a binary tree in which each internal vertex has exactly two children.This means a binary tree with n internal vertices has 2n + 1 vertices, 2n edges, and n + 1 leaves.
the minimum number of vertices in a binary tree of height n (with no further constraints) is always n.
proof:
if there were less than n nodes, the tree wouldn't be a valid binary tree (I guess I don't need to further explain this point)
if there were more than n nodes, it means at least one of the nodes has a brother (pigeonhole principle), which could be taken off the tree and it would result in a valid, smaller binary tree, so we know that this tree is not minimal, which opposes our assumption of a minimal tree.
-> a binary tree T is minimal -> T has n nodes

Data structures questions

Can we sort 7 numbers in 10 comparisons?
Depth of a binary tree with n node is? log(n)+1 or something else
If every node in a binary tree has either 0 or 2 children then the height of the tree is log(n): is it true or false?
Inserting an element into a binary search tree of size n takes time proportional to ------?
A binary tree not balanced can have all children at the right (for example), so the maximum height of a tree with n nodes is n
Same as 2.
If the tree is not balanced the insertion is proportional to the number of nodes that you need to traverse to find the correct position. Potentially n nodes.

No of trees can be constructed from given Inorder/Preorder/Postorder traversal

I know one cannot construct a tree without having both Inorder and Preorder/postorder traversals. Because for a given (only Inorder/Preorder/postorder) there could be a possibility of generating more number of trees. Are there any algorithms or mechanism one can compute the number of unique trees from a given (only Inorder/Preorder/postorder traversal).
Eg : a b c d e f g this is my Inorder traversal.
How many unique trees that can be constructed with the given Inorder traversal.
I tried them is google but none of the explanations are clear
Any help would be appreciated...
Well the algorithm is as follows:
Let, P(N) denote the number of trees possible with N nodes. Let the indexes of the nodes be 1,2,3,...
Now, lets pick the root of the tree. Any of the given N nodes can be the root. Say node i has been picked as root. Then, all the elements to the left of i in the inorder sequence must be in the left sub-tree. Similarly, to the right.
So, total possibilities are: P(i-1)*P(N-i)
In the above expression i varies from 1 to N.
Hence we have,
P(N) = P(0)*P(N-1) + P(1)*P(N-2) + P(2)*P(N-3)....
The base cases will be:
P(0) = 1
P(1) = 1
Thus this can be solved by using Dynamic Programming.
Note that a particular traversal is just a way of labeling the nodes in a tree, so that the number of possible binary trees is the same for any two traversals of the same length. The number of binary trees with n nodes is given by the n-1st Catalan number.
The formula
(2n)!/ (n)!(n+1)!
OR
2n * C(n) / (n+1)
gives the number of possible binary trees for any given INORDER/PREORDER/POSTORDER traversal.

Relationship between number of nodes and height

I am reading The Algorithm Design Manual. The author states that the height of a tree is:
h = log n,
where
h is height
n = number of leaf nodes
log is log to base d, where d is the maximum number of children allowed per node.
He then goes on to say that the height of a perfectly balanced binary search tree, would be:
h = log n
I wonder if n in this second statement denotes 'total number of leaf nodes' or 'total number of nodes'.
Which brings up a bigger question, is there a mathematical relationship between total number of nodes and the height of a perfectly balanced binary search tree?
sure, n = 2^h where h, n denote height of the tree and the number of its nodes, respectively.
proof sketch:
a perfectly balanced binary tree has
an actual branching factor of 2 at each inner node.
equal root path lengths for each leaf node.
about the leaf nodes in a perfectly balanced binary tree:
as the number of leafs is the number of nodes minus the number of nodes in a perfectly balanced binary tree with a height decremented by one, the number of leafs is half the number of all nodes (to be precise, half of n+1).
so h just varies by 1, which usually doesn't make any real difference in complexity considerations. that claim can be illustrated by remembering that it amounts to the same variations as defining the height of a single node tree as either 0 (standard) or 1 (unusual, but maybe handy in distinguishing it from an empty tree).
It doesn't really matter if you talk of all nodes or just leaf nodes: either is bound by above and below by the other multiplied by a constant factor. In a perfectly balanced binary tree the number of nodes on a full level is the number of all nodes in levels above plus one.
In a complete binary tree number of nodes (n) and height of tree (h) have a relationship like this in below.
n = 2^(h+1) -1
this is the all the nodes of the tree

Resources