表达树问题

时间:2019-03-04 00:21:52

标签: c# algorithm tree expression

我目前正在尝试找出表达式树的算法。我当前将获得的字符串将类似于Hello+12+WorldA2-12-A3-14。这些字符串将具有相同的运算符。使用我的算法,当前的最后一个操作数没有放入树中。我已经看过网上了,但我很难理解如何使其正常工作。

Stack<BaseNode> TreeStack = new Stack<BaseNode>();
BaseNode temp1 = new BaseNode();
BaseNode temp2 = new BaseNode();
for (int i = 0; i < tree.Length; i++)
{
    VariableNode varNode = new VariableNode();
    NumericalNode numNode = new NumericalNode();
    if (CheckExpressions(tree[i])) // if the character is an operator
    {
        OperatorNode expression = new OperatorNode(tree[i]);
        temp1 = TreeStack.Pop();
        if (TreeStack.Count != 0)
        {
            temp2 = TreeStack.Pop();
        }
        expression.Right = temp1;
        expression.Left = temp2;

        TreeStack.Push(expression);
    }
    else if (!CheckExpressions(tree[i]))
    {
        if (Char.IsLetter(tree[i]))
        {
            while (Char.IsLetter(tree[i])) // for the variable node
            {
                varNode.name += tree[i];
                if (i + 1 == tree.Length)
                {
                    break;
                }
                i++;
            }
            TreeStack.Push(varNode);
            if (i + 1 != tree.Length)
            {
                i--;
            }
        }
        else if (Char.IsDigit(tree[i])) // for constant value
        {
            int zero = 0; // for appending the numbers to combine them
            while (Char.IsDigit(tree[i]))
            {
                if (zero == 0)
                {
                    zero = tree[i] - '0';
                }
                else
                {
                    zero = int.Parse(zero.ToString() + tree[i].ToString());
                }
                if (i < tree.Length)
                {
                    i++;
                }
            }
            if (i + 1 != tree.Length)
            {
                i--;
            }
            numNode.number = zero;
            TreeStack.Push(numNode);
        }
    }
}

1 个答案:

答案 0 :(得分:0)

我认为您了解您缺少第二个操作数,因为一旦您看到运算符,便会尝试为2个值弹出堆栈。在示例中,您可以很容易地看出它永远不会出现(由于运算符是二进制的,所以堆栈将有2个变量)。因此,您应该了解如何存储第二个变量和运算符,并在以后尝试对其求值。下面的算法可能会有所帮助,请务必理解它是简单运算符的基础。

  1. 从2个堆栈,一个变量和一个运算符开始。
  2. 在将运算符压入运算符堆栈之前,请检查其优先级/优先级。如果现有运算符在堆栈顶部的优先级高于或等于您要推送的优先级,则开始弹出堆栈以显示运算符和变量。
  3. 最后,当您遍历整个字符串时,然后检查堆栈中是否有任何运算符,并对其求值,您应该得到最终结果。

在2 * 3 + 4中,当达到+时,一个堆栈中将有2,3个,而另一个堆栈中将有*。现在,在尝试推+时,您将看到优先级更高的堆栈中的*,因此将其弹出,因为其二进制运算符会弹出2个变量,构建一个表达式求值并将其压入变量堆栈(因为评估将是一个变量/数字),然后再次查看堆栈上是否还有其他具有更高优先级的运算符。

添加解决方案时,请切记: 1.运算符优先级/哪种运算符(一元/二元)/对称性/非对称性(+是对称但幂不是)将发挥重要作用。

尽量不要在循环内修改i变量,迟早会遇到麻烦。

给出的代码仅适用于给出的2个示例,并且检查优先级的部分为空白,但可以根据现有代码进行填充。

您需要修改变量命名loigc,以防出现诸如“ A2”之类的混合名称,您当前的逻辑将失败。

string tree = "AB-12-AC-14";

            Stack<BaseNode> TreeStack = new Stack<BaseNode>();
            Stack<BaseNode> TreeStack1 = new Stack<BaseNode>();
            BaseNode temp1 = new BaseNode();
            BaseNode temp2 = new BaseNode();
            for (int i = 0; i < tree.Length; i++)
            {
                VariableNode varNode = new VariableNode();
                NumericalNode numNode = new NumericalNode();
                if (CheckExpressions(tree[i])) // if the character is an operator
                {
                    OperatorNode expression = new OperatorNode(tree[i]);

                    //check priority should pass the current operator to really check for priority
                    if (CheckPriority() || TreeStack1.Count == 0)
                    {
                        TreeStack1.Push(expression);
                    }
                    else
                    {
                        // assuming binary operators only
                        temp1 = TreeStack.Pop();
                        temp2 = TreeStack.Pop();
                        expression.Right = temp1;
                        expression.Left = temp2;
                        TreeStack.Push(expression);
                        // need to check if there are more operators on stack1 are they higher priority then current operator
                        // if they are then pop them and apply them too
                    }
                }
                else if (!CheckExpressions(tree[i]))
                {
                    if (Char.IsLetter(tree[i]))
                    {
                        while (Char.IsLetter(tree[i])) // for the variable node
                        {
                            varNode.name += tree[i];
                            if (i + 1 == tree.Length)
                            {
                                break;
                            }
                            i++;
                        }
                        TreeStack.Push(varNode);
                        if (i + 1 != tree.Length)
                        {
                            i--;
                        }
                    }
                    else if (Char.IsDigit(tree[i])) // for constant value
                    {
                        int zero = 0; // for appending the numbers to combine them
                        while (i < tree.Length && Char.IsDigit(tree[i])) // need to check for length otherwise index will go out of bounds
                        {
                            if (zero == 0)
                            {
                                zero = tree[i] - '0';
                            }
                            else
                            {
                                zero = int.Parse(zero.ToString() + tree[i].ToString());
                            }
                            if (i < tree.Length)
                            {
                                i++;
                            }
                        }
                        if (i + 1 != tree.Length)
                        {
                            i--;
                        }
                        numNode.number = zero;
                        TreeStack.Push(numNode);
                    }
                }
            }

            // finish any remaining operators and push the final expression on the stack
            while (TreeStack1.Count!=0)
            {
                OperatorNode expression1 = new OperatorNode(((OperatorNode)TreeStack1.Pop()).v);
                expression1.Right = TreeStack.Pop();
                expression1.Left = TreeStack.Pop();
                TreeStack.Push(expression1);
            }