Sign in to follow this  
Nicholas Kong

How to store the parsed data of the expression from a file to construct an expression tree

Recommended Posts

Nicholas Kong    1535

This is one expression from the first line of the file. 

 

- + + 4.0 - / - 22.0 2.0 5.0 * 3.0 4.0 7.0 / 6.0 3.0

 

The problem lies I do not understand where do these operands and numbers get stored to construct a expression tree during the time the above expression gets parsed. After constructing an expression tree, the tree is suppose to be able to output  preorder,inorder and postorder expression of that tree.

 

From the example given, the operands are connected by an operator, but the example does not help guide me in terms of how to lay the operands and numbers out for this expression.

 

I tried using a stack diagram to visualize the design of my program better but I am stuck when 22.0 appears on the stack. What is suppose to happen when 22.0 appears on the stack?

 

[url=http://s33.photobucket.com/user/warnexus/media/stack_zps213b2cb1.png.html]stack_zps213b2cb1.png[/URL]

Edited by warnexus

Share this post


Link to post
Share on other sites

List looks backwards to me? Try pushing values from the right hand side of the list onto the stack and replace the top 2 elements with the result of the operation when you push an operator?

 

so 3.0 6.0 / => pushes 3, 6 and then replaces them with 3.0 / 6.0 = 0.5 when the division operator is pushed.

Share this post


Link to post
Share on other sites
Nicholas Kong    1535

List looks backwards to me? Try pushing values from the right hand side of the list onto the stack and replace the top 2 elements with the result of the operation when you push an operator?

 

so 3.0 6.0 / => pushes 3, 6 and then replaces them with 3.0 / 6.0 = 0.5 when the division operator is pushed.

reversing the list was the perfect strategy to evaluating the expression. Thanks Paradigm.

Edited by warnexus

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