Unit 02 - Stack 2021-2022 Sem I
Unit 02 - Stack 2021-2022 Sem I
❖ Tree Traversal
▪ Traversing through Directories
Applications of Stack
❖ Recursion to Find
Factorial of a Number
int find_fact(unsigned int i)
{
if(i <= 1)
{
return 1;
}
return i * find_fact (i - 1);
}
Applications of Stack
❖ Evaluation of Arithmetic Expressions
▪ Operands are variables or constants
▪ Operators indicate the operations to be performed on the operands
▪ Operator Precedence and Operator Associativity
Applications of Stack
❖ Notations for Arithmetic Expressions
▪ Infix Notation : When operator appears in between the operands then
the expression is called as an infix expression. For Ex, A + B
▪ Postfix Notation : When operator appears after the operands then the
expression is called as a postfix expression. For Ex, AB+. It is also
termed as Reversed Polish Notation
Applications of Stack
❖ Why Prefix/Postfix Notations
▪ Infix notation is easy to understand/read for humans; but additional load of
operator precedence is associated with it. Prefix/Postfix notation is easier to
parse for a machine, and there is no question of operator precedence.
▪ Time Complexity
• Evaluation of Infix Notation → O(n^2)
• Infix to Postfix → O(n)
• Postfix Evaluation → O(n)
• O(n) + O(n) = O(n)
Applications of Stack
❖ Infix to
Postfix
Conversion
using Stack