# Enumeration of Binary Trees

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

```Below two are considered same unlabeled trees
o                 o
/   \             /   \
o     o           o     o

Below two are considered different labeled trees
A                C
/   \             /  \
B     C           A    B
```

How many different Unlabeled 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 pair of counts for nodes in left and right subtrees and multiply the counts for a particular pair. Finally add 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,…

Here,
T(i-1) represents number of nodes on the left-sub-tree
T(n−i-1) represents number of nodes on the right-sub-tree

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

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

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

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

Therefore,

```Number of Labeled Tees = (Number of unlabeled trees) * n!
= [(2n)! / (n+1)!n!]  × n!```

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

# GATE CS Corner    Company Wise Coding Practice

Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.
3.1 Average Difficulty : 3.1/5.0
Based on 64 vote(s)
{"found_math": false, "script_math_tex": 0, "script_math_asciimath": 0, "math_annotations": 0, "math_alttext": 0, "mathml": 0, "mathjax_tag": 0, "mathjax_inline_tex": 0, "mathjax_display_tex": 0, "mathjax_asciimath": 0, "img_math": 0, "codecogs_latex": 0, "wp_latex": 0, "mimetex.cgi": 0, "/images/math/codecogs": 0, "mathtex.cgi": 0, "katex": 0, "math-container": 0, "wp-katex-eq": 0, "align": 0, "equation": 0, "x-ck12": 0, "texerror": 0, "url": "http://www.geeksforgeeks.org/enumeration-of-binary-trees/", "fetch_time": 1511231333000000000, "content_mime_type": "text/html", "warc_filename": "crawl-data/CC-MAIN-2017-47/segments/1510934806310.85/warc/CC-MAIN-20171121021058-20171121041058-00386.warc.gz", "warc_record_offset": 399606523, "warc_record_length": 17920, "token_count": 765, "char_count": 2526, "score": 4.3125, "int_score": 4, "crawl": "CC-MAIN-2017-47", "snapshot_type": "latest", "language": "en", "language_score": 0.8556632399559021, "file_path": "/sailhome/yjruan/project/latent-lm/data/processed/finemath-4plus/finemath-4plus/train-00014-of-00064.parquet"}