I can see two basic issues with the code.
1) In the parseExpression
, the operands and operators are blindly pushed into their respective stacks. You need to take care of operator precedence (ie. * and / is higher precedence than + -). To take care of operator precedence, basically when you see an operator of lower or equal precedence than what is currently in the operator stack, you must evaluate whatever is currently in the stacks before pushing the operator into the stack.
2) In create_tree
, the code for assigning first and second operand from the stack is using the first pop as first operand and second pop as second operand. It should be the other way around (ie. first pop is second operand and second pop is first operand)
Try these changes to see if it solves your problem.