def simple_tree(): #build a binary tree of three nodes in two levels st = BinaryTree() curr_root = TreeNode(3) root_left = TreeNode(1) root_right = TreeNode(7) curr_root.left_child = root_left curr_root.right_child = root_right st.root_node = curr_root return st
def unbalanced_tree(): #build a binary tree of seven nodes in four levels ut = BinaryTree() curr_root = TreeNode(6) root_left = TreeNode(4) root_left_left = TreeNode(2) root_right = TreeNode(16) root_right_left = TreeNode(10) root_right_right = TreeNode(26) root_right_right_right = TreeNode(42) ut.root_node = curr_root curr_root.left_child = root_left curr_root.left_child.left_child = root_left_left curr_root.right_child = root_right curr_root.right_child.left_child = root_right_left curr_root.right_child.right_child = root_right_right curr_root.right_child.right_child.right_child = root_right_right_right return ut
def balanced_tree(): #build a binary tree of seven nodes in three levels bt = BinaryTree() curr_root = TreeNode(7) root_left = TreeNode(3) root_left_left = TreeNode(1) root_left_right = TreeNode(5) root_right = TreeNode(11) root_right_left = TreeNode(9) root_right_right = TreeNode(13) bt.root_node = curr_root curr_root.left_child = root_left curr_root.left_child.left_child = root_left_left curr_root.left_child.right_child = root_left_right curr_root.right_child = root_right curr_root.right_child.left_child = root_right_left curr_root.right_child.right_child = root_right_right return bt
def test_empty_tree_instantiates(): tree = BinaryTree() expected = None actual = tree.root_node assert actual == expected
def test_non_empty_binary_tree_fails(): fbt = BinaryTree() root = TreeNode(7) fbt.root_node = root assert fbt.root_node.value != None
def test_non_empty_binary_tree_passes(): pbt = BinaryTree() root = TreeNode(3) pbt.root_node = root assert pbt.root_node.value == 3
def test_empty_binary_tree_fails(): febt = BinaryTree() assert febt != 7
def test_empty_binary_tree_instantiates(): et = BinaryTree() assert et.root_node == None