search
Log In
0 votes
606 views
What is time and space complexity to evaluate postfix expression ?
in DS 606 views

1 Answer

0 votes

Algorithm

1. Read all the symbols from left to right of the postfix expression

2. If the reading symbol is  $operand$ then push into the stack

3. If the reading symbol is  $operator$, then perform two pop operations, do the arithmetic operation and again pushed the resultant into the stack

4. finally, perform a pop operation and print the popped value as the final result.

Code: https://ide.geeksforgeeks.org/k906uWSOei

Time complexity of evaluation algorithm is $O(n)$ where n is the number of characters in input expression.

0
Can u explain how the time complexity is O(n) ?
0
there are n characters in input expression array and we  are scanning it from left to right, so the loop will run for n times

Related questions

1 vote
1 answer
1
925 views
How is ans 15 for this question Please can anybody solve this
asked Jul 15, 2017 in Programming Himanshu Goyal 925 views
1 vote
3 answers
2
1.6k views
Consider the following expression with infix notation A * B - (C + D) * (E / 5) ^ F What is the maximum height of the operator stack during conversion from infix to postfix ? a) 3 b) 4 c) 5 d) 6
asked Jan 28, 2016 in DS Tushar Shinde 1.6k views
1 vote
1 answer
3
0 votes
0 answers
4
227 views
Please convert it to postfix by using stack and explain in detail void (*bsd_signal(int sig, void (*func)(int)))(int);
asked Oct 20, 2018 in Programming Mk Utkarsh 227 views
...