Question

Here's my code. However, whenever I enter an expression such as "2*3+6/3", it prints out the output as "36/3+2*" which isn't the correct postfix format. Anyone see any errors in my code? Much appreciated!

package expressiontree;
import java.util.LinkedList;
import java.util.List;

class Stack
{
    private List stack = new LinkedList();

    public void push(TreeNode node)
    {
        stack.add(node);
    }

    public TreeNode pop()
    {
        int top = stack.size()-1;
        if (top < 0) return null;
        TreeNode node = (TreeNode)stack.get(top);
        stack.remove(top);
        return node;
    }
}

class TreeNode
{
    TreeNode left;
    TreeNode right;
    char data;

    TreeNode(){}

    TreeNode(char c)
    {
        this.data = c;
    }
}

public class ExpressionTree {

    String expression;
    TreeNode tree;
    Stack operandstack = new Stack();
    Stack operatorstack = new Stack();

    private boolean isOperator(char c)
    {
        String operator = "+-*/";
        if(operator.indexOf(c)==-1)
        return false;

        return true;
    }

    // parses an expression and creates two stacks
    private void parseExpression(String exp)
    {
        for(int i=0; i < exp.length(); i++) {
            char c = exp.charAt(i);
            if(isOperator(c))
            operatorstack.push(new TreeNode(c));
            else{
                operandstack.push(new TreeNode(c));
            }
        }
    }

    private TreeNode create_tree()
    {
        TreeNode node = new TreeNode();
        while((node = operatorstack.pop())!=null)
        {
            TreeNode oper1 = operandstack.pop();
            TreeNode oper2 = operandstack.pop();

            TreeNode exp = create_expression(oper1, node, oper2);
            operandstack.push(exp);
        }

        return operandstack.pop();
    }

    private TreeNode create_expression(TreeNode oper1, TreeNode operator, TreeNode oper2)
    {
        TreeNode expression = new TreeNode(operator.data);
        expression.left = oper1;
        expression.right = oper2;

        return expression;
    }

    public static void postfix(TreeNode tn){
        if (tn == null){
            return;
        }
        else{
            postfix(tn.left);
            postfix(tn.right);  
            System.out.print(tn.data);
        }
    }

    public static void main(String[] args)
    {
        ExpressionTree et = new ExpressionTree();
        String expression = "2*3+6/3";
        et.parseExpression(expression);
        TreeNode tn = et.create_tree();
        postfix(tn);
        System.out.println();
    }
}
Was it helpful?

Solution

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.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top