当前位置: 首页>>代码示例>>Python>>正文


Python Tree.postorder方法代码示例

本文整理汇总了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])
开发者ID:klugjohannes,项目名称:arithmetic-parser,代码行数:19,代码来源:test.py

示例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)
#.........这里部分代码省略.........
开发者ID:itsankoff,项目名称:AI,代码行数:103,代码来源:unittests.py


注:本文中的tree.Tree.postorder方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。