def test_lca(empty_tree, rand_tree):
    # assert function returns -1 for empty tree
    assert lca.find_lca(empty_tree.root, 4, 5) == -1

    # assert returns root for lca of root.left and root.right
    assert lca.find_lca(rand_tree.root, rand_tree.root.left,
                        rand_tree.root.right) == rand_tree.root
Beispiel #2
0
 def test_negative_root_value_returns_none(self):
     negative_root = -1
     n1, n2, = 9, 10
     self.assertIsNone(find_lca(GOOD_MATRIX, negative_root, n1, n2))
Beispiel #3
0
 def test_empty_matrix_returns_none(self):
     empty_matrix = []
     root = 6
     n1, n2 = 9, 10
     self.assertIsNone(find_lca(empty_matrix, root, n1, n2))
Beispiel #4
0
 def test_lca_is_root(self):
     root = 6
     n1, n2 = 9, 10
     self.assertEqual(find_lca(GOOD_MATRIX, root, n1, n2), root)
Beispiel #5
0
 def test_nonexistent_node_returns_none(self):
     root = 6
     bad_node = 12
     node = 2
     self.assertIsNone(find_lca(GOOD_MATRIX, root, bad_node, node))
Beispiel #6
0
 def test_given_root_is_wrong_returns_none(self):
     wrong_root = 8
     n1 = 10
     n2 = 5
     self.assertIsNone(find_lca(GOOD_MATRIX, wrong_root, n1, n2))
Beispiel #7
0
 def test_bad_matrix_returns_none(self):
     root = 4
     n1 = 0
     n2 = 3
     self.assertIsNone(find_lca(BAD_MATRIX, root, n1, n2))
Beispiel #8
0
    def test_find_lca(self):
        #Testing lca of root node and root node
        self.assertEqual(lca.find_lca(root, 1, 1), 1)
        #Tesing lca of root node and farthest node
        self.assertEqual(lca.find_lca(root, 1, 7), 1)
        #Testing lca of root node and middle node
        self.assertEqual(lca.find_lca(root, 1, 3), 1)

        #Testing lca of two nodes in tree
        self.assertEqual(lca.find_lca(root, 4, 5), 2)
        self.assertEqual(lca.find_lca(root, 6, 7), 3)
        self.assertEqual(lca.find_lca(root, 4, 6), 1)
        self.assertEqual(lca.find_lca(root, 5, 7), 1)
        self.assertEqual(lca.find_lca(root, 4, 7), 1)
        self.assertEqual(lca.find_lca(root, 5, 6), 1)

        #Testing input errors
        self.assertEqual(lca.find_lca(root, 100, 100), -1)
        self.assertEqual(lca.find_lca(root, 10, 6), -1)
        self.assertEqual(lca.find_lca(root, -3, 4), -1)

        #Testing for invalid input
        self.assertEqual(lca.find_lca(root, "a", 5), -1)
        self.assertEqual(lca.find_lca(root, 100, "a"), -1)
        self.assertEqual(lca.find_lca("a", 100, 100), -1)