I am looking for an algorithm to convert a postfix expressions to infix expressions but with minimal parentheses.
I am asking it here after searching Google Stack Overflow. I only found a single answer to my question but it was in Java and I don't understand that language. I am looking for a algorithm but if you can give a JavaScript or Python (the only languages I understand) implementation I will be very thankful to you.
This is what I have been able to do on the base of my current understanding.
const postfixToInfix = RPN => {
let convert = RPN.replace(/\^/g,'**').split(/\s+/g).filter(el => !/\s+/.test(el) && el !== '')
let stack = []
let result = []
let friends = {"+" : ["+","-","*","/"],"-":[],"/":["*"],"*":["/","*"],"**":["+","-","*","/"]}
convert.forEach(symbol => {
if(!isNaN(parseFloat(symbol)) && isFinite(symbol)){
result.push(symbol)
}
else if (Object.keys(friends).includes(symbol)) {
a = result.pop()
b = result.pop()
if(stack.length !==0){
if(friends[symbol].includes(stack.pop())){
result.push(`${b} ${symbol} ${a}`)
stack.push(symbol)
}
else{
result.push(`(${b}) ${symbol} ${a}`)
stack.push(symbol)
}
}
else {result.push(`${b} ${symbol} ${a}`);stack.push(symbol)}
}
else throw `${symbol} is not a recognized symbol`
})
if(result.length === 1) return result.pop()
else throw `${RPN} is not a correct RPN`
}
But this code is giving unexpected results.