stacks in algorithems and data structure
DESCRIPTION
stacks in algorithems and data structureTRANSCRIPT
Oxford University Press © 2012Data Structures Using C++ by Dr Varsha Patil
Algorithms and Algorithms and Data Structures Data Structures
Lecture 5
2
Applications Of Stack
Convert infix expression to postfix and prefixexpressions
Evaluate the postfix expression Reverse a string Check well-formed (nested) parenthesis Reverse a string Process subprogram function calls Parse (analyze the structure) of computer programs Simulate recursion In computations like decimal to binary conversion In Backtracking algorithms (often used in
optimizations and in games)
3
Expression Evaluation And Conversion
The most frequent application of stacks is in evaluation of arithmetic expressions.
An arithmetic expression is made up of operands, operators, and delimiters.
When higher level programming language came into existence one of the major difficulty faced by computer scientists was to generate machine language instruction, which would properly evaluate any arithmetic expression.
4
To fix the order of evaluation, assign to each operator a priority
Because even though we write expression in parenthesis, still we have a question in mind, about whether to evaluate which one bracket first
Once the priorities are assigned then within any pairs of parenthesis we can understand that operators with highest priority are to be evaluated first
While evaluating expression usually the following operation precedence is used
Expression Evaluation
5
The following operators are written is in descending order of their precedence:
Exponentiation ^, Unary +, Unary –, and not ~Multiplication * and division /Addition + and subtraction –<, £ , =, ¹, ³, >ANDOR
6
The Operators and priorities
Data Structures in C++ by Dr. Varsha Patil Oxferd Univercity
7
Polish Notation and Expression Conversion
Polish Mathematician Han Lukasiewicz suggested a notation called pPolish notation, which gives two alternatives to represent an arithmetic expression, the notation are postfix and prefix notations
The fundamental property of Polish notation is that the order in which the operations are to be performed is determined by the positions of the operators and operands in the expression.
Hence the advantage is parenthesis is not required while writing expressions in pPolish notation
Oxford University Press © 2012Data Structures Using C++ by Dr Varsha Patil8
…The example expression in various forms-infix, prefix and postfix
The postfix expressions can be evaluated easily hence infix expression is converted into postfix expression using stack.
Oxford University Press © 2012Data Structures Using C++ by Dr Varsha Patil9
Need of prefix and postfix expressions
Evaluation of infix expression using computer needs proper code generation by compiler without any ambiguity and is difficult due to various aspects such as operators priority and associativityThis problem can be overcome by writing or converting infix to alternate notation such as prefix or postfixThe postfix and prefix expressions possess many advantages as listed
Oxford University Press © 2012Data Structures Using C++ by Dr Varsha Patil10
The need for parenthesis as in infix expression is overcome in postfix and prefix Notations
The priority of operators is no longer relevantOrder of evaluation depends on position of operator not on priority and
associativityThe expression evaluation process is much simpler than attempting a direct
evaluation from infix notation.
Oxford University Press © 2012Data Structures Using C++ by Dr Varsha Patil11
Postfix Expression Evaluation process
The postfix expression may be evaluated by making a left to right scan, stacking operands, and evaluating operators using as operands the correct number from the stack and again placing the result again onto stackThis evaluation process is much simpler than attempting a direct evaluation from infix notationProcess continues till stack is not empty or on occurrence of a character # which denotes the end of expression
Oxford University Press © 2012Data Structures Using C++ by Dr Varsha Patil12
Steps for manually converting expression from one notation to other
1)Initially fully parenthesize the given infix expression.2) Use operator precedence and associability rules for the same3) Now move all operators so that they replace their corresponding right parenthesis;4)Finally delete all parentheses and we get the postfix expression