I'm trying to calculate the postfix expression using binary tree, but the function is not performing well. if I give as input : 2+4*8/4 the result should be : 10, while the code gives me : 106
this is the code :
int evaluateExpression() {
return evaluateExpression(root);
}
int evaluateExpression(Node adr) {
if (adr == null) {
return Integer.MIN_VALUE;
}
if (adr.left == null && adr.right == null) {
return (int) (adr.info);
}
int left = evaluateExpression(adr.left);
int right = evaluateExpression(adr.right);
if (adr.info == '+') {
return left + right;
} else if (adr.info == '-') {
return left - right;
} else if (adr.info == '*') {
return left * right;
} else if (adr.info == '^') {
return (int) (Math.pow(left, right));
} else {
return left / right;
}
}
More code here : https://pastebin.com/5GDm94QJ