Infix to Prefix Converter | Interactive Step-By-Step Stack Tutorial (2024)

What is an Infix Expression?

An Infix Expression (or Infix Notation) is characterized by a math expression wherein the operators are placed between operands, (as in 2 + 3).

In the case of infix expressions, parentheses (or brackets) must be used to indicate the order in which the author wants the operations to be executed. Otherwise, the person solving the problem is left to apply an order of operation convention (PEMDAS, BODMAS, etc.) to solve the expression.

What is a Prefix Expression?

As the name implies, a Prefix Expression (or Prefix Notation, or Polish Notation) is characterized by a math expression wherein the operators are placed before their operands (2 + 3 infix becomes + 2 3 prefix).

Since each prefix operator is evaluated from right to left, this eliminates the need for parenthesis. This is why prefix expressions are easier for computer algorithms to evaluate than infix expressions.

How to Convert Infix to Prefix Using Stack

In case you're not familiar, a stack is a collection or list wherein the last element added to the stack is always the first element to be removed.

Using a stack to temporarily store operators is necessary because as we are evaluating each operator token of the infix expression from left to right, we can't instantly know an operator's right-hand operand. Therefore we need to temporarily add (push) an operator to the stack and only remove (pop) it from the stack and append it to output once we know its right operand.

So now that you know what a stack is and why it is used, the next order of business is to assign precedence and associativity to each operator type based on an order of operations convention (PEMDAS or BODMAS). The only exception is, in the stack method parentheses don't have precedence because they don't exist in a prefix expression. Instead, parentheses are used to isolate parts of the infix notation that are to be executed independently of the operators outside of them. Any nested parentheses are evaluated prior to evaluating their encompassing parentheses.

OperatorSymbolPrecedenceAssociativity
Exponent^4Right to Left
Multiplication*3Left to Right
Division/3Left to Right
Addition+2Left to Right
Subtraction-2Left to Right

Now that you know what a stack is and have assigned precedence and associativity to each operator, the following are the steps to converting infix to prefix using stack.

First, reverse the order of the infix expression. For example, if the infix expression is 4*3+(5/2), the reverse would be )2/5(+3*4.

Next, working from left to right on the reversed infix expression, one token at a time, determine whether or not the current token is an operand, an operator, or an opening or closing parenthesis.

If the token is an operand, append it to the output. Operands always appear in the same order in the output as they do in the infix expression.

If the token is a closing parenthesis, push it to the stack. This marks the beginning of an expression that should be evaluated separately.

If the token is an opening parenthesis, until a closing parenthesis is encountered at the top of the stack, pop each operator from the stack and append to the output. This marks the end of the operands and operators located within the current set of parentheses.

If the token is an operator and it has greater precedence than the operator on the top of the stack, push the token to the stack.

If the token is an operator and it has lower precedence than the operator on the top of the stack, until the operator on top of the stack has lower or equal precedence than the token, or until the stack is empty, pop each operator from the stack and append them to the output. Then push the current token to the stack.

If the token is an operator and it has the same precedence as the operator on the top of the stack, and the operator on top of the stack is left-to-right associative, push the token to the stack.

If the token is an operator and it has the same precedence as the operator on the top of the stack, but the operator on top of the stack is right-to-left associative, until the operator on top of the stack has lower or equal precedence than the token and is left-to-right associative, or until the stack is empty, pop each operator from the stack and append them to the output. Then push the current token to the stack.

Once all of the infix tokens have been evaluated, pop any remaining operators from the stack, one at a time, and append each of them to the output expression.

Finally, reverse the order of the output expression to get the prefix expression.

Infix to Prefix Conversion Examples

The following three infix-to-prefix examples each give a step-by-step illustration of how the rules stated in the previous section are applied on a character-by-character basis.

Example #1: A+B*C+D

Reverse Infix Expression:

D + C * B + A

Scan tokens one at a time from left to right.

CharacterStackOuput
DD

Since D is an operand, print to output.

++D

Since + is an operator, and the stack is empty, push it to the stack.

C+D C

Since C is an operand, print to output.

*+ *D C

Since * is an operator, and it has higher precedence than the + at the top of the stack, push * to the stack.

B+ *D C B

Since B is an operand, print to output.

++ +D C B *

Since + is an operator, and it has lower precedence than the * at the top of the stack, pop * from the stack and append it to the output, and then push + to the stack.

A+ +D C B * A

Since A is an operand, print to output.

Scanning complete.
Pop remaining 2 operators from stack, one at a time, and append to output.
+D C B * A +
D C B * A + +
Reverse final output.
Prefix:+ + A * B C D
Example #2: 5*(6^2-2)

Reverse Infix Expression:

) 2 - 2 ^ 6 ( * 5

Scan tokens one at a time from left to right.

CharacterStackOuput
))

Since ) is a closing parenthesis, push it to the stack.

2)2

Since 2 is an operand, print to output.

-) -2

Since - is an operator, and it has higher precedence than the ) at the top of the stack, push - to the stack.

2) -2 2

Since 2 is an operand, print to output.

^) - ^2 2

Since ^ is an operator, and it has higher precedence than the - at the top of the stack, push ^ to the stack.

6) - ^2 2 6

Since 6 is an operand, print to output.

(2 2 6 ^ -

Since ( is an opening parenthesis, pop from the stack until a closing parenthesis is encountered. So pop ^ from the stack and append it to the output, pop - from the stack and append it to the output, and then pop the closing parenthesis from the stack and discard both parenthesis.

**2 2 6 ^ -

Since * is an operator, and the stack is empty, push it to the stack.

5*2 2 6 ^ - 5

Since 5 is an operand, print to output.

Scanning complete.
Pop remaining operator from stack and append to output.
2 2 6 ^ - 5 *
Reverse final output.
Prefix:* 5 - ^ 6 2 2
Example #3: 2*(1+(4*(2+1)+3))

Reverse Infix Expression:

) ) 3 + ) 1 + 2 ( * 4 ( + 1 ( * 2

Scan tokens one at a time from left to right.

CharacterStackOuput
))

Since ) is a closing parenthesis, push it to the stack.

)) )

Since ) is a closing parenthesis, push it to the stack.

3) )3

Since 3 is an operand, print to output.

+) ) +3

Since + is an operator, and it has higher precedence than the ) at the top of the stack, push + to the stack.

)) ) + )3

Since ) is a closing parenthesis, push it to the stack.

1) ) + )3 1

Since 1 is an operand, print to output.

+) ) + ) +3 1

Since + is an operator, and it has higher precedence than the ) at the top of the stack, push + to the stack.

2) ) + ) +3 1 2

Since 2 is an operand, print to output.

() ) +3 1 2 +

Since ( is an opening parenthesis, pop from the stack until a closing parenthesis is encountered. So pop + from the stack and append it to the output, and then pop the closing parenthesis from the stack and discard both parenthesis.

*) ) + *3 1 2 +

Since * is an operator, and it has higher precedence than the + at the top of the stack, push * to the stack.

4) ) + *3 1 2 + 4

Since 4 is an operand, print to output.

()3 1 2 + 4 * +

Since ( is an opening parenthesis, pop from the stack until a closing parenthesis is encountered. So pop * from the stack and append it to the output, pop + from the stack and append it to the output, and then pop the closing parenthesis from the stack and discard both parenthesis.

+) +3 1 2 + 4 * +

Since + is an operator, and it has higher precedence than the ) at the top of the stack, push + to the stack.

1) +3 1 2 + 4 * + 1

Since 1 is an operand, print to output.

(3 1 2 + 4 * + 1 +

Since ( is an opening parenthesis, pop from the stack until a closing parenthesis is encountered. So pop + from the stack and append it to the output, and then pop the closing parenthesis from the stack and discard both parenthesis.

**3 1 2 + 4 * + 1 +

Since * is an operator, and the stack is empty, push it to the stack.

2*3 1 2 + 4 * + 1 + 2

Since 2 is an operand, print to output.

Scanning complete.
Pop remaining operator from stack and append to output.
3 1 2 + 4 * + 1 + 2 *
Reverse final output.
Prefix:* 2 + 1 + * 4 + 2 1 3

Back to CalculatorBack to CalculatorBack toInfix to Prefix converterBack up toInfix to Prefix Converter

Infix to Prefix Converter | Interactive Step-By-Step Stack Tutorial (2024)

References

Top Articles
Craigslist Motorcycles Stockton
47,000+ Esthetician jobs in United States
Fernald Gun And Knife Show
Time in Baltimore, Maryland, United States now
Devon Lannigan Obituary
Euro (EUR), aktuální kurzy měn
The Daily News Leader from Staunton, Virginia
Nwi Police Blotter
Boggle Brain Busters Bonus Answers
Klustron 9
27 Places With The Absolute Best Pizza In NYC
CSC error CS0006: Metadata file 'SonarAnalyzer.dll' could not be found
1TamilMV.prof: Exploring the latest in Tamil entertainment - Ninewall
Maxpreps Field Hockey
What Was D-Day Weegy
Cvs Learnet Modules
Palm Coast Permits Online
Aldine Isd Pay Scale 23-24
Gia_Divine
Today Was A Good Day With Lyrics
Routing Number For Radiant Credit Union
Belledelphine Telegram
Afni Collections
WPoS's Content - Page 34
Alternatieven - Acteamo - WebCatalog
Gncc Live Timing And Scoring
Club Keno Drawings
Halsted Bus Tracker
Have you seen this child? Caroline Victoria Teague
Kagtwt
Scioto Post News
Lucky Larry's Latina's
How to Watch the X Trilogy Starring Mia Goth in Chronological Order
Rise Meadville Reviews
How to Destroy Rule 34
Ket2 Schedule
Mckinley rugzak - Mode accessoires kopen? Ruime keuze
The Best Restaurants in Dublin - The MICHELIN Guide
Culver's of Whitewater, WI - W Main St
St Anthony Hospital Crown Point Visiting Hours
Craigslist Com Panama City Fl
SF bay area cars & trucks "chevrolet 50" - craigslist
Pekin Soccer Tournament
Is Ameriprise A Pyramid Scheme
Doe mee met ons loyaliteitsprogramma | Victoria Club
Huntsville Body Rubs
Aloha Kitchen Florence Menu
Canonnier Beachcomber Golf Resort & Spa (Pointe aux Canonniers): Alle Infos zum Hotel
Wood River, IL Homes for Sale & Real Estate
Craigslist Free Cats Near Me
Lorcin 380 10 Round Clip
Loss Payee And Lienholder Addresses And Contact Information Updated Daily Free List Bank Of America
Latest Posts
Article information

Author: Ouida Strosin DO

Last Updated:

Views: 5900

Rating: 4.6 / 5 (56 voted)

Reviews: 87% of readers found this page helpful

Author information

Name: Ouida Strosin DO

Birthday: 1995-04-27

Address: Suite 927 930 Kilback Radial, Candidaville, TN 87795

Phone: +8561498978366

Job: Legacy Manufacturing Specialist

Hobby: Singing, Mountain biking, Water sports, Water sports, Taxidermy, Polo, Pet

Introduction: My name is Ouida Strosin DO, I am a precious, combative, spotless, modern, spotless, beautiful, precious person who loves writing and wants to share my knowledge and understanding with you.