コード例 #1
0
def postorder_nonrec(t, proc):
    s = SStack()
    while t is not None or not s.is_empty():
        while t is not None:  # iterate until top has no child
            s.push(t)
            t = t.left if t.left is not None else t.right
            # if we can go left, go, otherwise, go right
        t = s.pop()  # get the node to be access
        proc(t.data)
        if not s.is_empty() and s.top().left == t:
            t = s.top().right  # end of left visit, turn right
        else:
            t = None  # end of right visit, force to backtrack
コード例 #2
0
def preorder_iter(t):
    s = SStack()
    while t is not None or not s.is_empty():
        while t is not None:
            s.push(t.right)
            yield t.data
            t = t.left
        t = s.pop()
コード例 #3
0
def preorder_nonrec(t, proc):
    s = SStack()
    while t is not None or not s.is_empty():
        while t is not None:  # go down along left chain
            s.push(t.right)  # push right branch into stack
            proc(t.data)
            t = t.left
        t = s.pop()  # left chain ends, backtrack
コード例 #4
0
def inorder_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
        t = s.pop()
        proc(t.data)
        t = t.right
コード例 #5
0
def trans_infix_suffix(line):
    st = SStack()
    llen = len(line)
    exp = []
    for x in tokens(line):  # tokens 是一个待定义的生成器
        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() != "(":
                exp.append(st.pop())
            if st.is_empty():  # 没找到左括号,就是不配对
                raise SyntaxError("Missing \'(\'.")
            st.pop()  # 弹出左括号,右括号也不进栈
        else:  # 处理算术运算符,运算符都看作是左结合
            while (not st.is_empty() and priority[st.top()] >= priority[x]):
                exp.append(st.pop())
            st.push(x)  # 算术运算符进栈
    while not st.is_empty():  # 送出栈里剩下的运算符
        if st.top() == "(":  # 如果还有左括号,就是不配对
            raise SyntaxError("Extra \'(\' in expression.")
        exp.append(st.pop())
    return exp
コード例 #6
0
def maze_solver1(maze, start, end):
    st = SStack()
    pos, nxt = start, 0
    while True:
        if pos == end:
            print_path1(pos, st)
            return
        mark(maze, pos)
        for i in range(nxt, 4):  # 依次检查潜在探索方向
            nextp = (pos[0] + dirs[i][0], pos[1] + dirs[i][1])  # 算出下一点
            if passable(maze, nextp):  # 遇到未探查点
                st.push((pos, i + 1))  # 原位置进栈
                pos, nxt = nextp, 0
                break
        else:
            if st.is_empty():
                break
            pos, nxt = st.pop()
    print("No path found.")  # 找不到路径
コード例 #7
0
def maze_solver(maze, start, end):
    if start == end:
        print(start)
        return
    st = SStack()
    mark(maze, start)
    st.push((start, 0))  # start position into stack 入口和方向 0 的序对入栈
    while not st.is_empty():  # have possibility to try 走不通时回退
        pos, nxt = st.pop()  # get last branch position 取栈顶及其探查方向
        for i in range(nxt, 4):  # try to find unexploring dir(s) 依次检查未探查方向
            nextp = (pos[0] + dirs[i][0], pos[1] + dirs[i][1]
                     )  # next point 算出下一点
            if nextp == end:  # find end, great! :-) 到达出口,打印路径
                print_path(end, pos, st)
                return
            if passable(maze, nextp):  # new position is passable 遇到未探查的新位置
                st.push((pos, i + 1))  # original position in stack 原位置和下一方向入栈
                mark(maze, nextp)
                st.push((nextp, 0))  # new position into stack 新位置入栈
                break  # 退出内层循环,下次迭代将以新栈顶为当前位置继续
    print("No path.")  # :-( 找不到路径