1
votes

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

The shown code does not compute anything. Include the relevant parts of the code on site.luk2302
@luk2302 here is the complete code : pastebin.com/cZNR6G4ucarli
The second part of my comment was important, code needs to be posted here, not behind some link.luk2302
@luk2302 I tried, but " It looks like your post is mostly code; please add some more details."carli
And your solution to that was to not include more details but instead removed the code? Last time: code (and more details) need to be posted here directly.luk2302