Open In App

Enumeration of Binary Trees

Last Updated : 26 Oct, 2021
Improve
Improve
Like Article
Like
Save
Share
Report

A Binary Tree is labeled if every node is assigned a label and a Binary Tree is unlabelled if nodes are not assigned any label. 

 

Below two are considered same unlabelled trees
    o                 o
  /   \             /   \ 
 o     o           o     o 

Below two are considered different labelled trees
    A                C
  /   \             /  \ 
 B     C           A    B 



How many different Unlabelled Binary Trees can be there with n nodes? 
 

For n  = 1, there is only one tree
   o

For n  = 2, there are two trees
   o      o
  /        \  
 o          o

For n  = 3, there are five trees
    o      o           o         o      o
   /        \         /  \      /         \
  o          o       o    o     o          o
 /            \                  \        /
o              o                  o      o



The idea is to consider all possible pairs of counts for nodes in left and right subtrees and multiply the counts for a particular pair. Finally, add the results of all pairs. 

 

For example, let T(n) be count for n nodes.
T(0) = 1  [There is only 1 empty tree]
T(1) = 1
T(2) = 2

T(3) =  T(0)*T(2) + T(1)*T(1) + T(2)*T(0) = 1*2 + 1*1 + 2*1 = 5

T(4) =  T(0)*T(3) + T(1)*T(2) + T(2)*T(1) + T(3)*T(0)
     =  1*5 + 1*2 + 2*1 + 5*1 
     =  14 



The above pattern basically represents n’th Catalan Numbers. First few Catalan numbers are 1 1 2 5 14 42 132 429 1430 4862,… 
T(n)=\sum_{i=1}^{n}T(i-1)T(n-i)=\sum_{i=0}^{n-1}T(i)T(n-i-1)=C_n
Here, 
T(i-1) represents the number of nodes on the left-sub-tree 
T(n?i-1) represents the number of nodes on the right-sub-tree 

n’th Catalan Number can also be evaluated using the direct formula. 

   T(n) = (2n)! / (n+1)!n!


The number of Binary Search Trees (BST) with n nodes is also the same as the number of unlabelled trees. The reason for this is simple, in BST also we can make any key a root, If the root is i’th key in sorted order, then i-1 keys can go on one side, and (n-i) keys can go on another side. 

How many labeled Binary Trees can be there with n nodes? 
To count labeled trees, we can use the above count for unlabelled trees. The idea is simple, every unlabelled tree with n nodes can create n! different labeled trees by assigning different permutations of labels to all nodes. 

Therefore, 

Number of Labelled Trees = (Number of unlabelled trees) * n!
                       = [(2n)! / (n+1)!n!]  × n!



For example for n = 3, there are 5 * 3! = 5*6 = 30 different labelled trees 



 


Previous Article
Next Article

Similar Reads

Total number of possible Binary Search Trees and Binary Trees with n keys
Total number of possible Binary Search Trees with n different keys (countBST(n)) = Catalan number Cn = (2n)! / ((n + 1)! * n!) For n = 0, 1, 2, 3, … values of Catalan numbers are 1, 1, 2, 5, 14, 42, 132, 429, 1430, 4862, .... So are numbers of Binary Search Trees. Total number of possible Binary Trees with n different keys (countBT(n)) = countBST(n
12 min read
Introduction to Generic Trees (N-ary Trees)
Generic trees are a collection of nodes where each node is a data structure that consists of records and a list of references to its children(duplicate references are not allowed). Unlike the linked list, each node stores the address of multiple nodes. Every node stores address of its children and the very first node's address will be stored in a s
5 min read
Count the Number of Binary Search Trees present in a Binary Tree
Given a binary tree, the task is to count the number of Binary Search Trees present in it. Examples: Input: 1 / \ 2 3 / \ / \ 4 5 6 7 Output: 4Here each leaf node represents a binary search tree and there are total 4 nodes. Input: 11 / \ 8 10 / / \ 5 9 8 / \ 4 6 Output: 6 Sub-tree rooted under node 5 is a BST 5 / \ 4 6 Another BST we have is rooted
10 min read
Construct a Maximum Binary Tree from two given Binary Trees
Given two Binary Trees, the task is to create a Maximum Binary Tree from the two given binary trees and print the Inorder Traversal of that tree. What is the maximum Binary Tree? The maximum binary is constructed in the following manner: In the case of both the Binary Trees having two corresponding nodes, the maximum of the two values is considered
8 min read
Data Structures | Binary Trees | Question 1
Which of the following is true about Binary Trees? (A) Every binary tree is either complete or full. (B) Every complete binary tree is also a full binary tree. (C) Every full binary tree is also a complete binary tree. (D) No binary tree is both complete and full. (E) None of the above Answer: (E)Explanation: A full binary tree (sometimes proper bi
2 min read
Data Structures | Binary Trees | Question 15
If arity of operators is fixed, then which of the following notations can be used to parse expressions without parentheses? a) Infix Notation (Inorder traversal of a expression tree) b) Postfix Notation (Postorder traversal of a expression tree) c) Prefix Notation (Preorder traversal of a expression tree) (A) b and c (B) Only b (C) a, b and c (D) N
1 min read
Data Structures | Binary Trees | Question 3
What are the main applications of tree data structure? Manipulate hierarchical data Make information easy to search Manipulate sorted lists of data Router algorithms Form of a multi-stage decision-making, like Chess Game. As a workflow for compositing digital images for visual effects (A) 1, 2, 3, 4 and 6 (B) 1, 2, 3, 4 and 5 (C) 1, 3, 4, 5 and 6 (
1 min read
Data Structures | Binary Trees | Question 4
Level of a node is distance from root to that node. For example, level of root is 1 and levels of left and right children of root is 2. The maximum number of nodes on level i of a binary tree is In the following answers, the operator '^' indicates power. (A) 2^(i-1) (B) 2^i (C) 2^(i+1) (D) 2^[(i+1)/2] Answer: (A) Explanation: Number of nodes of bin
1 min read
Data Structures | Binary Trees | Question 15
In a complete k-ary tree, every internal node has exactly k children or no child. The number of leaves in such a tree with n internal nodes is: (A) nk (B) (n – 1) k+ 1 (C) n( k – 1) + 1 (D) n(k – 1) Answer: (C) Explanation: For an k-ary tree where each node has k children or no children, following relation holds L = (k-1)*n + 1 Where L is the numbe
1 min read
Data Structures | Binary Search Trees | Question 1
What is the worst case time complexity for search, insert and delete operations in a general Binary Search Tree for a skewed tree ? (A) O(n) for all (B) O(Logn) for all (C) O(Logn) for search and insert, and O(n) for delete (D) O(Logn) for search, and O(n) for insert and delete Answer: (A)Explanation: In skewed Binary Search Tree (BST), all three o
1 min read
Article Tags :
Practice Tags :
three90RightbarBannerImg