def test_tree_intersection(values_a, values_b, expected): """ The intersection of two trees is found. """ # Write add method for BinaryTree tree_a = BinaryTree() if len(values_a) == 6: tree_a.root = Node(values_a[0]) tree_a.root.left = Node(values_a[1]) tree_a.root.right = Node(values_a[2]) tree_a.root.left.left = Node(values_a[3]) tree_a.root.left.right = Node(values_a[4]) tree_a.root.right.right = Node(values_a[5]) # Write add method for BinaryTree tree_b = BinaryTree() if len(values_b) == 6: tree_b.root = Node(values_b[0]) tree_b.root.left = Node(values_b[1]) tree_b.root.right = Node(values_b[2]) tree_b.root.left.left = Node(values_b[3]) tree_b.root.left.right = Node(values_b[4]) tree_b.root.right.right = Node(values_b[5]) actual = tree_intersection(tree_a, tree_b) assert actual == expected
def test_func_match_some(tree): tree1 = tree tree2 = BinaryTree() one = Node(1) two = Node(2) three = Node(3) four = Node(4) five = Node(5) six = Node(6) seven = Node(7) eight = Node(8) nine = Node(9) one.left = two one.right = three three.left = four two.left = six six.right = seven seven.left = eight tree2.root = one expected = [6, 8, 7, 2, 1, 4, 3] actual = tree_intersection(tree1, tree2) assert expected == actual
def test_func_match_none(tree): tree1 = tree tree2 = BinaryTree() expected = [] actual = tree_intersection(tree1, tree2) assert expected == actual
def test_return_empty_list_if_neither_trees_are_present(tree): """ Returns an empty list if one or the other is None """ empty_tree = tree actual = tree_intersection(empty_tree, tree) expected = [] assert actual == expected
def test_func_matches_allofit(tree): tree1 = tree tree2 = tree1 expected = [6, 8, 7, 9, 2, 1, 4, 3, 5] actual = tree_intersection(tree1, tree2) assert expected == actual
def test_empty_tree(): tree_1 = BinaryTree() tree_2 = BinaryTree() tree_2.add(12) tree_2.add(14) tree_2.add(5) expected = [] actual = tree_intersection(tree_1, tree_2) assert actual == expected
def test_empty_tree(): tree_one = BinarySearchTree() tree_one.add(12) tree_one.add(5) tree_one.add(3) tree_one.add(15) tree_two = BinarySearchTree() assert tree_intersection(tree_one, tree_two) == []
def test_tree_intersection(): tree_stuff = BinarySearchTree() tree_stuff_two = BinarySearchTree() tree_stuff.add(8) tree_stuff.add(9) tree_stuff.add(10) tree_stuff_two.add(9) tree_stuff_two.add(10) tree_stuff_two.add(11) actual = tree_intersection(tree_stuff, tree_stuff_two) expected = [9, 10] assert actual == expected
def test_everything_matches(): tree_one = BinarySearchTree() tree_one.add(12) tree_one.add(5) tree_one.add(3) tree_one.add(15) tree_two = BinarySearchTree() tree_two.add(12) tree_two.add(5) tree_two.add(3) tree_two.add(15) assert tree_intersection(tree_one, tree_two) == [3, 5, 15, 12]
def test_multiple_matching_values_in_one_tree(): tree_one = BinarySearchTree() tree_one.add(4) tree_one.add(4) tree_one.add(4) tree_one.add(4) tree_two = BinarySearchTree() tree_two.add(12) tree_two.add(5) tree_two.add(3) tree_two.add(15) assert tree_intersection(tree_one, tree_two) == []
def test_tree_intersection(): tree_one = BinarySearchTree() tree_one.add(12) tree_one.add(5) tree_one.add(3) tree_one.add(15) tree_two = BinarySearchTree() tree_two.add(1) tree_two.add(5) tree_two.add(12) tree_two.add(55) assert tree_intersection(tree_one, tree_two) == [12, 5]
def test_tree_intersection(): tree_1 = BinaryTree() tree_1.add(15) tree_1.add(12) tree_1.add(8) tree_1.add(5) tree_2 = BinaryTree() tree_2.add(12) tree_2.add(14) tree_2.add(5) tree_2.add(2) expected = [5, 12] actual = tree_intersection(tree_1, tree_2) assert actual == expected
def test_return_all_common_values_from_both_trees(): """ Returns all common values from both trees """ t1 = BinaryTree() t2 = BinaryTree() t1_list = [2, 6, 1, 5, 10, 9, 3, 2] t2_list = [22, 4, 6, 5, 9] for num in t1_list: t1.breadth_add(num) for num in t2_list: t2.breadth_add(num) actual = tree_intersection(t1, t2) expected = [5, 9, 6] assert actual == expected
def test_tree_intersection_other_number(): t1 = Tree() t1.add(0.45) t1.add(-5) t1.add(-20) t1.add(-8.8) t1.add(15) t2 = Tree() t2.add(-20) t2.add(8.8) t2.add(-1) t2.add(0.45) t2.add(5) actual = tree_intersection(t1, t2) expected = {-20, 0.45} assert actual == expected
def test_tree_intersection_number(): t1 = Tree() t1.add(1) t1.add(5) t1.add(20) t1.add(10) t1.add(15) t2 = Tree() t2.add(20) t2.add(15) t2.add(1) t2.add(30) t2.add(5) actual = tree_intersection(t1, t2) expected = {1, 5, 15, 20} assert actual == expected
def test_tree_intersection_string(): t1 = Tree() t1.add('dog') t1.add('cat') t1.add('deer') t1.add('rabbit') t1.add('bear') t2 = Tree() t2.add('cat') t2.add('monkey') t2.add('bat') t2.add('bear') t2.add('dolphin') actual = tree_intersection(t1, t2) expected = {'cat', 'bear'} assert actual == expected
def test_inner_contained_tree(): tree_one = BinaryTree() tree_one.add(55) tree_one.add(12) tree_one.add(6) tree_one.add(5) tree_one.add(3) tree_one.add(100) tree_one.add(23) tree_one.add(1) tree_two = BinaryTree() tree_two.add(12) tree_two.add(5) tree_two.add(3) assert tree_intersection(tree_one, tree_two) == [5, 3, 12]
def test_intersections_with_all_dupes(): x = BinaryTree() y = BinaryTree() a = Node("A") b = Node("B") c = Node("C") d = Node("A") e = Node("B") f = Node("C") x.root = a x.root.left = b x.root.right = c y.root = d y.root.left = e y.root.right = f actual = Counter(tree_intersection(x, y)) expected = Counter(["C", "B", "A"]) assert actual == expected
def test_intersections_with_NO_dupes(): x = BinaryTree() y = BinaryTree() a = Node("A") b = Node("B") c = Node("C") d = Node("D") e = Node("E") f = Node("F") x.root = a x.root.left = b x.root.right = c y.root = d y.root.left = e y.root.right = f actual = tree_intersection(x, y) expected = [] assert actual == expected
def test_everything_match(my_tree): tree1 = my_tree values = [7, 3, 2, 6, 9, 10, 1, 5, 8] tree2 = build(values) assert tree_intersection(tree1, tree2) == {7, 3, 2, 6, 9, 10, 1, 5, 8}
def test_no_match(my_tree): tree1 = my_tree values = [100, 100, 100, 100] tree2 = build(values) assert tree_intersection(tree1, tree2) == None
def test_simply_tree_case(my_tree, my_tree2): tree1 = my_tree tree2 = my_tree2 assert tree_intersection(tree1, tree2) == {10,9,8,5}
def test_tree_intersection_equal(small_bst, small_bst_two): """Test tree intersection with two small BSTs.""" result = tree_intersection(small_bst, small_bst_two) assert result == {'20', '5'}
def test_tree_intersection_uneven(small_bst, lopsided): """Test tree intersection with one uneven tree.""" result = tree_intersection(small_bst, lopsided) assert result == {'11', '5'}
def test_floats(): tree1 = build([0.3, 5, -54, -3, 34]) tree2 = build([-54, 95, 0.3, 123, 34]) assert tree_intersection(tree1, tree2) == {0.3, -54, 34}
def test_2nd_tree_is_empty(): tree1 = build([3, 3, 4, 5]) tree2 = build([]) assert tree_intersection(tree1, tree2) == None
def test_empty_tree(): tree1 = build([]) tree2 = build([3, 4]) assert tree_intersection(tree1, tree2) == None
def test_func_emptytree(): tree1 = BinaryTree() tree2 = BinaryTree() expected = [] actual = tree_intersection(tree1, tree2)
def test_same_values(): tree1 = build([10, 10, 10, 8]) tree2 = build([10, 10, 10, 10, 10]) assert tree_intersection(tree1, tree2) == {10}
def test_func_invalidparams(): with pytest.raises(ValueError): tree_intersection(None, None)