求解二叉树
谁能解释当我给定 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
你难道不只是说一些关于以下内容的事情吗:(
对你的结构随意,因为我不知道一切的全部意图。)
(我假设你的结构被定义为评估只有一个变量的函数,这就是为什么您传入
x
而不是传入变量值字典。)Wouldn't you just say something on the order of:
(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.)