本文整理汇总了Python中bst.BST.post_order方法的典型用法代码示例。如果您正苦于以下问题:Python BST.post_order方法的具体用法?Python BST.post_order怎么用?Python BST.post_order使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类bst.BST
的用法示例。
在下文中一共展示了BST.post_order方法的3个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: test_post_order_empty
# 需要导入模块: from bst import BST [as 别名]
# 或者: from bst.BST import post_order [as 别名]
def test_post_order_empty():
"""Test an empty Node returns empty list."""
from bst import BST
b = BST()
post_ord = b.post_order()
post_ord_list = []
for ii in post_ord:
post_ord_list.append(ii)
assert post_ord_list == []
示例2: test_post_order_solo
# 需要导入模块: from bst import BST [as 别名]
# 或者: from bst.BST import post_order [as 别名]
def test_post_order_solo():
"""Test running post-order on a tree with one Node."""
from bst import BST
b = BST([35])
post_ord = b.post_order()
post_ord_list = []
for ii in post_ord:
post_ord_list.append(ii)
assert post_ord_list == [35]
示例3: test_pre_post_order_traversal
# 需要导入模块: from bst import BST [as 别名]
# 或者: from bst.BST import post_order [as 别名]
def test_pre_post_order_traversal():
"""Tests both pre and post-order traversal"""
import types
bintree = BST()
with pytest.raises(TypeError):
bintree.pre_order(1) # No args/kwargs
with pytest.raises(TypeError):
bintree.pre_order(None)
assert isinstance(bintree.pre_order(), types.GeneratorType)
with pytest.raises(TypeError):
bintree.post_order(1) # No args/kwargs
with pytest.raises(TypeError):
bintree.post_order(None)
assert isinstance(bintree.post_order(), types.GeneratorType)
bintree.insert(5)
bintree.insert(3)
bintree.insert(7)
bintree.insert(6)
bintree.insert(2)
bintree.insert(4)
bintree.insert(1)
bintree.insert(9)
bintree.insert(10)
# 5
# / \
# 3 7
# /\ / \
# 2 4 6 9
# | |
# 1 10
pre_gen = bintree.pre_order()
post_gen = bintree.post_order()
assert pre_gen.next() == 5
assert pre_gen.next() == 3
assert pre_gen.next() == 2
assert pre_gen.next() == 1
assert pre_gen.next() == 4
assert pre_gen.next() == 7
assert pre_gen.next() == 6
assert pre_gen.next() == 9
assert pre_gen.next() == 10
assert post_gen.next() == 1
assert post_gen.next() == 2
assert post_gen.next() == 4
assert post_gen.next() == 3
assert post_gen.next() == 6
assert post_gen.next() == 10
assert post_gen.next() == 9
assert post_gen.next() == 7
assert post_gen.next() == 5
with pytest.raises(StopIteration):
pre_gen.next()
with pytest.raises(StopIteration):
post_gen.next()