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)