Example #1
0
 def root_tree(self, u):
     try:
         tree = self.trees[u]
     except KeyError:
         tree = Tree()
         tree.data["minimum"] = u
         self.trees[u] = tree
         return tree
     return self._root_tree(tree)
Example #2
0
    def test(self):
        t1 = Tree()
        t2_1 = Tree(t1)
        t2_2 = Tree(t1)
        lca = TreeLeastCommonAncestor([t2_1, t2_2])
        self.assertEqual(lca.least_common_ancestor, t1)

        t3_1 = Tree(t2_1)
        t3_2 = Tree(t2_1)
        t3_3 = Tree(t2_2)

        self.assertEqual(
            TreeLeastCommonAncestor([t3_1, t3_2]).least_common_ancestor, t2_1)
        self.assertEqual(
            TreeLeastCommonAncestor([t3_1, t3_2, t3_3]).least_common_ancestor,
            t1)
        self.assertEqual(
            TreeLeastCommonAncestor([t2_1, t3_1]).least_common_ancestor, t2_1)
        self.assertEqual(
            TreeLeastCommonAncestor([t2_2, t3_1]).least_common_ancestor, t1)

        lca = TreeLeastCommonAncestor([t3_1, t3_2])

        try:
            self.assertItemsEqual(lca.get_all_paths_to_common_ancestor(),
                                  [t2_1, t3_1, t3_2])
        except AttributeError:
            self.assertCountEqual(lca.get_all_paths_to_common_ancestor(),
                                  [t2_1, t3_1, t3_2])
 def test_make_branch(self):
     t1 = Tree()
     t2 = t1.make_branch()
     self.assertEqual(t2.parent, t1)
     self.assertEqual(t1.number_of_subtrees(), 2)
 def test_make_branch(self):
     t1 = Tree()
     t2 = t1.make_branch()
     self.assertEqual(t2.parent, t1)
     self.assertEqual(t1.number_of_subtrees(), 2)