Esempio n. 1
0
 def test_graft_doubly_links_parent_and_child(self):
     parent = TreeNode(name = 'parent')
     child = TreeNode(name = 'child')
     parent.graft_child(child)
     #assert children of parent are correct
     child_ids_of_parent = [id(c) for c in parent.children]
     child_ids_of_parent_spec = [id(child)]
     self.assertItemsEqual(child_ids_of_parent, child_ids_of_parent_spec)
     #assert parent of children is correct
     self.assertEqual(id(child.parent), id(parent))
Esempio n. 2
0
 def test_trim_removes_double_link_between_parent_and_child_to_avoid_cycles(self):
     parent = TreeNode(name = 'parent')
     child = TreeNode(name = 'child')
     parent.graft_child(child)
     child.trim()
     #assert child no longer in children of parent
     child_ids_of_parent = [id(c) for c in parent.children]
     child_ids_of_parent_spec = list()
     self.assertItemsEqual(child_ids_of_parent, child_ids_of_parent_spec)
     #assert parent is None for the child
     self.assertIsNone(child.parent)
Esempio n. 3
0
 def test_TN_string_representation_displays_all_data_items_and_relatives_summary(self):
     parent = TreeNode()
     main = TreeNode(a=1, b=2)
     parent.graft_child(main)
     main.graft_child(TreeNode())
     main.graft_child(TreeNode())
     string_spec = 'TreeNode:\n'\
                   '    parent: yes\n'\
                   '    children: 2\n'\
                   '    a: 1\n'\
                   '    b: 2'
     self.assertEqual(str(main), string_spec)
Esempio n. 4
0
 def test_simple_binary_tree_with_three_levels_has_4_leaves(self):
     #first level
     a0 = TreeNode(name='a0')
     #second level
     b0 = TreeNode(name='b0')
     b1 = TreeNode(name='b1')
     #third level
     c0 = TreeNode(name='c0')
     c1 = TreeNode(name='c1')
     c2 = TreeNode(name='c2')
     c3 = TreeNode(name='c3')
     #attach everything
     a0.graft_child(b0)
     a0.graft_child(b1)
     b0.graft_child(c0)
     b0.graft_child(c1)
     b1.graft_child(c2)
     b1.graft_child(c3)
     #confirm leaves
     leaf_names = [leaf.name for leaf in a0.leaves]
     leaf_names_spec = ['c0','c1','c2','c3']
     self.assertItemsEqual(leaf_names, leaf_names_spec)
Esempio n. 5
0
 def test_node_self_identifies_as_not_leaf_if_any_children(self):
     node = TreeNode()
     node.graft_child(TreeNode())
     self.assertFalse(node.is_leaf)
Esempio n. 6
0
 def test_children_is_a_sequence(self):
     node = TreeNode()
     node.graft_child(TreeNode())
     self.assertTrue(node.children) #True, e.g. not empty list
Esempio n. 7
0
class Traversing_A_Tree(ut.TestCase):
    def setUp(self):
        self.root = TreeNode(name = 'root')
        self.a = TreeNode(name = 'a')
        self.b = TreeNode(name = 'b')
        self.c = TreeNode(name = 'c')

    def test_node_generates_in_order_traversal_from_self(self):
        self.root.graft_child(self.a)
        self.root.graft_child(self.b)
        self.b.graft_child(self.c)
        names = [node.name for node in self.root.in_order_nodes]
        names_spec = ['root', 'a', 'b', 'c']
        self.assertSequenceEqual(names, names_spec)

    def test_node_generates_post_order_traversal_from_self(self):
        self.root.graft_child(self.a)
        self.root.graft_child(self.b)
        self.b.graft_child(self.c)
        names = [node.name for node in self.root.post_order_nodes]
        names_spec = ['c', 'b', 'a', 'root']
        self.assertSequenceEqual(names, names_spec)

    def test_node_generates_only_self_when_no_parent_or_children(self):
        node_string_list_spec = ['root']
        node_string_list = [node.name for node in self.root.in_order_nodes]
        self.assertEqual(node_string_list_spec, node_string_list)

    def test_node_generates_only_nodes_including_and_below_self_in_tree(self):
        self.root.graft_child(self.a)
        self.root.graft_child(self.b)
        self.b.graft_child(self.c)
        names = [node.name for node in self.b.in_order_nodes]
        names_spec = ['b', 'c'] #i.e. 'a' is not in there
        self.assertSequenceEqual(names, names_spec)