Posted on by Kalkicode
Code Stack

Prefix to Postfix Conversion

In computer science, infix, prefix, and postfix are three different notations to represent arithmetic expressions. In this code, we are specifically interested in converting a given arithmetic expression from prefix notation to postfix notation. Prefix notation is also known as Polish notation, and postfix notation is also known as Reverse Polish notation (RPN).

Problem Statement

The task is to convert a given arithmetic expression in prefix notation into its equivalent expression in postfix notation. The prefix notation places the operator before its operands, while postfix notation places the operator after its operands. For example, the prefix expression "+ab^cd" is equivalent to the postfix expression "abcd^+".

Explanation with an Example

Let's consider the prefix expression "+*ab^cd".

  1. Initialize an empty stack to store intermediate results and other necessary variables.
  2. Start scanning the given prefix expression from right to left.
  3. If the current character is an operator (e.g., +, -, *, /, ^, %), pop the top two elements from the stack (which represent operands) and concatenate them with the operator. Then push the result back to the stack.
  4. If the current character is an operand (e.g., a, b, c, d, etc.), simply push it onto the stack.
  5. Continue this process until the entire prefix expression is scanned.

Pseudocode

1. Create a stack to store intermediate results.
2. Initialize an empty string to store the postfix expression.
3. Scan the given prefix expression from right to left.
4. If the current character is an operand, push it onto the stack.
5. If the current character is an operator, pop two elements from the stack (op1 and op2).
6. Concatenate op1, op2, and the current operator, and push the result back to the stack.
7. After scanning the entire prefix expression, the stack will contain the postfix expression.

More

prefixToPostfix(prefix):
    size = length of prefix
    stack = create an empty stack
    auxiliary = ""
    op1 = ""
    op2 = ""
    isValid = true
    
    for i from size - 1 to 0:
        if prefix[i] is an operator:
            if stack size > 1:
                op1 = pop from stack
                op2 = pop from stack
                auxiliary = op1 + op2 + prefix[i]
                push auxiliary to stack
            else:
                isValid = false
        else if prefix[i] is an operand:
            auxiliary = prefix[i]
            push auxiliary to stack
        else:
            isValid = false
            
    if isValid is false or stack size is not 1:
        print "Invalid Prefix : " + prefix
    else:
        print " Prefix : " + prefix
        print " Postfix : " + pop from stack

Algorithm

  1. Create a stack data structure to hold strings.
  2. Implement methods to push, pop, and check if the stack is empty.
  3. Create a class "Conversion" with methods to check if a character is an operator or operand.
  4. Implement the "prefixToPostfix" method to convert the given prefix expression to postfix using the stack.
  5. Scan the prefix expression from right to left.
  6. Based on the character type (operator or operand), perform the required operations as explained earlier.
  7. Display the final postfix expression.

Code Solution

Resultant Output Explanation

The provided code implements the above algorithm correctly. When the program runs, it converts the two given prefix expressions into their corresponding postfix expressions. The output shows the original prefix expressions and the resulting postfix expressions.

Time Complexity

The time complexity of the algorithm is O(n), where n is the size of the prefix expression. This is because each character in the expression is processed once, and stack operations take constant time. Hence, the overall time complexity is linear in the size of the input expression.

Comment

Please share your knowledge to improve code and content standard. Also submit your doubts, and test case. We improve by your feedback. We will try to resolve your query as soon as possible.

New Comment