Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/349.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
在Java中使用树计算后缀表达式_Java_Data Structures_Stack_Expression Trees - Fatal编程技术网

在Java中使用树计算后缀表达式

在Java中使用树计算后缀表达式,java,data-structures,stack,expression-trees,Java,Data Structures,Stack,Expression Trees,我应该使用表达式树计算后缀表达式。 编译程序时出错 “类型ExpressionTree.Node的方法getElement()未定义” getElement不适用于堆栈类吗? 如果不是,我应该使用什么来获取堆栈中的元素 import java.util.Stack; public class ExpressionTree { Node root; // Node sub-class public class Node { private St

我应该使用表达式树计算后缀表达式。 编译程序时出错

“类型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;

    }

}
import java.util.Stack;
公共类表达式树
{
节根;
//节点子类
公共类节点
{
私有字符串密钥;//密钥
私有节点左;//指向子树的链接
私有节点权;
公共节点(字符串键)
{
this.key=key;
}
}
//构造函数使用后缀表达式构建树
公共表达式树(字符串postFixExpression)
{
堆栈=新堆栈();
String[]tokens=postfix表达式.split(“”);
for(字符串标记:标记)
{
if(等运算符(令牌))
{
Node right=stack.pop();
Node left=stack.pop();
节点=新节点(令牌);
node.left=左;
node.right=右;
栈推(节点);
}
其他的
{
stack.push(新节点(令牌));
}
}
root=stack.pop();
}
专用布尔等运算符(字符串标记)
{
布尔结果=假;
交换机(令牌)
{
格“+”:
案例“-”:
案例“*”:
案例“/:
结果=真;
打破
违约:
结果=假;
}
返回结果;
}
/**
*@返回表达式的结果
*@抛出异常
*/
public double evaluate()引发异常
{
if(root==null)
结果=0;
其他的
{
temp=(ExpressionTreeOp)root.getElement();
if(温度等参器())
{
操作数1=evaluateNode(root.getLeft());
操作数2=evaluateNode(root.getRight());
if(运算符=='+')
结果=操作数1+操作数2;
else if(运算符=='-')
结果=操作数1-操作数2;
else if(运算符=='*')
结果=操作数1*操作数2;
其他的
结果=操作数1/操作数2;
}
其他的
结果=临时getValue();
}
返回结果;
}
}
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不适用于堆栈类吗

示例代码中没有
堆栈
类。为了说明这一点,Java不会自动创建方法,您需要从某个现有类继承一个方法,或者自己实现它

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

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;

    }

}
我想你必须自己实现这个方法

Node getElement() {
    //something
}
或者在某个地方使用实际的实现。此接口提供了访问堆栈头部元素的
peek
pop
方法

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不适用于堆栈类吗

示例代码中没有
堆栈
类。为了说明这一点,Java不会自动创建方法,您需要从某个现有类继承一个方法,或者自己实现它

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

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;

    }

}
我想你必须自己实现这个方法

Node getElement() {
    //something
}
或者在某个地方使用实际的实现。此接口提供了
peek
pop
方法来访问堆栈的头元素。

java中的堆栈或向量类中都没有getElement()方法

Stack.java类的方法

push
pop
peek
empty
search
add
addAll
get
elementAt
firstElement
lastElement
setElementAt
removeElementAt
...........
Vector.java类的方法

push
pop
peek
empty
search
add
addAll
get
elementAt
firstElement
lastElement
setElementAt
removeElementAt
...........
java中的堆栈或向量类中都没有getElement()方法

Stack.java类的方法

push
pop
peek
empty
search
add
addAll
get
elementAt
firstElement
lastElement
setElementAt
removeElementAt
...........
Vector.java类的方法

push
pop
peek
empty
search
add
addAll
get
elementAt
firstElement
lastElement
setElementAt
removeElementAt
...........

请确保您的代码是正确的。您的问题是,检查编辑并尝试使您的代码更易于阅读。您可能希望将
root.getElement()
替换为
root.getKey()
,请确保您的代码是正确的。您的问题是,检查编辑并尝试使代码更易于阅读。您可能希望将
root.getElement()
替换为
root.getKey()