def get_rectangle(hist, sz): max_area = 0 idx = 0 st = Stack() while idx < sz: if st.top() is None or hist[idx] >= hist[st.top()]: st.push(idx) idx += 1 else: top_ele = st.pop() current_area = hist[top_ele] * (idx if st.top() is None else (idx - st.top() - 1)) #print "current_area %s %s"%(hist[top_ele], current_area) max_area = max(max_area, current_area) #print "max= %s"%(max_area) while st.top() is not None: top_ele = st.pop() current_area = hist[top_ele] * (idx if st.top() is None else (idx - st.top() - 1)) max_area = max(max_area, current_area) print "Max rectangele = %s" % (max_area)
def preorder_travel2(node): """前序遍历,非递归法""" stack = Stack() while node is not None or not stack.is_empty(): while node is not None: # 沿左分支下行 print(node, end=" ") stack.push(node.right) # 右分支入栈 node = node.left # 指针指向左分支 node = stack.pop() # 遇到空树,回溯
def inorder_travel2(node): """中序遍历,非递归法""" stack = Stack() while node is not None or not stack.is_empty(): while node is not None: stack.push(node) node = node.left if node.left is not None else node.right node = stack.pop() print(node, end=" ") node = node.right
def postorder_travel2(node): """后序遍历,非递归法""" stack = Stack() while node is not None or not stack.is_empty(): while node is not None: # 下行循环,直到栈顶元素为空 stack.push(node) node = node.left if node.left is not None else node.right node = stack.pop() print(node, end=" ") if not stack.is_empty() and stack.top( ).left == node: # 栈非空且当前节点是栈顶的左子节点 node = stack.top().right else: # 没有右子树或右子树遍历完毕,强迫退栈 node = None
def dfs(self, idx): st = Stack() #self.visited[idx] = True st.push(idx) while st.empty() is False: tmp = st.pop() #print 'node :',tmp #st.get_stack() if self.visited[tmp] is False: print tmp, " ", self.visited[tmp] = True for node in self.graph[tmp]: if self.visited[node] is False: st.push(node)
def decode_str(str): l = str.__len__() int_stack = Stack() str_stack = Stack() for i in range(l): if str[i] >= '0' and str[i] <= '9': num = 0 while (str[i] >= '0' and str[i] <= '9'): num = num * 10 + (ord(str[i]) - ord('0')) i += 1 int_stack.push(num) elif str[i] == '(': #push into char stack and the int multiplier if str[i - 1] >= '0' and str[i - 1] <= '9': str_stack.push('(') else: int_Stack.push(1) str_stack.push(')') elif str[i] == ')': #process multiplier = int_stack.top() int_stack.pop() new_str = '' while (len(str_stack) != 0 and str_stack.top() != '('): new_str = str_stack.top() + new_str str_stack.pop() str_stack.pop() #popping the ( replicated_str = '' for j in range(multiplier): replicated_str += new_str for j in range(len(replicated_str)): str_stack.push(replicated_str[j]) else: #normal alphabets, just push them str_stack.push(str[i]) print(str_stack)
def use_stack(): st = Stack() for _ in range(10): st.push(random.randint(0, 50)) st.get_stack() st.pop() print st.top() st.push(67) st.get_stack()
def dfs_graph(graph, v0): """深度优先图遍历""" from queue_stack import Stack vnum = graph.vertex_num() visited = [0] * vnum # visited 记录已访问顶点 visited[v0] = 1 DFS_seq = [v0] # DFS_seq 记录遍历序列 st = Stack() st.push((0, graph.out_edges(v0))) # 入栈 (i, edges) while not st.is_empty(): # 下次访问边edges[i] i, edges = st.pop() if i < len(edges): v, e = edges[i] st.push((i + 1, edges)) # 下次回来访问边edges[i+1] if not visited[v]: DFS_seq.append(v) visited[v] = 1 st.push((0, graph.out_edges(v))) return DFS_seq