Converting Infix to Postfix Notation Lecture Slides prepared by Absar Moeen ROOTS SCHOOL ISLAMABAD 1 Hierarchy of Operators This is the hierarchy of the operators – () brackets first – * or / that comes first from the left hand side is done first, precedence level is same – + or - that comes first from the left hand side is done first, precedence level is same Lecture Slides prepared by Absar Moeen ROOTS SCHOOL ISLAMABAD 2 Example X = (A + B) / (( C - D) * ( E + F)) Lecture Slides prepared by Absar Moeen ROOTS SCHOOL ISLAMABAD 3 • Number the operators according to the precedence of the equation 1. 2. 3. 4. 5. 6. – + + * / = This is the hierarchy of the operators for this equation Lecture Slides prepared by Absar Moeen ROOTS SCHOOL ISLAMABAD 4 6 1 5 2 4 3 • X = (A + B) / (( C - D) * ( E + F)) Lecture Slides prepared by Absar Moeen ROOTS SCHOOL ISLAMABAD 5 • Now we will put them in a binary tree, by following the rule – The last operator goes in the tree first (ROOT) – Then the left hand side of the operator goes on the left side of the root node and the right side of the operator goes on the right side of the node • If there is an operand then write it directly • If there is an expression then follow the same principal of insertion Lecture Slides prepared by Absar Moeen ROOTS SCHOOL ISLAMABAD 6 = X / + * A B - C + D E Lecture Slides prepared by Absar Moeen ROOTS SCHOOL ISLAMABAD F 7 6 X = 1 A + 5 B / 2 C - 4 D * 3 E Lecture Slides prepared by Absar Moeen ROOTS SCHOOL ISLAMABAD + F 8 • Now to get the post fix notation we are going to perform a post order traversal on the binary tree • Visit the left node • Visit the right node • Print the root node Lecture Slides prepared by Absar Moeen ROOTS SCHOOL ISLAMABAD 9 = X XAB+CD–EF+*/= / + A * B C + D E F Lecture Slides prepared by Absar Moeen ROOTS SCHOOL ISLAMABAD 10