简体   繁体   English

postifx使用堆栈来固定二进制表达式树(括号)

[英]postifx to infix binary expression tree (parenthesis) using stack

I have a program (c++) I'm writing where I'm converting a postfix expression to infix 我有一个程序(c ++),我正在编写将后缀表达式转换为中缀的地方

Example Postfix: ab + cde + * * Converted to infix: ( a + b ) * ( c * ( d + e ) ) 后缀示例:ab + cde + * *转换为中缀:(a + b)*(c *(d + e))

You do this by traversing "inorderly" through the binary expression tree 您可以通过在二进制表达式树中“有序”遍历来完成此操作

I have a recursiving function that prints out the expression properly, but I can't quite figure out how to place the paranthesis in the correct place. 我有一个递归函数,可以正确打印出表达式,但是我不太清楚如何将寄生虫放置在正确的位置。 My best attempt was the result ( a + b ) * ( c * ( d + e //but I can't place the end parenthesis correctly without messing up other parts 我最好的尝试是结果(a + b)*(c *(d + e // //但是如果不弄乱其他部分就无法正确放置结束括号

This is the function that produced this result (and I'm pretty sure I need to rethink my strategy for consistent results: 这是产生此结果的函数(而且我很确定我需要重新考虑自己的策略以获得一致的结果:

//isHigher is a lamba function that checks for higher precedence operators (*, /)
//isoperator checks if its an operator (+, - , / * )
void BET::printInfixExpression(BinaryNode *n)
{
  if(n->left != NULL)
  {
    if(isOperator(n->left->element) && isHigher(n->element) && !isHigher(n->left->element))
      cout << "( ";
    if(isHigher(n->element) && !isOperator(n->left->element))
      cout << "( ";
    printInfixExpression(n->left);
  }
  if(isHigher(n->element) && isHigher(n->right->element))
    cout << ") ";
  cout << n->element << " ";
  if(isHigher(n->element) && isOperator(n->right->element) && !isHigher(n->right->element))
    cout << "( ";
  if(n->right != NULL)
  {
    printInfixExpression(n->right);
  }
}

This is the original function that outputs an infix with no parenthesis: 这是原始函数,输出不带括号的中缀:

void BET::printInfixExpression(BinaryNode *n)
{
  if(n->left != NULL)
  {
    printInfixExpression(n->left);
  }
  cout << n->element << " ";

  if(n->right != NULL)
  {
    printInfixExpression(n->right);
  }
}

So my problem is getting the parenthesis placed correctly. 所以我的问题是正确放置括号。 Any help would be greatly appreciated, It's the weekend so my TA's/teacher haven't gotten back to me. 任何帮助将不胜感激,这是周末,所以我的助教/老师没有回到我身边。

edit: Redundant parenthesis is not allowed. 编辑:多余的括号是不允许的。 It has to be appropriately placed when necessary but not when it isn't necessary. 必须在必要时适当放置它,而在不必要时则不必放置。

You don't need to know the operator precedence. 您不需要知道运算符的优先级。 It is already implicit in the postfix. 它已经隐含在后缀中。 You just need to put parentheses around every operand-operator-operand tuple as you output them. 输出它们时,只需在每个操作数-运算符-操作数元组的周围加上括号。 So for example, ab+ becomes (a+b), abc++ becomes (a+(b+c)). 因此,例如, ab+变为(a+b), abc++变为(a+(b+c)). So just output a "(" at the beginning of printInfixExpression() and a ")" at the end of it. 因此,只需在printInfixExpression()的开头输出一个“(”,并在其结尾输出一个“)”。 You will get redundant parentheses, but I don't see anything in your question about suppressing those. 您会得到多余的括号,但您的疑问中没有任何关于抑制括号的内容。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM