Enumeration of Binary TreesThe 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:
These two different types have different types of enumerations of the binary tree. Enumeration of Labelled Binary TreeThe 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, 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.
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++ CodeLet'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 CodeLet'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 TreeThe 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, 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: As shown above, these five distinct unlabelled trees are created.
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++ CodeLet'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 CodeLet'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 Next TopicMaximum Width of a Binary Tree |