All Exams Test series for 1 year @ ₹349 only
Question

What is the polish notation of the given infix expression?

(A * B + C)/(E – F * G)

The correct answer is

/+*ABC–E*FG

Converting Infix to Polish Notation (Prefix)

Converting an expression from infix notation to Polish notation (also known as prefix notation) involves rearranging the operators to come before their operands. This process requires understanding operator precedence and associativity.

The given infix expression is: \( (A * B + C)/(E - F * G) \)

Understanding Polish Notation

In Polish notation, operators precede the operands they act upon. For example, \(A + B\) in infix becomes \(+ A B\) in prefix, and \(A * B\) becomes \(* A B\).

Step-by-Step Conversion

To convert the given infix expression \( (A * B + C)/(E - F * G) \) to Polish notation, we follow the standard rules based on operator precedence and associativity. Operations within parentheses are evaluated first.

1. Identify the Main Operation

The main operator in the expression \( (A * B + C)/(E - F * G) \) is division (/). This operator acts on two main sub-expressions: the numerator \( (A * B + C) \) and the denominator \( (E - F * G) \).

In prefix notation, the main operator will come first, followed by the prefix forms of its operands. So, the structure will be \( / (\text{prefix of numerator}) (\text{prefix of denominator}) \).

2. Convert the Numerator \( (A * B + C) \) to Prefix

Consider the sub-expression \( A * B + C \). Inside the parentheses, we have operators \( * \) and \( + \).

  • Multiplication (\(*\)) has higher precedence than addition (\(+\)).
  • First, convert \( A * B \). In prefix, this is \(* A B\).
  • Now the expression is effectively \( (* A B + C) \).
  • Next, apply the addition operator \( + \) to its operands: \( (* A B) \) and \( C \).
  • In prefix notation, this becomes \( + (* A B) C \).

So, the prefix form of the numerator \( (A * B + C) \) is \( + * A B C \).

3. Convert the Denominator \( (E - F * G) \) to Prefix

Consider the sub-expression \( E - F * G \). Inside the parentheses, we have operators \( - \) and \( * \).

  • Multiplication (\(*\)) has higher precedence than subtraction (\(-\)).
  • First, convert \( F * G \). In prefix, this is \(* F G\).
  • Now the expression is effectively \( (E - (* F G)) \).
  • Next, apply the subtraction operator \( - \) to its operands: \( E \) and \( (* F G) \).
  • In prefix notation, this becomes \( - E (* F G) \).

So, the prefix form of the denominator \( (E - F * G) \) is \( - E * F G \).

4. Combine the Prefix Sub-expressions with the Main Operator

The structure is \( / (\text{prefix of numerator}) (\text{prefix of denominator}) \).

Substitute the results from steps 2 and 3:

\( / (+ * A B C) (- E * F G) \)

Removing the grouping parentheses used during conversion (they are implicit in prefix notation's structure), we get the final Polish notation:

\( / + * A B C - E * F G \)

Final Polish Notation

The Polish notation (prefix) of the given infix expression \( (A * B + C)/(E - F * G) \) is \( / + * A B C - E * F G \).

Infix Expression Part Operators Precedence/Order Prefix Conversion
\(A * B\) (inside numerator) * Highest in sub-expression \( * A B \)
\(+ C\) (with \(A*B\)) + Lower than * \( + (* A B) C \rightarrow + * A B C \)
Numerator \( (A * B + C) \) \( + * A B C \)
\(F * G\) (inside denominator) * Highest in sub-expression \( * F G \)
\(E - (* F G)\) (with \(F*G\)) - Lower than * \( - E (* F G) \rightarrow - E * F G \)
Denominator \( (E - F * G) \) \( - E * F G \)
Whole Expression \( (\text{Numerator}) / (\text{Denominator}) \) / Main operator \( / (\text{Numerator}) (\text{Denominator}) \rightarrow / (+ * A B C) (- E * F G) \rightarrow / + * A B C - E * F G \)

Matching with Options

Comparing the resulting prefix expression \( / + * A B C - E * F G \) with the provided options, we find that it matches option 2.

Option 2: \( /+*ABC–E*FG \)

Revision Table: Expression Notations

Notation Type Operator Position Example (A+B) Example (A*B+C)
Infix Between operands \(A + B\) \(A * B + C\)
Polish (Prefix) Before operands \(+ A B\) \(+ * A B C\)
Reverse Polish (Postfix) After operands \(A B +\) \(A B * C +\)

Additional Information: Operator Precedence for Prefix Conversion

When converting infix expressions to prefix notation, the order in which operators are processed is crucial. This order is determined by operator precedence rules, similar to evaluating infix expressions.

Common operator precedence (from highest to lowest):

  1. Parentheses \( ( ) \) - Innermost expressions are processed first.
  2. Exponentiation \( (\text{e.g., } \hat{}, **) \)
  3. Multiplication \( (*) \) and Division \( (/) \)
  4. Addition \( (+) \) and Subtraction \( (-) \)

For operators with the same precedence (like \( * \) and \( / \), or \( + \) and \( - \)), associativity rules apply. For prefix notation conversion, when processing operators of the same precedence *within* a subexpression, you might process them from right to left to build the prefix structure correctly, although the primary driver is the structure dictated by precedence and parentheses.

In the example \( E - F * G \), \( * \) is higher precedence than \( - \). So \( F * G \) is converted first (\( * F G \)), and then \( - \) is applied to \( E \) and \( (* F G) \) resulting in \( - E * F G \).

In the example \( A * B + C \), \( * \) is higher precedence than \( + \). So \( A * B \) is converted first (\( * A B \)), and then \( + \) is applied to \( (* A B) \) and \( C \) resulting in \( + * A B C \).

Was this answer helpful?

Important Questions from Stack

  1. Single array a[0: MAXSIZE - 1] is used to implement two stacks. The two stacks grow from apposite ends of the array. Variables top 1 and top 2 (top1 < top2) point to the location of the topmost element in each of the stacks if the space is to be used efficiently, The condition for "stack full" is____________.

  2. Match the following :
    (1) PUSH(a) STACK
    (2) FIFO(b) Stack insertion
    (3) POP(c) QUEUE
    (4) LIFO(d) Stack deletion
  3. What is the final value of the postfix expression BCDAD-+-+.
    where A=2, B=3, C=4, D=5.
  4. Consider the following operations performed on a stack of size 5 :
    Push (a); Pop() ; Push(b); Push(c); Pop();
    Push(d); Pop();Pop(); Push (e)
    Which of the following statements is correct ?
  5. Which of the following is not an inherent application of stack ?
Need Expert Advice?

Start Your Preparation with Prepp Mobile App

Download the app from Google Play & App Store
Download the app from Google Play & App Store
Prepp Mobile App