Prefix-Postfix-Expression

Question 1
Choose the equivalent prefix form of the following expression (a + (b − c))* ((d − e)/(f + g − h))
A
* +a − bc /− de − +fgh
B
* +a −bc − /de − +fgh
C
* +a − bc /− ed + −fgh
D
* +ab − c /− ed + −fgh
       Data-Structures       Prefix-Postfix-Expression       ISRO-2017 May
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
A+B−C∗D
B
+A∗−BCD
C
ABC−D∗+
D
A+BC−D∗
       Data-Structures       Prefix-Postfix-Expression       ISRO CS 2009
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  
A
abc x + def ^ ^ −
B
abc x + de ^ f ^ −
C
ab + c × d − e^f^
D
− + a × b c^^ def
       Data-Structures       Prefix-Postfix-Expression       ISRO CS 2009
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
A
3230
B
16230
C
49152
D
173458
       Data-Structures       Prefix-Postfix-Expression       ISRO CS 2009
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
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 :

A
6, 3
B
8, 1
C
8, 2
D
6, 2
       Data-Structures       Prefix-Postfix-Expression       UGC-NET DEC Paper-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
Question 6
Convert the following infix expression into its equivalent postfix expression (A + B^ D) / (E – F) + G
A
ABD^ + EF – / G+
B
ABD + ^EF – / G+
C
ABD + ^EF / – G+
D
ABD^ + EF / – G+
       Data-Structures       Prefix-Postfix-Expression       UGC NET CS 2014 Dec-Paper-2
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
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 :
A
6, 3
B
8, 1
C
8, 2
D
6, 2
       Data-Structures       Prefix-Postfix-Expression       UGC NET CS 2018-DEC Paper-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.


There are 7 questions to complete.
PHP Code Snippets Powered By : XYZScripts.com