The Wayback Machine - https://web.archive.org/web/20240925102730/https://www.geeksforgeeks.org/infix-postfix-prefix-notation/
Open In App

Infix, Postfix and Prefix Expressions/Notations

Last Updated : 21 Mar, 2024
Summarize
Comments
Improve
Suggest changes
Like Article
Like
Save
Share
Report
News Follow

Mathematical formulas often involve complex expressions that require a clear understanding of the order of operations. To represent these expressions, we use different notations, each with its own advantages and disadvantages. In this article, we will explore three common expression notations: infix, prefix, and postfix.

Infix Expressions

Infix expressions are mathematical expressions where the operator is placed between its operands. This is the most common mathematical notation used by humans. For example, the expression “2 + 3” is an infix expression, where the operator “+” is placed between the operands “2” and “3”.

Infix notation is easy to read and understand for humans, but it can be difficult for computers to evaluate efficiently. This is because the order of operations must be taken into account, and parentheses can be used to override the default order of operations.

Common way of writing Infix expressions:

  • Infix notation is the notation that we are most familiar with. For example, the expression “2 + 3” is written in infix notation.
  • In infix notation, operators are placed between the operands they operate on. For example, in the expression “2 + 3”, the addition operator “+” is placed between the operands “2” and “3”.
  • Parentheses are used in infix notation to specify the order in which operations should be performed. For example, in the expression “(2 + 3) * 4”, the parentheses indicate that the addition operation should be performed before the multiplication operation.

Operator precedence rules:

Infix expressions follow operator precedence rules, which determine the order in which operators are evaluated. For example, multiplication and division have higher precedence than addition and subtraction. This means that in the expression “2 + 3 * 4”, the multiplication operation will be performed before the addition operation.

Here’s the table summarizing the operator precedence rules for common mathematical operators:

Operator Precedence
Parentheses () Highest
Exponents ^ High
Multiplication * Medium
Division / Medium
Addition + Low
Subtraction – Low

Evaluating Infix Expressions

Evaluating infix expressions requires additional processing to handle the order of operations and parentheses. First convert the infix expression to postfix notation. This can be done using a stack or a recursive algorithm. Then evaluate the postfix expression.

Advantages of Infix Expressions

  • More natural and easier to read and understand for humans.
  • Widely used and supported by most programming languages and calculators.

Disadvantages Infix Expressions

  • Requires parentheses to specify the order of operations.
  • Can be difficult to parse and evaluate efficiently.

Prefix Expressions (Polish Notation)

Prefix expressions are also known as Polish notation, are a mathematical notation where the operator precedes its operands. This differs from the more common infix notation, where the operator is placed between its operands.

In prefix notation, the operator is written first, followed by its operands. For example, the infix expression “a + b” would be written as “+ a b” in prefix notation.

Evaluating Prefix Expressions

Evaluating prefix expressions can be useful in certain scenarios, such as when dealing with expressions that have a large number of nested parentheses or when using a stack-based programming language.

Advantages of Prefix Expressions

  • No need for parentheses, as the operator always precedes its operands.
  • Easier to parse and evaluate using a stack-based algorithm.
  • Can be more efficient in certain situations, such as when dealing with expressions that have a large number of nested parentheses.

Disadvantages of Prefix Expressions

  • Can be difficult to read and understand for humans.
  • Not as commonly used as infix notation.

Postfix Expressions (Reverse Polish Notation)

Postfix expressions are also known as Reverse Polish Notation (RPN), are a mathematical notation where the operator follows its operands. This differs from the more common infix notation, where the operator is placed between its operands.

In postfix notation, operands are written first, followed by the operator. For example, the infix expression “5 + 2” would be written as “5 2 +” in postfix notation.

Evaluating Postfix Expressions (Reverse Polish Notation)

Evaluating postfix expressions can be useful in certain scenarios, such as when dealing with expressions that have a large number of nested parentheses or when using a stack-based programming language.

Advantages of Postfix Notation

  • Also eliminates the need for parentheses.
  • Easier to read and understand for humans.
  • More commonly used than prefix notation.

Disadvantages of Postfix Expressions

  • Requires a stack-based algorithm for evaluation.
  • Can be less efficient than prefix notation in certain situations.

Comparison of Infix, Prefix and Postfix Expressions

Let’s compare infix, prefix, and postfix notations across various criteria:

spect Infix Notation Prefix Notation (Polish Notation) Postfix Notation (Reverse Polish Notation)
Readability Human-readable Less human-readable, requires familiarity Less human-readable, requires familiarity
Operator Placement Between operands Before operands After operands
Parentheses Requirement Often required Not required Not required
Operator Precedence Tracking Required, parentheses determine precedence Not required, operators have fixed precedence Not required, operators have fixed precedence
Evaluation Method Left-to-right Right-to-left Left-to-right
Ambiguity Handling May require explicit use of parentheses Ambiguity rare, straightforward evaluation Ambiguity rare, straightforward evaluation
Unary Operator Handling Requires careful placement Simplified handling due to explicit notation Simplified handling due to explicit notation
Computer Efficiency Less efficient due to precedence tracking and parentheses handling More efficient due to fixed precedence and absence of parentheses More efficient due to fixed precedence and absence of parentheses
Usage Common in everyday arithmetic and mathematical notation Common in computer science and programming languages Common in computer science and programming languages

Frequently Asked Questions on Infix, Postfix and Prefix Expressions

1. What is an infix expression?

An infix expression is a mathematical expression in which the operators are placed between the operands.

2. What is a postfix expression?

A postfix expression is a mathematical expression in which the operators are placed after the operands.

3. What is a prefix expression?

A prefix expression is a mathematical expression in which the operators are placed before the operands.

4. How are infix expressions evaluated?

Infix expressions are evaluated by following the rules of operator precedence and using parentheses to specify the order of operations.

5. How are postfix expressions evaluated?

Postfix expressions are evaluated by scanning the expression from left to right and using a stack to keep track of operands and operators.

6. How are prefix expressions evaluated?

Prefix expressions are evaluated by scanning the expression from right to left and using a stack to keep track of operands and operators.

7. What are the advantages of postfix and prefix expressions over infix expressions?

Postfix and prefix expressions eliminate the need for parentheses to specify the order of operations and make it easier to evaluate expressions using algorithms.

8. Can infix expressions be converted to postfix or prefix expressions?

Yes, infix expressions can be converted to postfix or prefix expressions using algorithms such as the shunting yard algorithm for postfix conversion and the reverse polish notation for prefix conversion.

9. Are postfix and prefix expressions more efficient than infix expressions in terms of evaluation?

Postfix and prefix expressions are more efficient for evaluation as they are easier to parse and evaluate using algorithms that do not require parentheses or multiple passes through the expression.

10. When should I use infix, postfix, or prefix expressions in my programming code?

Infix expressions are commonly used for human readability, while postfix and prefix expressions are preferred for efficient evaluation in programming languages and mathematical computations.

Related Articles:



Similar Reads

Postfix to Infix
Infix expression: The expression of the form a op b. When an operator is in-between every pair of operands. Postfix expression: The expression of the form a b op. When an operator is followed for every pair of operands. Postfix notation, also known as reverse Polish notation, is a syntax for mathematical expressions in which the mathematical operat
7 min read
Infix to Postfix Converter using JavaScript
Postfix expressions are easier for a compiler to understand and evaluate. So this is a converter that converts infix expression to postfix expression using JavaScript. Pre-requisites: Stack operationInfix to Postfix conversionBasic JavaScript Approach: Button Convert call function InfixtoPostfix() and this function converts the given infix expressi
4 min read
Convert Infix expression to Postfix expression
Write a program to convert an Infix expression to Postfix form. Infix expression: The expression of the form "a operator b" (a + b) i.e., when an operator is in-between every pair of operands.Postfix expression: The expression of the form "a b operator" (ab+) i.e., When every pair of operands is followed by an operator. Examples: Input: A + B * C +
13 min read
Convert Infix To Prefix Notation
Given an infix expression, the task is to convert it to a prefix expression. Infix Expression: The expression of type a 'operator' b (a+b, where + is an operator) i.e., when the operator is between two operands. Prefix Expression: The expression of type 'operator' a b (+ab where + is an operator) i.e., when the operator is placed before the operand
12 min read
Infix to Prefix conversion using two stacks
Infix: An expression is called the Infix expression if the operator appears in between the operands in the expression. Simply of the form (operand1 operator operand2). Example : (A+B) * (C-D) Prefix: An expression is called the prefix expression if the operator appears in the expression before the operands. Simply of the form (operator operand1 ope
13 min read
Prefix to Infix Conversion
Infix : An expression is called the Infix expression if the operator appears in between the operands in the expression. Simply of the form (operand1 operator operand2). Example : (A+B) * (C-D) Prefix : An expression is called the prefix expression if the operator appears in the expression before the operands. Simply of the form (operator operand1 o
6 min read
Why do we need Prefix and Postfix notations?
Prefix Notation: Prefix notation is the notation in which operators are placed before the corresponding operands in the expression. Example: Infix notation: A + B Prefix notation: +AB Postfix Notation: Postfix notation is the notation in which operators are placed after the corresponding operands in the expression. Example: Infix notation: A + B Po
1 min read
Find original Array from given Array where each element is sum of prefix and postfix sum
Given an array arr[] of length N, where arr is derived from an array nums[] which is lost. Array arr[] is derived as: arr[i] = (nums[0] + nums[1] + ... + nums[i]) + (nums[i] + nums[i+1] + ... + nums[N-1]). The task is to find nums[] array of length N. Examples: Input: N = 4, arr[] = {9, 10, 11, 10}Output: {1, 2, 3, 2}Explanation: If nums[] = {1, 2,
10 min read
Postfix to Prefix Conversion
Postfix: An expression is called the postfix expression if the operator appears in the expression after the operands. Simply of the form (operand1 operand2 operator). Example : AB+CD-* (Infix : (A+B) * (C-D) ) Prefix : An expression is called the prefix expression if the operator appears in the expression before the operands. Simply of the form (op
7 min read
Prefix to Postfix Converter Online
Prefix to Postfix Calculator is a free online tool to calculate the postfix of a prefix notation. In this converter user has to put the prefix notation in the input box and postfix notation will be displayed as a result. Prefix Expression: An expression is called the prefix expression if the operator appears in the expression before the operands. S
2 min read
Prefix to Postfix Conversion
Given a Prefix expression, convert it into a Postfix expression. Conversion of Prefix expression directly to Postfix without going through the process of converting them first to Infix and then to Postfix is much better in terms of computation and better understanding the expression (Computers evaluate using Postfix expression). let's discuss about
6 min read
Online Postfix to Prefix Converter
Our Postfix to Prefix converter tool helps you convert an expression written in postfix notation (Reverse Polish Notation) to its equivalent prefix notation (Polish Notation). Convert your postfix expressions to prefix notation easily with this free online tool. How to Use Postfix to Prefix Converter?Step 1: Enter your postfix expression in the tex
2 min read
Check if count of substrings in S with string S1 as prefix and S2 as suffix is equal to that with S2 as prefix and S1 as suffix
Given three strings S, S1, and S2, the task is to check if the number of substrings that start and end with S1 and S2 is equal to the number of substrings that start and end with S2 and S1 or not. If found to be true, then print "Yes". Otherwise, print "No". Examples: Input: S = "helloworldworldhelloworld", S1 = "hello", S2 = "world"Output: NoExpla
8 min read
Maximum sum increasing subsequence from a prefix and a given element after prefix is must
Given an array of n positive integers, write a program to find the maximum sum of increasing subsequence from prefix till ith index and also including a given kth element which is after i, i.e., k > i. Examples : Input: arr[] = {1, 101, 2, 3, 100, 4, 5} i-th index = 4 (Element at 4th index is 100) K-th index = 6 (Element at 6th index is 5.) Outp
14 min read
Maximum prefix sum which is equal to suffix sum such that prefix and suffix do not overlap
Given an array arr[] of N Positive integers, the task is to find the largest prefix sum which is also the suffix sum and prefix and suffix do not overlap. Examples: Input: N = 5, arr = [1, 3, 2, 1, 4]Output: 4Explanation: consider prefix [1, 3] and suffix [4] which gives maximum prefix sum which is also suffix sum such that prefix and suffix do not
7 min read
Program to convert Infix notation to Expression Tree
Given a string representing infix notation. The task is to convert it to an expression tree.Expression Tree is a binary tree where the operands are represented by leaf nodes and operators are represented by intermediate nodes. No node can have a single child. Construction of Expression tree The algorithm follows a combination of shunting yard along
12 min read
Evaluation of Postfix Expression
Given a postfix expression, the task is to evaluate the postfix expression. Postfix expression: The expression of the form "a b operator" (ab+) i.e., when a pair of operands is followed by an operator. Examples: Input: str = "2 3 1 * + 9 -"Output: -4Explanation: If the expression is converted into an infix expression, it will be 2 + (3 * 1) - 9 = 5
15+ min read
Count of binary strings of length N having equal count of 0's and 1's and count of 1's ≥ count of 0's in each prefix substring
Given an integer N, the task is to find the number of possible binary strings of length N with an equal frequency of 0's and 1's in which frequency of 1's are greater or equal to the frequency of 0's in every prefix substring. Examples: Input: N = 2Output: 1Explanation:All possible binary strings of length 2 are {"00", "01", "10" and "11"}. Out of
7 min read
Longest prefix that contains same number of X and Y in an array
Content has been removed on Author's request.
1 min read
Longest palindromic string formed by concatenation of prefix and suffix of a string
Given string str, the task is to find the longest palindromic substring formed by the concatenation of the prefix and suffix of the given string str. Examples: Input: str = "rombobinnimor" Output: rominnimor Explanation: The concatenation of string "rombob"(prefix) and "mor"(suffix) is "rombobmor" which is a palindromic string. The concatenation of
11 min read
Maximum array sum with prefix and suffix multiplications with -1 allowed
Given N elements (both positive and negative). Find the maximum sum, provided that the first operation is to take some prefix of the sequence and multiply all numbers in this prefix by -1. The second operation is to take some suffix and multiply all numbers in it by -1. The chosen prefix and suffix may intersect. What is the maximum total sum of th
8 min read
String from prefix and suffix of given two strings
Given two strings a and b, form a new string of length l, from these strings by combining the prefix of string a and suffix of string b. Examples : Input : string a = remuneration string b = acquiesce length of pre/suffix(l) = 5 Output :remuniesce Input : adulation obstreperous 6 Output :adulatperous Approach : Get first l letters from string a, an
4 min read
Check if suffix and prefix of a string are palindromes
Given a string 's', the task is to check whether the string has both prefix and suffix substrings of length greater than 1 which are palindromes. Print 'YES' if the above condition is satisfied or 'NO' otherwise. Examples: Input : s = abartbb Output : YES Explanation : The string has prefix substring 'aba' and suffix substring 'bb' which are both p
8 min read
Index with Minimum sum of prefix and suffix sums in an Array
Given an array of integers. The task is to find the index [Tex]i [/Tex]in the array at which the value of prefixSum(i) + suffixSum(i) is minimum.Note: PrefixSum(i) = The sum of first i numbers of the array.SuffixSum(i) = the sum of last N - i + 1 numbers of the array.1-based indexing is considered for the array. That is an index of the first elemen
10 min read
Find index i such that prefix of S1 and suffix of S2 till i form a palindrome when concatenated
Given two strings A and B of equal lengths, the task is to find an index i such that A[0...i] and B[i+1...n-1] give a palindrome when concatenated together. If it is not possible to find such an index then print -1. Examples: Input: S1 = "abcdf", S2 = "sfgba" Output: 1 S1[0..1] = "ab", S2[2..n-1] = "gba" S1 + S2 = "abgba" which is a palindrome. Inp
12 min read
Find the longest sub-string which is prefix, suffix and also present inside the string
Given string str. The task is to find the longest sub-string which is a prefix, a suffix, and a sub-string of the given string, str. If no such string exists then print -1.Examples: Input: str = "fixprefixsuffix" Output: fix "fix" is a prefix, suffix and present inside in the string too.Input: str = "aaaa" "aa" is a prefix, suffix and present insid
10 min read
Find the longest sub-string which is prefix, suffix and also present inside the string | Set 2
Given string str. The task is to find the longest sub-string which is a prefix, a suffix and a sub-string of the given string, str. If no such string exists then print -1.Examples: Input: str = "geeksisforgeeksinplatformgeeks" Output: geeksInput: str = “fixprefixsuffix” Output: fix Note: The Set-1 of this article is attached here.Approach: The impl
9 min read
Maximize the sum of array after multiplying a prefix and suffix by -1
Given an array arr[] of length N, the task is to maximize the sum of all the elements of the array by performing the following operations at most once. Choose a prefix of the array and multiply all the elements by -1.Choose a suffix of the array and multiply all the elements by -1. Examples: Input: arr[] = {-1, -2, -3} Output: 6 Explanation: Operat
7 min read
Longest Palindrome in a String formed by concatenating its prefix and suffix
Given a string str consisting of lowercase English letters, the task is to find the longest palindromic string T which satisfies the following condition: T = p + m + s where p and s are the prefix and the suffix of the given string str respectively and the string m is either the prefix or suffix of the string str after removing both p and s from it
13 min read
Count pairs of indices having equal prefix and suffix sums
Given an array arr[] of length N, the task is to find the count of pairs of indices (i, j) (0-based indexing) such that prefix sum of the subarray {arr[0], ... arr[i]} is equal to the suffix sum of the subarray {arr[N - 1], ..., arr[j]} ( 0 ? i, j < N). Examples: Input: arr[] = {1, 2, 1, 1}Output: 3Explanation: The 3 possible pairs of indices ar
6 min read
Article Tags :