Sign in to follow this  
parasolstars

General all possible expression trees

Recommended Posts

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
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

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