 Welcome to this course on data structures and algorithms. In this and the subsequent session, we shall examine how the post fix expression is actually evaluated. We have already seen the conversion of infix to post fix expression. Let us look at evaluation of the post fix expression. We have already seen this example in one of the earlier sessions. So, take the first example where the value of a is 5 and value of b is 3, let us say and the expression is a b plus a b minus star. So, we know how this is to be evaluated. a b plus will be evaluated as 5 3 plus a b minus will be evaluated as 5 3 minus. Each one of these sub expressions will result in values 8 and 2 respectively and then the last operator star will have to be applied to these two operands giving you the value 60. In exactly the same fashion, you can confirm that the evaluation of an expression a b raise to c d e star plus f minus star g slash which is a complex expression will be evaluated with these given values in this particular fashion. All that we are looking for is that given a post fix expression like this, we should be able to collect values for each of the named operands from the user and return the final value of the entire expression. So, let us look at this implementation. The evaluation of post fix expression is again facilitated by using a stack. How do we use a stack? Consider as an example the post fix expression a b c star plus which we treat as an input string. Let us assume that the values are simply 5 6 and 7 for the three operands a b and c. Now, we have three operands and two operators in this case. All that we do is we examine the input string character by character. When we examine each character, we find out whether the character is an operand or character is an operator. If it is an operand, we push its value on the stack. If it is an operator, we perform the operation on the top two elements of the stack which we observe on the stack and after performing that operation, we push the result back on the stack. We go to the next character. So, it is an extremely simple algorithm. Keep looking at every character. Keep pushing operands on to stack. The moment you come across an operator, take the two previous operands, perform that operation, push the result back on to the stack and carry on. Let us look at this example a plus b star c. The in fix expression will become a b c star plus in post fix as we have already seen. Let us assume values of 5 6 and 7. So, see what happens when this expression is evaluated as per the logic that we just saw. We examine the characters in sequence a comes first. We push the value 5 on to the stack which is the value of a. Next comes b. We push the value 6 on the stack. Next comes c. We push value 7 on the stack. So, far we have traversed the string examining the first three characters. Let us see what happens when we look at the subsequent characters. The next character we look at is star. Now, star is an operator. By our logic whenever we encounter an operator, we have to take the top two operands from the stack. Currently the stack contains three values. Values of a, b and c. Out of these we will take the top value which is 7 assign it to operand 2. Then we will take the value 6 and put it in operand 1. So, operand 2 and operand 1 are assigned the values of the two top operands that are available on the stack. Once this is done, we calculate the value 6 multiplied by 7. That means the multiply operation is now carried out giving us the result 42. This result is to be pushed on stack. Note that 5 was already on the stack. Now, 42 gets added to the stack. So, currently on my stack I have now two values 5 and 42. Let us look at further plus string that we will encounter next. When the plus string comes, remember we had 5 and 42 on the stack. The 42 value is pushed on operand 2 as before. The operand 1 contains an old value which is not to be used obviously, but in the next operation, we take the second operand from the stack and put it in operand 1. Now, we have operand 1 and operand 2 as 42 and 5. The result currently contains an old value, but when we perform the plus operation, this will be replaced by 47. This is the result of performing the operation plus. Since this is the result as per our logic, we push that result on the stack 47. Since there is no more character left in the postfix string, we conclude that this is the final result and we will be able to pull that result out of the stack and display it or print it. Here is another example A star B plus C in fix operation which becomes in postfix A B star C plus. Again we assume the same values. Here as we encounter A and B, we will push 5 and 6 onto the stack. Then we encounter star like we saw before. This operation is to be performed by the 2 operands on the stack. We have these 2 operands. They will go respectively to operand 2 and operand 1 and this operation will be performed resulting in a value 30. This multiplication result value 30 will now be pushed onto the stack. Note that on the stack, we have only this value 30 now, nothing else. That is because we have traversed up to this point. Let us go to the subsequent characters C plus. When C comes, it is an operand. So, we push it onto the stack. Then comes the operator plus. When the operator comes, we again apply the same logic. We have 2 operands on the stack. We get them 1 by 1 into operand 2 and operand 1 and perform the operation plus. The result will be 37. The result 37 will now be pushed onto the stack. Please note that when we assign value to operand 2 and operand 1 from the top of the stack, we have to pop that item out because we have already consumed it by processing it. Here is a more complex example. We will not go through the details. You can satisfy yourself in exactly the same style we have given the detailed explanation of what happens when the postfix string a, b raise to c, d, e star f minus plus star is input as the postfix string. These are the sample values. You can go through these operations. You will notice we are doing exactly the same thing. Whenever we see an operand, we push it onto the stack. Whenever we see an operator, we actually evaluate that operator and push the result on the stack. Again, we see an operand, we push it on the stack and so on. If we continue like this, these are the characters which are now considered being scanned and these characters when they come here, postfix element depending upon whether it is an operand in which case something gets pushed onto the stack and if it is an operator, something gets evaluated. At the end, we will have the value 848 as the result value on the stack which is actually the final value of the expression. In this session, we look at the evaluation of a postfix expression using a stack. We saw how easy the evaluation is. In fact, this is one of the reasons why we use the postfix notation in all computing structures. That is how computer evaluates all our expressions. In the next session, we will look at a computer program implementing this logic. Thank you.