def build_parse_tree(exp: str): _exp_list = exp.split() _stack = Stack() _tree = BinaryTree('') _stack.push(_tree) _current = _tree for _item in _exp_list: if _item == '(': _current.insert_left('') _stack.push(_current) _current = _current.get_left_child() elif _item in ['+', '-', '*', '/']: # set current node value _current.set_root_val(_item) _current.insert_right('') _stack.push(_current) _current = _current.get_right_child() # number elif _item not in ['+', '-', '*', '/', ')']: _current.set_root_val(int(_item)) _current = _stack.pop() elif _item == ')': _current = _stack.pop() else: raise ValueError return _tree
def int2str_by_stack(num: int, base: int): _stack = Stack() _ref = '0123456789ABCDEF' while num > 0: if num < base: _stack.push(_ref[num]) else: _stack.push(_ref[num % base]) num //= base _ret = '' while not _stack.is_empty(): _ret += str(_stack.pop()) return _ret
def postfix_math(s: str): _stack = Stack() _srclst = s.split() _refer = string.digits for _item in _srclst: if _item in _refer: _stack.push(int(_item)) else: _op1 = int(_stack.pop()) _op2 = int(_stack.pop()) # op2 operator op1 _value = calc(_item, _op2, _op1) _stack.push(_value) return _stack.pop()
def check_par_brackets(s): _stack = Stack() _ok = True _index = 0 while _index < len(s) and _ok: _item = s[_index] if _item == '(': _stack.push(_item) else: if _stack.is_empty(): _ok = False return _ok else: _stack.pop() _index += 1 # _stack must be empty return _ok and _stack.is_empty()
def devide_by2(decimal): _remstack = Stack() while decimal > 0: _rem = decimal % 2 _remstack.push(_rem) decimal //= 2 _ret = '' while not _remstack.is_empty(): _ret += str(_remstack.pop()) return _ret
def devide_by_base(decimal, base): _digits = '0123456789ABCDEF' _remstack = Stack() while decimal > 0: _rem = decimal % base _remstack.push(_rem) decimal //= base _ret = '' while not _remstack.is_empty(): _ret += str(_digits[_remstack.pop()]) return _ret
def check_brackets(s): _stack = Stack() _ok = True _index = 0 while _index < len(s) and _ok: if s[_index] in '([{': _stack.push(s[_index]) else: if _stack.is_empty(): _ok = False return _ok else: _open = _stack.pop() # s[_index] is close symbol if not matches(_open, s[_index]): _ok = False return _ok _index += 1 return _ok and _stack.is_empty()
def infix2prefix(s: str): _priority = { '*': 3, '/': 3, '+': 2, '-': 2, '(': 1, } _ret = [] _opstack = Stack() _srclst = s.split() _refer = string.ascii_uppercase + string.digits _index = 0 while _index < len(_srclst): _item = _srclst[_index] if _item == '(': _opstack.push(_item) elif _item in _refer: _ret.append(_item) elif _item == ')': _top = _opstack.pop() # maybe exist multi operations rather than one. # so need to add continually until meet '(' while _top != '(': _ret.append(_top) _top = _opstack.pop() else: while (not _opstack.is_empty() ) and _priority[_opstack.peek()] >= _priority[_item]: _ret.append(_opstack.pop()) _opstack.push(_item) _index += 1 while not _opstack.is_empty(): _ret.append(_opstack.pop()) return ''.join(_ret)
#!/usr/bin/env python # -*- coding: utf-8 -*- # @Time : 6/14/18 11:17 AM # @Author : Miracle Young # @File : app.py from python_data_structure.base_data_structure.stack import Stack _stack = Stack() print(_stack.is_empty()) _stack.push(4) _stack.push('dog') print(_stack.peek()) _stack.push(True) print(_stack.size()) print(_stack.is_empty()) _stack.push(8.4) print(_stack.pop()) print(_stack.pop()) print(_stack.size()) from python_data_structure.base_data_structure.queue import Queue _q = Queue() print(_q.is_empty()) print(_q.enqueue(1)) print(_q.enqueue(10)) print(_q.dequeue()) print(_q.size())