Пример #1
0
 def test2Nodes(self):
     root = LCA.Node(1)
     root.left = LCA.Node(2)
     self.assertEqual(1, LCA.findLCA(
         root,
         1,
         2,
     ))
Пример #2
0
 def test_acyclic_one_lca(self):
     root = LCA.Node(1)
     node_2 = LCA.Node(2)
     node_3 = LCA.Node(3)
     root.succ = [node_2]
     node_2.pred = [root]
     node_2.succ = [node_3]
     node_3.pred = [node_2]
     self.assertEqual(LCA.dagLCA(root, node_2, node_3), [2])
Пример #3
0
 def testNotPath(self):
     root = LCA.Node(1)
     root.left = LCA.Node(2)
     root.right = LCA.Node(3)
     root.left.left = LCA.Node(4)
     self.assertEqual(-1, LCA.findLCA(
         root,
         2,
         9,
     ))
Пример #4
0
    def test_LCA(self):
        root = LCA.Node(1)
        a = LCA.Node(2)
        b = LCA.Node(3)
        c = LCA.Node(4)

        root.insert(a, "left")
        root.insert(b, "right")
        a.insert(c, "left")
        self.assertEqual(root.LowestCommonAncestor(c, b), 1)
        print("The LCA between Node C and Node B is expected 1 and got " +
              str(root.LowestCommonAncestor(c, b)))
Пример #5
0
 def test_cyclic(self):
     root = LCA.Node(1)
     node_2 = LCA.Node(2)
     node_3 = LCA.Node(3)
     node_4 = LCA.Node(4)
     root.succ = [node_2, node_3]
     root.pred = [node_3]
     node_2.succ = [node_3]
     node_2.pred = [root, node_4]
     node_3.succ = [root]
     node_3.pred = [node_2, node_4, root]
     node_4.succ = [node_2, node_3]
     self.assertEqual(LCA.dagLCA(root, node_2, node_3), [1, 4])
Пример #6
0
 def test_strongly_connected(self):
     root = LCA.Node(1)
     node_2 = LCA.Node(2)
     node_3 = LCA.Node(3)
     node_4 = LCA.Node(4)
     root.succ = [node_2, node_3, node_4]
     root.pred = [node_2, node_3, node_4]
     node_2.succ = [root, node_3, node_4]
     node_2.pred = [root, node_3, node_4]
     node_3.succ = [root, node_2, node_4]
     node_3.pred = [root, node_2, node_4]
     node_4.succ = [root, node_2, node_3]
     node_4.pred = [root, node_2, node_3]
     self.assertEqual(LCA.dagLCA(root, node_2, node_4), [1, 3])
Пример #7
0
    def test_insert_normal(self):
        root = LCA.Node(1)
        a = LCA.Node(2)
        b = LCA.Node(3)
        c = LCA.Node(4)

        root.insert(a, "left")
        root.insert(b, "right")
        a.insert(c, "left")
        if (self.assertEqual(root.data,
                             1), self.assertEqual(root.left.data, 2),
                self.assertEqual(root.right.data,
                                 3), self.assertEqual(root.left.left.data, 4)):
            print("Test for normal insertion ran succesfully")
        else:
            print("Test for normal insertion failed")
Пример #8
0
    def test_insert_already_occupied(self):
        root = LCA.Node(1)
        a = LCA.Node(2)
        b = LCA.Node(3)
        c = LCA.Node(4)

        root.insert(a, "left")
        root.insert(b, "right")
        root.insert(c, "left")

        if (self.assertEqual(root.data,
                             1), self.assertEqual(root.left.data, 2),
                self.assertNotEqual(root.right.data, 4)):
            print("Test for node already occupied ran successfully")
        else:
            print("Test for node already occupied failed")
Пример #9
0
    def test_insert_wrong_parameter(self):
        root = LCA.Node(1)
        a = LCA.Node(2)
        b = LCA.Node(3)
        c = LCA.Node(4)

        root.insert(a, "left")
        root.insert(b, "right")
        a.insert(c, "lft")

        if (self.assertEqual(root.data,
                             1), self.assertEqual(root.left.data, 2),
                self.assertEqual(root.right.data,
                                 3), self.assertEqual(root.left.left, None)):
            print("Test for wrong node parameter ran succesfully")
        else:
            print("Test for wrong node parameter failed")
Пример #10
0
    def test_empty_tree(self):
        # test a tree with no values

        root = lca.Node(None)
        result = lca.LCA_total(root, root.left, root.right)
        expected = False

        self.assertEqual(expected, result)
Пример #11
0
    def test_lca(self):
        root = LCA.Node(20)
        root.left = LCA.Node(8)
        root.right = LCA.Node(22)
        root.left.left = LCA.Node(4)
        root.left.right = LCA.Node(12)
        root.left.right.left = LCA.Node(10)
        root.left.right.right = LCA.Node(14)
        n1 = 10
        n2 = 14
        t = LCA.lca(root, n1, n2)
        print "LCA of %d and %d is %d" % (n1, n2, t.data)
        self.assertEqual(LCA.lca(root, n1, n2).data, 12)

        n1 = 14
        n2 = 8
        t = LCA.lca(root, n1, n2)
        print "LCA of %d and %d is %d" % (n1, n2, t.data)
        self.assertEqual(LCA.lca(root, n1, n2), t)

        n1 = 10
        n2 = 22
        t = LCA.lca(root, n1, n2)
        print "LCA of %d and %d is %d" % (n1, n2, t.data)
        self.assertEqual(LCA.lca(root, n1, n2), t)
Пример #12
0
 def test_LCA_base_cases(self):
     root = LCA.Node(1)
     node_2 = LCA.Node(2)
     node_3 = LCA.Node(3)
     root.succ = [node_2]
     node_2.succ = [node_3]
     node_2.pred = [root]
     node_3.pred = [node_2]
     self.assertEqual(LCA.dagLCA(root, root, node_3),
                      1)  # root and different key
     self.assertEqual(LCA.dagLCA(root, node_3, root),
                      1)  # different key and root
     self.assertEqual(LCA.dagLCA(root, node_3, node_3),
                      3)  # different key and root
     self.assertEqual(LCA.dagLCA(root, root, root), 1)  # root and root
     self.assertEqual(LCA.dagLCA(root, None, root), None)  # None and root
     self.assertEqual(LCA.dagLCA(root, root, None), None)  # root and none
     self.assertEqual(LCA.dagLCA(None, root, node_3), None)  # root is none
Пример #13
0
 def setUp(self):
     self.root = LCA.Node(4)
     self.root.left = LCA.Node(2)
     self.root.right = LCA.Node(6)
     self.root.left.left = LCA.Node(1)
     self.root.left.right = LCA.Node(3)
     self.root.right.left = LCA.Node(5)
     self.root.right.right = LCA.Node(7)
Пример #14
0
 def test_InvalidNode(self):
     root = LCA.Node(1)
     root.left = LCA.Node(2)
     root.right = LCA.Node(3)
     root.left.left = LCA.Node(4)
     root.left.right = LCA.Node(5)
     root.right.left = LCA.Node(6)
     root.right.right = LCA.Node(7)
     self.assertEqual(-1, LCA.lowestCommonAncestor(root, 4, 9), "Unfound node returns -1")
Пример #15
0
 def test_bothNodesInvalid(self):
     root = LCA.Node(1)
     root.left = LCA.Node(2)
     root.right = LCA.Node(3)
     root.left.left = LCA.Node(4)
     root.left.right = LCA.Node(5)
     root.right.left = LCA.Node(6)
     root.right.right = LCA.Node(7)
     self.assertEqual(-1, LCA.lowestCommonAncestor(root, 8, 9))
Пример #16
0
 def test_duplicatedNodes(self):
     root = LCA.Node(1)
     root.left = LCA.Node(2)
     root.right = LCA.Node(3)
     root.left.left = LCA.Node(4)
     root.left.right = LCA.Node(6)
     root.right.left = LCA.Node(6)
     root.right.right = LCA.Node(7)
     self.assertEqual(1, LCA.lowestCommonAncestor(root, 6, 7))
Пример #17
0
    def testLca(self):

        root = LCA.Node(1)
        root.left = LCA.Node(2)
        root.right = LCA.Node(3)
        root.left.left = LCA.Node(4)
        root.left.right = LCA.Node(5)
        root.right.left = LCA.Node(6)
        root.right.right = LCA.Node(7)
        self.assertEquals(2, findLCA(root, 4, 5))
Пример #18
0
 def test_lca(self):
     a = LCA.Node('a', None)
     b = LCA.Node('b', a)
     c = LCA.Node('c', a)
     d = LCA.Node('d', b)
     e = LCA.Node('e', b)
     f = LCA.Node('f', e)
     g = LCA.Node('g', e)
     h = LCA.Node('h', c)
     self.assertEqual('b', LCA.getLCA(d, f))
     self.assertEqual('e', LCA.getLCA(f, g))
     self.assertEqual('a', LCA.getLCA(g, c))
     self.assertEqual('a', LCA.getLCA(d, h))
     self.assertEqual('none', LCA.getLCA(a, b))
Пример #19
0
 def test_commonAncestorIsNode(self):
     root = LCA.Node(1)
     root.left = LCA.Node(2)
     root.right = LCA.Node(3)
     root.left.left = LCA.Node(4)
     root.left.right = LCA.Node(5)
     root.right.left = LCA.Node(6)
     root.right.right = LCA.Node(7)
     self.assertEqual(2, LCA.lowestCommonAncestor(root, 2, 4))
     self.assertEqual(2, LCA.lowestCommonAncestor(root, 2, 2))
     self.assertEqual(2, LCA.lowestCommonAncestor(root, 4, 2))
Пример #20
0
 def test_basicTree(self):
     root = LCA.Node(1)
     root.left = LCA.Node(2)
     root.right = LCA.Node(3)
     root.left.left = LCA.Node(4)
     root.left.right = LCA.Node(5)
     root.right.left = LCA.Node(6)
     root.right.right = LCA.Node(7)
     self.assertEqual(1, LCA.lowestCommonAncestor(root, 4, 3))
     self.assertEqual(1, LCA.lowestCommonAncestor(root, 2, 7))
     self.assertEqual(3, LCA.lowestCommonAncestor(root, 6, 7))
Пример #21
0
 def test_LCA(self):
     root = LCA.Node(20) 
     root.left = LCA.Node(8) 
     root.right = LCA.Node(22) 
     root.left.left = LCA.Node(4) 
     root.left.right = LCA.Node(12) 
     root.left.right.left = LCA.Node(10) 
     root.left.right.right = LCA.Node(14)
     self.assertEqual(LCA.lca(root, 10,14).data,12) 
     self.assertEqual(LCA.lca(root, 14,8).data,8) 
     self.assertEqual(LCA.lca(root, 10,22).data, 20)  
Пример #22
0
 def test_acyclic_with_multiple_lca_maxima_(self):
     root = LCA.Node(1)
     node_2 = LCA.Node(2)
     node_3 = LCA.Node(3)
     node_4 = LCA.Node(4)
     root.succ = [node_2, node_3]
     node_2.pred = [root, node_4]
     node_2.succ = [node_3]
     node_3.pred = [root, node_2, node_4]
     node_4.succ = [node_2, node_4]
     self.assertEqual(LCA.dagLCA(root, node_2, node_3), [1, 4])  #lca of 2
     # slash with dot/apostrophe signifies direction of relation
     #    4    5
     #    | \/ |
     #   .|./\.|.
     # 1->2 -> 3
     #  \_____/'
     node_5 = LCA.Node(5)
     node_4.succ = [node_2, node_3]
     node_3.pred = [root, node_2, node_4, node_5]
     node_2.pred = [root, node_4, node_5]
     self.assertEqual(LCA.dagLCA(root, node_2, node_3),
                      [1, 4, 5])  # lca of 3
Пример #23
0
 def testStandard(self):
     root = LCA.Node(1)
     root.left = LCA.Node(2)
     root.right = LCA.Node(3)
     root.left.left = LCA.Node(4)
     root.left.right = LCA.Node(5)
     root.right.left = LCA.Node(6)
     root.right.right = LCA.Node(7)
     self.assertEqual(2, LCA.findLCA(
         root,
         4,
         5,
     ))
Пример #24
0
    def test_tree_with_invalid_findlca_input(self):
        # test tree where n1 or n1 is not Node type

        root = lca.Node(1)
        root.left = lca.Node(2)
        root.right = lca.Node(3)
        root.left.left = lca.Node(4)
        root.left.right = lca.Node(5)
        root.right.left = lca.Node(6)
        root.right.right = lca.Node(7)

        result = lca.LCA_total(root, "string", root.left.left)
        expected = False
        self.assertEqual(expected, result)
Пример #25
0
    def test_tree_with_list_nodes(self):
        # test tree with lists as node values

        root = lca.Node([12, 7, 8, 100])
        root.left = lca.Node([1, "blacksmith", "July", 4])
        root.right = lca.Node([])
        root.left.left = lca.Node([13, ["purple"], 3456])
        root.left.right = lca.Node(["15", 15, 88])
        root.right.left = lca.Node(["North", 74, 98, 0, "00", "001Court"])
        root.right.right = lca.Node(["last"])

        result = lca.LCA_total(root, root.right.left, root.right.right)
        expected = root.right

        self.assertEqual(expected, result)
Пример #26
0
    def test_string_int_list_tree(self):
        # test tree with mixed string, int, lists as node values

        root = lca.Node(42)
        root.left = lca.Node("Software Engineering")
        root.right = lca.Node([1, "blacksmith", "July", 4])
        root.left.left = lca.Node(78)
        root.left.right = lca.Node("salt")
        root.right.left = lca.Node(["North", 74, 98, 0, "00", "001Court"])
        root.right.right = lca.Node(365)

        result = lca.LCA_total(root, root.right.right, root.left.right)
        expected = root

        self.assertEqual(expected, result)
Пример #27
0
    def test_tree_with_one_none_input(self):
        # test tree where one node is None

        root = lca.Node(1)
        root.left = lca.Node(2)
        root.right = lca.Node(3)
        root.left.left = lca.Node(4)
        root.left.right = lca.Node(5)
        root.right.left = lca.Node(6)
        root.right.right = lca.Node(7)

        result = lca.LCA_total(root, root.right.right, None)
        expected = False

        self.assertEqual(expected, result)
Пример #28
0
    def test_lca_as_root(self):
        # test a tree where the LCA is the root node

        root = lca.Node(1)
        root.left = lca.Node(2)
        root.right = lca.Node(3)
        root.left.left = lca.Node(4)
        root.left.right = lca.Node(5)
        root.right.left = lca.Node(6)
        root.right.right = lca.Node(7)

        result = lca.LCA_total(root, root.right.left, root.left.left)
        expected = root

        self.assertEqual(expected, result)
Пример #29
0
    def test_regular_tree(self):
        # test simple tree

        root = lca.Node(1)
        root.left = lca.Node(2)
        root.right = lca.Node(3)
        root.left.left = lca.Node(4)
        root.left.right = lca.Node(5)
        root.right.left = lca.Node(6)
        root.right.right = lca.Node(7)

        result = lca.LCA_total(root, root.right.left, root.right.right)
        expected = root.right

        # check for expected output
        self.assertEqual(expected, result)
Пример #30
0
    def test_LCA(self):
        # Driver program to test above function
        root = LCA.Node(1)
        self.assertIsNone(LCA.findLCA(root, 2, 3))
        root.left = LCA.Node(2)
        root.right = LCA.Node(3)
        root.left.left = LCA.Node(4)
        root.left.right = LCA.Node(5)
        root.right.left = LCA.Node(6)
        root.right.right = LCA.Node(7)

        self.assertEqual(2, LCA.findLCA(root, 4, 5).key)

        self.assertIsNone(LCA.findLCA(root, 4, 10))

        self.assertIsNone(LCA.findLCA(root, 10, 4))