Home » Enumeration of Binary Trees

Enumeration of Binary Trees

by Online Tutorials Library

Enumeration of Binary Trees

The enumeration of a binary tree can be defined as the number of distinct binary trees created from a given number of nodes or a binary tree. These distinct binary trees can be different according to the labelling of the nodes of the binary tree. Depending upon the labelling of the nodes present in a binary tree, there are two types of a binary tree:

  1. Labelled Binary Tree: All the nodes present in a binary tree are labelled with proper labels in the Labelled Binary tree. It means all the nodes in a labelled binary tree are arranged in a particular order or sequence.
  2. Unlabelled Binary Tree: In the unlabelled binary tree, the nodes are not given specific labels. The order or sequence of the nodes in the binary tree is not relevant because there is no specific parameter to distinctly identify the various nodes of a binary tree.

These two different types have different types of enumerations of the binary tree.

Enumeration of Labelled Binary Tree

The enumeration of the labelled binary tree can be defined as the number of distinct unlabeled binary trees formed from a given labelled binary tree or set of labelled nodes.

The main difference between the enumeration of the labelled binary tree and the enumeration of the unlabelled binary tree is that for a given number of nodes, there is the only structure of the new tree created in an unlabelled tree, but in the labelled, there will be two different trees with the same number of nodes because the positioning node does in the tree can be different from all the nodes and not identical.

Let’s understand the enumeration of an unlabelled binary tree with the help of an example.

Suppose we have a set of nodes with three nodes named node 1, node two, and node 3.

For the first round, let’s consider N=1, where n represents the number of nodes to create one distinct binary tree with one node in the tree. The tree will look like this,

Node 1

Now let’s consider N=2, which means we have Node 1 and node 2 to form binary trees. So with two nodes, we can create four different binary trees.

For N=2, two distinct binary trees formed. These two trees look like,

Enumeration of Binary Trees

In the above trees, we have four trees depending upon the position of the nodes in the tree. There are two left-skewed trees and two right-skewed trees.

  • In the first left-skewed tree, node 1 is the root node, and node 2 is the left child node. And in the second left-skewed tree, node two acts as the root node of the left-skewed binary tree having node one as the left child node.
  • At the same time, two right-skewed binary trees have the same number of nodes, just varying the position of those nodes in the tree. In the first right-skewed binary tree, node one acts as the root node of the binary tree and node two as the child node of the tree. Whereas in the second right-skewed binary tree, node two acts as the root node of the binary tree and node one as the child node of the tree.

So, for two labelled nodes, four different trees are created. Similarly, for three labelled nodes, there can be thirty different (distinct) binary trees created.

So, to calculate the enumeration for the three nodes,

For N=1, the number of distinct labelled trees = 1

For N=2, the number of distinct labelled trees = 4

For N=3, the number of distinct labelled trees = 30

Here is the formula for the summation of all these numbers of distinct binary trees by the number of nodes.

Where N is the number of nodes.

Let’s calculate it for N=3,

So, for three labelled nodes, we get thirty different trees that can be created.

C++ Code

Let’s write a C++ code to find the number of distinct labelled binary trees created from a given set of labelled nodes.

Output:

The output of the above code is

The number of Distinct labelled Binary Tree is 30  

Java Code

Let’s write a Java code to find the number of distinct labelled binary trees created from a given set of labelled nodes.

Enumeration of Unlabelled Binary Tree

The enumeration of the unlabelled binary tree can be defined as the number of distinct unlabeled binary trees formed from a given unlabelled binary tree or set of unlabelled nodes.

Let’s understand the enumeration of an unlabelled binary tree with the help of an example.

Suppose we have a set of nodes having three nodes in it.

For the first round, consider N=1, where n represents the number of nodes so that we can create one distinct binary tree with one node in the tree. The tree will look like this,

O

Now consider N=2, which means we have two nodes to form binary trees. So with two nodes, we can create two different binary trees.

For N=2, two distinct binary trees are formed, and these two trees look like,

Enumeration of Binary Trees

In one of the trees, the root node has only the left child node, and in the other tree, the root node is present with only one of the right child nodes.

Now consider N=3. There are three nodes available for creating distinct binary trees. With N=3, there can be five distinct unlabeled binary trees that can be created. These five distinct unlabelled binary trees are:

Enumeration of Binary Trees

As shown above, these five distinct unlabelled trees are created.

  • In the unlabelled binary tree 1, the root node has only one left child node, with only one left child node. The tree shown in the unlabeled binary tree one is also known as the left-skewed binary tree.
  • In the unlabelled binary tree 2, the root has one left child node, and that left child node has one right child node.
  • In the unlabelled binary tree 3, the root node has one right child node and one left child node. It is a binary tree.
  • In the unlabelled binary tree 4, the root has one right child node and has one left child node.
  • In the unlabelled binary tree 5, the root node has only one right child node that has only one right child node. The tree shown in the unlabeled binary tree 5 is also known as the right.-skewed binary tree.

So, to calculate the enumeration for the three nodes,

For N=1, the number of distinct unlabelled trees = 1

For N=2, the number of distinct unlabelled trees = 2

For N=3, the number of distinct unlabelled trees = 5

Here is the formula for the summation of all these numbers of distinct binary trees by the number of nodes.

Where N is the number of nodes.

Let’s calculate it for N=3,

So, with three nodes, there will be five distinct unlabelled binary trees that can be created.

C++ Code

Let’s write a C++ code to find the number of distinct unlabelled binary trees created from a given set of unlabeled nodes.

Output: The output of the above C++ code is

The number of distinct unlabeled binary trees is 5  

Java Code

Let’s write a Java code to find the number of distinct unlabelled binary trees created from a given set of unlabeled nodes.

Output: The output of the above java code is

The number of distinct unlabeled binary trees is 5  

You may also like