Given the height of an AVL tree ‘h’, the task is to find the minimum number of nodes the tree can have.
Input : H = 0 Output : N = 1 Only '1' node is possible if the height of the tree is '0' which is the root node. Input : H = 3 Output : N = 7
Recursive Approach : In an AVL tree, we have to maintain the height balance property, i.e. difference in the height of the left and the right subtrees can not be other than -1, 0 or 1 for each node.
We will try to create a recurrence relation to find minimum number of nodes for a given height, n(h).
- For height = 0, we can only have a single node in an AVL tree, i.e. n(0) = 1
- For height = 1, we can have a minimum of two nodes in an AVL tree, i.e. n(1) = 2
- Now for any height ‘h’, root will have two subtrees (left and right). Out of which one has to be of height h-1 and other of h-2. [root node excluded]
- So, n(h) = 1 + n(h-1) + n(h-2) is the required recurrence relation for h>=2 [1 is added for the root node]
Below is the implementation of the above approach:
Tail Recursive Approach :
- The recursive function for finding n(h) (minimum number of nodes possible in an AVL Tree with height ‘h’) is n(h) = 1 + n(h-1) + n(h-2) ; h>=2 ; n(0)=1 ; n(1)=2;
- To create a Tail Recursive Function, we will maintain 1 + n(h-1) + n(h-2) as function arguments such that rather than calculating it, we directly return its value to main function.
Below is the implementation of the above approach :
n(5) = 20
Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready.
- Count greater nodes in AVL tree
- Red Black Tree vs AVL Tree
- AVL Tree | Set 1 (Insertion)
- AVL Tree | Set 2 (Deletion)
- Optimal sequence for AVL tree insertion (without any rotations)
- Check if a given Binary Tree is height balanced like a Red-Black Tree
- AVL with duplicate keys
- Maximum height of the binary search tree created from the given array
- Generate two BSTs from the given array such that maximum height among them is minimum
- How to determine if a binary tree is height-balanced?
- Maximum sub-tree sum in a Binary Tree such that the sub-tree is also a BST
- Leaf nodes from Preorder of a Binary Search Tree
- Print all even nodes of Binary Search Tree
- Print all odd nodes of Binary Search Tree
- Pre-Order Successor of all nodes in Binary Search Tree
- Find maximum count of duplicate nodes in a Binary Search Tree
- Print nodes of a Binary Search Tree in Top Level Order and Reversed Bottom Level Order alternately
- Create Balanced Binary Tree using its Leaf Nodes without using extra space
- Remove all leaf nodes from the binary search tree
- Binary Tree to Binary Search Tree Conversion
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to firstname.lastname@example.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.
Improved By : krikti, tufan_gupta2000, SURENDRA_GANGWAR, Akanksha_Rai, Code_Mech, more