本文整理汇总了Python中tree.Tree.postorder方法的典型用法代码示例。如果您正苦于以下问题:Python Tree.postorder方法的具体用法?Python Tree.postorder怎么用?Python Tree.postorder使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类tree.Tree
的用法示例。
在下文中一共展示了Tree.postorder方法的2个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: TreeOrderTestCase
# 需要导入模块: from tree import Tree [as 别名]
# 或者: from tree.Tree import postorder [as 别名]
class TreeOrderTestCase(unittest.TestCase):
def setUp(self):
self.tree = Tree(1)
self.tree.left = Tree(2)
self.tree.right = Tree(3)
self.tree.right.left = Tree(4)
self.tree.right.right = Tree(5)
def test_preorder_tree(self):
self.assertEqual(list(self.tree.preorder()), [1, 2, 3, 4, 5])
def test_postorder_tree(self):
self.assertEqual(list(self.tree.postorder()), [2, 4, 5, 3, 1])
def test_inorder_tree(self):
self.assertEqual(list(self.tree.inorder()), [2, 1, 4, 3, 5])
示例2: TestTree
# 需要导入模块: from tree import Tree [as 别名]
# 或者: from tree.Tree import postorder [as 别名]
class TestTree(unittest.TestCase):
def setUp(self):
self.tree = Tree()
def testInsertSingleValue(self):
self.tree.insert(1)
def testInsertTwoValues(self):
self.tree.insert(1)
self.tree.insert(2)
self.assertEqual(self.tree.root.data, 1)
self.assertEqual(self.tree.root.right.data, 2)
def testInsertTreeValues(self):
self.tree.insert(1)
self.tree.insert(2)
self.tree.insert(3)
self.assertEqual(self.tree.root.data, 1)
self.assertEqual(self.tree.root.right.data, 2)
self.assertEqual(self.tree.root.right.right.data, 3)
def testMakeSearchTree(self):
self.tree.insert(2)
self.tree.insert(1)
self.tree.insert(3)
self.assertEqual(self.tree.root.data, 2)
self.assertEqual(self.tree.root.left.data, 1)
self.assertEqual(self.tree.root.right.data, 3)
def decrement(value):
return value - 1
def increment(value):
return value + 1
def testPreorderTraversalManipulations(self):
self.tree.insert(2)
self.tree.insert(1)
self.tree.insert(3)
self.tree.insert(4)
self.tree.preorder(TestTree.increment)
self.assertEqual(self.tree.root.data, 3)
self.assertEqual(self.tree.root.left.data, 2)
self.assertEqual(self.tree.root.right.data, 4)
self.assertEqual(self.tree.root.right.right.data, 5)
def testInOrderTraversalManupulations(self):
self.tree.insert(2)
self.tree.insert(1)
self.tree.insert(3)
self.tree.insert(4)
self.tree.inorder(TestTree.decrement)
self.assertEqual(self.tree.root.data, 1)
self.assertEqual(self.tree.root.left.data, 0)
self.assertEqual(self.tree.root.right.data, 2)
self.assertEqual(self.tree.root.right.right.data, 3)
def testPostOrderTraversalManipulations(self):
self.tree.insert(2)
self.tree.insert(1)
self.tree.insert(3)
self.tree.insert(4)
self.tree.postorder(TestTree.decrement)
self.tree.postorder(TestTree.decrement)
self.assertEqual(self.tree.root.data, 0)
self.assertEqual(self.tree.root.left.data, -1)
self.assertEqual(self.tree.root.right.data, 1)
self.assertEqual(self.tree.root.right.right.data, 2)
def testPreOrderTraversalPure(self):
self.tree.insert(2)
self.tree.insert(1)
self.tree.insert(3)
self.tree.insert(4)
self.tree.preorder(TestTree.decrement, False)
self.assertEqual(self.tree.root.data, 2)
self.assertEqual(self.tree.root.left.data, 1)
self.assertEqual(self.tree.root.right.data, 3)
self.assertEqual(self.tree.root.right.right.data, 4)
def testInOrderTraversalPure(self):
self.tree.insert(2)
self.tree.insert(1)
self.tree.insert(3)
self.tree.insert(4)
self.tree.inorder(TestTree.increment, False)
self.assertEqual(self.tree.root.data, 2)
self.assertEqual(self.tree.root.left.data, 1)
self.assertEqual(self.tree.root.right.data, 3)
self.assertEqual(self.tree.root.right.right.data, 4)
def testPostOrderTraversalPure(self):
self.tree.insert(2)
self.tree.insert(1)
self.tree.insert(3)
#.........这里部分代码省略.........