def trans_infix_suffix(line): st = SStack() 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.pop() == '(': # 如果还有左括号,就是不配对 raise SyntaxError('Extra "(".') exp.append(st.pop()) return exp
def check_parens(text): """ 括号匹配检查函数,text是被检查的正文串 """ parens = '()[]{}' open_parens = '([{' opposite = {')': '(', ']': '[', '}': '{'} # 表示配对关系的字典 st = SStack() # 保存括号的栈 st_i = SStack() for pr, i in parentheses(text, parens): # 对text里各括号和位置迭代 if pr in open_parens: # 开括号,压进栈并继续 st.push(pr) st_i.push(i) elif st.is_empty() or st.pop() != opposite[pr]: # 不匹配就是失败,退出 if not st.is_empty(): st_i.pop() print('Unmatching is found at', i, 'for', pr) return False else: # 这是一次括号配对成功,什么也不做,继续 st_i.pop() if not st.is_empty(): ''' st_i_ = '' st_ = '' while not st.is_empty(): st_i_ = str(st_i.pop()) + st_i_ st_ = st.pop() + st_ print('Unmatching is found at', st_i_, 'for', st_) ''' print('Unmatching is found at', st_i.pop(), 'for', st.pop()) return False print('All parentheses are correctly matched.') return True
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.right #能左就左否则向右 t = s.pop() #栈顶是应访问节点 proc(t.data) if not s.is_empty() and s.top().left == t: t = s.top().right #栈不空且当前节点是栈定左子节点 else: #没有右子树或右子树遍历完毕,强迫退栈 t = None
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_.right # 注意这个条件表达式的意义:能左就左,否则向右一步 t_ = s.pop() # 栈顶是访问结点 proc(t_.dat) if not s.is_empty() and s.top().left == t_: t_ = s.top().right # 栈不空且当前结点是栈顶的左子结点 else: t_ = None # 没有右子树或右子树遍历完毕,强迫退栈
def dfs_non_recursive_traverse_postorder(t, proc): #dfs非递归 后根序 s = SStack() while t is not None or not s.is_empty(): while t is not None: s.push(t) if t.left is not None: t = t.left else: t = t.right t = s.pop() proc(t.data) if not s.is_empty() and s.top().left == t: t = s.top().right #这一步很关键 else: t = None
def preorder_elements(t_): s = SStack() while t_ is not None or not s.is_empty(): while t_ is not None: s.push(t_.right) yield t_.dat t_ = t_.left t_ = s.pop()
def preorder_elements(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()
def preorder_nonrec(t, proc): s = SStack() while t is not None or not s.is_empty(): while t is not None: proc(t.data) s.push(t.right) t = t.left t = s.pop()
def pre_order_nr(tree, func): s = SStack() while tree or not s.is_empty(): while tree: func(tree.root) s.push(tree.right) tree = tree.left tree = s.pop()
def norec_fact(n): # 自己管理栈,模拟函数调用过程 res = 1 st = SStack() while n > 0: st.push(n) n -= 1 while not st.is_empty(): res *= st.pop() return res
def dfs_non_recursive_traverse(t, proc): #dfs非递归 先根序 s = SStack() while t is not None or not s.is_empty(): while t is not None: proc(t.data) s.push(t.right) t = t.left t = s.pop()
def preorder_nonrec(t_, proc): """ 时间复杂性: O(n) 空间复杂性: O(log(n)) """ s = SStack() while t_ is not None or not s.is_empty(): while t_ is not None: proc(t_.dat) s.push(t_.right) t_ = t_.left t_ = s.pop()
def maze_solver(maze, start, end): if start == end: print(start) return st = SStack() mark(maze, start) st.push((start, 0)) # 入口和方向0的序对入栈 while not st.is_empty(): # 走不通时回退 pos, nxt = st.pop() # 取栈顶及其探查方向 for i in range(nxt, 4): # 依次检查未探查方向 nextp = (pos[0] + dirs[i][0], pos[1] + dirs[i][1]) # 算出下一位置 if nextp == end: # 到达出口打印路径 print(end, end=' ') print(pos, end=' ') while not st.is_empty(): print(st.pop()[0], end=' ') return if passable(maze, nextp): # 遇到未探查的新位置 st.push((pos, i + 1)) # 原位置和下一方向入栈 mark(maze, nextp) st.push((nextp, 0)) # 新位置入栈 break # 退出内层循环,下次迭代将以新栈顶为当前位置继续 print('No path found.') # 找不到路径
def dfs(graph, s): stack = SStack() stack.push(s) seen = set() seen.add(s) parent = {s: None} while not stack.is_empty(): vertex = stack.pop() nodes = graph[vertex] for i in nodes: if i not in seen: stack.push(i) seen.add(i) parent[i] = vertex print vertex return parent
def DFS_graph(graph, v0): vnum = graph.vertex_num() visited = [0] * vnum visited[v0] = 1 DFS_seq = [v0] st = SStack() st.push((0, graph.out_edges(v0))) while not st.is_empty(): i, edges = st.pop() if i < len(edges): v, e = edges[i] st.push((i+1, edges)) if not visited[v]: DFS_seq.append(v) visited[v] = 1 st.push((0, graph.out_edges(v))) return DFS_seq
def DFS_graph_non_recursive(graph, v0): # 借用辅助栈 非递归实现 ver_num = graph.get_ver_num() visited = [0] * ver_num visited[v0] = 1 dfs_seq = [v0] #dfs序列 st = SStack() st.push((0, graph.get_outedges(v0))) while not st.is_empty(): i, edges = st.pop() if i < len(edges): v, w = edges[i] st.push((i + 1, edges)) #回溯时访问i+1 if not visited[v]: #v未被访问,记录并继续dfs dfs_seq.append(v) visited[v] = 1 st.push((0, graph.get_outedges(v))) return dfs_seq
def DFS_SpanTree(graph, v0): vertex_num = graph.get_ver_num() dfs_spantree = [] visited = [0] * vertex_num visited[v0] = 1 st = SStack() st.push((0, v0, graph.get_outedges(v0))) while not st.is_empty(): i, prev_v, edges = st.pop() if i < len(edges): v, w = edges[i] st.push((i + 1, prev_v, edges)) if not visited[v]: visited[v] = 1 st.push((0, v, graph.get_outedges(v))) dfs_spantree.append((prev_v, w, v)) return dfs_spantree