本文整理汇总了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