Ejemplo n.º 1
0
    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
Ejemplo n.º 2
0
    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
Ejemplo n.º 3
0
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()
Ejemplo n.º 4
0
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()  # 返回最终的计算结果
Ejemplo n.º 5
0
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)
Ejemplo n.º 6
0
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()
Ejemplo n.º 7
0
 def test_is_empty_with_empty(self):
     mystack = Stack()
     self.assertTrue(mystack.is_empty())
Ejemplo n.º 8
0
 def test_clear_empty(self):
     mystack = Stack()
     result = mystack.clear()
     self.assertEqual(result, None)
Ejemplo n.º 9
0
def create_stack(items):
    mystack = Stack()
    for item in items:
        mystack.push(item)
    return mystack
Ejemplo n.º 10
0
 def test_peek_empty(self):
     mystack = Stack()
     result = mystack.peek()
     self.assertEqual(result, None)
     self.assertEqual(mystack.count(), 0)
Ejemplo n.º 11
0
 def test_pop_empty(self):
     mystack = Stack()
     result = mystack.pop()
     self.assertEqual(result, None)
Ejemplo n.º 12
0
 def test_push(self):
     mystack = Stack()
     mystack.push('Python0')
     self.assertEqual(mystack.count(), 1)
     self.assertEqual(mystack.peek(), 'Python0')
Ejemplo n.º 13
0
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()
Ejemplo n.º 14
0
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()
Ejemplo n.º 15
0
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)