Пример #1
0
def trans_infix_suffix(line):
    st = SStack()
    exp = []

    for x in token(line):
        if x not in infix_operators:
            exp.append(x)
        elif st.is_empty() or x == '(':
            st.push(x)
        elif x == ')':
            while not st.is_empty() and st.top() != '(':
                st.append(st.pop())
            if st.is_empty():
                raise ValueError
            st.pop()
        else:
            while (not st.is_empty() and property[st.top()] >= property[x]
                   ):  # 当top是*/,x是+-的时候,将top加入
                exp.append(st.pop())
            st.push(x)

    while not st.is_empty():
        if st.top() == '(':
            raise ValueError
        exp.append(st.pop())

    return exp
Пример #2
0
def postorder_nonrec(t, proc):
    s = SStack()
    while t is not None or not s.is_empty():
        while t is not None:  # 下行,直到找到栈顶的两子数为空
            s.push(t)
            t = t.left if t.left is not None else t.ringt  # 这里不用 try,如果t下面没有子树不会报错,而是None
            # 看结点定义def __init__(self,dat,right=None,left=None)

        t = s.pop()
        proc(t.data)
        if not s.is_empty() and s.top.left == t:  # 如果t是现在栈顶的左子树
            t = s.top().right
        else:
            t = None  #