def test_can_successfully_add_a_left_child_and_right_child_to_a_single_root_node(): new_tree = BinaryTree() new_tree.root = Node(1) new_tree.root.left = Node(2) new_tree.root.right = Node(3) expected = [1, 2, 3] actual = new_tree.preOrder() assert expected == actual
def test_add_right_and_left(): tree = BinaryTree() tree.root = Node(10) tree.root.left = Node(3) tree.root.right = Node(15) actual = f"{tree.root.value}, {tree.root.left.value}, {tree.root.right.value}" expected = "10, 3, 15" assert actual == expected
def test_add_left_and_right(): tree = BinaryTree() tree.root = Node(111) tree.left = Node(11) tree.right = Node(1) actual = f"{tree.root.value}, {tree.left.value}, {tree.right.value}" expected = "111, 11, 1" assert actual == expected
def test_postorder(): tree = BinaryTree() tree.root = Node(50) tree.root.left = Node(30) tree.root.right = Node(70) tree.root.left.left = Node(20) tree.root.left.right = Node(40) actual = tree.post_order() expected = [20, 40, 30, 70, 50] assert actual == expected
def test_tree_postorder(): tree = BinaryTree() tree.root = Node(1) tree.root.left = Node(11) tree.root.right = Node(2) tree.root.left.left = Node(111) tree.root.left.right = Node(112) actual = tree.post_order() expected = [111, 112, 11, 2, 1] assert actual == expected
def test_tree_inorder(): tree = BinaryTree() tree.root = Node(1) tree.root.left = Node(11) tree.root.right = Node(2) tree.root.left.left = Node(111) tree.root.right.right = Node(22) actual = tree.in_order() expected = [111, 11, 1, 2, 22] assert actual == expected
def test_tree_preorder(): tree = BinaryTree() tree.root = Node(1) tree.root.left = Node(11) tree.root.right = Node(111) tree.root.left.left = Node(1111) tree.root.left.right = Node(11111) actual = tree.pre_order() expected = [1, 11, 1111, 11111, 111] assert actual == expected
def binary_tree3(): tree3 = BinaryTree() tree3.root = Node(2) tree3.root.left = Node(18) tree3.root.right = Node(6) tree3.root.left.right = Node(8) tree3.root.left.right.left = Node(20) tree3.root.left.right.right = Node(12) tree3.root.right.right = Node(14) tree3.root.right.right.left = Node(16) return tree3
def binary_tree(): tree = BinaryTree() tree.root = Node(1) tree.root.left = Node(5) tree.root.right = Node(10) tree.root.left.right = Node(25) tree.root.left.right.left = Node(13) tree.root.left.right.right = Node(15) tree.root.right.right = Node(9) tree.root.right.right.left = Node(4) return tree
def test_can_successfully_return_a_collection_from_a_postorder_traversal(): new_tree = BinaryTree() new_tree.root = Node("A") new_tree.root.left = Node("B") new_tree.root.right = Node("C") new_tree.root.right.left = Node("F") new_tree.root.left.left = Node("D") new_tree.root.left.right = Node("E") expected = ["D", "E", "B", "F", "C", "A"] actual = new_tree.postOrder() assert expected == actual
def binary_tree2(): tree2= BinaryTree() tree2.root = Node(1) tree2.root.left = Node(15) tree2.root.right = Node(12) tree2.root.left.right = Node(23) tree2.root.left.right.left = Node(3) tree2.root.left.right.right = Node(55) tree2.root.right.right = Node(19) tree2.root.right.right.left = Node(34) return tree2
def test_binary_tree_breath_first(): bt = BinaryTree() root = Node(2) bt.root = root root.left = Node(7) root.right = Node(5) root.left.left = Node(2) root.left.right = Node(6) root.right.right = Node(9) root.left.right.left = Node(5) root.left.right.right = Node(11) root.right.right.left = Node(4) bt.breadth_first() assert bt.breadth_first() == [2, 7, 5, 2, 6, 9, 5, 11, 4]
def test_instantiate_tree(): tree = BinaryTree() tree.root = Node(2) tree.root.left = Node(7) tree.root.left.left = Node(2) tree.root.left.right = Node(6) tree.root.left.right.left = Node(5) tree.root.left.right.right = Node(11) tree.root.right = Node(5) tree.root.right.right = Node(9) tree.root.right.right.left = Node(4) actual = tree.find_maximum_value() expected = 11 assert actual == expected
def test_dupes_tree2(): tree2 = BinaryTree() #tree2 tree2.root = Node(1) #left tree2.root.left = Node(3) tree2.root.left.left = Node(15) tree2.root.left.right = Node(12) tree2.root.left.right.left = Node(15) # right tree2.root.right = Node(20) tree2.root.right.left = Node(23) tree2.root.right.right = Node(11) tree2.root.right.right.left = Node(2) tree2.root.right.right.right = Node(12) return tree2
def test_dupes_tree1(): tree1 = BinaryTree() #tree1 tree1.root = Node(1) #left tree1.root.left = Node(1) tree1.root.left.left = Node(5) tree1.root.left.right = Node(10) tree1.root.left.right.left = Node(5) # right tree1.root.right = Node(20) tree1.root.right.left = Node(2) tree1.root.right.right = Node(14) tree1.root.right.right.left = Node(2) tree1.root.right.right.right = Node(100) return tree1
def test_build_tree1(): tree1 = BinaryTree() #tree1 tree1.root = Node(150) #left tree1.root.left = Node(100) tree1.root.left.left = Node(75) tree1.root.left.right = Node(160) tree1.root.left.right.left = Node(125) tree1.root.left.right.right = Node(175) # right tree1.root.right = Node(250) tree1.root.right.left = Node(200) tree1.root.right.right = Node(350) tree1.root.right.right.left = Node(300) tree1.root.right.right.right = Node(500) return tree1
def test_build_tree2(): tree2 = BinaryTree() #tree2 tree2.root = Node(42) #left tree2.root.left = Node(100) tree2.root.left.left = Node(15) tree2.root.left.right = Node(160) tree2.root.left.right.left = Node(125) tree2.root.left.right.right = Node(175) # right tree2.root.right = Node(600) tree2.root.right.left = Node(200) tree2.root.right.right = Node(350) tree2.root.right.right.left = Node(4) tree2.root.right.right.right = Node(500) return tree2
def test_can_successfully_instantiate_a_tree_with_a_single_root_node(): new_tree = BinaryTree() new_tree.root = Node(1) expected = 1 actual = new_tree.root.nodeVal assert expected == actual
def test_single_root(): tree = BinaryTree() tree.root = Node(1) actual = tree.root.value expected = 1 assert actual == expected
def test_no_matches(test_build_tree1): no_match_tree = BinaryTree() no_match_tree.root = Node(400) actual = tree_intersection(test_build_tree1, no_match_tree) expected = 'None' assert actual == expected