当前位置: 首页>>代码示例>>Python>>正文


Python Stack.length方法代码示例

本文整理汇总了Python中Stack.length方法的典型用法代码示例。如果您正苦于以下问题:Python Stack.length方法的具体用法?Python Stack.length怎么用?Python Stack.length使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在Stack的用法示例。


在下文中一共展示了Stack.length方法的4个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。

示例1: postOrderTravel2

# 需要导入模块: import Stack [as 别名]
# 或者: from Stack import length [as 别名]
    def postOrderTravel2(self):
        """
        second method of traveling tree by post-order sequence
        """
        newTree = self.clone()
        if not newTree:
            return

        stack = Stack()
        # push tree into stack twice
        stack.push(newTree)
        stack.push(newTree)

        while stack.length():
            # pop out the first node
            item = stack.pop()

            # first pop pushes node's children into stack
            # second pop visits the node
            # 
            # if item equals the top item of stack means that item's children have not been visited yet
            # then push them into stack
            if stack.length() and item == stack.top():
                if item.getRightChild():
                    stack.push(item.getRightChild())
                    stack.push(item.getRightChild())

                if item.getLeftChild():
                    stack.push(item.getLeftChild())
                    stack.push(item.getLeftChild())
            else:
                print item.getValue()
开发者ID:xiaoyueli,项目名称:MOOC,代码行数:34,代码来源:week3_travelTree.py

示例2: postOrderTravel

# 需要导入模块: import Stack [as 别名]
# 或者: from Stack import length [as 别名]
    def postOrderTravel(self):
        """
        Travel tree by post-order sequence
        """
        newTree = self.clone()
        if not newTree:
            return
        stack= Stack()
        last = None
        stack.push(newTree)

        while stack.length():
            item = stack.top()
            if not item.getLeftChild() and not item.getRightChild() \
                or last == item.getLeftChild() and not item.getRightChild() \
                or last == item.getRightChild():
                # visit the node who is either leaf node 
                # or node without right child and the left child has been visited
                # or node whose right child is the last one been visited
                print stack.pop().getValue()             
                last = item
            else:
                # push right child into stack first
                if item.getRightChild():
                    stack.push(item.getRightChild())

                # push left child into stack second
                if item.getLeftChild():
                    stack.push(item.getLeftChild())
开发者ID:xiaoyueli,项目名称:MOOC,代码行数:31,代码来源:week3_travelTree.py

示例3: midOrderTravel

# 需要导入模块: import Stack [as 别名]
# 或者: from Stack import length [as 别名]
    def midOrderTravel(self):
        """
        Travel tree by mid-order sequence
        """
        newTree = self.clone()
        if not newTree:
            return
        stack = Stack()
        while newTree or stack.length():
            while (newTree):
                stack.push(newTree)
                newTree = newTree.getLeftChild()

            if stack.length():
                node = stack.pop()
                print node.getValue()
                newTree = node.getRightChild()
开发者ID:xiaoyueli,项目名称:MOOC,代码行数:19,代码来源:week3_travelTree.py

示例4: convertStack

# 需要导入模块: import Stack [as 别名]
# 或者: from Stack import length [as 别名]
 def convertStack(self,stack,newBase,oldBase):
     revStack = Stack()
     while stack.length() != 0:
         revStack.push(stack.pop())
     while revStack.length() != 0:
         stack.push(Operation.__convertFromDecimal(
             Operation.__convertToDecimal(revStack.pop(),oldBase),newBase))
     del revStack
开发者ID:WrathRockeu,项目名称:CS2513-L4,代码行数:10,代码来源:Operation.py


注:本文中的Stack.length方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。