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_flattened():
"""Test flattened tree into a list, retrieving items"""
# a
# | \ \ \
# b c d e
# | \ |
# f g h
root = TreeNode("a")
b = TreeNode("b", root)
TreeNode("c", root)
TreeNode("d", root)
e = TreeNode("e", root)
TreeNode("f", b)
TreeNode("g", b)
TreeNode("h", e)
assert flattened(root) == ['a', 'b', 'f', 'g', 'c', 'd', 'e', 'h']
def test_node_creation_with_parent():
"""Test assignement of parent on initialization"""
parent = TreeNode("test")
child = TreeNode("test", parent)
assert child.parent is parent
assert parent.children[0] is child
def test_children_children():
"""Test path through two level of children"""
child = TreeNode("test")
parent = TreeNode("test", children=[child])
grand_parent = TreeNode("test", children=[parent])
assert child.parent is parent
assert child.parent.parent is grand_parent
assert grand_parent.children[0] is parent
assert grand_parent.children[0].children[0] is child
def test_depth_first_traversal():
"""Test order retrieval of DepthFirstTraversal"""
# a
# | \ \ \
# b c d e
# | \ |
# f g h
root = TreeNode("a")
b = TreeNode("b", root)
TreeNode("c", root)
TreeNode("d", root)
e = TreeNode("e", root)
TreeNode("f", b)
TreeNode("g", b)
TreeNode("h", e)
rval = [node.item for node in DepthFirstTraversal(root)]
assert rval == ['f', 'g', 'b', 'c', 'd', 'h', 'e', 'a']
def test_node_add_child():
"""Test assignement of child"""
parent = TreeNode("test")
child = TreeNode("test")
parent.add_children(child)
assert parent.children[0] is child
assert child.parent is parent
def test_flattened():
"""Test flattened tree into a list, retrieving items"""
# a
# | \ \ \
# b c d e
# | \ |
# f g h
root = TreeNode("a")
b = TreeNode("b", root)
TreeNode("c", root)
TreeNode("d", root)
e = TreeNode("e", root)
TreeNode("f", b)
TreeNode("g", b)
TreeNode("h", e)
assert flattened(root) == ['a', 'b', 'f', 'g', 'c', 'd', 'e', 'h']
def test_node_remove_all_children():
"""Test drop_children() drops all of them"""
parent = TreeNode("test")
child1 = TreeNode("test1")
child2 = TreeNode("test2")
parent.add_children(child1, child2)
assert parent.children[0] is child1
assert parent.children[1] is child2
assert child1.parent is parent
assert child2.parent is parent
parent.drop_children()
assert len(parent.children) == 0
assert child1.parent is None
assert child2.parent is None
# a
# | \ \ \
# b c d e
# | \ |
# f g h
#
# Will give
#
# 4
# |
# 3
# |
# 2
root = TreeNode(1)
b = TreeNode(1, root)
TreeNode(1, root)
TreeNode(1, root)
e = TreeNode(1, root)
f = TreeNode(1, b)
TreeNode(1, b)
h = TreeNode(1, e)
def increment_parent(node, parent):
if parent is not None:
for parent in parent.root:
parent.item += 1
return node.item + 1, parent
def test_children_children():
"""Test path through two level of children"""
child = TreeNode("test")
parent = TreeNode("test", children=[child])
grand_parent = TreeNode("test", children=[parent])
assert child.parent is parent
assert child.parent.parent is grand_parent
assert grand_parent.children[0] is parent
assert grand_parent.children[0].children[0] is child
# | \ |
# f g h
#
# Will give
#
# 4
# |
# 3
# |
# 2
root = TreeNode(1)
b = TreeNode(1, root)
TreeNode(1, root)
TreeNode(1, root)
e = TreeNode(1, root)
f = TreeNode(1, b)
TreeNode(1, b)
h = TreeNode(1, e)
def increment_parent(node, parent):
if parent is not None:
for parent in parent.root:
parent.item += 1
return node.item + 1, parent
rval = f.map(increment_parent, f.parent)
assert [node.item for node in rval.root] == [4, 3, 2]