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:
It can also be written as:
Hence,
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:
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:
Since the number of unlabelled binary trees with n nodes is the nth Catalan Number,
Therefore,
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.
#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;
}
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));
}
}
# Function to calculate factorial of n
def factorial(n):
fact = 1
for i in range(2, n + 1):
fact *= i
return fact
# Function 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 // denominator
if __name__ == "__main__":
n = 3
print(countLabelledBT(n))
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));
}
}
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();
Output
30