Infix To Prefix Notation

Last Updated : 15 Sep, 2025

Given a string s representing an infix expression ("operand1 operator operand2" ), Convert it into its prefix notation ("operator operand1 operand2").

Note: The precedence order is as follows: (^) has the highest precedence and is evaluated from right to left, (* and /) come next with left to right associativity, and (+ and -) have the lowest precedence with left to right associativity.

Examples: 

Input: s = "a*(b+c)/d"
Output: /*a+bcd 
Explanation: The infix expression is a*(b+c)/d. First, inside the brackets, b + c becomes +bc. Now the expression looks like a*(+bc)/d. Next, multiply a with (+bc), so it becomes *a+bc. Finally, divide this result by d, so it becomes /*a+bcd.

Try It Yourself
redirect icon

[Approach 1] Using Stack - O(n) Time and O(n) Space

The idea is to scan the expression from right to left, directly placing operands (a, b, c…) into the result as they appear. Operators (+, -, *, /, ^) are handled using a stack so that precedence and associativity are maintained.

How to Maintain Precedence and Associativity?

To maintain precedence and associativity, when a new operator appears, compare it with the operator on top of the stack. Pop operators from the stack if they have higher precedence, or if they have equal precedence and the new operator is right-associative (^). Left-associative operators (+, -, *, /) do not cause a pop. Push the new operator onto the stack. For parentheses (when scanning right to left), push ')' onto the stack, and when '(' is encountered, pop operators until a ')' is found.

At the end, pop all remaining operators from the stack and add them to the result. Finally, reverse the result to obtain the correct prefix expression.

C++
#include <iostream>
#include <stack>
#include <algorithm>
using namespace std;

// function to return precedence of operators
int precedence(char c) {
    if (c == '^') return 3;
    else if (c == '*' || c == '/') return 2;
    else if (c == '+' || c == '-') return 1;
    else return -1;
}

// function to check if operator is right-associative
bool isRightAssociative(char c) {
    return c == '^';
}

// function to check if a character is an operator
bool isOperator(char c) {
    return (c == '+' || c == '-' || c == '*' || c == '/' || c == '^');
}

// function to convert infix expression to prefix
string infixToPrefix(string s) {
    stack<char> st;
    string result = "";

    // scan from right to left
    for (int i = s.length() - 1; i >= 0; i--) {
        char c = s[i];

        if (isalnum(c)) {
            result += c;
        }
        else if (c == ')') {
            st.push(c);
        }
        else if (c == '(') {
            while (!st.empty() && st.top() != ')') {
                result += st.top();
                st.pop();
            }
            
            // pop ')'
            if (!st.empty()) st.pop(); 
        }
        else if (isOperator(c)) {
            while (!st.empty() && isOperator(st.top()) &&
                   (precedence(st.top()) > precedence(c) ||
                   (precedence(st.top()) == precedence(c) && isRightAssociative(c)))) {
                result += st.top();
                st.pop();
            }
            st.push(c);
        }
    }

    // pop remaining operators
    while (!st.empty()) {
        result += st.top();
        st.pop();
    }

    // reverse at the end to get correct prefix
    reverse(result.begin(), result.end());
    return result;
}

int main() {
    string s = "a*(b+c)/d";
    cout << infixToPrefix(s);
    return 0;
}
C
#include <stdio.h>
#include <string.h>
#include <ctype.h>
#include <stdlib.h>

// function to return precedence of operators
int precedence(char c) {
    if (c == '^') return 3;
    else if (c == '*' || c == '/') return 2;
    else if (c == '+' || c == '-') return 1;
    else return -1;
}

// function to check if operator is right-associative
int isRightAssociative(char c) {
    return c == '^';
}

// function to check if a character is an operator
int isOperator(char c) {
    return (c == '+' || c == '-' || c == '*' || c == '/' || c == '^');
}

// function to convert infix expression to prefix
char* infixToPrefix(char* s) {
    int n = strlen(s);
    char* result = (char*)malloc(n * 2); 
    int resIndex = 0;
    char st[100];
    int top = -1;

    // scan from right to left
    for (int i = n - 1; i >= 0; i--) {
        char c = s[i];

        if (isalnum(c)) {
            result[resIndex++] = c;
        }
        else if (c == ')') {
            st[++top] = c;
        }
        else if (c == '(') {
            while (top != -1 && st[top] != ')') {
                result[resIndex++] = st[top--];
            }
            if (top != -1) top--; // pop ')'
        }
        else if (isOperator(c)) {
            while (top != -1 && isOperator(st[top]) &&
                  (precedence(st[top]) > precedence(c) ||
                  (precedence(st[top]) == precedence(c) && isRightAssociative(c)))) {
                result[resIndex++] = st[top--];
            }
            st[++top] = c;
        }
    }

    // pop remaining operators
    while (top != -1) {
        result[resIndex++] = st[top--];
    }

    result[resIndex] = '\0';

    // reverse at the end to get correct prefix
    for (int i = 0; i < resIndex / 2; i++) {
        char temp = result[i];
        result[i] = result[resIndex - i - 1];
        result[resIndex - i - 1] = temp;
    }

    return result;
}

int main() {
    char s[] = "a*(b+c)/d";
    char* prefix = infixToPrefix(s);
    printf("%s\n", prefix);
    return 0;
}
Java
import java.util.Stack;

class GfG {
    
    // function to return precedence of operators
    static int precedence(char c) {
        if (c == '^') return 3;
        else if (c == '*' || c == '/') return 2;
        else if (c == '+' || c == '-') return 1;
        else return -1;
    }

    // function to check if operator is right-associative
    static boolean isRightAssociative(char c) {
        return c == '^';
    }

    // function to check if a character is an operator
    static boolean isOperator(char c) {
        return (c == '+' || c == '-' || c == '*' || c == '/' || c == '^');
    }

    // function to convert infix expression to prefix
    static String infixToPrefix(String s) {
        Stack<Character> st = new Stack<>();
        StringBuilder result = new StringBuilder();

        // scan from right to left
        for (int i = s.length() - 1; i >= 0; i--) {
            char c = s.charAt(i);

            if (Character.isLetterOrDigit(c)) {
                result.append(c);
            }
            else if (c == ')') {
                st.push(c);
            }
            else if (c == '(') {
                while (!st.isEmpty() && st.peek() != ')') {
                    result.append(st.pop());
                }
                if (!st.isEmpty()) st.pop();
            }
            else if (isOperator(c)) {
                while (!st.isEmpty() && isOperator(st.peek()) &&
                      (precedence(st.peek()) > precedence(c) ||
                      (precedence(st.peek()) == precedence(c) && isRightAssociative(c)))) {
                    result.append(st.pop());
                }
                st.push(c);
            }
        }

        // pop remaining operators
        while (!st.isEmpty()) {
            result.append(st.pop());
        }

        // reverse to get correct prefix
        return result.reverse().toString();
    }

    public static void main(String[] args) {
        String s = "a*(b+c)/d";
        System.out.println(infixToPrefix(s));
    }
}
Python
# function to return precedence of operators
def precedence(c):
    if c == '^':
        return 3
    elif c in ('*', '/'):
        return 2
    elif c in ('+', '-'):
        return 1
    else:
        return -1

# function to check if operator is right-associative
def isRightAssociative(c):
    return c == '^'

# function to check if a character is an operator
def isOperator(c):
    return c in "+-*/^"

# function to convert infix expression to prefix
def infixToPrefix(s):
    st = []
    result = []

    # scan from right to left
    for c in reversed(s):
        if c.isalnum():
            result.append(c)
        elif c == ')':
            st.append(c)
        elif c == '(':
            while st and st[-1] != ')':
                result.append(st.pop())
            if st:
                st.pop()  # remove ')'
        elif isOperator(c):
            while (st and isOperator(st[-1]) and
                  (precedence(st[-1]) > precedence(c) or
                  (precedence(st[-1]) == precedence(c) and isRightAssociative(c)))):
                result.append(st.pop())
            st.append(c)

    # pop remaining operators
    while st:
        result.append(st.pop())

    # reverse at the end to get correct prefix
    return ''.join(reversed(result))

if __name__ == "__main__":
    s = "a*(b+c)/d"
    print(infixToPrefix(s))
C#
using System;
using System.Collections.Generic;

class GFG {
    
    // function to return precedence of operators
    static int precedence(char c) {
        if (c == '^') return 3;
        else if (c == '*' || c == '/') return 2;
        else if (c == '+' || c == '-') return 1;
        else return -1;
    }

    // function to check if a character is an operator
    static bool isOperator(char c) {
        return (c == '+' || c == '-' || c == '*' || c == '/' || c == '^');
    }

    // function to check if operator is right-associative
    static bool isRightAssociative(char c) {
        return c == '^';
    }

    // function to convert infix expression to prefix
    static string infixToPrefix(string s) {
        Stack<char> st = new Stack<char>();
        string result = "";

        // scan from right to left
        for (int i = s.Length - 1; i >= 0; i--) {
            char c = s[i];

            if (Char.IsLetterOrDigit(c)) {
                result += c; 
            }
            else if (c == ')') {
                st.Push(c);
            }
            else if (c == '(') {
                while (st.Count > 0 && st.Peek() != ')') {
                    result += st.Pop();
                }
                if (st.Count > 0) st.Pop(); 
            }
            else if (isOperator(c)) {
                while (st.Count > 0 && isOperator(st.Peek()) &&
                      (precedence(st.Peek()) > precedence(c) ||
                      (precedence(st.Peek()) == precedence(c) && isRightAssociative(c)))) {
                    result += st.Pop();
                }
                st.Push(c);
            }
        }

        // pop remaining operators
        while (st.Count > 0) {
            result += st.Pop();
        }

        // reverse at the end to get correct prefix
        char[] arr = result.ToCharArray();
        Array.Reverse(arr);
        return new string(arr);
    }

    static void Main() {
        string s = "a*(b+c)/d";
        Console.WriteLine(infixToPrefix(s));
    }
}
JavaScript
// function to return precedence of operators
function precedence(c) {
    if (c === '^') return 3;
    else if (c === '*' || c === '/') return 2;
    else if (c === '+' || c === '-') return 1;
    else return -1;
}

// function to check if a character is an operator
function isOperator(c) {
    return ['+', '-', '*', '/', '^'].includes(c);
}

// function to check if operator is right-associative
function isRightAssociative(c) {
    return c === '^';
}

// function to convert infix expression to prefix
function infixToPrefix(s) {
    let st = [];
    let result = [];

    // scan from right to left
    for (let i = s.length - 1; i >= 0; i--) {
        let c = s[i];

        if (/[a-zA-Z0-9]/.test(c)) {
            result.push(c); 
        } else if (c === ')') {
            st.push(c);
        } else if (c === '(') {
            while (st.length > 0 && st[st.length - 1] !== ')') {
                result.push(st.pop());
            }
            if (st.length > 0) st.pop();
        } else if (isOperator(c)) {
            while (
                st.length > 0 &&
                isOperator(st[st.length - 1]) &&
                (precedence(st[st.length - 1]) > precedence(c) ||
                (precedence(st[st.length - 1]) === precedence(c) && isRightAssociative(c)))
            ) {
                result.push(st.pop());
            }
            st.push(c);
        }
    }

    // pop remaining operators
    while (st.length > 0) {
        result.push(st.pop());
    }

    // reverse to get correct prefix
    return result.reverse().join('');
}

// Driver Code
let s = "a*(b+c)/d";
console.log(infixToPrefix(s)); 

Output
/*a+bcd

[Approach 2] Using Postfix and Reverse Method - O(n) Time and O(n) Space

To convert an infix expression to prefix, we make use of the infix-to-postfix method with a small modification.

  • First, we reverse the infix expression because prefix is evaluated from right to left (opposite of postfix).
  • While reversing, we swap '(' with ')' to maintain the correct grouping of subexpressions.
  • Now, we convert this modified expression into postfix using the standard stack-based method.
  • Finally, we reverse the resulting postfix expression. This gives the correct prefix form.

Note: Prefix = reverse( postfix( reverse(infix) ) )

Illustration:

Convert infix expression to prefix expression
C++
#include <iostream>
#include <stack>
#include <algorithm>
#include <cctype>
using namespace std;

// function to return precedence of operators
int precedence(char c) {
    if (c == '^') return 3;
    else if (c == '*' || c == '/') return 2;
    else if (c == '+' || c == '-') return 1;
    else return -1;
}

// function to check if a character is an operator
bool isOperator(char c) {
    return (c == '+' || c == '-' || c == '*' || c == '/' || c == '^');
}

// Function to check if operator is right-associative
bool isRightAssociative(char op) {
    return (op == '^');
}

// function to convert infix to prefix
string infixToPrefix(string s) {
 
    // reverse the string
    reverse(s.begin(), s.end());

    // swap '(' and ')'
    for (int i = 0; i < (int)s.length(); i++) {
        if (s[i] == '(') s