當前位置: 首頁>>代碼示例>>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;未經允許,請勿轉載。