Sign in to follow this  
parasolstars

General all possible expression trees

Recommended Posts

parasolstars    100
I would like to enumerate/generate all possible expression trees from expressions like "1+2*3-8". My idea goes something like: generateExpressions: for each operator in expression leftExpressionLists = generateExpressions( subexpression-left-of-operator) rightExpressionLists = generateExpressions( subexpression-right-of-operator) combinedExpressionLists = combineAllPermutations( leftExpressionLists, rightExpressionLists ) But, this seems non-trivial for a quiz problem. There must be some trick for a simpler algorithm?

Share this post


Link to post
Share on other sites
iMalc    2466
I think this is the result you are looking for. There are 5 of them in this case. I assume that you want to know how to write a program to produce these?
   *            +              +                  -              -
+ - 1 * 1 - * 8 + 8
1 2 3 8 2 - * 8 + 3 1 *
3 8 2 3 1 2 2 3
Edit: I did post the answer, but I have removed it, as I need you to confirm that this isn't homework of some kind first.

Share this post


Link to post
Share on other sites
parasolstars    100
I think we are speaking of the same straight-forward recursive method that I briefly outlined.

Thus now we are just looking for better ways, which is purely optional though self motivated.

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this