Data Structures & Algorithm Lecture 6

Main note

To solve this question refer Infix To Postfix Conversion

Questions

Question 1

Input : infixVect = `1-2^3^3-(4+5*6)*7

ExpressionStackOutput
1empty1
--1
2-12
^-^12
3-^123
^-^123^
3-^123^3
^-^123^3^
--123^3^-
(-(123^3^-
4-(123^3^-4
+-(+123^3^-4
5-(+123^3^-45
*-(+*123^3^-45
6-(+*123^3^-456
)-123^3^-456*+
*-*123^3^-456*+
7Empty123^3^-456*+7*-
Answer : 123^3^-456*+7*-

Question 2

(A+B)*C+(D-E)/F+G

ExpressionStackOutput
((
A(A
+(+A
B(+AB
))AB+
**AB+
C*AB+C
++AB+C*
(+(AB+C*
D+(AB+C*D
-+(-AB+C*D-
E+(-AB+C*D-E
)+AB+C*D-E-
/+/AB+C*D-E
F+/AB+C*D-EF
++AB+C*D-EF/+
GAB+C*D-EF/+G+

Postfix Evaluation Example

Sure! Here is a properly formatted markdown table detailing the steps for evaluating the postfix expression 234*-5+:

StepSymbolOperand1Operand2ValueOperand Stack
122
232, 3
342, 3, 4
4*34122, 12
5-212-10-10
65-10, 5
7+-105-5-5

Thus, the total answer is -5.

For the second example 623+-382/+*2^3+, the steps are more complex. Here is a markdown table for that:

StepSymbolOperand1Operand2ValueOperand Stack
166
226, 2
336, 2, 3
4+2356, 5
5-6511
631, 3
781, 3, 8
821, 3, 8, 2
9/8241, 3, 4
10+3471, 7
11*1777
1227, 2
13^724949
14349, 3
15+4935252

Thus, the total answer is 52.

Algorithm for evaluation

Evaluating a postfix expression (Reverse Polish Notation) involves using a stack to store operands and apply operators. Here is an algorithm to evaluate a postfix expression:

  1. Initialize an empty stack.
  2. Traverse the postfix expression from left to right for each symbol: a. If the symbol is an operand, push it onto the stack. b. If the symbol is an operator, pop the top two operands from the stack. Apply the operator to these two operands. Push the result back onto the stack.
  3. After the entire expression has been traversed, the result of the expression will be the only value remaining in the stack. Pop and return this value.

Here is the algorithm in pseudocode:

function evaluatePostfix(expression):
    create an empty stack
    
    for each symbol in expression:
        if symbol is an operand:
            push symbol onto the stack
        else if symbol is an operator:
            operand2 = pop from the stack
            operand1 = pop from the stack
            result = apply the operator to operand1 and operand2
            push result onto the stack
    
    result = pop from the stack
    return result

Here is a Python implementation of the algorithm:

def evaluate_postfix(expression):
    stack = []
    
    for symbol in expression:
        if symbol.isdigit():  # If the symbol is an operand (assuming single-digit for simplicity)
            stack.append(int(symbol))
        else:  # If the symbol is an operator
            operand2 = stack.pop()
            operand1 = stack.pop()
            
            if symbol == '+':
                result = operand1 + operand2
            elif symbol == '-':
                result = operand1 - operand2
            elif symbol == '*':
                result = operand1 * operand2
            elif symbol == '/':
                result = operand1 / operand2
            elif symbol == '^':
                result = operand1 ** operand2
            
            stack.append(result)
    
    return stack.pop()
 
# Example usage:
expression = "623+-382/+*2^3+"
print(evaluate_postfix(expression))  # Output: 52

In this implementation:

  • The isdigit() function is used to check if a symbol is an operand.
  • The stack is used to store operands and intermediate results.
  • Operators are applied to the top two elements of the stack, and the result is pushed back onto the stack.
  • After processing the entire expression, the result is the only remaining element in the stack.

Infix To Prefix

First step is reversing

Example

(A+B)+C-(D-E)^F

Solution

First F^)E-D(-C+)B+A(

Homework

K+L-M-N+O^P^Q*W/U+V*T+Q To convert the infix expression (A+B)*C+(D-E)/F+G to prefix notation, follow these steps:

  1. Reverse the String:

    • Original Infix: (A+B)*C+(D-E)/F+G
    • Reversed: G+F/D-(E-D)*C+(B+A)
  2. Convert the Reversed String to Postfix: Here’s the table with the steps to convert (A+B)*C+(D-E)/F+G to postfix

ExpressionStackOutput
GG
++G
F+GF
//GF+
D/GF+D
--GF+D/
(- (GF+D/
E- (GF+D/E
--GF+D/E
D-GF+D/E-D
)-GF+D/E-D-
**GF+D/E-D-
C*GF+D/E-D-C*
++GF+D/E-D-C*+
(+ (GF+D/E-D-C*+
B+ (GF+D/E-D-C*+B
++GF+D/E-D-C*+B
A+GF+D/E-D-C*+BA
)GF+D/E-D-C*+BA+
  1. Reverse the Postfix Result to Get Prefix:

    • Postfix Result: GF+D/E-D-C*+BA+
    • Reversed Prefix: +A+B-C*DE/F+G

    Thus, the prefix notation of the infix expression (A+B)*C+(D-E)/F+G is + + A B / - D E F G.

References

Continued to Data Structures & Algorithm Lecture 7

Information
  • date: 2024.08.02
  • time: 09:17