def test_CommonAncestorIsTarg(self): # print("Test5: commonAncestorIsTarget") root = Node(1) root.left = Node(3) root.right = Node(5) root.left.left = Node(6) root.left.right = Node(8) self.assertEqual( 1, LCA.findLCA(root, 1, 3).key, "The output should be 1 since it is both the ancestor node and the target node" ) self.assertEqual( 1, LCA.findLCA(root, 1, 5).key, "The output should be 1 since it is both the ancestor node and the target node" ) self.assertEqual( 3, LCA.findLCA(root, 3, 6).key, "The output should be 3 since it is both the ancestor node and the target node" ) self.assertEqual( 3, LCA.findLCA(root, 3, 8).key, "The output should be 3 since it is both the ancestor node and the target node" )
def testFindLCA(self): root = Node(1) root.left = Node(2) root.right = Node(3) root.left.left = Node(4) root.left.right = Node(5) root.right.left = Node(6) root.right.right = Node(7) # test all nodes are None self.assertEqual(LCA.findLCA(None, None, None), None) #test when root is euqal to node self.assertEqual(LCA.findLCA(root, 1, 1).key, 1) #test if n1 is not present self.assertEqual(LCA.findLCA(root, None, 2), None) #test if n2 is not present self.assertEqual(LCA.findLCA(root, 2, None), None) #test when node is value is not in tree self.assertEqual(LCA.findLCA(root, 2, 13), None) #test LCA for different values self.assertEqual(findLCA(root, 3, 7).key, 3) self.assertEqual(findLCA(root, 4, 6).key, 1) self.assertEqual(findLCA(root, 2, 5).key, 2) self.assertEqual(findLCA(root, 3, 4).key, 1)
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)
def test2Nodes(self): root = LCA.Node(1) root.left = LCA.Node(2) self.assertEqual(1, LCA.findLCA( root, 1, 2, ))
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])
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, ))
def test(self): root = BinaryTree.Node(1) a = BinaryTree.Node(2) b = BinaryTree.Node(3) root.insertLeft(a) root.insertRight(b) self.assertEqual(LCA.findLCA(root.getGraph(), 2, 2), 2) self.assertEqual(LCA.findLCA(root.getGraph(), 3, 3), 3)
def test_DAGLCA(self): root = LCA.DAGNode(1) b = LCA.DAGNode(3) c = LCA.DAGNode(4) d = LCA.DAGNode(5) e = LCA.DAGNode(6) f = LCA.DAGNode(7) g = LCA.DAGNode(8) h = LCA.DAGNode(9) z = LCA.DAGNode(99) root.insert(b) root.insert(c) root.insert(d) b.insert(g) b.insert(e) c.insert(f) g.insert(h) e.insert(h) f.insert(h) self.assertEqual(h.LCA(b, root), b) #test 2 nodes that are close to each other self.assertEqual(h.LCA(d, root), root) #test when only the root is common self.assertEqual(h.LCA(z, root), None) #test no common
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))
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))
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)
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)))
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])
def test_CharsInNumberTree(self): root = Node(1) root.left = Node(2) root.right = Node(31) root.left.left = Node(4) root.left.right = Node(10) root.right.left = Node(16) root.right.right = Node(7) root.left.left.left = Node(24) self.assertEqual( None, LCA.findLCA(root, 'a', 'b'), "Should be -1 but got: " + str(LCA.findLCA(root, 'a', 'b')))
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])
def test_get_path(self): # First construct an arbitrary binary tree tree = BTree.BTree(78, 14, 9) tree.insert_left_node(56) tree.insert_right_node(300) tree.insert_left_node(164) tree.get_lt().insert_right_node(4) tree.get_rt().insert_left_node(5) tree.get_rt().get_rt().insert_left_node(1000) tree.get_lt().get_lt().get_lt().insert_right_node(99) print("\nTesting get_path() for the following tree:\n") tree.pretty_print() test_path = [] LCA.get_path(tree, 14, test_path) self.assertEqual(test_path, [78, 164, 56, 14], "Should be '[78, 164, 56, 14]'") test_path = [] LCA.get_path(tree, 5, test_path) self.assertEqual(test_path, [78, 300, 5], "Should be '[78, 300, 5'") test_path = [] LCA.get_path(tree, 4, test_path) self.assertEqual(test_path, [78, 164, 4], "Should be '[78, 164, 4]'") test_path = [] LCA.get_path(tree, 99, test_path) self.assertEqual(test_path, [78, 164, 56, 14, 99], "Should be '[78, 164, 56, 14, 99]'")
def test_CharsInCharTree(self): root = Node('a') root.left = Node('b') root.right = Node('c') root.left.left = Node('d') root.left.right = Node('e') root.right.left = Node('f') root.right.right = Node('g') root.left.left.left = Node('h') self.assertEqual( 'b', LCA.findLCA(root, 'd', 'e').key, "Should be b but got: " + str(LCA.findLCA(root, 'd', 'e')))
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")
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")
def __call__(self, T, leafRanks=None): self.__init__() # to indicate whether we are in the 2nd pass self.setLeafRanks = leafRanks T.dfs_traverse(self) lca = LCA.LCA(self.pa) return (lca, self.nodes, self.getLeafRanks)
def test_very_big_tree(self): # test binary tree of height 9 root = tree(height=9) result = lca.find_lca(root, root.right.right.right.right, root.right.right.right.right.right.right.right) expected = root.right.right.right.right self.assertEqual(expected, result) result2 = lca.find_lca(root, root.left.left.right.left, root.left.left.left.right.right) expected2 = root.left.left self.assertEqual(expected2, result2)
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))
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))
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")
def test_get_lca(self): # First construct an arbitrary binary tree tree = BTree.BTree(0, 1, 2) tree.get_lt().insert_right_node(3) tree.get_lt().get_rt().insert_left_node(5) tree.get_lt().insert_left_node(8) tree.get_lt().get_rt().insert_right_node(13) tree.get_rt().insert_left_node(21) tree.get_rt().insert_right_node(34) tree.get_rt().get_rt().insert_left_node(55) print("\nTesting get_LCA() for the following tree:\n") tree.pretty_print() self.assertEqual(LCA.get_LCA(tree, 1, 2), 0, "Should be 0") self.assertEqual(LCA.get_LCA(tree, 5, 13), 3, "Should be 3") self.assertEqual(LCA.get_LCA(tree, 55, 21), 2, "Should be 2") self.assertEqual(LCA.get_LCA(tree, 5, 55), 0, "Should be 0")
def test_OneNodeNotPresent(self): # print("Test4: testOneNodeNotPresent:") root = Node(1) root.left = Node(6) self.assertEqual( None, LCA.findLCA(root, 6, 7).key, " The output should -1 since one of the nodes is missing")
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")
def test_for_multiple_nodes(self): # test search for more than 2 nodes (set to return error message) G = nx.DiGraph() G.add_nodes_from([1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14]) G.add_edges_from([(1, 5), (1, 9), (2, 6), (2, 10), (3, 7), (4, 8), (4, 12)]) G.add_edges_from([(5, 13), (9, 14), (6, 13), (10, 14), (7, 13), (11, 14), (8, 13), (12, 14)]) result = lca.LCA_total(G, 14, 12, 8) expected = "Cannot compute lca of more than two nodes." self.assertEqual(expected, result) result2 = lca.LCA_total(G, 13, 14, 11) expected2 = "Cannot compute lca of more than two nodes." self.assertEqual(expected2, result2)
def test(self): root = BinaryTree.Node(1) a = BinaryTree.Node(2) b = BinaryTree.Node(3) c = BinaryTree.Node(4) d = BinaryTree.Node(5) e = BinaryTree.Node(6) f = BinaryTree.Node(7) root.insertLeft(a) root.insertRight(b) a.insertLeft(c) a.insertRight(d) b.insertLeft(e) b.insertRight(f) self.assertEqual(LCA.findLCA(root.getGraph(), 1, 8), -1) self.assertEqual(LCA.findLCA(b.getGraph(), 6, 4), -1)
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