本文整理匯總了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()
示例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())
示例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()
示例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