recategorized
5,609 views

2 Answers

3 votes
3 votes

prefix expression = ∗ + 3 + 3 ↑ 3+ 3 3 3

=>         * + 3 + 3 ^ 3 ( 3+ 3)  3    // here  ^  -> power operator.

=>         * + 3 + 3 ^ 3 6 3

=>        * + 3 + 3 (3^6) 3

=>        * + 3 + 3 729 3

=>        * + 3 ( 3 + 729 ) 3

=>        * + 3 732 3

=>        * ( 3 + 732) 3

=>        * 735 3

=>         (735 ) * 3

=>         2205  ( ans- C)


Method to follow -

Incase of  postfix expression, we push the element on seeing operand in to the stack and on seeing operator we pop top two elements from the stack and again push (pop2( 2nd poped) <operator> pop1(first popped) back into the stack.

Just like postfix, apply the same logic incase of prefix too but, start from the last of prefix expression with one modification, on seeing an operator and  after doing 2 pop, we need to push here (1st pop <operator> 2nd pop). And rest everything is same as postfix. 

Here-  ∗ + 3 + 3 ↑ 3+ 3 3 3     // stack S = Empty.

push 3.

 ∗ + 3 + 3 ↑ 3+ 3 3 3          // S = 3

push 3

 ∗ + 3 + 3 ↑ 3+ 3 3 3         // S = 3 3

push 3         

∗ + 3 + 3 ↑ 3 + 3 3 3         // S = 3 3 3           

This time its an operator ( + ).

So, pop1=3, pop2 = 3, Now do push ( 3 + 3) => push (6)

∗ + 3 + 3 ↑ 3 + 3 3 3              // S = 6 3

push 3

∗ + 3 + 3 3+ 3 3 3              // S = 3 6 3

This time its an operator ( ^ ).

So, pop1=3, pop2 = 6, Now do push ( 3 ^ 6) => push (729)  // note here,  pop1 (3) < operator > pop2 (6).

∗ + 3 + 3 ↑ 3+ 3 3 3    // S = 729 3

push 3                        

∗ + 3 + 3 3+ 3 3 3      // S = 3 729 3

This time its an operator ( + ).

So, pop1=3, pop2 = 729, Now do push ( 3 + 729) => push (732)

∗ + 3 + 3 3+ 3 3 3    // S = 732 3

push 3                       

+ 3 + 3 3+ 3 3 3    // S=  3  732  3

This time its an operator ( + ).

So, pop1=3, pop2 = 732, Now do push ( 3 + 732) => push (735)

+ 3 + 3 3+ 3 3 3      // S = 725 3

This time its an operator ( * ).

So, pop1=735, pop2 = 3, Now do push ( 735 * 3) => push (2205)

Ans = 2205

edited by

Related questions

0 votes
0 votes
1 answer
2
makhdoom ghaya asked Sep 26, 2016
9,094 views
The runtime for traversing all the nodes of a binary search tree with $n$ nodes and printing them in an order is$O(\lg n)$$O(n \lg n)$ $O(n)$$O(n^{2})$
2 votes
2 votes
1 answer
4
makhdoom ghaya asked Sep 28, 2016
2,871 views
In Data mining, ______ is a method of incremental conceptual clustering.STRINGCOBWEBCORBAOLAD