Prefix to Infix Conversion in PythonInfix: Infix expressions contain the operator in between the two operands. The operands can themselves contain operators. Though with respect to the middle operator, the expression will be an infix expression. Infix expressions are of the form Example: (X + Y) * (X1 + Y1) Prefix: Prefix expressions contain the operator in front of the two operands. The operands can themselves contain operators. Though with respect to the operator present at the beginning, the expression will be a prefix expression. Prefix expressions are of the form Example: *+ XY - X1Y1 (Infix : (X + Y) * (X1 + Y1)) The task is if we are given a prefix expression, we have to convert it to an infix expression. Computers are designed to compute the expressions most often in postfix or sometimes in prefixes. However, it is not easy for us to understand and compute a prefix expression. We are trained to solve infix expressions. Therefore, we need to convert a prefix expression to an infix expression. Here are some sample inputs and outputs for a better understanding of the problem at hand. ExamplesInput Prefix: *+ XY - X1Y1 Output Infix: ((X + Y) * (X1 + Y1)) Input Prefix: *+ P / QR -/ STU Output Infix: ((P - (Q / R)) * ((P / T) - U)) Algorithm for Prefix to Infix Conversion
Code Output: The infix string is: ((P+(Q/R))*((S/T)-U)) Time Complexity: This algorithm has O(n) time complexity, where n is the length of the given prefix string. Auxiliary Space: Since we are string the symbols of the string in the stack, this program takes O(n) memory space. Next TopicRotate a Linked List in Python |
We provides tutorials and interview questions of all technology like java tutorial, android, java frameworks
G-13, 2nd Floor, Sec-3, Noida, UP, 201301, India