1 00:00:00,000 --> 00:00:07,566 [No Audio] 2 00:00:07,567 --> 00:00:09,900 Hello and welcome back again in the course. 3 00:00:09,901 --> 00:00:12,366 In the previous tutorial, we looked at how to 4 00:00:12,367 --> 00:00:15,133 convert a given expression to a postfix 5 00:00:15,166 --> 00:00:17,900 expression. In this tutorial now, we will be 6 00:00:17,901 --> 00:00:20,400 looking at how to evaluate the final result 7 00:00:20,401 --> 00:00:24,100 from a postfix expression. Once we understand 8 00:00:24,101 --> 00:00:26,266 the process, we will be in a better position 9 00:00:26,267 --> 00:00:28,900 to implement the underlying algorithm in the 10 00:00:28,933 --> 00:00:32,400 upcoming tutorial. As announced already, this 11 00:00:32,401 --> 00:00:35,366 tutorial will be comparatively short. There 12 00:00:35,367 --> 00:00:38,666 are two basic and simple rules that we need 13 00:00:38,667 --> 00:00:40,900 to remember for converting a postfix 14 00:00:40,901 --> 00:00:43,800 expression into an infix expression. 15 00:00:44,066 --> 00:00:47,266 Again, we will be scanning a given expression from 16 00:00:47,267 --> 00:00:49,633 left to right, and while doing so, the first 17 00:00:49,634 --> 00:00:54,033 rule is that, if we encounter an operand, then 18 00:00:54,066 --> 00:00:57,633 simply push it onto the stack. Let me write 19 00:00:57,634 --> 00:01:00,000 this rule, so that we can easily remember it. 20 00:01:00,166 --> 00:01:03,200 I will write if operand then push. 21 00:01:03,201 --> 00:01:06,066 [No Audio] 22 00:01:06,067 --> 00:01:08,866 The second rule is that, if we encounter an 23 00:01:08,867 --> 00:01:11,666 operation, then pop up two elements from the 24 00:01:11,667 --> 00:01:14,833 stack and perform the operations, and then 25 00:01:14,834 --> 00:01:17,066 push the result back onto the stack. 26 00:01:17,666 --> 00:01:21,600 Let me write this rule also. I will write if operation 27 00:01:21,700 --> 00:01:24,566 then pop two elements and perform operation 28 00:01:24,567 --> 00:01:25,841 and then push. 29 00:01:25,842 --> 00:01:30,738 [No Audio] 30 00:01:30,739 --> 00:01:32,000 Now, let us apply the two 31 00:01:32,001 --> 00:01:34,633 rules on the expression that we have obtained 32 00:01:34,634 --> 00:01:37,300 in postfix form from the previous tutorial. 33 00:01:38,400 --> 00:01:41,066 I have written the expression already, and now 34 00:01:41,067 --> 00:01:43,266 we will be scanning this expression from left 35 00:01:43,267 --> 00:01:45,133 to right, and we'll apply the two rules on 36 00:01:45,134 --> 00:01:47,866 each symbol that we encountered to evaluate 37 00:01:47,867 --> 00:01:51,000 the final result. For ease of work, I have 38 00:01:51,001 --> 00:01:52,833 already made a table with two columns 39 00:01:52,834 --> 00:01:55,333 indicating the symbols and the stack. 40 00:01:55,633 --> 00:01:58,666 The first symbol in the expression is an operand. 41 00:01:58,800 --> 00:02:02,366 So under the symbol, I will write 33. Since it 42 00:02:02,367 --> 00:02:04,466 is an operand, so therefore we will push it 43 00:02:04,467 --> 00:02:07,933 onto the stack. The next symbol is 45. It is 44 00:02:07,934 --> 00:02:10,566 again an operand, so I will push it onto the 45 00:02:10,567 --> 00:02:13,133 stack by writing the already contained 46 00:02:13,166 --> 00:02:16,966 element of 33 followed by 45 under the stack column. 47 00:02:18,400 --> 00:02:20,500 Please note that the top of the stack 48 00:02:20,501 --> 00:02:23,066 in this case, is indicated by the element at 49 00:02:23,067 --> 00:02:26,300 the right most position, and the stack is in 50 00:02:26,301 --> 00:02:29,966 horizontal direction. The next symbol is 51 00:02:29,967 --> 00:02:33,433 3, which is operand, and we'll add 52 00:02:33,434 --> 00:02:36,066 it to the stack. So under the stack, I will 53 00:02:36,067 --> 00:02:40,400 write the updated status of stack. The 54 00:02:40,401 --> 00:02:43,100 next symbol is divides. Since this is an 55 00:02:43,101 --> 00:02:45,366 operation, so therefore we will pop up two 56 00:02:45,367 --> 00:02:47,633 elements from the stack, and we'll perform the 57 00:02:47,634 --> 00:02:50,633 operation on it. Please note that the element 58 00:02:50,634 --> 00:02:53,833 which is popped first, will be our second 59 00:02:53,866 --> 00:02:57,533 operand, and element which is being popped up 60 00:02:57,566 --> 00:03:00,333 second, will be our first operand. This means 61 00:03:00,334 --> 00:03:02,700 that in this case the value of three will be 62 00:03:02,701 --> 00:03:05,630 popped first, which is going to be the second operand. 63 00:03:05,631 --> 00:03:07,966 [No Audio] 64 00:03:07,967 --> 00:03:10,633 Next, the value of 45 will be popped, 65 00:03:10,634 --> 00:03:13,566 which is going to be our first operand. 66 00:03:13,567 --> 00:03:15,466 Let me also draw it. 67 00:03:15,467 --> 00:03:18,032 [No Audio] 68 00:03:18,033 --> 00:03:19,500 Finally, we will do the 69 00:03:19,501 --> 00:03:22,400 operation of divides, which will be operand1 70 00:03:22,401 --> 00:03:24,900 divided by operand 2. So let me 71 00:03:24,901 --> 00:03:31,166 indicate this by writing, op1/op2 = 45/3. 72 00:03:32,066 --> 00:03:34,333 The result of this division which has a value 73 00:03:34,334 --> 00:03:37,633 of 15, will be pushed onto the stack. So under 74 00:03:37,634 --> 00:03:40,166 the stack, I will indicate the new status of 75 00:03:40,167 --> 00:03:42,366 the stack. The next symbol is 2, which is 76 00:03:42,367 --> 00:03:44,733 operand, so we will push it on to the stack. 77 00:03:45,133 --> 00:03:47,100 Let me update the symbol and the stack 78 00:03:47,101 --> 00:03:48,700 columns accordingly. 79 00:03:50,166 --> 00:03:51,633 The next symbol is 9, 80 00:03:51,634 --> 00:03:54,133 which is operand, so we will push it on to the stack. 81 00:03:54,266 --> 00:03:56,100 Let me update the symbol and the stack 82 00:03:56,101 --> 00:03:58,500 columns again. Next, we have the symbol of 83 00:03:58,501 --> 00:04:02,200 addition, this is an operation. So again, we 84 00:04:02,201 --> 00:04:04,766 will be popping up the two elements, and we'll 85 00:04:04,767 --> 00:04:06,900 perform the operations on them, and the result 86 00:04:06,901 --> 00:04:09,766 will be pushed back onto the stack. In this 87 00:04:09,767 --> 00:04:12,233 case, we will pop 9 and 2, and we'll add them. 88 00:04:12,333 --> 00:04:14,933 The result of addition which is 11, will be 89 00:04:14,934 --> 00:04:17,233 pushed onto the stack. So let me update the 90 00:04:17,234 --> 00:04:18,375 status of the stack. 91 00:04:18,376 --> 00:04:21,000 [No Audio] 92 00:04:21,001 --> 00:04:23,733 Next we have multiplication operation, so let me write it 93 00:04:23,734 --> 00:04:27,266 under symbol. Again, we will be popping two 94 00:04:27,267 --> 00:04:29,766 elements and we'll perform the operation on them. 95 00:04:29,966 --> 00:04:33,066 In this case, the value of 11 and 15 96 00:04:33,067 --> 00:04:35,166 will be popped, and will be multiplied 97 00:04:35,167 --> 00:04:38,500 together, resulting in a value of 165, which 98 00:04:38,501 --> 00:04:40,833 will, which will be pushed onto the stack. 99 00:04:41,133 --> 00:04:43,033 So let me update the status of the stack. 100 00:04:43,042 --> 00:04:45,933 [No Audio] 101 00:04:45,934 --> 00:04:50,466 Next is the addition symbol, so let me write it. 102 00:04:50,866 --> 00:04:53,900 Again, we will be popping two elements which 103 00:04:53,901 --> 00:04:58,266 are 165 and 33 in this case, and we will add 104 00:04:58,267 --> 00:05:00,500 them together, which will result in a value of 105 00:05:00,501 --> 00:05:03,833 198, which will be pushed onto the stack. 106 00:05:04,300 --> 00:05:06,533 So, let me update the status of the stack again. 107 00:05:06,556 --> 00:05:11,666 [No Audio] 108 00:05:11,667 --> 00:05:14,400 Next, is the symbol of 50, which will be pushed 109 00:05:14,401 --> 00:05:16,748 onto the stack. So, let me update the status. 110 00:05:16,749 --> 00:05:19,600 [No Audio] 111 00:05:19,601 --> 00:05:21,666 Finally, we have the symbol indicating the 112 00:05:21,667 --> 00:05:24,766 subtraction operation. Again the two elements 113 00:05:24,767 --> 00:05:27,300 will be popped. Please note again the element 114 00:05:27,301 --> 00:05:29,066 which is popped first is going to be the 115 00:05:29,067 --> 00:05:31,566 second operand, and the element which is 116 00:05:31,567 --> 00:05:34,200 popped a second, will be the first operand. 117 00:05:34,466 --> 00:05:38,900 So, I will do op1- op2, 118 00:05:38,901 --> 00:05:43,200 that is 198 - 50, resulting in a value of 148 119 00:05:43,500 --> 00:05:46,466 which will be pushed back onto the stack. 120 00:05:47,966 --> 00:05:50,700 Now, when there is no more symbols to scan, 121 00:05:50,966 --> 00:05:53,200 then anything which is left behind in the 122 00:05:53,201 --> 00:05:55,500 stack is being popped out. And that is the 123 00:05:55,501 --> 00:05:59,600 final answer, which in this case has a value of 148. 124 00:06:01,066 --> 00:06:03,100 Okay, let us quickly check if we have 125 00:06:03,101 --> 00:06:05,733 computed the result correctly or not. I have 126 00:06:05,734 --> 00:06:08,333 already written the original expression. In 127 00:06:08,334 --> 00:06:11,100 maths, when we have an expression like this 128 00:06:11,101 --> 00:06:13,600 one, we will first compute anything which is 129 00:06:13,601 --> 00:06:17,033 inside the parentheses. If there are more 130 00:06:17,034 --> 00:06:19,400 than one operation inside the parentheses, 131 00:06:19,401 --> 00:06:21,600 then we will apply the operation precedence 132 00:06:21,601 --> 00:06:24,266 rule, according to which the divides has the 133 00:06:24,267 --> 00:06:26,700 highest precedence, followed by multiplication, 134 00:06:26,701 --> 00:06:29,000 followed by addition, and finally subtraction. 135 00:06:30,600 --> 00:06:32,666 So, in this case, the value of 2 + 9 136 00:06:32,667 --> 00:06:35,533 will be first computed, resulting in a value of 11. 137 00:06:36,198 --> 00:06:39,933 So, I will write the simplified expression again. 138 00:06:41,600 --> 00:06:42,733 Next inside the outer 139 00:06:42,734 --> 00:06:45,533 brackets, there are many operations. So we 140 00:06:45,534 --> 00:06:47,666 will apply the operation precedence according 141 00:06:47,667 --> 00:06:50,133 to which the divides has the highest priority. 142 00:06:50,433 --> 00:06:53,900 So, we will divide 45/3 which has a value of 15. 143 00:06:54,733 --> 00:06:56,100 Next, we will do the 144 00:06:56,101 --> 00:06:58,366 multiplication operation, because it has the 145 00:06:58,367 --> 00:07:02,033 highest precedence. So let me write the updated expression. 146 00:07:02,034 --> 00:07:07,100 [No Audio] 147 00:07:07,101 --> 00:07:09,633 Next, we will perform the addition, which will 148 00:07:09,634 --> 00:07:14,400 result in a value of 198. Let me write new updated expression. 149 00:07:15,133 --> 00:07:17,300 Finally, when we will do 150 00:07:17,301 --> 00:07:19,266 the subtraction operation, which will result 151 00:07:19,267 --> 00:07:23,400 in a value of 148. You may have noted, that we 152 00:07:23,401 --> 00:07:25,933 have computed the value of 148 in our 153 00:07:25,934 --> 00:07:29,300 example, which confirms that our result is correct. 154 00:07:29,666 --> 00:07:31,466 Okay, great. Now in the next 155 00:07:31,467 --> 00:07:33,066 tutorial, we will be looking at the 156 00:07:33,067 --> 00:07:34,933 implementation details of Expression 157 00:07:34,934 --> 00:07:38,033 Evaluation in Rust. That is going to be fun 158 00:07:38,034 --> 00:07:39,800 and interesting. So do come back for learning 159 00:07:39,801 --> 00:07:43,356 that, and until next tutorial, happy Rust programming. 160 00:07:43,357 --> 00:07:47,900 [No Audio]