Ejemplo n.º 1
0
 def iter_visit_first(self,fn):
     stack = Stack()
     stack.push(self)
     while stack.top:
         s = stack.pop()
         fn(s.root.value)
         if s.right:
             stack.push(s.right)
         if s.left:
             stack.push(s.left)
Ejemplo n.º 2
0
def postfixEval(postfixString):
    operateStack = Stack()
    tokenList = postfixString.split()

    for token in tokenList:
        if token in '0123456789':
            operateStack.push(int(token))

        else:
            operate2 = operateStack.pop()
            operate1 = operateStack.pop()
            result = doMath(token, operate1, operate2)
            operateStack.push(result)

    return operateStack.pop()
Ejemplo n.º 3
0
    def iter_visit_last(self,fn):
        stack = Stack()
        isLeaf = self.left is None and self.right is None
        if not isLeaf:
            stack.push(self)
            if self.right:
                stack.push(self.right)
            if self.left:
                stack.push(self.left)

        while stack.top:
            s = stack.pop()
            if s is self:
                fn(s.root.value)
                continue
            isLeaf = s.left is None and s.right is None
            if not isLeaf:
                stack.push(s)
                if self.right:
                    stack.push(s.right)
                if self.left:
                    stack.push(s.left)
            else:
                fn(s.root.value)
                fn(stack.pop().root.value)
                fn(stack.pop().root.value)
Ejemplo n.º 4
0
    def iter_visit_middel(self,fn):
        stack = Stack()
        isLeaf = self.left is None and self.right is None
        if not isLeaf:
            if self.right:
                stack.push(self.right)

            stack.push(self)

            if self.left:
                stack.push(self.left)

        while stack.top:
            s = stack.pop()
            if s is self:
                fn(s.root.value)
                s = stack.pop()
            isLeaf = s.left is None and s.right is None
            if not isLeaf:
                if s.right:
                    stack.push(s.right)
                stack.push(s)
                if s.left:
                    stack.push(s.left)
            else:
                fn(s.root.value)
                if stack.top:
                    fn(stack.pop().root.value)
Ejemplo n.º 5
0
def treeParser(parsedExpress):
    """

    :rtype: object
    """
    stack = Stack()
    for item in parsedExpress:
        if item == '(':
            stack.push(item)
        elif item == ')':
            st = stack.pop()
            if stack.pop() != '(':
                raise Exception('Bracket not compatible ')
            stack.push(st)
        elif item in '&|':
            tree = Tree(item)
            st = stack.pop()
            if st  == '(':
                raise Exception('Binary operator not compatible')
            tree.add_left(st)
            stack.push(tree)
        elif item == '!':
            stack.push(Tree(item))
        else:
            st = stack.pop()
            if st == '(':
                stack.push(st)
                stack.push(Tree(item))
            else:
                st.add_right(Tree(item))
                stack.push(st)

    tree = stack.pop()
    while stack.top:
        st = stack.pop()
        st.add_right(tree)
        if not stack.top:
            return(st)
        tree = st
Ejemplo n.º 6
0
def infixToPostfix(infixString):
    #运算符优先级
    prec = {'*': 3, '/': 3, '+': 2, '-': 2, '(': 1}

    #存放操作符
    opStack = Stack()
    #存放后缀表达式
    postfixList = []
    #以空白字符分割中缀表达式
    tokenList = infixString.split()

    for token in tokenList:
        if token in 'ABCDEFGHIJKLMNOPQRSTUVWXYZ' or token in '0123456789':
            postfixList.append(token)

        elif token == '(':
            opStack.push(token)

        elif token == ')':
            topToken = opStack.pop()
            while topToken != '(':
                postfixList.append(topToken)
                topToken = opStack.pop()

        else:
            while not opStack.isEmpty() and prec[
                    opStack.peek()] >= prec[token]:
                postfixList.append(opStack.pop())

            opStack.push(token)

    while not opStack.isEmpty():
        postfixList.append(opStack.pop())

    return ' '.join(postfixList)