【问题标题】:Evaluate Postfix expression using a tree in Java在 Java 中使用树评估 Postfix 表达式
【发布时间】:2017-10-16 10:30:15
【问题描述】:

我应该使用表达式树来评估后缀表达式。 编译程序时出错

"没有为 ExpressionTree.Node 类型定义方法 getElement()"

getElement 是否不适用于堆栈类? 如果不是,我应该使用什么来获取堆栈中的元素?

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;

    }

}

【问题讨论】:

  • 请确保您的代码是正确的。您的问题是already edited once,请检查编辑并尝试使您的代码更易于阅读。
  • 你可能想用root.getKey()替换root.getElement()

标签: java data-structures stack expression-trees


【解决方案1】:
root.getElement();

root 这里是Node 类的对象。出现编译错误是因为该类没有名为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;
    }
}

getElement 是否不适用于堆栈类?

您的示例代码中没有 stack 类。明确地说,Java 不会自动创建方法,您需要从某个现有类继承方法或自己实现它。

如果不是,我应该使用什么来获取堆栈中的元素?

我猜你必须自己实现这个方法。

Node getElement() {
    //something
}

或者在某处使用实际的Stack 实现。该接口提供了peekpop方法来访问栈的头元素。

【讨论】:

    【解决方案2】:

    Java 中的 Stack 或 Vector 类中都没有 getElement() 方法

    Stack.java 类的方法

    push
    pop
    peek
    empty
    search
    

    Vector.java 类的方法

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

    【讨论】:

      猜你喜欢
      • 2013-11-11
      • 1970-01-01
      • 2016-08-03
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2022-11-07
      相关资源
      最近更新 更多