Convert the infix expression to prefix expression. Show all steps Infix expression को प्रीफिक्स एक्सप्रेशन में बदलिए | सभी चरण दिखाएं i) A + B - C * D + E / F - G + H ii) ( A + B ) * C + ( D - E ) * ( F - G )
Q.) Convert the infix expression to prefix expression. Show all steps
Infix expression को प्रीफिक्स एक्सप्रेशन में बदलिए | सभी चरण दिखाएं i) A + B - C * D + E / F - G + H ii) ( A + B ) * C + ( D - E ) * ( F - G )
Subject: Data StructuresConverting an infix expression to a prefix expression involves reordering the operators and operands in the expression so that the operator precedes its operands. This is also known as Polish notation. To convert an infix expression to prefix, we generally use the following steps:
- Reverse the infix expression.
- Replace '(' with ')' and vice versa.
- Convert the resulting expression to postfix.
- Reverse the postfix expression to get the prefix expression.
Let's apply these steps to the given infix expressions.
i) A + B - C * D + E / F - G + H
Step 1: Reverse the infix expression
H + G - F / E + D * C - B + A
Step 2: Replace '(' with ')' and vice versa
There are no parentheses in this expression, so we can skip this step.
Step 3: Convert the resulting expression to postfix
We will use the Shunting Yard algorithm to convert the reversed infix expression to postfix. The algorithm uses a stack to hold operators and outputs the operands immediately.
Current Input | Action | Output Queue | Operator Stack |
---|---|---|---|
H | Output operand | H | |
+ | Push operator | H | + |
G | Output operand | H G | + |
- | Pop stack to queue until an operator with less precedence is at the top or stack is empty. Push current operator. | H G + | - |
F | Output operand | H G + F | - |
/ | Pop stack to queue until an operator with less precedence is at the top or stack is empty. Push current operator. | H G + F - | / |
E | Output operand | H G + F - E | / |
+ | Pop stack to queue until an operator with less precedence is at the top or stack is empty. Push current operator. | H G + F - E / | + |
D | Output operand | H G + F - E / D | + |
* | Pop stack to queue until an operator with less precedence is at the top or stack is empty. Push current operator. | H G + F - E / D + | * |
C | Output operand | H G + F - E / D + C | * |
- | Pop stack to queue until an operator with less precedence is at the top or stack is empty. Push current operator. | H G + F - E / D + C * | - |
B | Output operand | H G + F - E / D + C * B | - |
+ | Pop stack to queue until an operator with less precedence is at the top or stack is empty. Push current operator. | H G + F - E / D + C * B - | + |
A | Output operand | H G + F - E / D + C * B - A | + |
Now, pop all operators from the stack and add them to the output queue.
Final postfix expression: H G + F - E / D + C * B - A +
Step 4: Reverse the postfix expression to get the prefix expression
+ A - B * C + D / E - F + G H
So, the prefix expression for A + B - C * D + E / F - G + H
is + A - B * C + D / E - F + G H
.
ii) ( A + B ) * C + ( D - E ) * ( F - G )
Step 1: Reverse the infix expression
) G - F ( * ) E - D ( + C * ) B + A (
Step 2: Replace '(' with ')' and vice versa
( G - F ) * ( E - D ) + C * ( B + A )
Step 3: Convert the resulting expression to postfix
We will again use the Shunting Yard algorithm.
Current Input | Action | Output Queue | Operator Stack |
---|---|---|---|
( | Push to stack | ( | |
G | Output operand | G | ( |
- | Push operator | G | ( - |
F | Output operand | G F | ( - |
) | Pop stack to queue until '(' is found. Discard the pair of parentheses. | G F - | |
* | Push operator | G F - | * |
( | Push to stack | G F - | * ( |
E | Output operand | G F - E | * ( |
- | Push operator | G F - E | * ( - |
D | Output operand | G F - E D | * ( - |
) | Pop stack to queue until '(' is found. Discard the pair of parentheses. | G F - E D - | * |
+ | Pop stack to queue until an operator with less precedence is at the top or stack is empty. Push current operator. | G F - E D - * | + |
C | Output operand | G F - E D - * C | + |
* | Push operator | G F - E D - * C | + * |
( | Push to stack | G F - E D - * C | + * ( |
B | Output operand | G F - E D - * C B | + * ( |
+ | Push operator | G F - E D - * C B | + * ( + |
A | Output operand | G F - E D - * C B A | + * ( + |
) | Pop stack to queue until '(' is found. Discard the pair of parentheses. | G F - E D - * C B A + | + * |
Now, pop all operators from the stack and add them to the output queue.
Final postfix expression: G F - E D - * C B A + * +
Step 4: Reverse the postfix expression to get the prefix expression
+ * + A B C * - D E - F G
So, the prefix expression for ( A + B ) * C + ( D - E ) * ( F - G )
is + * + A B C * - D E - F G
.