0% found this document useful (0 votes)
290 views7 pages

Counting Tree

This document discusses counting the number of distinct binary trees with n nodes. It provides the following counts: 1 node = 1 tree, 2 nodes = 2 trees, 3 nodes = 5 trees. It explains that the number of distinct binary trees is equal to the number of possible inorder permutations obtained by passing numbers through a stack. A recursive formula is provided: bn is the sum of all possible binary trees formed with a root and two subtrees with bi and bn-i-1 nodes, for 0 ≤ i ≤ n-1. Finally, it notes the relationship between this counting problem and Catalan numbers.

Uploaded by

femi
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
290 views7 pages

Counting Tree

This document discusses counting the number of distinct binary trees with n nodes. It provides the following counts: 1 node = 1 tree, 2 nodes = 2 trees, 3 nodes = 5 trees. It explains that the number of distinct binary trees is equal to the number of possible inorder permutations obtained by passing numbers through a stack. A recursive formula is provided: bn is the sum of all possible binary trees formed with a root and two subtrees with bi and bn-i-1 nodes, for 0 ≤ i ≤ n-1. Finally, it notes the relationship between this counting problem and Catalan numbers.

Uploaded by

femi
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
You are on page 1/ 7

COUNTING BINARY TREES

MODULE 1 - TREES
How many distinct Binary Trees are there with n nodes?
• When n=1
count = 1
• When n=2
count = 2

• When n=3
count = 5

12/16/2018 2
 Given preorder and inorder traversal, there is a
distinct binary tree.
Preorder : 1,2,3,4,5,6,7,8,9
Inorder : 2,3,1,5,4,7,8,6,9

 Given preorder traversal, different inorder traversal


define different binary trees

 Number of distinct binary trees is equal to number of


different inorder permutations obtainable from binary
tree with preorder traversal as 1,2,3,4,5,6,7,8,9
12/16/2018 3
Number of distinct permutation obtainable by
passing the numbers 1 to n through a stack and
deleting in all possible ways is equal to number of
distinct binary trees with n nodes (concept of
inorder permutation)
eg. n=3: 3,2,1; 2,3,1; 1,3,2; 1,2,3;
2,1,3 (3,1,2 not possible)

12/16/2018 4
• Let bn be the number of permutations of 1 to n
obtainable within the stack. Then

12/16/2018 5
• Let bn be the number of distinct binary trees with n
nodes
• bn is the sum of all possible binary trees formed with
a root and two subtrees with bi and bn-i-1 nodes,
for 0 ≤ i ≤ n-1

12/16/2018 6
b n  b 0 b n -1  b1b n -2  ...  b n -1b 0
C Catalan number

(2n)!
n  1!n!

12/16/2018 7

You might also like