def test_traverse_1(self):
        r = TreeNode(0)
        pre_order, in_order, post_order = [0], [0], [0]
        act_pre, act_in, act_post = TU.pre_order(r), TU.in_order_iter(
            r), TU.post_order(r)

        self.assertEqual(pre_order, act_pre)
        self.assertEqual(in_order, act_in)
        self.assertEqual(post_order, act_post)
    def test_traverse_4(self):
        r = TreeNode(0)
        r.left = TreeNode(1)
        r.right = TreeNode(2)

        pre_order, in_order, post_order = [0, 1, 2], [1, 0, 2], [1, 2, 0]
        act_pre, act_in, act_post = TU.pre_order(r), TU.in_order_iter(
            r), TU.post_order(r)

        self.assertEqual(pre_order, act_pre)
        self.assertEqual(in_order, act_in)
        self.assertEqual(post_order, act_post)
    def test_traverse_5(self):
        r = TreeNode(0)
        r.left = TreeNode(1)
        r.right = TreeNode(2)

        r.left.left = TreeNode(3)
        r.left.right = TreeNode(4)

        r.right.right = TreeNode(6)

        pre_order, in_order, post_order = ([0, 1, 3, 4, 2, 6],
                                           [3, 1, 4, 0, 2, 6],
                                           [3, 4, 1, 6, 2, 0])
        act_pre, act_in, act_post = TU.pre_order(r), TU.in_order_iter(
            r), TU.post_order(r)

        self.assertEqual(pre_order, act_pre)
        self.assertEqual(in_order, act_in)
        self.assertEqual(post_order, act_post)
    def test_traverse_6(self):
        root = TreeNode(0)
        root.left = TreeNode(1)
        root.right = TreeNode(2)

        root.left.left = TreeNode(3)
        root.left.right = TreeNode(4)
        root.left.left.right = TreeNode(5)

        root.right.right = TreeNode(6)
        root.right.right.left = TreeNode(7)

        pre_order, in_order, post_order = ([0, 1, 3, 5, 4, 2, 6, 7],
                                           [3, 5, 1, 4, 0, 2, 7, 6],
                                           [5, 3, 4, 1, 7, 6, 2, 0])
        act_pre, act_in, act_post = \
            TU.pre_order(root), TU.in_order_iter(root), TU.post_order(root)

        self.assertEqual(pre_order, act_pre)
        self.assertEqual(in_order, act_in)
        self.assertEqual(post_order, act_post)
示例#5
0
        -746, -747, -748, -749, -750, -751, -752, -753, -754, -755, -756, -757,
        -758, -759, -760, -761, -762, -763, -764, -765, -766, -767, -768, -769,
        -770, -771, -772, -773, -774, -775, -776, -777, -778, -779, -780, -781,
        -782, -783, -784, -785, -786, -787, -788, -789, -790, -791, -792, -793,
        -794, -795, -796, -797, -798, -799, -800, -801, -802, -803, -804, -805,
        -806, -807, -808, -809, -810, -811, -812, -813, -814, -815, -816, -817,
        -818, -819, -820, -821, -822, -823, -824, -825, -826, -827, -828, -829,
        -830, -831, -832, -833, -834, -835, -836, -837, -838, -839, -840, -841,
        -842, -843, -844, -845, -846, -847, -848, -849, -850, -851, -852, -853,
        -854, -855, -856, -857, -858, -859, -860, -861, -862, -863, -864, -865,
        -866, -867, -868, -869, -870, -871, -872, -873, -874, -875, -876, -877,
        -878, -879, -880, -881, -882, -883, -884, -885, -886, -887, -888, -889,
        -890, -891, -892, -893, -894, -895, -896, -897, -898, -899, -900, -901,
        -902, -903, -904, -905, -906, -907, -908, -909, -910, -911, -912, -913,
        -914, -915, -916, -917, -918, -919, -920, -921, -922, -923, -924, -925,
        -926, -927, -928, -929, -930, -931, -932, -933, -934, -935, -936, -937,
        -938, -939, -940, -941, -942, -943, -944, -945, -946, -947, -948, -949,
        -950, -951, -952, -953, -954, -955, -956, -957, -958, -959, -960, -961,
        -962, -963, -964, -965, -966, -967, -968, -969, -970, -971, -972, -973,
        -974, -975, -976, -977, -978, -979, -980, -981, -982, -983, -984, -985,
        -986, -987, -988, -989, -990, -991, -992, -993, -994, -995, -996, -997,
        -998, -999
    ]

    s = Solution()
    r = s.buildTree(pre, ino)
    ts = TreeUtils(r)
    print(ts.in_order(r))
    print(ts.pre_order(r))
    print(ts.post_order(r))