Ejemplo n.º 1
0
    def test_cost_two_nodes(self):
        """cost -- two nodes"""

        test_cases = [
            (Tree(self.B, Tree(self.A), None), 22),
            (Tree(self.A, None, Tree(self.B)), 14),
        ]
        for tree, ans in test_cases:
            self.assertEqual(cost(tree), ans)
Ejemplo n.º 2
0
    def test_cost_five_nodes(self):
        """cost -- five nodes"""

        test_cases = [
            (Tree(self.C, Tree(self.B, Tree(self.A), None),
                  Tree(self.E, Tree(self.D), None)), 81),
            (Tree(self.D, Tree(self.A, None, Tree(self.C, Tree(self.B), None)),
                  Tree(self.E)), 65),
        ]
        for tree, ans in test_cases:
            self.assertEqual(cost(tree), ans)
Ejemplo n.º 3
0
    def test_cost_three_nodes(self):
        """cost -- three nodes"""
        test_cases = [(Tree(self.B, Tree(self.A), Tree(self.C)), 30),
                      (Tree(self.A, None, Tree(self.C, Tree(self.B),
                                               None)), 24)]

        for tree, ans in test_cases:
            self.assertEqual(cost(tree), ans)
Ejemplo n.º 4
0
    def test_str_BalancedTree(self):
        """str -- Balanced tree"""
        tree1 = Tree(Node('B'), Tree(Node('A')), Tree(Node('C')))
        tree2 = Tree(Node('F'), Tree(Node('E')), Tree(Node('G')))
        tree3 = Tree(Node('D'), tree1, tree2)

        test_cases = [(tree1, '[[A] B [C]]'), (tree2, '[[E] F [G]]'),
                      (tree3, '[[[A] B [C]] D [[E] F [G]]]')]

        for tree, ans in test_cases:
            s = str(tree)
            self.assertEqual(s, ans, f'Failed Str')
Ejemplo n.º 5
0
def make_min_tree(nodes: list):
    def _place_trees(*trees):
        left, right = sorted(trees)
        if cost(left) > cost(right):
            if left.right:
                right = _place_trees(left.right, right)
            left.right = right
            return left

        else:
            if right.left:
                left = _place_trees(right.left, left)
            right.left = left
            return right

    nodes = sorted(nodes, key=lambda x: x.value)

    tree = Tree(nodes.pop())

    while nodes:
        tree = _place_trees(Tree(nodes.pop()), tree)

    return tree
Ejemplo n.º 6
0
    def test_str_MissingSubtrees(self):
        """str -- Missing subtrees"""
        tree1 = Tree(Node('B'), None, Tree(Node('C')))
        tree2 = Tree(Node('F'), Tree(Node('E')), None)
        tree3 = Tree(Node('D'), tree1, tree2)
        tree4 = Tree(Node('F'), None, None)
        tree5 = Tree(Node('D'), tree1, tree4)

        test_cases = [
            (tree1, '[_ B [C]]'),
            (tree2, '[[E] F _]'),
            (tree3, '[[_ B [C]] D [[E] F _]]'),
            (tree4, '[F]'),
            (tree5, '[[_ B [C]] D [F]]'),
        ]

        for tree, ans in test_cases:
            s = str(tree)
            self.assertEqual(s, ans, f'Failed str')
Ejemplo n.º 7
0
    def test_extras(self):
        test1 = Tree(
            Node('IW'), Tree(Node('DG')),
            Tree(
                Node('SV'),
                Tree(
                    Node('NI'),
                    Tree(
                        Node('KC'),
                        Tree(Node('JT'), None,
                             Tree(Node('JU'), None, Tree(Node('JZ')))),
                        Tree(Node('NH'))),
                    Tree(
                        Node('RV'),
                        Tree(Node('OS'), None,
                             Tree(Node('PK'), None, Tree(Node('QC')))), None)),
                Tree(
                    Node('TL'), Tree(Node('TB')),
                    Tree(
                        Node('YX'),
                        Tree(Node('XU'),
                             Tree(Node('WB'), Tree(Node('TX')), None), None),
                        None))))
        test2 = Tree(
            Node('ZI'),
            Tree(
                Node('IW'), Tree(Node('DG')),
                Tree(
                    Node('SV'),
                    Tree(
                        Node('NI'),
                        Tree(
                            Node('KC'),
                            Tree(Node('JT'), None,
                                 Tree(Node('JU'), None, Tree(Node('JZ')))),
                            Tree(Node('NH'))),
                        Tree(
                            Node('RV'),
                            Tree(Node('OS'), None,
                                 Tree(Node('PK'), None, Tree(Node('QC')))),
                            None)),
                    Tree(
                        Node('TL'), Tree(Node('TB')),
                        Tree(
                            Node('YX'),
                            Tree(Node('XU'),
                                 Tree(Node('WB'), Tree(Node('TX')), None),
                                 None), None)))), None)

        ans2 = Tree(
            Node('HX'), Tree(Node('ES'), None, Tree(Node('GU'))),
            Tree(
                Node('NE'),
                Tree(
                    Node('KV'), Tree(Node('KD')),
                    Tree(Node('NA'), Tree(Node('MY'), Tree(Node('KW')), None),
                         None)),
                Tree(
                    Node('ZG'),
                    Tree(
                        Node('WN'),
                        Tree(
                            Node('QJ'),
                            Tree(Node('PO'),
                                 Tree(Node('PB'), None, Tree(Node('PI'))),
                                 None),
                            Tree(Node('VE'),
                                 Tree(Node('RH'), None, Tree(Node('SU'))),
                                 None)), Tree(Node('XC'))), Tree(Node('ZO')))))

        test_cases = [
            (test1, Tree(Node('ES'), None, Tree(Node('GU')))),
            (test2, ans2),
        ]

        for tree, ans in test_cases:
            self.assertNotEqual(tree, ans)
Ejemplo n.º 8
0
 def test_equality_leaf(self):
     """Equality -- Leaf"""
     tree1 = Tree(Node('A'))
     tree2 = Tree(Node('A'))
     self.assertEqual(True, tree1 == tree2, "Failed tree equality")
Ejemplo n.º 9
0
 def test_str_Leaf(self):
     """str -- Leaf"""
     self.assertEqual(str(Tree(Node('A'))), '[A]', 'Failed str')
Ejemplo n.º 10
0
 def test_Inequality_SameNodesDifferentStructure(self):
     """Inequality -- Same nodes, different structure"""
     tree1 = Tree(Node('B'), Tree(Node('A')), Tree(Node('C')))
     tree2 = Tree(Node('A'), None, Tree(Node('C'), Tree(Node('B')), None))
     self.assertTrue(tree1 != tree2, "Failed tree inequality")
Ejemplo n.º 11
0
 def test_Inequality_MissingSubtrees(self):
     """Inequality -- Missing subtrees"""
     tree1 = Tree(Node('B'), Tree(Node('A')), Tree(Node('C')))
     tree2 = Tree(Node('B'), None, Tree(Node('C')))
     self.assertTrue(tree1 != tree2, "Failed tree inequality")
Ejemplo n.º 12
0
 def test_Inequality_BalancedTree(self):
     """Inequality -- Balanced tree"""
     tree1 = Tree(Node('B'), Tree(Node('A')), Tree(Node('C')))
     tree2 = Tree(Node('B'), Tree(Node('A')), Tree(Node('D')))
     self.assertTrue(tree1 != tree2, "Failed tree inequality")
Ejemplo n.º 13
0
 def test_Inequality_SingleNode(self):
     """Inequality -- Single node"""
     tree1 = Tree(Node('A'))
     tree2 = Tree(Node('B'))
     self.assertTrue(tree1 != tree2, "Failed tree inequality")
Ejemplo n.º 14
0
 def test_cost_leaf(self):
     """cost -- leaf"""
     tree_a = Tree(self.A)
     self.assertEqual(cost(tree_a), 10)