def test_breadth_first_binarytree_with_letters(): tree = BinaryTree() tree._root = _Node(8) tree._root.left = _Node("a") tree._root.right = _Node(-2) assert BinaryTree.breadth_first(tree) == [8, "a", -2] tree._root.left.left = _Node(195) tree._root.left.right = _Node("cat") tree._root.right.right = _Node(8) tree._root.left.left.left = _Node(-0.56) tree._root.left.left.right = _Node(9) tree._root.right.right.right = _Node(23) tree._root.right.right.right.left = _Node([5, 7]) assert BinaryTree.breadth_first(tree) == [ 8, "a", -2, 195, "cat", 8, -0.56, 9, 23, [5, 7] ]
def test_easy_queue_breadth(): tree = BinaryTree() Node1 = Node(8) Node2 = Node(9) Node3 = Node(10) Node1.left = Node2 Node1.right = Node3 tree.root = Node1 actual = tree.breadth_first() expected = [8,9,10] assert actual == expected
def test_breadth(): bt = BinaryTree() bst = BinarySearchTree() bt.add(4) bt.add(7) bt.add(5) bt.add(9) bt.add(2) bt.add(30) bt.add(-1) actual = bt.breadth_first() expected = [4, 7, 5, 9, 2, 30, -1] assert actual == expected
def test_breadth_first_properly_walks_through_and_collects_values(): x = BinaryTree() a = Node(1) b = Node(2) c = Node(3) d = Node(4) e = Node(5) f = Node(6) x.root = a x.root.left = b x.root.right = c x.root.left.left = d x.root.left.right = e x.root.right.left = f actual = x.breadth_first() expected = [1, 2, 3, 4, 5, 6] assert actual == expected
def test_breadth_first_properly_walks_through_and_collects_values_in_asym_tree( ): x = BinaryTree() a = Node(1) b = Node(2) c = Node(3) d = Node(4) e = Node(5) f = Node(6) g = Node(7) h = Node(8) i = Node(9) x.root = a x.root.left = b x.root.right = c x.root.left.left = d x.root.left.right = e x.root.left.left.left = f x.root.left.left.right = g x.root.right.right = h x.root.right.right.right = i actual = x.breadth_first() expected = [1, 2, 3, 4, 5, 8, 6, 7, 9] assert actual == expected
def test_breadth_first_raises_error_with_empty_tree(): x = BinaryTree() with pytest.raises(Nope): x.breadth_first()
walk_one(node.right) walk_one(tree_one.root) def walk_two(node): if node is None: return if node.value in seen: result.append(node.value) walk_two(node.left) walk_two(node.right) walk_two(tree_two.root) return result if __name__ == "__main__": 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) print(t1.breadth_first()) print(t2.breadth_first()) print(tree_intersection(t1, t2))
def test_empty_queue_breadth(): tree = BinaryTree() actual = tree.breadth_first() expected = "No bueno" assert actual == expected
def test_single_breadth(): tree = BinaryTree() tree.root = Node(8) actual = tree.breadth_first() expected = [8] assert actual == expected
def test_breadth_first_binarytree_one_element(): tree = BinaryTree() tree._root = _Node(8) assert BinaryTree.breadth_first(tree) == [8]
def test_breadth_first_binarytree_empty(): tree = BinaryTree() assert BinaryTree.breadth_first(tree) == []
def test_breadth_first_binarysearch(my_bst): assert BinaryTree.breadth_first(my_bst) == [ 15, 11, 19, 7, 13, 17, 23, 5, 8 ]