 Welcome to this session in the course on data structures and algorithms. Today, we will discuss in fix to post fix conversion of expressions. Later on, we shall see how such a conversion can be programmed. We recall that in any expression we shall have operators and operands. In this conversion, we will put operators after all its operands. There is no need of parenthesis as we already note. Here are some examples. a plus b will become a b plus. a plus b in parenthesis star a minus b in parenthesis will become a b plus a b minus star. We have already discussed this. What we need to now do is understand how we could convert given an string containing an infix expression and create a string containing a post fix expression. So, this is the procedure that we propose to follow using a stack. Consider this infix expression a plus b which is the input string. We know that the post fix expression should be a b plus which will be output string. In this particular case, we have two operands a and b and we have one operator plus. What we propose to do is we will use a stack to facilitate this process. The operation is very simple. Anytime we come across operators, we will push these operators on the stack. This will be done based on some conditions related to the priority of the operators. As far as operands are concerned, we will continue to append them to an output string appropriately. In fact, the output string will assemble not just operands but also operators in the proper sequence. So, let us look at this example. Infix expression is a plus b. We push operator on stack and append operands and operator to this string. Let us see one by one as characters of the input expression appear before us. First character is a which is an operand. We will put it onto the post-fix string. Then we get an operator plus. This will be pushed onto the operator stack. The post-fix string continues to have a. Then we get b. b is now appended to the post-fix string. There is nothing more coming in. Now we will look at the top of the stack and we will take that operator and append it to the post-fix string. In short, by this simple process of assembling a post-fix string whenever we get operands and looking at the top of the operator stack and appending those operators at the end, we will get the desired expression. Let us look at another example. a plus b star c. We have 2 operators and 3 operands. For the same process, we look at every character of the expression one by one. We see a here. We append it to the post-fix string. We see plus here. We push plus to the operator stack. We see b next. This b is appended to the post-fix string. Now we see star. We note that star has a higher precedence than plus which is the previous operator. Therefore, we push this star operator onto the stack. Then we see c. c is appended to the post-fix string. Since there are no more operands or operators, now we will look at the top of the stack and one by one. We will remove the operators and append them to the post-fix string. So, we append star followed by appending the plus. Note that star has a higher precedence than plus and that is why star came after plus in my stack. Let us look at another example where the infix expression is a star b plus c. Now here we follow exactly the same process. First operand we get. We append it to post-fix string. Then we get an operator. We push it onto the stack. Then we get an operand. We append that to the post-fix string. Now we get plus as an operator, but the plus has a lower precedence than star which was the previous operator on the stack. Therefore, what we do is we take the star out of the stack and append it to post-fix string and then push plus onto the stack. Finally, when we get c, we append c to the post-fix string and then we empty the stack. The operator there is plus. We append that plus to the post-fix string. So, a simple mechanism. The only precaution that we need to take is anytime a operator is encountered before pushing it onto the stack, we should examine its precedence with respect to the previous operator on the top of the stack. Let us look at a slightly more complex example where I have brackets. Please note that the parenthesis are also treated as operators. In fact, an opening parenthesis and a closing parenthesis together is equivalent to an operator because it defines a certain precedence of operation. Let us look at how this expression will be handled. We look at each character of the infix expression as usual. We look at opening parenthesis. We push it onto the operator stack. We look at a. We append it to post-fix string. We look at this operator. This operator is pushed onto the stack. We look at another operand b which is appended to post-fix string. We now come across star. You compare the precedence of star with the previous operator which is raised to. Since this has a higher priority, we take it out, append it to the post-fix string and then push star onto the stack. Once we encounter an opening parenthesis, we push it onto stack. We come across c. We just append it to the post-fix string. The blue color shows the characters of the string which are handled in this slide. Now, we will continue scanning the infix expression. Let us look at the subsequent characters that we will encounter. We will come across plus, this plus. This is an operator. Since there is no preceding operator on the stack, we will simply push it onto the stack. We come across opening parenthesis. We again push it onto the stack. Now we come across an operand b which we append to the post-fix string. Now we come across an operator star. There is no previous operator. So, we push it onto the stack. Next, we come across operand e which is appended to post-fix string. Now we come across an operator minus this one. Now when we come across the operator minus, as we stated earlier, we compare its precedence with the previous operator because there is one stored on that stack. Since the previous operator star has a higher precedence, we append this star to the post-fix string and pop it out of the stack and then push this minus operator onto the stack. We have come up to minus sign here. There are still several characters in the infix expression left. We will examine them on the next slide. We come across this f. f is an operand. So, we simply append it to the post-fix string. Now we come across the first closing bracket. When we come across the closing bracket, we append minus operator which was on the stack to the post-fix string. Now we look at the stack. Stack has an opening parenthesis. Please note that the opening parenthesis now matches this closing parenthesis which we have come across. Please note we never have to do anything with the closing parenthesis. It will never get pushed anywhere. It will simply disappear along with the corresponding opening parenthesis which is now popped out. We encounter the second closing bracket. We do the same thing. For the second closing parenthesis, we look at the stack. There is a plus operator. We append it to the post-fix string and pop off this opening parenthesis and forget this closing parenthesis. We again encountered the last closing parenthesis. We do the same thing. We look at the stack. There is an operator star. We append it to post-fix string. Now we come across the opening parenthesis which matches this closing parenthesis. We simply pop it off and forget this. Notice that at the end, stack contains nothing but my post-fix string contains the proper post-fix expression corresponding to the given infix expression. This is a simple mechanism. Those of you who are interested may want to read more about it. This particular reference has been already communicated to you in a previous slide. I am notifying one more interesting reference which is available at the website of the PACE University written by Professor Wolf. We are not sure whether this material is open source but it is publicly accessible on this site. Those of you who wish to know more about the infix post-fix kind of conversion might want to look at this. In conclusion, today we have seen how an infix expression can be converted to a post-fix expression using a stack as a facilitator. In the next session, we shall look at how a program can be written to implement the process that we have outlined here which is effectively an algorithm for such conversion. Thank you.