Evaluating Postfix Expressions for(char ch : st) { if ch is an operand push it on the stack else // ch is an operator op { //evaluate and push the result operand2 = pop stack operand1 = pop stack result = operand1 op operand2 push result on stack } } � 81
Lecture Activity Describe an algorithm that translates the infix expression below into postfix (you can use drawings to explain): Infix: Postfix: 2 * (3 + 4) 2 3 4 + * Hint : use 2 stacks, one for operators and parentheses another one for the operands and postfix expression. Once converted use the empty stack to invert the order � 82
Infix: Postfix: 2 * (3 + 4) 2 3 4 + * 3. Push everything onto empty 1. Read characters onto 2. Pop operator stack and push stack to invert it onto postfix stack ignoring ‘(‘ corresponding stack until ‘)’ Then read pop and print. 2 4 + * ( 3 + 3 4 * 4 2 3 + * 2 Postfix Operator Postfix Operator Postfix Operator Stack Stack Stack Stack Stack Stack � 83
Infix: Postfix: (2 * 3 )+ 4 2 3 *4 + 2. If reading a ‘)’, move operators 3. Keep reading 4. Move operators to 1. Read characters to Postfix Stack until a ‘(‘ until ‘)’ -> 2. onto corr. stack until ‘)’ Postfix Stack discard it and continue reading string or end of string -> 4. or end of string + + * 3 * 4 4 ( 3 2 * * 3 2 3 2 2 Operator Postfix Operator Postfix Operator Postfix Operator Postfix Stack Stack Stack Stack Stack Stack Stack Stack � 84
Infix: Postfix: (2 * 3 )+ 4 2 3 *4 + 4. Move operators to 5. Pop and push onto empty stack to invert, then print Postfix Stack + 2 4 3 * * 3 4 2 + Operator Postfix Operator Postfix Stack Stack Stack Stack
Search a Flight Map Fly from Origin to Destination following map 1. Reach destination 2. Reach city with no departing flights (dead end) 3. Go in circles forever � 86
Backtracking C = visited Avoid dead end by backtracking C = backtracked Avoid traveling in circles by marking visited cities Origin = P , Destination = Z P � 87
Backtracking C = visited Avoid dead end by backtracking C = backtracked Avoid traveling in circles by marking visited cities Origin = P , Destination = Z P R � 88
Backtracking C = visited Avoid dead end by backtracking C = backtracked Avoid traveling in circles by marking visited cities Origin = P , Destination = Z P R X � 89
Backtracking C = visited Avoid dead end by backtracking C = backtracked Avoid traveling in circles by marking visited cities Origin = P , Destination = Z P R X � 90
Backtracking C = visited Avoid dead end by backtracking C = backtracked Avoid traveling in circles by marking visited cities Origin = P , Destination = Z P R X � 91
Backtracking C = visited Avoid dead end by backtracking C = backtracked Avoid traveling in circles by marking visited cities Origin = P , Destination = Z P R X � 92
Backtracking C = visited Avoid dead end by backtracking C = backtracked Avoid traveling in circles by marking visited cities Origin = P , Destination = Z P R W X � 93
Backtracking C = visited Avoid dead end by backtracking C = backtracked Avoid traveling in circles by marking visited cities Origin = P , Destination = Z P R W X S � 94
Backtracking C = visited Avoid dead end by backtracking C = backtracked Avoid traveling in circles by marking visited cities Origin = P , Destination = Z P R W X S T � 95
Backtracking C = visited Avoid dead end by backtracking C = backtracked Avoid traveling in circles by marking visited cities Origin = P , Destination = Z P R W X S T � 96
Backtracking C = visited Avoid dead end by backtracking C = backtracked Avoid traveling in circles by marking visited cities Origin = P , Destination = Z P R W X S T � 97
Backtracking C = visited Avoid dead end by backtracking C = backtracked Avoid traveling in circles by marking visited cities Origin = P , Destination = Z P R W X S T � 98
Backtracking C = visited Avoid dead end by backtracking C = backtracked Avoid traveling in circles by marking visited cities Origin = P , Destination = Z P R W X S Y T � 99
Backtracking C = visited Avoid dead end by backtracking C = backtracked Avoid traveling in circles by marking visited cities Origin = P , Destination = Z P R W X S Y T Z � 100
Recommend
More recommend