The problem. Given a valid prefix expression — every operator written before its two operands — convert it to infix, wrapping every operation in brackets. Operands are single letters or digits, and the operators are + - * / ^. For example, *+AB-CD becomes ((A+B)*(C-D)).
Input: "*+AB-CD" Output: "((A+B)*(C-D))"
Input: "*-A/BC-/AKL" Output: "((A-(B/C))*((A/K)-L))"
Input: "A" Output: "A"In prefix, an operator is followed by its left operand and then its right operand — and each operand may itself be a whole expression. The Infix, prefix and postfix lesson showed how to evaluate prefix with a stack; converting is the same scan with strings instead of numbers.
Somewhere in a valid prefix expression, an operator is immediately followed by two plain operands — like +AB. That's an innermost operation: replace those three items with the single piece (A+B). The piece now acts as one operand. Repeat until one item is left.
function prefixToInfix(s) { const isOp = (t) => t.length === 1 && "+-*/^".includes(t); const items = s.split(""); // operators, operands and finished pieces while (items.length > 1) { for (let i = 0; i + 2 < items.length; i++) { // an operator followed by two finished operands is an innermost operation if (isOp(items[i]) && !isOp(items[i + 1]) && !isOp(items[i + 2])) { items.splice(i, 3, "(" + items[i + 1] + items[i] + items[i + 2] + ")"); break; // the list changed: start over } } } return items[0];}Time O(n²), space O(n). Each rewrite removes one operator, and each one needs a fresh scan from the start.
Prefix describes itself from the left: read one character. If it's an operand, that's a complete expression. If it's an operator, the next complete expression is its left operand and the one after that is its right operand — so call yourself twice. One left-to-right pass, no rescanning.
function prefixToInfix(s) { let i = 0; // the next character to read function build() { const c = s[i++]; if (!"+-*/^".includes(c)) return c; // an operand is complete on its own const left = build(); // the operator's first operand... const right = build(); // ...then its second return "(" + left + c + right + ")"; } return build();}Time O(n), space O(n) for the recursion. Every character is read once. The catch: a long lopsided expression like ++++… recurses once per operator, and very deep recursion can overflow the call stack (Python stops at about 1,000 levels by default).
Do the recursion's job with your own stack. Read the expression from the right: by the time you meet an operator, both of its operands have already been read and are sitting on the stack. Because we read backwards, the left operand is on top:
left, pop right, push "(" + left + op + right + ")".Each operator pops its two finished pieces and pushes one bigger piece.
Time O(n), space O(n). Each character is pushed once and each operator pops twice. Same cost as the recursion, but it can't overflow the call stack, and it's the template for every conversion. (Gluing strings together also copies characters; that cost is the same for every approach, so the comparison counts passes and stack operations.)
O(n²)Each rewrite removes one operator and needs a fresh scan.
O(n)The string being rewritten.
O(n)"A" stays "A" (no brackets)."^A^BC" → "(A^(B^C))"."--ABC" → "((A-B)-C)": the order of the two pops matters for -, / and ^."+12" → "(1+2)".Every character is read once.
O(n)One call per operator on the stack.
O(n)Each character is pushed once; each operator pops two.
O(n)The explicit stack, which can't overflow the call stack.