Imagine I have four letters a,b,c and d. I want to determine what are the ways to validly parenthesize and multiply them. For instance (a.b).(c.d) can be a solution or (a.(b.c)).d another solution. The number of combinations is 5 for 4 letters. (Which is also equivalent to Catalan Number n-1 in that case catalan number 3 which is 5).
I have realized that these combinations can be written as full binary trees and each binary tree represents one combination :
abcd abcd
/ \ / \ .....
/ \ / \
a bcd ab cd
/ \ / \ /\
bc d a b c d
/ \
b c
Starting from the deepest leaf, algorithm can generate for instance for the first tree : 1 - (b.c) then 2 - (b.c).d then 3- a.((b.c).d).
I want to have a recursive or normal function which can generate all the possible trees and do the multiplications but not sure how I can achieve that. Any help and suggestion is greatly appreciated.