c++ - 迭代器不可取消引用堆栈(表达式树)和循环无法正常工作

标签 c++ stack binary-tree

我正在尝试通过在 C++ 中使用树将中缀转换为后缀和前缀。我将中缀表达式传递给 createTree 函数,并在代码中使用 2 个堆栈节点(用于存储已从堆栈 s 中弹出的操作数和运算符)和堆栈 s(用于存储运算符)。当我输入具有相同优先级运算符的表达式或第一个运算符的优先级高于第二个运算符时,代码可以正常工作。例如:1*2/3 或 1/2+3。但是当我输入第一个运算符的优先级低于第二个运算符的表达式时,它会进行调试。例如:1+2/3。 我尝试调试它并发现 DEBUG_ERROR("iterator not dereferencable");我试图在互联网上找到所有与我以前从未使用过的 vector 相关的问题。 希望大家帮帮我

  node *CreateTree(string post)
{
node *temp, *op1 , *op2;
stack<node*> tree;
stack<char> s;
int i = 0;
while (!tree.empty())
{
    tree.pop();
}
for(i = 0; i<post.length();i++)
{
    if (post[i]==' ') continue;
    else if(IsOperand(post[i]))
    {
        node *temp = new node();
        temp->in = post[i];
        temp->left = NULL;
        temp->right = NULL;

        for(int j=i+1;j<post.length();j++)
        {
            if (IsOperand(post[j]))
            {
                temp->in+=post[j];
                i = j;
            }
            else break;
        }
        tree.push(temp);
    }
    else if(isOperator(post[i]))
    {
        while(!s.empty() && s.top()!='(' && s.top()!='{' && s.top()!='[' && !hasHigherPrecedence(post[i],s.top()))
       {
            node *op1;
            node *op2;
            op2 = tree.top();
            tree.pop();
            op1 = tree.top();
            tree.pop();
            node *temp = new node();
            temp->in = s.top();
            temp->left = op1;
            temp->right = op2;
            root = temp;
            s.pop();
            tree.push(root);
       }
        s.push(post[i]);
    }
    else if(post[i]=='(')
   {
       s.push(post[i]);
   }
   else if(post[i]==')')
   {
       while(s.top()!='('&&!s.empty())
       {
            node *op1;
            node *op2;
            op2 = tree.top();
            tree.pop();
            op1 = tree.top();
            tree.pop();
            node *temp = new node();
            temp->in = s.top();
            temp->left = op1;
            temp->right = op2;
            root = temp;
            s.pop();
            tree.push(root);
       }
       s.pop();
   }
   else if(post[i]=='[')
   {
       s.push(post[i]);
   }
   else if(post[i]==']')
   {
       while(s.top()!='['&&!s.empty())
       {
            node *op1;
            node *op2;
            op2 = tree.top();
            tree.pop();
            op1 = tree.top();
            tree.pop();
            node *temp = new node();
            temp->in = s.top();
            temp->left = op1;
            temp->right = op2;
            root = temp;
            s.pop();
            tree.push(root);
       }
       s.pop();
   }
   else if(post[i]=='{')
   {
       s.push(post[i]);
   }
   else if(post[i]=='}')
   {
       while(s.top()!='{'&&!s.empty())
       {
            node *op1;
            node *op2;
            op2 = tree.top();
            tree.pop();
            op1 = tree.top();
            tree.pop();
            node *temp = new node();
            temp->in = s.top();
            temp->left = op1;
            temp->right = op2;
            root = temp;
            s.pop();
            tree.push(root);
       }
       s.pop();
   }
   else cout<<post[i]<<" is not registered as operator or operand";
}
while (!s.empty())
{
    node *op1;
    node *op2;
    op2 = tree.top();
    tree.pop();
    op1 = tree.top();
    tree.pop();
    node *temp = new node();
    temp->in = s.top();
    temp->left = op1;
    temp->right = op2;
    root = temp;
    s.pop();
}
return root;
}

下面是上面代码中调用的函数

bool isOperator(char a)
{
      return(a=='+' || a=='*' || a=='^' || a=='/' ||
       a=='-' || a=='%')? true:false;
}

int getWeight(char op)
{
 int weight;
 switch(op)
{
    case '+' : case '-':
        weight = 1;
        break;
    case '*':
    case '/':
    case '%':
        weight = 2;
        break;
    case '^':
        weight = 3;
        break;

}
return weight;

}

bool hasHigherPrecedence(char a, char top)
{
   int aW = getWeight(a);
   int tW = getWeight(top);
   return (aW > tW)? true : false;
}

最佳答案

我找到了答案 我只是忘记插入 tree.push(root)

while (!s.empty())
{
    node *op1;
    node *op2;
    op2 = tree.top();
    tree.pop();
    op1 = tree.top();
    tree.pop();
    node *temp = new node();
    temp->in = s.top();
    temp->left = op1;
    temp->right = op2;
    root = temp;
    tree.push(root);//I forgot this
    s.pop();
}

关于c++ - 迭代器不可取消引用堆栈(表达式树)和循环无法正常工作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33734290/

相关文章:

c - 如何使用堆栈而不是递归?

c++ - 给定一组不同高度的堆栈,我如何选择所有可能的组合?

assembly - GDB ret "cannot access memory at address"

java - 找出任意两个节点之间的最长路径

c - 如何从二叉树中删除节点?

c++ - 在 C++ 中检查空循环

c++ - 返回指针、返回值或传入引用,哪个在C++中优雅?

c++ - 我如何用 C 中的按位或其他高效代码实现逻辑蕴涵?

c++ - 读取文件并将其写入字符 vector 时出现段错误

algorithm - 二叉搜索树改组和重置