Infix To Prefix Conversion Questions

These questions are generally asked in university exam

infix, prefix based stack question


Jump To Question

Infix to Prefix Conversion in DSA

What is Infix to Prefix Conversion?

Infix to Prefix conversion is a process in Data Structures and Algorithms (DSA) where an expression written in infix notation (e.g., A + B) is converted to prefix notation (e.g., +AB). In prefix notation, also known as Polish notation, operators precede their operands.

Why is There a Need to Convert Infix to Prefix?

Infix notation is how expressions are commonly written, but it requires precedence rules and parentheses to determine the order of operations. Converting infix to prefix simplifies the evaluation process, particularly for computer programs and calculators, as it removes the need for precedence and associativity rules, ensuring faster processing and less ambiguity in mathematical expressions.

Advantages of Prefix Notation

  • Removes the need for parentheses, as the order of operations is clear in prefix notation.
  • Evaluation of expressions is faster and more straightforward for computers.
  • Improves the efficiency of compilers and interpreters in parsing expressions.

Disadvantages of Prefix Notation

  • Not as intuitive for humans to read compared to infix notation.
  • Conversion from infix to prefix may be complex and time-consuming for beginners.
  • Less commonly used outside of programming and computer science contexts.

Method to Convert Infix to Prefix Notation

  • Reverse the infix expression (e.g., if the infix expression is A + B * C, the reversed form would be C * B + A).
  • Replace every '(' with ')' and every ')' with '(' in the reversed expression.
  • Convert the modified expression to postfix notation using standard infix to postfix rules.
  • Reverse the result to obtain the prefix expression.

21. Convert infix expression (a-b/c)*(a/c-d) to prefix expression.

Answer: The infix expression is (a-b/c)*(a/c-d). The corresponding prefix expression is *-a/bc-/ac-d.

22. Convert infix expression (x+y)*(z-w) to prefix expression.

Answer: The infix expression is (x+y)*(z-w). The corresponding prefix expression is *+xy-zw.

23. Convert infix expression (a+b/c)*(d-e) to prefix expression.

Answer: The infix expression is (a+b/c)*(d-e). The corresponding prefix expression is *+a/bcd-e.

24. Convert infix expression (p*q)/(r-s) to prefix expression.

Answer: The infix expression is (p*q)/(r-s). The corresponding prefix expression is /*pq-rs.

25. Convert infix expression (a-b)*(c+d)/e to prefix expression.

Answer: The infix expression is (a-b)*(c+d)/e. The corresponding prefix expression is /*-ab+cd e.

26. Convert infix expression a+(b*c-d/e) to prefix expression.

Answer: The infix expression is a+(b*c-d/e). The corresponding prefix expression is +a-*bc/de.

27. Convert infix expression (a+b)*(c/(d-e)) to prefix expression.

Answer: The infix expression is (a+b)*(c/(d-e)). The corresponding prefix expression is *+ab/c-de.

28. Convert infix expression a*(b+c/d)-e to prefix expression.

Answer: The infix expression is a*(b+c/d)-e. The corresponding prefix expression is -*a+bc/de.

29. Convert infix expression (x/y+z)*(p-q) to prefix expression.

Answer: The infix expression is (x/y+z)*(p-q). The corresponding prefix expression is *+/xyz-pq.

30. Convert infix expression (p+q)/(r-s+t) to prefix expression.

Answer: The infix expression is (p+q)/(r-s+t). The corresponding prefix expression is /+pq-rt+s.

31. Convert infix expression ((a+b)*c-d)/e to prefix expression.

Answer: The infix expression is ((a+b)*c-d)/e. The corresponding prefix expression is /-*+abcde.

32. Convert infix expression (a/b-c)+(d*e-f) to prefix expression.

Answer: The infix expression is (a/b-c)+(d*e-f). The corresponding prefix expression is +-ab/c-*def.

33. Convert infix expression a+(b/(c-d)) to prefix expression.

Answer: The infix expression is a+(b/(c-d)). The corresponding prefix expression is +a/bc-d.

34. Convert infix expression (x+y)/(z-w)*(p+q) to prefix expression.

Answer: The infix expression is (x+y)/(z-w)*(p+q). The corresponding prefix expression is *+/xy-zw+pq.

35. Convert infix expression a-(b+c)/(d-e*f) to prefix expression.

Answer: The infix expression is a-(b+c)/(d-e*f). The corresponding prefix expression is -a/+bc-*def.

36. Convert infix expression a*(b-c)/(d+e) to prefix expression.

Answer: The infix expression is a*(b-c)/(d+e). The corresponding prefix expression is /*a-bc+de.

37. Convert infix expression (p-q+r)*(s/t-u) to prefix expression.

Answer: The infix expression is (p-q+r)*(s/t-u). The corresponding prefix expression is *+-pqr-/st u.

38. Convert infix expression ((a*b)+c-d)/(e+f) to prefix expression.

Answer: The infix expression is ((a*b)+c-d)/(e+f). The corresponding prefix expression is /-+*abcd+ef.

39. Convert infix expression ((x+y)*(z-w))/p to prefix expression.

Answer: The infix expression is ((x+y)*(z-w))/p. The corresponding prefix expression is /*+xy-zw p.

40. Convert infix expression (a*b+c)/(d-e+f) to prefix expression.

Answer: The infix expression is (a*b+c)/(d-e+f). The corresponding prefix expression is /+*abc-+def.