The Gateway to Computer Science Excellence
First time here? Checkout the FAQ!
+20 votes
Compute the post fix equivalent of the following expression $3^*\log(x+1)-\frac{a}{2}$
asked in DS by Veteran (406k points)
retagged by | 3k views
postfix of log(x+1)  will be ??
it will be       x1+log
how ?
in uninary operator we do firstly takes the value i.e x+1 and then apply what opration we want here log so firstly x+1 convert into psotfix then applly log on it  other example is (a+1)++ for that we  firstly  convert a+1 into postfix  then apply operation increment

so unary opern of  log(x1+) == x1+log ??

yes u can say
just construct expression tree and do post order need to worry about priorities

9 Answers

+51 votes
Best answer
The priority of the operators follows the usual conventions:
  • The highest priority is assigned to unary operators (note that, in this context, a function such as sin  is considered a unary operator). All unary operators have the same priority.
  • Exponentiation has the second highest priority.
  • The third highest priority is assigned to the multiplication and division operators.
  • The lowest priority is given to the addition and subtraction operators. 
Infix expression:   $3  * \log(  10  )$
Postfix expression:
$=3  *   (10 \log)$   //(Priority of unary operator log forces $\log( 10 )$ to evaluate first.)
$=3  \ 10  \log  *$

Now For our case 

$3*\log(x + 1) - a / 2$

first content inside parenthesis will evaluate 

so $x+1$ will become $x1+$

now among (*,/,log,+,-)operators ..log has highest priority due to unary opeartor

So, $\log(x1+)$ will become $\mathbf{x1+\log}$

now suppose $\mathbf{z= x1+\log}$ 

so convert this   $3* z - a / 2$  to postfix

$3z* a2/ -$

now substitue $z= x1+\log$

$\mathbf{3x1+log*  a2/  -}$ Ans.

answered by Boss (23.4k points)
edited by
thanx a lot
@rajesh when we evaluate the parentheses we got x1+. So this became one operand. Now as you told that log has higher priority. To evaluate it we need two operands but we have only 1 operand i.e. (x1+)  so how log(x1+) became x1+log.

Plss clarify
But in C lang, there is no exponentiation operator!
+13 votes
$3 x 1 + \log * a 2 / -$
answered by Boss (11.4k points)
my doubt is how to separate log(x+1)
log is a unary operator here, so when we see log in postfix expression, we pop only top element of stack, which is x+1, and apply log on that.
consider this:

1)  a++  : write postfix

2) ++a + b++  : write postfix
i m not getting it ..... can you please explain me ...


by the expanding it .... fully
+9 votes

We can do directly(manually) =>

3*log(x + 1) - a / 2

= ((3*(log(x + 1))) - (a / 2))

= ((3*log(x1+))) - (a2/))

= ((3*x1+log) - (a2/))

= ((3x1+log*) - (a2/))

= 3x1+log*a2/- 

answered by Active (3.4k points)
+5 votes

Given infix expression : $3*log(x+1)- \frac{a}{2}$

Expression tree :

Do a postorder traversal of the above tree to get postfix expression: $3x1+log*a2/-$

answered by Boss (14.6k points)
How to make the expression tree just by looking at the Infix expression.
this expression tree is wrong correct expression tree should be:
 expression tree just do postorder evaluation of this to get the amswer.    
          /    \
         *      /
        /  \    /  \
       3   log a  2
            /   \
                /  \
               x    1
+1 vote

Make the tree for the expression and then do postorder traversal ..smiley

answered by Active (2.2k points)
+1 vote

Using Stack (Standard method)

answered by Boss (34.5k points)
0 votes

Answer attached

answered by (129 points)
0 votes

Why to go for long description, draw tree and enjoy what you want prefix, infix postfix or even converse pre, in and post.


Once go from my ans.

answered by Junior (667 points)
0 votes

we know that in infix expression, operator that is being evaluated at last is root.

Therefore,  3∗log(x+1)−a/2 infix expression can be shown in tree as-

- is evaluated at last here, so we can write it at first in tree, hence the root.

Similarly for other operators like

(x+1) is evaluated at very first therefore we can write it at last in the below tree.


Now traverse it in postfix fashion like Left Right Root, we can get the following postfix expression as:



answered by Active (1.4k points)

Related questions

Quick search syntax
tags tag:apple
author user:martin
title title:apple
content content:apple
exclude -tag:apple
force match +apple
views views:100
score score:10
answers answers:2
is accepted isaccepted:true
is closed isclosed:true
49,541 questions
54,083 answers
70,992 users