## Prefix-Postfix-Expression

Question 1 |

Choose the equivalent prefix form of the following expression (a + (b − c))* ((d − e)/(f + g − h))

* +a − bc /− de − +fgh | |

* +a −bc − /de − +fgh | |

* +a − bc /− ed + −fgh | |

* +ab − c /− ed + −fgh |

Question 1 Explanation:

→ An expression is called the prefix expression if the operator appears in the expression before the operands.

Question 2 |

**The infix expression A+(B–C)*D is correctly represented in prefix notation as**A+B−C∗D | |

+A∗−BCD | |

ABC−D∗+ | |

A+BC−D∗ |

Question 2 Explanation:

Given Expression = A + (B – C)* D

Prefix Notation:

A + (- B C) * D

A + (* - B C D)

+ A * - B C D

Question 3 |

Assume that the operators +, −, × are left associative and ^ is right associative. The order of precedence (from highest to lowest) is ^, ×, +, −. The postfix expression corresponding to the infix expression is
a + b × c − d ^ e ^ f

abc x + def ^ ^ − | |

abc x + de ^ f ^ − | |

ab + c × d − e^f^ | |

− + a × b c^^ def |

Question 3 Explanation:

The operators in the expression are +,x,- ^

First we will convert e^f ad ef^ (as highest precedence and right associativity) and later

d ^( e f ^ ) to def^^ and so on , you can find the same thing from the below steps.

__The postfix expression:__

a + b × c − ( d ^( e ^ f))

a + b × c − ( d ^( e f ^ ))

a + b × c − ( d e f ^ ^)

(a + (b × c)) − d e f ^ ^

(a + (b c x)) − d e f ^ ^

(a (b c x) +) − d e f ^ ^

(a b c x +) - (d e f ^ ^)

(a b c x +) - (d e f ^ ^)

a b c x + d e f ^ ^ -

Question 4 |

The expression 1 * 2 ^ 3 * 4 ^ 5 * 6 will be evaluated as

3230 | |

16230 | |

49152 | |

173458 |

Question 4 Explanation:

The expression consists of the following operators *, ^

Between * and ^ , operator ‘^’ is highest precedence so it will execute first.

The expression consists of more than one ‘^’ operator is presented then it will follow right to left associativity.

Multiplication operator associativity is left to right.

1 * 2 ^ 3 * 4 ^ 5 * 6 = 1 * (2 ^ 3)* (4 ^ 5) * 6

= 1 * 8 * 1024 * 6

= 49152

Between * and ^ , operator ‘^’ is highest precedence so it will execute first.

The expression consists of more than one ‘^’ operator is presented then it will follow right to left associativity.

Multiplication operator associativity is left to right.

1 * 2 ^ 3 * 4 ^ 5 * 6 = 1 * (2 ^ 3)* (4 ^ 5) * 6

= 1 * 8 * 1024 * 6

= 49152

Question 5 |

Consider the following postfix expression with single digit operands :

` 6 2 3 * / 4 2 * + 6 8 * -`

The top two elements of the stack after second * is evaluated, are :

6, 3 | |

8, 1 | |

8, 2 | |

6, 2 |

Question 5 Explanation:

For evaluating a postfix expression we start traversing from first element of the expression.

While traversing the expression if you find an element value then push it on the top of stack this way the top element of the stack will represent the second operand of an operation and the element presents after top element will represent the first operand of the operation.

While traversing the postfix expression if you find an operation symbol then pop 2 elements from the top of the stack and then after performing operation store it’s result on the top of stack.

Step 1:

Step 2:

Step 3:

Step 4:

So Pop top 2 elements from stack and save the result on the top of stack.

2*3=6

Step 5:

Again Repeat step-4.

6/6=1

Step 6:

Step 7:

Step 8:

Do what we did in step-4.

4*2 = 8

While traversing the expression if you find an element value then push it on the top of stack this way the top element of the stack will represent the second operand of an operation and the element presents after top element will represent the first operand of the operation.

While traversing the postfix expression if you find an operation symbol then pop 2 elements from the top of the stack and then after performing operation store it’s result on the top of stack.

Step 1:

Step 2:

Step 3:

Step 4:

So Pop top 2 elements from stack and save the result on the top of stack.

2*3=6

Step 5:

Again Repeat step-4.

6/6=1

Step 6:

Step 7:

Step 8:

Do what we did in step-4.

4*2 = 8

Question 6 |

Convert the following infix expression into its equivalent postfix expression (A + B^ D) / (E – F) + G

ABD^ + EF – / G+ | |

ABD + ^EF – / G+ | |

ABD + ^EF / – G+ | |

ABD^ + EF / – G+ |

Question 6 Explanation:

→ According to priority (or) infix expression we can write expression like this ((A + B^D) / (E – F)) + G

→ Actual tree structure is

→ Actual tree structure is

Question 7 |

consider the following postfix expression with single digit operands :

6 2 3 * / 4 2 * + 6 8 * -

The top two elements of the stack after second * is evaluated, are :

6 2 3 * / 4 2 * + 6 8 * -

The top two elements of the stack after second * is evaluated, are :

6, 3 | |

8, 1 | |

8, 2 | |

6, 2 |

Question 7 Explanation:

For evaluating a postfix expression we start traversing from first element of the expression

While traversing the expression if you find an element value then push it on the top of stack this way the top element of the stack will represent the second operand of an operation and the element presents after top element will represent the first operand of the operation. While traversing the postfix expression if you find an operation symbol then pop 2 elements from the top of the stack and then after performing operation store it’s result on the top of stack.

While traversing the expression if you find an element value then push it on the top of stack this way the top element of the stack will represent the second operand of an operation and the element presents after top element will represent the first operand of the operation. While traversing the postfix expression if you find an operation symbol then pop 2 elements from the top of the stack and then after performing operation store it’s result on the top of stack.

There are 7 questions to complete.