Exemplo n.º 1
0
 def __init__(self):
     super(double_queue_with_stack, self).__init__()
     self.left = stack.stack()
     self.right = stack.stack()
     self.mid = stack.stack()
     self.mid_type = True
     self.len = 0
Exemplo n.º 2
0
def prase_nfa(reg):
    '''
    @brief 使用nfa解析正则表达式
    @param ref  模式串
    @return
    '''
    stk = stack()
    g = diagraph()
    for i in range(len(reg)):
        lp = i
        if reg[i] == '(' or reg[i] == '|':
            stk.push(i)
        elif reg[i] == ')':
            or_id = stk.top()
            stk.pop()
            if (reg[i] == '|'):
                lp = stk.top()
                g.add_edge(lp, or_id + 1)
                g.add_edge(or_id, i)
            else:
                lp = or_id

        if i < len(reg) - 1 and reg[i + 1] == '*':
            g.add_edge(lp, i + 1)
            g.add_edge(i + 1, lp)

        if reg[i] == '(' or reg[i] == '*' or reg[i] == ')':
            g.add_edge(i, i + 1)

    return g
Exemplo n.º 3
0
def in2post(line):
    '''
    @brief  1.3.10  中序表达式转换为后序表达式,不含括号的表达式转换
    @param  line    中序表达式
    '''
    op_stk = stack.stack()
    ret_line = ''
    for ch in line:
        if ch.isdigit():
            ret_line += ch
        elif ch == '(':
            op_stk.push(ch)
        elif ch == ')':
            tmp = op_stk.pop()
            ret_line += tmp
            while tmp != ')':
                tmp = op_stk.pop()
        else:
            while get_prior(op_stk.top()) > get_prior(ch):
                ret_line += op_stk.pop()
                
            op_stk.push(ch)
    
    while not op_stk.empty():
        ret_line += op_stk.pop()
    
    return op_stk
            
        else:
Exemplo n.º 4
0
 def post_traversal_no_II(root, visit_func):
     '''
     @brief  后序遍历非递归
     @param  root    当前访问的结点
     @param  visit_func  访问结点的函数
     '''
     if root is None:
         return
     
     stk = stack.stack()
     pre = None
     cur = None
     stk.push(root)
     while not stk.empty():
         cur = stk.top()
         if pre is None or pre.left == cur or pre.right == cur:
             if cur.left is not None:
                 stk.push(cur.left)
             elif cur.right is not None:
                 stk.push(cur.right)
         elif cur.left == pre:
             if cur.right is not None:
                 stk.push(cur.right)
         else:
             visit_func(cur)
             stk.pop()
         
         pre = cur
Exemplo n.º 5
0
def complete_left_brackets(line):
    '''
    @brief  1.3.9 补全表达式中的左括号,表达式中只有右括号而没有左括号,将左括号补全
    @param  line    表达式
    @return 补全后的表达式字符串
    '''
    val_stk = stack.stack()
    op_stk = stack.stack()
    for ch in line:    
        if ch.isdigit():
            val_stk.push(ch)
        elif ch in '+-*/':
            op_stk.push(ch)
        elif ch == ')':
            cur_value = '(' + op_stk.pop() + val_stk.pop() + ch
            val_stk.push(cur_value)
        else:
            raise Exception('bad character')
            
    return val_stk.pop()
Exemplo n.º 6
0
def bracket_match(line):
    '''
    @brief  括号匹配,支持(),[],{},对于不是相关字符接略过
    @param  line    括号字符串
    '''
    stk = stack.stack()
    for ch in line:
        if is_left_bracket(ch):
            stk.push(ch)
        elif is_right_bracket(ch):
            if not match_bracket(ch, stk.pop()):
                return False
        else:
            continue
Exemplo n.º 7
0
 def post_traversal_no(root, visit_func):
     '''
     @brief  后序遍历非递归
     @param  root    当前访问的结点
     @param  visit_func  访问结点的函数
     '''
     rst = stack.stack()
     snd = stack.stack()
     while root is not None or not rst.empty():
         while root is not None:
             rst.push(root)
             snd.push(root)
             root = root.right
         
         if not rst.empty():
             root = rst.top()
             rst.pop()
             root = root.left
             
     while not snd.empty():
         root = snd.top();
         snd.pop()
         visit_func(root)
Exemplo n.º 8
0
def quick_sort_cyc(l, start, end, hook_func=None):
    '''
    @brief  快速排序非递归版本
    @param  l   list
    @param  start
    @param  end
    @parma  hook_func
    @note   建立一个容器,容器的每个元素是一个tuple表示需要进行partition的start和end
    '''
    rst = stack.stack()
    snd = stack.stack()
    rst.push((start, end))
    while not rst.empty():
        while not rst.empty():
            start, end = rst.pop()
            if end < start:
                continue

            anchor = partition(l, start, end, hook_func)
            snd.push((start, anchor - 1))
            snd.push((anchor + 1, end))

        rst, snd = snd, rst
    return l
Exemplo n.º 9
0
 def in_traversal_no(root, visit_func):
     '''
     @brief  中序遍历非递归版本
     @param  root    当前访问的结点
     @param  visit_func  访问结点的函数
     '''
     stk = stack.stack()
     index = root
     while not stk.empty() or index is not None:
         while index is not None:
             stk.push(index)
             index = index.left
             
         index = stk.top()
         visit_func(index)
         stk.pop()
         index = index.right
Exemplo n.º 10
0
 def select(self, node, k):
     '''
     @brief 返回排名为k的节点
     @note   使用中序遍历的思路
     '''
     stk = stack.stack()
     while not stk.empty() and node is None and k != 0:
         while node is not None:
             stk.push(node)
             node = node.left
             
         if not stk.empty():
             node = stk.pop()
             k -= 1
                 
             node = node.right
         
     return node
Exemplo n.º 11
0
 def pre_traversal_no(root, visit_func):
     '''
     @brief  先序遍历非递归
     @param  root    当前访问的结点
     @param  visit_func  访问结点的函数
     '''
     stk = stack.stack()
     index = root
     while index is not None and not stk.empty():
         while index is not None:
             visit_func(index)
             stk.push(index)
             index = index.left
         
         if not stk.empty():
             index = stk.top()
             stk.pop()
             index = index.right
Exemplo n.º 12
0
 def rank(self, node, key):
     '''
     @brief  返回node中的子树种小于key键的数量
     '''
     stk = stack.stack()
     no = 0
     while not stk.empty() and node is None:
         while node is not None:
             stk.push(node)
             node = node.left
             
         if not stk.empty():
             node = stk.pop()
             if node.key < key:
                 no += 1
             else:
                 return no
                 
             node = node.right
         
     return no
Exemplo n.º 13
0
 def __init__(self):
     self.rst = stack.stack()
     self.snd = stack.stack()