1 votes 1 votes What is the complexity of evaluating an infix expression having n operators? Programming in C stack algorithms time-complexity + – bahirNaik asked Dec 11, 2015 bahirNaik 2.8k views answer comment Share Follow See all 0 reply Please log in or register to add a comment.
3 votes 3 votes It will be O(n) only. Method 1 : It takes O(n) time to convert a infix expression into the postfix expression and only one left to right scan is enough to compute the value of the expression which will take O(n) again. So totally its O(n). Reference : 1] http://geeksquiz.com/stack-set-2-infix-to-postfix/ & http://geeksquiz.com/stack-set-4-evaluation-postfix-expression/ 2] http://faculty.cs.niu.edu/~hutchins/csci241/eval.htm Method 2 : Alternative to method 1, you can also go for evaluation using two stacks, Operator stack & Operand stack. Check this for more reference : http://stackoverflow.com/questions/13421424/how-to-evaluate-an-infix-expression-in-just-one-scan-using-stacks Sandeep Singh answered Jan 22, 2016 Sandeep Singh comment Share Follow See all 0 reply Please log in or register to add a comment.
0 votes 0 votes O(n) Aspi R Osa answered Dec 13, 2015 Aspi R Osa comment Share Follow See 1 comment See all 1 1 comment reply bahirNaik commented Dec 14, 2015 reply Follow Share We do not use infix expression evaluation in compiler because its not linear.It should be more than O(n).Unlike postfix which evaluates in O(n),in infix scanning the expression back and forth is required,to handle associativity and precedance. 1 votes 1 votes Please log in or register to add a comment.