Enumeration of Binary Trees (original) (raw)
Last Updated : 24 May, 2026
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:

**Below two are considered different labelled trees:

**How many different Unlabelled Binary Trees can be there with n nodes?
For n = 1, there is only one tree:

For n = 2, there are two trees:

For n = 3, there are five trees:

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 is based on the concept of Catalan Numbers. The first few Catalan numbers are: 1, 1, 2, 5, 14, 42, 132, 429, 1430, 4862, ...
The recurrence relation for Catalan Numbers is: T(n) = \sum_{i=1}^{n} T(i-1) \times T(n-i)
It can also be written as: T(n) = \sum_{i=0}^{n-1} T(i) \times T(n-i-1)
Hence,
T(n) = C_n, where CnC_nCn is the nth Catalan Number.
Here:
- T(i−1) represents the number of nodes in the left subtree.
- T(n−i) represents the number of nodes in the right subtree.
The nth Catalan Number can also be calculated directly using the formula:
T(n) = \frac{(2n)!}{(n+1)! \times n!}
The number of Binary Search Trees (BSTs) possible with n nodes is also equal to the nth Catalan Number. This is because in a BST, any key can be chosen as the root. If the i-th key is chosen as the root in sorted order:
- The left subtree contains i−1 keys.
- The right subtree contains n−i keys.
Both subtrees independently form BSTs, which leads to the same Catalan recurrence relation.
**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.
The main formula for labelled binary trees is:
Number\ of\ Labelled\ Trees = (Number\ of\ Unlabelled\ Trees) \times n!
Since the number of unlabelled binary trees with n nodes is the nth Catalan Number,
C_n = \frac{(2n)!}{(n+1)! \times n!}
Therefore,
Number\ of\ Labelled\ Trees = \left(\frac{(2n)!}{(n+1)! \times n!}\right) \times n! = \frac{(2n)!}{(n+1)!}
Hence, the total number of labelled binary trees with n nodes is:
Number\ of\ Labelled\ Binary\ Trees = \frac{(2n)!}{(n+1)!}
**For example, when n=3:
- Number of unlabelled binary trees =5
- Number of ways to assign labels =3!=6
Therefore, 5×6=30
So, there are 30 different labelled binary trees possible with 3 nodes.
C++ `
#include <bits/stdc++.h> using namespace std;
// Function to calculate factorial of n int factorial(int n) { int fact = 1;
for (int i = 2; i <= n; i++)
fact *= i;
return fact;}
// Function to count labelled binary trees int countLabelledBT(int n) { // Find (2n)! int numerator = factorial(2 * n);
// Find (n + 1)!
int denominator = factorial(n + 1);
// Return (2n)! / (n + 1)!
return numerator / denominator;}
int main() { int n = 3;
cout << countLabelledBT(n);
return 0;}
Java
import java.io.*;
// Function to calculate x^y public class GfG {
// Function to calculate factorial of n
public static int factorial(int n)
{
int fact = 1;
for (int i = 2; i <= n; i++)
fact *= i;
return fact;
}
// Function to count labelled binary trees
public static int countLabelledBT(int n)
{
// Find (2n)!
int numerator = factorial(2 * n);
// Find (n + 1)!
int denominator = factorial(n + 1);
// Return (2n)! / (n + 1)!
return numerator / denominator;
}
public static void main(String[] args)
{
int n = 3;
System.out.println(countLabelledBT(n));
}}
Python
Function to calculate factorial of n
def factorial(n): fact = 1
for i in range(2, n + 1):
fact *= i
return factFunction to count labelled binary trees
def countLabelledBT(n):
# Find (2n)!
numerator = factorial(2 * n)
# Find (n + 1)!
denominator = factorial(n + 1)
# Return (2n)! / (n + 1)!
return numerator // denominatorif name == "main":
n = 3
print(countLabelledBT(n))C#
using System;
// Function to calculate x^y public class GfG {
// Function to calculate factorial of n
public static int factorial(int n) {
int fact = 1;
for (int i = 2; i <= n; i++)
fact *= i;
return fact;
}
// Function to count labelled binary trees
public static int countLabelledBT(int n) {
// Find (2n)!
int numerator = factorial(2 * n);
// Find (n + 1)!
int denominator = factorial(n + 1);
// Return (2n)! / (n + 1)!
return numerator / denominator;
}
public static void Main() {
int n = 3;
Console.WriteLine(countLabelledBT(n));
}}
JavaScript
function factorial(n) { let fact = 1;
for (let i = 2; i <= n; i++)
fact *= i;
return fact;}
// Function to count labelled binary trees function countLabelledBT(n) { // Find (2n)! let numerator = factorial(2 * n);
// Find (n + 1)!
let denominator = factorial(n + 1);
// Return (2n)! / (n + 1)!
return Math.floor(numerator / denominator);}
function main() { const n = 3;
console.log(countLabelledBT(n));}
main();
`