【发布时间】:2011-05-21 10:29:47
【问题描述】:
经过谷歌搜索,我找到了!
中缀前缀
该算法是一种非尾递归方法。 反转的输入字符串被完全压入堆栈。
prefixToInfix(stack)
1) IF stack is not empty
a. Temp -->pop the stack
b. IF temp is a operator
i. Write a opening parenthesis to output
ii. prefixToInfix(stack)
iii. Write temp to output
iv. prefixToInfix(stack)
v. Write a closing parenthesis to output
c. ELSE IF temp is a space -->prefixToInfix(stack)
d. ELSE
i. Write temp to output
ii. IF stack.top NOT EQUAL to space -->prefixToInfix(stack)
当栈顶为
F(ABC)
我们进入算法,“A”被写入输出,因为它是当前的值
temp=A(比如说)
现在我如何根据算法在输出列上获得“-”,下一个临时值将是“B”,它是在最后一次递归调用后从堆栈中弹出的。 图表如何显示输出“((A-” ...
我在哪里做不正确的假设? 有人能麻烦解释一下吗?
【问题讨论】:
-
你不会从 F(ABC) 得到“-”。
标签: algorithm recursion stack prefix infix-notation