简体   繁体   English

C ++二进制表达式树:如何打印带有适当括号的中缀表达式?

[英]C++ Binary Expression Tree: How do I print an infix expression with appropriate parentheses?

Right now I have this simple print algorithm that prints perfect parentheses. 现在,我有了这个简单的打印算法,可以打印出完美的括号。 The problem is, the parentheses aren't always necessary, and I need to figure out how to not print them when they aren't needed. 问题是,括号并不总是必需的,我需要弄清楚当不需要时不打印括号的方法。

My current function: 我当前的功能:

void printIn(Node* t){

if(t!= NULL) {
    if(isleafNode(t))
        cout << t->element;
    else {      
        cout << "(";
        printIn(t->left);
        cout << t->data;
        printIn(t->right);
        cout << ")";
        }
  }

The problem here is that some postfix expressions, such as 2 50 + 8 + can be printed in infix as 2 + 50 + 8 instad of ((2 + 50) + 8)) 这里的问题是某些后缀表达式,例如2 50 + 8 +可以在infix中显示为(((2 + 50)+ 8))的2 + 50 + 8 instad。

Here is a chart of postfix to infix how it should look. 这是一张后缀图表,用于固定它的外观。 Mine just adds parentheses around the whole outside, and to all addition no matter what. 我的只是在整个外部加上括号,无论如何都加上括号。

4 50 6 + +           4 + ( 50 + 6 )
4 50 + 6 +           4 + 50 + 6
4 50 + 6 2 * +       4 + 50 + 6 * 2
4 50 6 + + 2 *       ( 4 + ( 50 + 6 ) ) * 2
a b + c d e + * *    ( a + b ) * ( c * ( d + e ) )

Here is a chart of how mine look: 这是我的样子的图表:

4 50 6 + +           ( 4 + ( 50 + 6 ))
4 50 + 6 +           ( ( 4 + 50 ) + 6)
4 50 + 6 2 * +       ( ( 4 + 50 ) + ( 6 * 2 ) )
4 50 6 + + 2 *       ( ( 4 + ( 50 + 6 ) ) * 2 )
a b + c d e + * *    ( ( a + b) * ( c * ( d + e ) ) )

How can I fix my algorithm to eliminate extra parentheses? 如何修复算法以消除多余的括号? p Keep in mind I do have a getPrecedence(string) function that returns 1 for high precedence (* or /) and 0 for low precedence (+ or -) p请记住,我确实有一个getPrecedence(string)函数,对于高优先级(*或/),返回1;对于低优先级(+或-),返回0。

When printing an expression tree in infix form you only need to print parenthesis around sub-expressions (ie children) where the operator has a lower precedence than the operator of the main (ie parent) expression. 以infix形式打印表达式树时,您只需要在子表达式(即子项)周围打印括号,在子表达式中,运算符的优先级比主表达式(即父级)的运算符低。

As an example, take the following expression tree (in postfix notation) and its infix form. 例如,采用以下表达式树(后缀表示法)及其中缀形式。

4 5 6 + 7 * +        4 + (5 + 6) * 7

Note that a parenthesis is needed around 5 + 6 since the operator of the sub-expression 5 6 + has lower precedence than the operator of the main expression 5 6 + 7 * but it is not needed for the sub-expression 5 6 + 7 * since the operator has higher precedence than the operator of the main expression 4 5 6 + 7 * + 请注意,由于子表达式5 6 +的运算符的优先级低于主表达式5 6 + 7 *的运算符,因此需要在5 + 6左右加上括号,但对于子表达式5 6 + 7则不需要括号。 *由于运算符的优先级高于主表达式的运算符4 5 6 + 7 * +

Using this information it is easy to modify the algorithm in the question to avoid parenthesis when they are not needed. 使用此信息,很容易修改问题中的算法,从而在不需要它们时避免使用括号。 Note that since there are no parent pointers in the tree it is easier to make the parent check if any of the children needs parenthesis than to make the node put parenthesis around itself. 注意,由于树中没有父指针,因此使父节点检查是否有任何子节点需要括号比使节点在自身周围加上括号要容易得多。

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

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