The Gateway to Computer Science Excellence
First time here? Checkout the FAQ!
x
0 votes
394 views
What is time and space complexity to evaluate postfix expression ?
in DS by (19 points) | 394 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.

by Boss (10.5k points)
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

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
50,092 questions
55,256 answers
190,786 comments
86,052 users