From f4b346c78ec92376b264e64be4b11ae36e621074 Mon Sep 17 00:00:00 2001 From: Sushmith Date: Fri, 15 Feb 2019 22:00:08 +0530 Subject: [PATCH] Add test cases for inorder traversal (#473) * Add test cases for inorder traversal * Fix k_closest_points test cases --- tests/test_heap.py | 8 +++--- tests/test_tree.py | 64 ++++++++++++++++++++++++++-------------------- 2 files changed, 40 insertions(+), 32 deletions(-) diff --git a/tests/test_heap.py b/tests/test_heap.py index 6814c6196..f445f2cfd 100644 --- a/tests/test_heap.py +++ b/tests/test_heap.py @@ -56,11 +56,11 @@ def test_max_sliding_window(self): def test_k_closest_points(self): points = [(1, 0), (2, 3), (5, 2), (1, 1), (2, 8), (10, 2), (-1, 0), (-2, -2)] - self.assertEqual([(-1, 0), (1, 0)], k_closest(points, 2)) - self.assertEqual([(1, 1), (-1, 0), (1, 0)], k_closest(points, 3)) - self.assertEqual([(-2, -2), (1, 1), (1, 0), (-1, 0)], k_closest(points, 4)) + self.assertEqual([(-1, 0), (1, 0)], k_closest_points.k_closest(points, 2)) + self.assertEqual([(1, 1), (-1, 0), (1, 0)], k_closest_points.k_closest(points, 3)) + self.assertEqual([(-2, -2), (1, 1), (1, 0), (-1, 0)], k_closest_points.k_closest(points, 4)) self.assertEqual([(10, 2), (2, 8), (5, 2), (-2, -2), (2, 3), - (1, 0), (-1, 0), (1, 1)], k_closest(points, 8)) + (1, 0), (-1, 0), (1, 1)], k_closest_points.k_closest(points, 8)) if __name__ == "__main__": diff --git a/tests/test_tree.py b/tests/test_tree.py index a9d53ca6d..1fbcd6a2e 100644 --- a/tests/test_tree.py +++ b/tests/test_tree.py @@ -2,45 +2,53 @@ preorder, preorder_rec, postorder, - postorder_rec + postorder_rec, + inorder, + inorder_rec ) import unittest + class Node: def __init__(self, val, left=None, right=None): self.val = val self.left = left self.right = right - + class TestTraversal(unittest.TestCase): - + def test_preorder(self): - n1 = Node(100) - n2 = Node(50) - n3 = Node(150) - n4 = Node(25) - n5 = Node(75) - n6 = Node(125) - n7 = Node(175) - n1.left, n1.right = n2, n3 - n2.left, n2.right = n4, n5 - n3.left, n3.right = n6, n7 - self.assertEqual([100, 50, 25, 75, 150, 125, 175], preorder(n1)) - self.assertEqual([100, 50, 25, 75, 150, 125, 175], preorder_rec(n1)) - + tree = create_tree() + self.assertEqual([100, 50, 25, 75, 150, 125, 175], preorder(tree)) + self.assertEqual([100, 50, 25, 75, 150, 125, 175], preorder_rec(tree)) + def test_postorder(self): - n1 = Node(100) - n2 = Node(50) - n3 = Node(150) - n4 = Node(25) - n5 = Node(75) - n6 = Node(125) - n7 = Node(175) - n1.left, n1.right = n2, n3 - n2.left, n2.right = n4, n5 - n3.left, n3.right = n6, n7 - self.assertEqual([25, 75, 50, 125, 175, 150, 100], postorder(n1)) - self.assertEqual([25, 75, 50, 125, 175, 150, 100], postorder_rec(n1)) + tree = create_tree() + self.assertEqual([25, 75, 50, 125, 175, 150, 100], postorder(tree)) + self.assertEqual([25, 75, 50, 125, 175, 150, 100], postorder_rec(tree)) + + def test_inorder(self): + tree = create_tree() + self.assertEqual([25, 50, 75, 100, 125, 150, 175], inorder(tree)) + self.assertEqual([25, 50, 75, 100, 125, 150, 175], inorder_rec(tree)) + + +def create_tree(): + n1 = Node(100) + n2 = Node(50) + n3 = Node(150) + n4 = Node(25) + n5 = Node(75) + n6 = Node(125) + n7 = Node(175) + n1.left, n1.right = n2, n3 + n2.left, n2.right = n4, n5 + n3.left, n3.right = n6, n7 + return n1 + + +if __name__ == '__main__': + unittest.main()