Chào mọi người em đang học cấu trúc dữ liệu và giải thuật.
Mà gặp phần chuyển đổi này phải chình bày cả phương pháp và kết quả mới được điểm.
Em thì chỉ biết làm thôi. Từ biểu thức em chuyển sang cây nhị phân r em thực hiện theo yêu cầu (duyệt trước, sau, giữa).
Em muốn xin mọi người phương pháp chuyển từ tiền tố về trung tố, hậu tố về trung tố, tiền tố về hậu tố.(Không phải code mọi người nhé)
Dưới đây là cách thầy đưa cho mà em dịch ra không hiểu.
Convert Prefix to Infix Expression
Input: Prefix expression: + A B
Output: Infix expression: (A + B)
Input: Prefix expression: *-A/BC-/AKL
Output: Infix expression: ((A-(B/C))*((A/K)-L))
Method:
Iterate the given expression from right to left (in reverse order), one character at a time
-
If character is operand, push it to stack.
-
If character is operator,
2.1. pop operand from stack, say it’s s1.
2.2. pop operand from stack, say it’s s2.
2.3. perform (s1 operator s2) and push it to stack. -
Once the expression iteration is completed, initialize result string and pop out from stack and add it to result.
-
Return the result.