求解二叉树

发布于 2024-12-18 15:09:54 字数 607 浏览 0 评论 0原文

谁能解释当我给定 x 作为参数时如何求解表达式树?

例如,我有方程 ((2*x)) + 4,假设参数为 x = 3。 这会给我们 10 并且该方法将返回它。

我想到的方法是递归地执行此操作,但我无法真正执行此操作,因为参数必须是双 x。

有什么想法吗?

这是我到目前为止的代码。

  public double evaluate(double x) throws ExpressionTreeNodeException {
    ExpressionTreeNode n = new ExpressionTreeNode();
    n.setValue(getValue());
    n.setType(getType());
    if ( n.getRightChild() == null && n.getLeftChild() == null){
        double RootLeaf = Double.parseDouble(n.getValue());
        return RootLeaf;
    } else {
        double operand1 = 
        return ()
    }
}

Can anyone explain how I Solve a expression Tree when I'm given x as a parameter?

For example, I have the equation ((2*x)) + 4and let's say in the parameter, x = 3.
This would give us 10 and the method would return this.

The way I thought about doing this was to do it recursively but I can't really do it because the parameter has to be the double x.

Any thoughts?

Here's the code I have so far.

  public double evaluate(double x) throws ExpressionTreeNodeException {
    ExpressionTreeNode n = new ExpressionTreeNode();
    n.setValue(getValue());
    n.setType(getType());
    if ( n.getRightChild() == null && n.getLeftChild() == null){
        double RootLeaf = Double.parseDouble(n.getValue());
        return RootLeaf;
    } else {
        double operand1 = 
        return ()
    }
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

太阳公公是暖光 2024-12-25 15:09:54

你难道不只是说一些关于以下内容的事情吗:(

if ( n.getRightChild() == null && n.getLeftChild() == null){
    double RootLeaf = Double.parseDouble(n.getValue());
    return RootLeaf;
} else if (n.getLeftChild() == null) {
    // Evaluate prefix operator -- assume no postfix operators
    double operand1 = n.getRightChild().evaluate(x);
    double result = n.getType().evaluateMonadic(operand1);
    return result;
} else {
    // Evaluate diadic operator
    double operand1 = n.getLeftChild().evaluate(x);
    double operand2 = n.getRightChild().evaluate(x);
    double result = n.getType().evaluateDiadic(operand1, operand2);
    return result;
}

对你的结构随意,因为我不知道一切的全部意图。)

(我假设你的结构被定义为评估只有一个变量的函数,这就是为什么您传入 x 而不是传入变量值字典。)

Wouldn't you just say something on the order of:

if ( n.getRightChild() == null && n.getLeftChild() == null){
    double RootLeaf = Double.parseDouble(n.getValue());
    return RootLeaf;
} else if (n.getLeftChild() == null) {
    // Evaluate prefix operator -- assume no postfix operators
    double operand1 = n.getRightChild().evaluate(x);
    double result = n.getType().evaluateMonadic(operand1);
    return result;
} else {
    // Evaluate diadic operator
    double operand1 = n.getLeftChild().evaluate(x);
    double operand2 = n.getRightChild().evaluate(x);
    double result = n.getType().evaluateDiadic(operand1, operand2);
    return result;
}

(Taking liberties with your structure because I don't know the full intent of everything.)

(I'm assuming your structure is defined to be evaluating a function of only one variable, which is why you pass in x rather than passing in a dictionary of variable values.)

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文