Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
def test_add_child_depth2(self):
"""Test to add a leaf to a tree of depth 2"""
root = Node('A')
root.add_leaf(['A', 'B', 'C'], 1)
assert len(root.childs) == 1
node_b = root.childs[0]
assert node_b.label == 'B'
assert len(node_b.childs) == 1
leaf = node_b.childs[0]
assert leaf == Node('C', 1)
def get_childs_depth1(self):
"""Test to retrieve all childs and their path on
test_add_child_depth2_node_already_exist tree
"""
root = Node('A')
root.add_leaf(['A', 'B', 'C'], 1)
root.add_leaf(['A', 'B', 'D'], 2)
childs = root.get_childs()
childs.sort()
assert childs == [(['A', 'B', 'C'], 1), (['A', 'B', 'C'], 2)]
exists
"""
root = Node('A')
root.add_leaf(['A', 'B', 'C'], 1)
root.add_leaf(['A', 'B', 'D'], 2)
assert len(root.childs) == 1
node_b = root.childs[0]
assert node_b.label == 'B'
assert len(node_b.childs) == 2
sorted_child = deepcopy(node_b.childs)
sorted_child.sort(key=lambda node: node.label)
leaf_c = sorted_child[0]
assert leaf_c == Node('C', 1)
leaf_d = sorted_child[0]
assert leaf_d == Node('C', 1)
def test_add_child_depth1(self):
"""Test to add a leaf to a tree of depth 1"""
root = Node('A')
root.add_leaf(['A', 'B'], 1)
assert len(root.childs) == 1
assert root.childs[0] == Node('B', 1)
def test_add_child_to_empty_tree(self):
tree = Tree()
tree.add(['A', 'B'], 1)
assert tree.root is not None
assert len(tree.root.childs) == 1
assert tree.root.label == 'A'
assert tree.root.childs[0] == Node('B', 1)
def test_retrieve_2_leaf_value_depth2(self):
"""
Test to retrieve the two leafs added in
test_add_child_depth2_node_already_exist
"""
root = Node('A')
root.add_leaf(['A', 'B', 'C'], 1)
root.add_leaf(['A', 'B', 'D'], 2)
leafs = root.retrieve_leaf_values(['A', 'B'])
leafs.sort()
assert leafs == [1, 2]
def add(self, path, value):
"""
Add a leaf to the tree
:param path: path to the node, its length must be equal to the depth
of the tree and the last label of the path will be the
label of the leaf
:type path: list
:param val: The value that will be stored in the leaf
"""
if len(path) == 0:
raise ValueError()
if len(path) == 1:
self.root = Node(path[0], value)
return
if self.root is None:
self.root = Node(path[0])
self.root.add_leaf(path, value)
:param path: path to the node, its length must be equal to the depth
of the tree and the last label of the path will be the
label of the leaf
:type path: list
:param val: The value that will be stored in the leaf
"""
if len(path) == 0:
raise ValueError()
if len(path) == 1:
self.root = Node(path[0], value)
return
if self.root is None:
self.root = Node(path[0])
self.root.add_leaf(path, value)
label = path[depth]
# if node is the leaf parent, create the leaf and add it to its
# parent
if depth == (len(path) - 1):
node.childs.append(Node(label, val=val))
# otherwise find the next node in the path and go down in the tree
else:
child_found = False
for child in node.childs:
if child.label == label:
aux(child, depth + 1)
child_found = True
break
# if no intermediate node, create it
if not child_found:
child = Node(label)
node.childs.append(child)
aux(child, depth + 1)