Assuming you have only binary operators treeNode createNode(operators, operands) { // take first operator and create a new treeNode with it. Pop it from the operators stack // if it is the last operator in the list then there should be only two operands left in the operands and you can assign them to the left and right child of the treeNode. Return this treeNode.
// if it is not the last operator then split the operands in two stacks equally // leftOperands and rightOperands // left child becomes createNode(operators, leftoperands) // right child becomes createNode(operators, rightoperands) // return this treeNode }.
I am unsure how to split my stack of operands. I assume I will need to find the # of operands some way? What if its odd, does it matter which gets more operands?
– Blackbinary Nov 11 '10 at 15:49 I was assuming you have only binary operators so you would have a power of two # of operands. I am not sure if my algorithm will work. You should see it as how I would have approached this problem, it may contain loopholes.
Success! – Jan Nov 11 '10 at 15:54 Ah that is correct, forgot it could not be odd in this case. Thanks, i'll give it a try.
– Blackbinary Nov 11 '10 at 16:01.
Recursive algorithm: find the highest priority operator split the expression around this operator recursively apply on both parts.
Apply this method with a stack of 24+43*- (which corresponds to (2+4)-(4*3)) and see the result... – Adrien Plisson Nov 11 '10 at 15:30 See my comment above, I'd like some further details. Thanks. – Blackbinary Nov 11 '10 at 15:31 @Adrien I missed the postfix part.
This algorithm is for infix format. – Let_Me_Be Nov 11 '10 at 15:35 @Blackbinary I was talking about infix format. X+y*z would be split around * into x+y and z.
– Let_Me_Be Nov 11 '10 at 15:36.
If your expression is always symmetrical (the same number of operands and operators on each side of an operator), then the method you describe works fine, with a little modification: create a node, assign the value of the top operator in the operator stack. If there are no operator left on the operator stack, pop the 2 operands from the operands stack and assign them to the left and right branch, then exit if there are any operator on the stack, go to the left brach of your node and call your algorithm, then go to the right branch and call your algorithm. (Jan explained it so much clearer in his answer...).
I'm going to assume it is always symmetrical. I will try your approach. Thanks for the help.
– Blackbinary Nov 11 '10 at 15:45.
I cant really gove you an answer,but what I can give you is a way to a solution, that is you have to find the anglde that you relate to or peaks your interest. A good paper is one that people get drawn into because it reaches them ln some way.As for me WW11 to me, I think of the holocaust and the effect it had on the survivors, their families and those who stood by and did nothing until it was too late.