Data Structures and Algorithms Prof. Ajit A. Diwan Prof. Ganesh Ramakrishnan Prof. Deepak B. Phatak Department of Computer Science and Engineering IIT Bombay Session: Infix to Postfix Ajit A. Diwan, Ganesh Ramakrishnan, and Deepak B. Phatak, IIT Bombay
Infix to Postfix Expressions Operator is placed after all its operands No need of parentheses Examples Infix Expressions Postfix Expressions a + b a b + (a + b) * (a - b) a b + a b - * (a ^ b * (c + (d * e) - f ) ) / g a b ^ c d e * + f - * g / Ajit A. Diwan, Ganesh Ramakrishnan, and Deepak B. Phatak, IIT Bombay
Infix to Postfix using Stack Consider infix expression a+b (Input string) Postfix expression will be ab+ (Output string) We have 2 operands (a, b) and 1 operator (+) Use stack to facilitate the process Push operators on the stack based on some conditions Output String: Assemble operands and operators Ajit A. Diwan, Ganesh Ramakrishnan, and Deepak B. Phatak, IIT Bombay
Example 1: Implementation Infix Expression: a+b Push operator on stack Append operands and operator to the string Infix element Operator Stack Postfix String Comments a Append ‘a’ + Push ‘+’ b ab Append ‘b’ ab+ Append ‘+’ Note: Operator in red color denotes the element at the top of the stack Ajit A. Diwan, Ganesh Ramakrishnan, and Deepak B. Phatak, IIT Bombay
Example 2: Implementation Infix Expression: a+b*c Infix element Operator Stack Postfix String Comments a Append ‘a’ + Push ‘+’ b ab Append ‘b’ * +* Push ‘*’ c abc Append ‘c’ abc* Append ‘*’ abc*+ Append ‘+’ ‘*’ has higher precedence than ‘+’ Note: Operator in red color denotes the element at the top of the stack Ajit A. Diwan, Ganesh Ramakrishnan, and Deepak B. Phatak, IIT Bombay
Example 3: Implementation Infix Expression: a*b+c Infix element Operator Stack Postfix String Comments a Append ‘a’ * Push ‘*’ b ab Append ‘b’ + ab* Append ‘*’ Push ‘+’ c ab*c Append ‘c’ ab*c+ Append ‘+’ ‘+’ has lower precedence than ‘*’ Note: Operator in red color denotes the element at the top of the stack Ajit A. Diwan, Ganesh Ramakrishnan, and Deepak B. Phatak, IIT Bombay
Example 4: Implementation Infix Expression: (a^b*(c+(d*e-f))) Infix String Operator Stack Postfix String Comments ( Push ‘(‘ a Append ‘a’ ^ (^ Push ‘^’ b ab Append ‘b’ * ab^ Append ‘^’ (* Push ‘*’ (*( c ab^c Append ‘c’ Note: Operator in red color denotes the element at the top of the stack Ajit A. Diwan, Ganesh Ramakrishnan, and Deepak B. Phatak, IIT Bombay
Example 4: Implementation Infix Expression: (a^b*(c+(d*e-f))) Infix String Operator Stack Postfix String Comments + (*(+ ab^c Push ‘+’ ( (*(+( Push ‘(‘ d ab^cd Append ‘d’ * (*(+(* Push ‘*‘ e ab^cde Append ‘e’ - ab^cde* Append ‘*’ (*(+(- Push ‘-’ Note: Operator in red color denotes the element at the top of the stack Ajit A. Diwan, Ganesh Ramakrishnan, and Deepak B. Phatak, IIT Bombay
Example 4: Implementation Infix Expression: (a^b*(c+(d*e-f))) Infix String Operator Stack Postfix String Comments f (*(+(- ab^cde*f Append ‘f’ ) (*(+( ab^cde*f- Append ‘-’ (*(+ Pop ‘(‘ (*( ab^cde*f-+ Append ‘+’ (* ( ab^cde*f-+* Append ‘*’ Note: Operator in red color denotes the element at the top of the stack Ajit A. Diwan, Ganesh Ramakrishnan, and Deepak B. Phatak, IIT Bombay
References https://en.wikipedia.org/wiki/Stack_(abstract_data_type) http://csis.pace.edu/~wolf/CS122/infix-postfix.htm Ajit A. Diwan, Ganesh Ramakrishnan, and Deepak B. Phatak, IIT Bombay
Thank you Ajit A. Diwan, Ganesh Ramakrishnan, and Deepak B. Phatak, IIT Bombay