
- Data Structures & Algorithms
- DSA - Home
- DSA - Overview
- DSA - Environment Setup
- Algorithm
- DSA - Algorithms Basics
- DSA - Asymptotic Analysis
- DSA - Greedy Algorithms
- DSA - Divide and Conquer
- DSA - Dynamic Programming
- Data Structures
- DSA - Data Structure Basics
- DSA - Array Data Structure
- Stack & Queue
- DSA - Stack
- DSA - Expression Parsing
- DSA - Queue
- Searching Techniques
- DSA - Linear Search
- DSA - Binary Search
- DSA - Interpolation Search
- DSA - Hash Table
- Sorting Techniques
- DSA - Sorting Algorithms
- DSA - Bubble Sort
- DSA - Insertion Sort
- DSA - Selection Sort
- DSA - Merge Sort
- DSA - Shell Sort
- DSA - Quick Sort
- Graph Data Structure
- DSA - Graph Data Structure
- DSA - Depth First Traversal
- DSA - Breadth First Traversal
- Tree Data Structure
- DSA - Tree Data Structure
- DSA - Tree Traversal
- DSA - Binary Search Tree
- DSA - AVL Tree
- DSA - Spanning Tree
- DSA - Heap
- DSA Useful Resources
- DSA - Questions and Answers
- DSA - Quick Guide
- DSA - Useful Resources
- DSA - Discussion
Convert Infix to Prefix Expression
To solve expressions by the computer, we can either convert it in postfix form or to the prefix form. Here we will see how infix expressions are converted to prefix form.
At first infix expression is reversed. Note that for reversing the opening and closing parenthesis will also be reversed.
for an example: The expression: A + B * (C - D)
after reversing the expression will be: ) D – C ( * B + A
so we need to convert opening parenthesis to closing parenthesis and vice versa.
After reversing, the expression is converted to postfix form by using infix to postfix algorithm. After that again the postfix expression is reversed to get the prefix expression.
Input and Output
Input: Infix Expression: x^y/(5*z)+2 Output: Prefix Form Is: +/^xy*5z2
Algorithm
infixToPrefix(infix)
Input − Infix expression to convert into prefix form.
Output − The prefix expression.
Begin reverse the infix expression for each character ch of reversed infix expression, do if ch = opening parenthesis, then convert ch to closing parenthesis else if ch = closing parenthesis, then convert ch to opening parenthesis done postfix := find transformed infix expression to postfix expression prefix := reverse recently calculated postfix form return prefix End
Example
#include<iostream> #include<stack> #include<locale> //for function isalnum() #include<algorithm> using namespace std; int preced(char ch) { if(ch == '+' || ch == '-') { return 1; //Precedence of + or - is 1 }else if(ch == '*' || ch == '/') { return 2; //Precedence of * or / is 2 }else if(ch == '^') { return 3; //Precedence of ^ is 3 }else { return 0; } } string inToPost(string infix) { stack<char> stk; stk.push('#'); //add some extra character to avoid underflow string postfix = ""; //initially the postfix string is empty string::iterator it; for(it = infix.begin(); it!=infix.end(); it++) { if(isalnum(char(*it))) postfix += *it; //add to postfix when character is letter or number else if(*it == '(') stk.push('('); else if(*it == '^') stk.push('^'); else if(*it == ')') { while(stk.top() != '#' && stk.top() != '(') { postfix += stk.top(); //store and pop until ( has found stk.pop(); } stk.pop(); //remove the '(' from stack }else { if(preced(*it) > preced(stk.top())) stk.push(*it); //push if precedence is high else { while(stk.top() != '#' && preced(*it) <= preced(stk.top())) { postfix += stk.top(); //store and pop until higher precedence is found stk.pop(); } stk.push(*it); } } } while(stk.top() != '#') { postfix += stk.top(); //store and pop until stack is not empty stk.pop(); } return postfix; } string inToPre(string infix) { string prefix; reverse(infix.begin(), infix.end()); //reverse the infix expression string::iterator it; for(it = infix.begin(); it != infix.end(); it++) { //reverse the parenthesis after reverse if(*it == '(') *it = ')'; else if(*it == ')') *it = '('; } prefix = inToPost(infix); //convert new reversed infix to postfix form. reverse(prefix.begin(), prefix.end()); //again reverse the result to get final prefix form return prefix; } int main() { string infix = "x^y/(5*z)+2"; cout << "Prefix Form Is: " << inToPre(infix) << endl; }
Output
Prefix Form Is: +/^xy*5z2
- Related Articles
- Convert Infix to Postfix Expression
- Prefix to Infix Conversion in C++
- C++ Program to Construct an Expression Tree for a given Prefix Expression
- Postfix to Infix in C++
- How to convert Regular expression to Finite Automata?
- How to create infix to postfix converter in JavaScript?
- Explain Arden’s theorem to convert DFA to Regular Expression
- Convert Ternary Expression to a Binary Tree in C++
- How to convert an object x to an expression string in Python?
- MySQL add “prefix” to every column?
- Prefix to Postfix Conversion in C++
- How to demonstrate Prefix Operator using C#?
- K Prefix in Python
- Python - Prefix sum list
- How to prefix string to a pandas series labels?

Advertisements