3.1k views
Compute the post fix equivalent of the following expression $3^*\log(x+1)-\frac{a}{2}$
in DS
retagged | 3.1k views
0
postfix of log(x+1)  will be ??
+1
it will be       x1+log
0
how ?
0
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
0

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

0
yes u can say
+2
just construct expression tree and do post order traversal.....no need to worry about priorities

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.
Example:->>
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 be evaluated

So, $x+1$ will become $x\;1+$

Now among $(*,/,\log,+,-)$ operators, $\log$ has highest priority as it is the only unary operator

So, $\log(x\;1+)$ will become ${x\;1+\log}$

Now suppose ${z= x1+\log}$ and we get  $3* z - a / 2$

$\implies 3z* a\;2/ -$

Now, substitute $z= x\;1+\log$ and we get

$\mathbf{3x\;1+\log* a\;2/ -}$ as answer.

by Boss (23.4k points)
edited by
+3
thanx a lot
0
@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
0
But in C lang, there is no exponentiation operator!
$3 x 1 + \log * a 2 / -$
by Boss (11.2k points)
0
my doubt is how to separate log(x+1)
+8
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.
+2
consider this:

1)  a++  : write postfix

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

by the expanding it .... fully

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/-

by Active (3.4k points)

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/-$

by Boss (14.8k points)
0
How to make the expression tree just by looking at the Infix expression.
0
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

by (139 points)
0
great..
+1 vote

Using Stack (Standard method)

by Boss (34.7k points)

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.

by Junior (677 points)

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:

3x1+log∗a2/−

by Active (1.5k points)

1