【问题标题】:Evaluate Postfix Expression Tree - Calculator评估后缀表达式树 - 计算器
【发布时间】:2020-05-26 20:58:51
【问题描述】:

我有代码从中缀生成后缀表达式并从后缀表示法生成表达式树。问题是,如果我给它一个像 45 / 15 * 3 这样的表达式,它会给我结果 1 而不是 9,因为它首先解决了树的最深层次。我可以做另一种遍历来正确评估表达式吗?

def evaluate(self):

    if not self.is_empty():
        if not infix_to_postfix.is_operator(self._root):
            return float(self._root)
        else:
            A = self._left.evaluate()
            B = self._right.evaluate()
            if self._root == "+":
                return A + B
            elif self._root == "-":
                return A - B
            elif self._root == "*":
                return A * B
            elif self._root == "/":
                return A / B
def InfixToPostfix(exp: str):
    exp = exp.replace(" ", "")

    S = Stack()
    postfix = ""
    j = 0
    for i in range(len(exp)):
        if is_operand(exp[i]):
            if i + 1 <= len(exp) - 1 and is_operand(exp[i+1]):
                continue
            else:
                j = i
                while j - 1 >= 0 and is_operand(exp[j - 1]):
                    if is_operand(exp[j]):
                        j -= 1
                    else:
                        break
            postfix += exp[j:i + 1] + " "
        elif is_operator(exp[i]):
            while not S.is_empty() and S.top() != "(" and \ HasHigherPrecedence(S.top(), exp[i]):
                if is_operator(S.top()):
                    postfix += S.top() + " "
                else:
                    postfix += S.top()
                S.pop()
            S.push(exp[i])
        elif exp[i] == "(":
            S.push(exp[i])
        elif exp[i] == ")":
            while not S.is_empty() and S.top() != "(":
                if is_operator(S.top()):
                    postfix += S.top() + " "
                else:
                    postfix += S.top()
                S.pop()
        else:
            print("There's an invalid character")
            return

    while not S.is_empty():
        if S.top() == '(':
            S.pop()
            continue
        if is_operator(S.top()):
            postfix += S.top() + " "
        else:
            postfix += S.top()
        S.pop()

    return postfix


def HasHigherPrecedence(op1: str, op2: str):
    op1_weight = get_operator_weight(op1)
    op2_weight = get_operator_weight(op2)
    return op1_weight > op2_weight

【问题讨论】:

    标签: python expression-trees postfix-notation


    【解决方案1】:

    示例 45 / 15 * 3 的后缀表达式为:

    45 15 / 3 *
    

    所以生成的树看起来像:

            * 
       /        3
    45   15
    

    所以你的遍历算法看起来是正确的,它会做 45 / 15 = 3,然后 3 * 3 = 9。

    在您的后缀生成器中,这个问题实际上非常小。具体来说,在函数 HasHigherPrecedence 中,您应该返回 op1_weight &gt;= op2_weight。它应该大于或等于,因为在诸如这个运算符具有相同优先级的示例中,它们应该按照它们出现的顺序执行。所以先除法。

    【讨论】:

    • 我现在看到,我的后缀生成器出了点问题,因为我得到了不同的结果 45 15 3 * / 使用我的算法,我关注了 Youtube 上的视频。我将发布后缀算法以及树生成算法
    • @biotecher 请查看我的答案编辑。这似乎是一个非常小的问题,将解决问题。
    • 感谢您的帮助!现在工作
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-05-13
    • 1970-01-01
    • 2011-05-11
    • 1970-01-01
    相关资源
    最近更新 更多