Harris Rehaman
Harris Rehaman

Reputation: 1

Evaluate Postfix expression using a tree in Java

I am supposed to evaluate a postfix expression using an expression tree. Got an error while compiling the program

"The method getElement() is undefined for the type ExpressionTree.Node"

Does getElement doesn't work for stack class? If not what should I use to get the elements in the stack?

import java.util.Stack;
public class ExpressionTree 
{
    Node root;

    // Node sub-class
    public class Node 
    {
        private String key; // key
        private Node left; // links to subtrees
        private Node right;

        public Node(String key)
        {
            this.key = key;
        }
    }

    // constructor to build a tree with postFix expression
    public ExpressionTree(String postFixExpression)
    {
        Stack<Node> stack = new Stack<Node>();
        String[] tokens = postFixExpression.split(" ");
        for (String token: tokens) 
        {
            if (isOperator(token))
            {
                Node right = stack.pop();
                Node left = stack.pop();
                Node node = new Node(token);
                node.left = left;
                node.right = right;
                stack.push(node);
            }
            else
            {
                stack.push(new Node(token));
            }
        }
        root = stack.pop();
    }

    private boolean isOperator(String token)
    {
        boolean result = false;
        switch(token) 
        {
            case "+":
            case "-":
            case "*":
            case "/":
                result = true;
                break;
            default:
                result = false;
        }
        return result;
    }

    /**
     * @return result of the expression
     * @throws Exception
     */
    public double evaluate() throws Exception
    {
        if (root==null)
            result = 0;
        else
        {
            temp = (ExpressionTreeOp)root.getElement();

            if (temp.isOperator())
            {
                operand1 = evaluateNode(root.getLeft());
                operand2 = evaluateNode(root.getRight());
                if (operator == '+')
            result = operand1 + operand2;

        else if (operator == '-')
            result = operand1 - operand2;
        else if (operator == '*')
            result = operand1 * operand2;
        else 
            result = operand1 / operand2;
            }
            else
                result = temp.getValue();
        }

        return result;

    }

}

Upvotes: 0

Views: 792

Answers (2)

Bikramjit Rajbongshi
Bikramjit Rajbongshi

Reputation: 452

There is no getElement() method in either Stack or Vector class in java

Methods of Stack.java class

push
pop
peek
empty
search

Methods of Vector.java class

add
addAll
get
elementAt
firstElement
lastElement
setElementAt
removeElementAt
...........

Upvotes: 1

default locale
default locale

Reputation: 13436

root.getElement();

root here is an object of class Node. A compilation error occurs because this class doesn't have a method called getElement:

// Node sub-class
public class Node 
{
    private String key; // key
    private Node left; // links to subtrees
    private Node right;

    public Node(String key)
    {
        this.key = key;
    }
}

Does getElement doesn't work for stack class?

There is no stack class in your sample code. To make it clear, Java doesn't create methods automatically, you'll need to inherit a method from some existing class or to implement it yourself.

If not what should I use to get the elements in the stack?

I guess you'll have to implement this method yourself.

Node getElement() {
    //something
}

Or use an actual Stack implementation somewhere. This interface provides peek and pop methods to access the head element of the stack.

Upvotes: 1

Related Questions