## Remove redundant parentheses from an arithmetic expression

This is an interview question, for which I did not find any satisfactory answers on stackoverflow or outside. Problem statement:

Given an arithmetic expression, remove redundant parentheses. E.g. ((a*b)+c) should become a*b+c

I can think of an obvious way of converting the infix expression to post fix and converting it back to infix - but is there a better way to do this?

A pair of parentheses is necessary if and only if they enclose an unparenthesized expression of the form X % X % ... % X where X are either parenthesized expressions or atoms, and % are binary operators, and if at least one of the operators % has lower precedence than an operator attached directly to the parenthesized expression on either side of it; or if it is the whole expression. So e.g. in

q * (a * b * c * d) + c

the surrounding operators are {+, *} and the lowest precedence operator inside the parentheses is *, so the parentheses are unnecessary. On the other hand, in

q * (a * b + c * d) + c

there is a lower precedence operator + inside the parentheses than the surrounding operator *, so they are necessary. However, in

z * q + (a * b + c * d) + c

the parentheses are not necessary because the outer * is not attached to the parenthesized expression.

Why this is true is that if all the operators inside an expression (X % X % ... % X) have higher priority than a surrounding operator, then the inner operators are anyway calculated out first even if the parentheses are removed.

So, you can check any pair of matching parentheses directly for redundancy by this algorithm:

Let L be operator immediately left of the left parenthesis, or nil Let R be operator immediately right of the right parenthesis, or nil If L is nil and R is nil: Redundant Else: Scan the unparenthesized operators between the parentheses Let X be the lowest priority operator If X has lower priority than L or R: Not redundant Else: Redundant

You can iterate this, removing redundant pairs until all remaining pairs are non-redundant.

Example:

((a * b) + c * (e + f))

(Processing pairs from left to right):

((a * b) + c * (e + f)) L = nil R = nil --> Redundant ^ ^ (a * b) + c * (e + f) L = nil R = nil --> Redundant ^ ^ L = nil R = + X = * --> Redundant a * b + c * (e + f) L = * R = nil X = + --> Not redundant ^ ^

Final result:

a * b + c * (e + f)

**Remove Unnecessary Parentheses in Math Expression,** Its a interview question. I have not been able to get any optimal solution. Can someone share the logic and code for this. (preferably in Java) Given a string of balanced expression, find if it contains a redundant parenthesis or not. A set of parenthesis are redundant if same sub-expression is surrounded by unnecessary or multiple brackets. Print ‘Yes’ if redundant else ‘No’. Note: Expression may contain ‘+‘, ‘*‘, ‘–‘ and ‘/‘ operators.

I just figured out an answer:

the premises are:

1. the expression has been tokenized 2. no syntax error 3. there are only binary operators

input:

list of the tokens, for example: (, (, a, *, b, ), +, c, )

output:

set of the redundant parentheses pairs (the orders of the pairs are not important), for example, 0, 8 1, 5

please be aware of that : the set is not unique, for instance, ((a+b))*c, we can remove outer parentheses or inner one, but the final expression is unique

the data structure:

a stack, each item records information in each parenthese pair the struct is: left_pa: records the position of the left parenthese min_op: records the operator in the parentheses with minimum priority left_op: records current operator

the algorithm

1.push one empty item in the stack 2.scan the token list 2.1 if the token is operand, ignore 2.2 if the token is operator, records the operator in the left_op, if min_op is nil, set the min_op = this operator, if the min_op is not nil, compare the min_op with this operator, set min_op as one of the two operators with less priority 2.3 if the token is left parenthese, push one item in the stack, with left_pa = position of the parenthese 2.4 if the token is right parenthese, 2.4.1 we have the pair of the parentheses(left_pa and the right parenthese) 2.4.2 pop the item 2.4.3 pre-read next token, if it is an operator, set it as right operator 2.4.4 compare min_op of the item with left_op and right operator (if any of them exists), we can easily get to know if the pair of the parentheses is redundant, and output it(if the min_op < any of left_op and right operator, the parentheses are necessary, if min_op = left_op, the parentheses are necessary, otherwise redundant) 2.4.5 if there is no left_op and no right operator(which also means min_op = nil) and the stack is not empty, set the min_op of top item as the min_op of the popped-up item

examples

example one

((a*b)+c)

after scanning to b, we have stack:

index left_pa min_op left_op 0 1 0 2 1 * * <-stack top

now we meet the first ')'(at pos 5), we pop the item

left_pa = 1 min_op = * left_op = *

and pre-read operator '+', since min_op priority '*' > '+', so the pair(1,5) is redundant, so output it. then scan till we meet last ')', at the moment, we have stack

index left_pa min_op left_op 0 1 0 + +

we pop this item(since we meet ')' at pos 8), and pre-read next operator, since there is no operator and at index 0, there is no left_op, so output the pair(0, 8)

example two

a*(b+c)

when we meet the ')', the stack is like:

index left_pa min_op left_op 0 * * 1 2 + +

now, we pop the item at index = 1, compare the min_op '+' with the left_op '*' at index 0, we can find out the '(',')' are necessary

**Expression contains redundant bracket or not,** How and when to remove parentheses? Enter a mathematical expression including parentheses and click on the button. If unnecessary or redundant parentheses are detected then the expression can be simplified (visually). The rules to remove the parentheses/braces depend on the precedence of mathematical operators. given a mathematical expression remove redundant parenthesis's for e.g (a+(b*c)) = a+b*c Logic/Solution:-We are going to solve this problem using Expression trees

This solutions works if the expression is a valid. We need mapping of the operators to priority values.

a. Traverse from two ends of the array to figure out matching parenthesis from both ends. Let the indexes be i and j respectively.

b. Now traverse from i to j and find out the lowest precedence operator which is not contained inside any parentheses.

c. Compare the priority of this operator with the operators to left of open parenthesis and right of closing parenthesis. If no such operator exists, treat its priority as -1. If the priority of the operator is higher than these two, remove the parenthesis at i and j.

d. Continue the steps a to c until i<=j.

**Given a mathematical expressio,** Problem statement: Given an arithmetic expression, remove redundant parentheses. E.g. ((a*b)+c) should become a*b+c I can think of an obvious way of I have been looking for a way to remove redundant parentheses from a given expression. eg ((a-3)-(b-5)) would become a-3-(b-5). I have read several times that converting to postfix and then back to infix removes redundant parentheses, but have not seen any algorithm or code that proves/shows how this can be done, all I can find is how converting to postfix removes the need for parentheses.

- Push one empty item in the stack
Scan the token list

2.1 if the token is operand, ignore.

2.2 if the token is operator, records the operator in the left_op, if min_op is nil, set the min_op = this operator, if the min_op is not nil, compare the min_op with this operator, set min_op as one of the two operators with less priority.

2.3 if the token is left parenthese, push one item in the stack, with left_pa = position of the parenthesis.

2.4 if the token is right parenthesis:

2.4.1 we have the pair of the parentheses(left_pa and the right parenthesis)

2.4.2 pop the item

2.4.3 pre-read next token, if it is an operator, set it as right operator

2.4.4 compare min_op of the item with left_op and right operator (if any of them exists), we can easily get to know if the pair of the parentheses is redundant, and output it(if the min_op < any of left_op and right operator, the parentheses are necessary, if min_op = left_op, the parentheses are necessary, otherwise redundant)

2.4.5 if there is no left_op and no right operator(which also means min_op = nil) and the stack is not empty, set the min_op of top item as the min_op of the popped-up item examples

**Ayush Anand: Removing redundant parenthesis from an expression,** A set of parenthesis are redundant if same sub-expression is surrounded by unnecessary or multiple brackets. Print 'Yes' if redundant else 'No'. Note: Expression String contains a long expression. Part of it is: '+ (G0* (G1-G2+G3))+ ( (G4* (G5+G6)))- ( ( (G7+G8))* ( (G9+G10+G11)))+'. The redundant parentheses could be removed and the expression should look like: '+G0* (G1-G2+G3)+G4* (G5+G6)- (G7+G8)* (G9+G10+G11)+'.

The below code is a straightforward solution, limited to `+-*/`

; if you want you can add them per your requirements.

#include <iostream> #include <stack> #include <set> using namespace std; int size; int loc; set<char> support; string parser(string input , int _loc){ string expi; set<char> op; loc = _loc; while(1){ if(input[loc] == '('){ expi += parser(input,loc+1); }else if(input[loc] == ')'){ if((input[loc+1] != '*') && (input[loc+1] != '/')){ return expi; }else{ if ((op.find('+') == op.end()) && (op.find('-') == op.end())){ return expi; }else{ return '('+expi+')'; } } }else{ char temp = input[loc]; expi=expi+temp; if(support.find(temp) != support.end()){ op.insert(temp); } } loc++; if(loc >= size){ break; } } return expi; } int main(){ support.insert('+'); support.insert('-'); support.insert('*'); support.insert('/'); string input("(((a)+((b*c)))+(d*(f*g)))"); //cin >> input; size = input.size(); cout<<parser(input,0); return 0; }

**Code Inspection: Remove redundant parentheses,** Given a mathematical expression, remove the redundant brackets from the /18400741/remove-redundant-parentheses-from-an-arithmetic-expression If the expression contains parentheses but no operators, there is a function here. Evaluate the function and go to step 5. Get operands and perform the operation. Remove used tokens from the vector and put the result in their place. Remove redundant parentheses. Join remaining tokens in the vector into the string and display the result on the screen.

**Removing redundant parentheses : javahelp,** given a mathematical expression remove redundant parenthesis's for e.g 2)build an expression tree out of the postfix equation 3)do a inorder I am looking for an unambiguous grammar for arithmetic expressions with no redundant parentheses. For example, parentheses are redundant in id+(id*id) , but not in (id+id)*id . context-free-grammar regexp-grammars

**Remove redundant parentheses - 두런두런,** In many cases, parentheses in expressions are optional. Sometimes they help you clarify precedence of operations — for example if ((a & b) | c) , sometimes, I recently noticed that ReSharper (2016.3) is now saying to remove redundant parentheses on C# statements that, I believe, would cause the intended behavior to change. Is this statement: private b

Removing redundant parentheses. I have been looking for a way to remove redundant parentheses from a given expression. eg ((a-3)-(b-5)) would become How to remove redundant {} from a nested list of lists? How to completely delete the head of a function expression; If you wish to strip the brackets from a single expression in a nontrivial case please consider Delete as described in my answer to the second referenced question above.