def infix_to_postfix(infix): """ 中缀转后缀 :param infix:中缀表达式 :return: 后缀表达式 """ opstack = Stack() # 操作符栈 result = [] for token in infix: if is_operand(token): # 操作数 result.append(token) elif token == '(': # 遇到左括号,入栈 opstack.push(token) elif token == ')': # 遇到右括号,弹栈并将值放至result列表末尾, 直到遇到左括号 _token = opstack.pop() while _token and _token != '(': result.append(_token) _token = opstack.pop() else: # 遇到操作符,压栈,但是如果在栈中有比当前操作符优先级高的任何操作符要先弹出 while not opstack.is_empty(): top_token = opstack.peek() if higher_precedence(top_token, token): result.append(opstack.pop()) else: break opstack.push(token) while not opstack.is_empty(): result.append(opstack.pop()) return ''.join(result)
def delete(self, word): # If tree is empty if not self.root: return False # If trie is not empty # Put the nodes in a stack current = self.root node_stack = Stack() for char in word: if char not in current.children: return False current = current.children[char] node_stack.push(current) if not current.is_end: return False # Delete node from the end of key if node does not have children current.is_end = False for i in range(node_stack.count()): current = node_stack.pop() prev = node_stack.peek() if not current.children and prev: del prev.children[current.char] return True
def convert_to_binary(number): s = Stack() while number: s.push(number % 2) number /= 2 bin_digits = [] while not s.is_empty(): bin_digits.append(str(s.pop())) return ''.join(bin_digits)
def brackets_match(bracket_str): """括号匹配""" balanced = True stack = Stack() for s in bracket_str: if s == '(': stack.push(s) else: if stack.is_empty(): balanced = False break else: stack.pop() return balanced and stack.is_empty()
def postfix_eval(postfix): """后缀表达式求值""" if not postfix: return operand_stack = Stack() for symbol in postfix: if symbol.isdigit(): operand_stack.push(symbol) else: operand2 = operand_stack.pop() operand1 = operand_stack.pop() result = do_math(symbol, operand1, operand2) operand_stack.push(result) return operand_stack.pop() # 返回最终的计算结果
def symbol_match(symbol_str): balanced = True stack = Stack() for s in symbol_str: if s in OPEN_CHARS: stack.push(s) else: if stack.is_empty(): balanced = False break else: top_val = stack.pop() if not match(top_val, s): balanced = False break return balanced and stack.is_empty()
def EvaluatePostfix(tokens): stack = Stack() for token in tokens: if token == '' or token == ' ': continue if token == '+': #import pdb; pdb.set_trace() sum = stack.pop() + stack.pop() stack.push(sum) continue if token == '*': product = stack.pop() * stack.pop() stack.push(product) continue else: stack.push(int(token)) continue return stack.pop()
def pre_order_iter(self, root): """ :type root: TreeNode :rtype: List[int] """ """递归实现先序遍历""" if root == None: return stack = Stack() ret = [] stack.push(root) while stack.get_length() > 0: node = stack.pop() ret.append(node.data) # 因为是先入后出,所以后push lchild if node.rchild: stack.push(node.rchild) if node.lchild: stack.push(node.lchild) return ret
def create_stack(items): mystack = Stack() for item in items: mystack.push(item) return mystack
def test_push(self): mystack = Stack() mystack.push('Python0') self.assertEqual(mystack.count(), 1) self.assertEqual(mystack.peek(), 'Python0')