def test_post_order_print_one_node(capsys): root_node = BinaryTree(1) root_node.post_order_print(root_node) expected = "1 " output = capsys.readouterr().out assert output == expected
def test_delete_node_with_one_left_child(capsys): root_node = BinaryTree(5) root_node.insert(3) root_node.insert(7) root_node.insert(1) root_node.delete(root_node, 3) root_node.in_order_print(root_node) expected = "1 5 7 " output = capsys.readouterr().out assert output == expected
def test_delete_node_not_in_full_tree(capsys): root_node = BinaryTree(5) root_node.insert(3) root_node.insert(7) root_node.insert(1) root_node.insert(4) root_node.delete(root_node, 12) root_node.in_order_print(root_node) expected = "1 3 4 5 7 " output = capsys.readouterr().out assert output == expected
def test_insert_multiple_nodes(capsys): root_node = BinaryTree(4) root_node.insert(2) root_node.insert(1) root_node.insert(7) root_node.insert(5) root_node.insert(6) root_node.insert(3) root_node.in_order_print(root_node) expected = "1 2 3 4 5 6 7 " output = capsys.readouterr().out assert output == expected
def test_find_node_not_in_full_tree(): root_node = BinaryTree(4) root_node.insert(2) root_node.insert(1) root_node.insert(7) root_node.insert(5) root_node.insert(6) expected = False output = root_node.find(3) assert output == expected
def test_insert_as_multiple_nodes_as_linked_list(capsys): root_node = BinaryTree(1) root_node.insert(3) root_node.insert(5) root_node.insert(7) root_node.in_order_print(root_node) expected = "1 3 5 7 " output = capsys.readouterr().out assert output == expected
def test_find_node_in_linked_list_tree(): root_node = BinaryTree(1) root_node.insert(2) root_node.insert(3) root_node.insert(4) root_node.insert(5) expected = True output = root_node.find(5) assert output == expected
def test_insert_one_node(capsys): root_node = BinaryTree(4) root_node.insert(2) root_node.in_order_print(root_node) expected = "2 4 " output = capsys.readouterr().out assert output == expected
def test_post_order_print_multiple_nodes(capsys): root_node = BinaryTree(4) root_node.left_child = BinaryTree(1) root_node.left_child.left_child = BinaryTree(7) root_node.left_child.right_child = BinaryTree(9) root_node.right_child = BinaryTree(5) root_node.right_child.left_child = BinaryTree(2) root_node.right_child.right_child = BinaryTree(6) root_node.post_order_print(root_node) expected = "7 9 1 2 6 5 4 " output = capsys.readouterr().out assert output == expected
def test_delete_root_node_with_full_tree(capsys): root_node = BinaryTree(5) root_node.insert(3) root_node.insert(7) root_node.insert(1) root_node.insert(4) root_node.insert(3) root_node.insert(6) root_node.insert(12) root_node.insert(10) root_node.delete(root_node, 5) root_node.in_order_print(root_node) expected = "1 3 4 6 7 10 12 " output = capsys.readouterr().out assert output == expected
def test_find_node_not_in_one_node_tree(): root_node = BinaryTree(7) expected = False output = root_node.find(1) assert output == expected
def setUp(self): self.bt = BinaryTree()
class BinaryTreeTestCase(unittest.TestCase): def setUp(self): self.bt = BinaryTree() def tearDown(self): del self.bt # Tests start here # 6 # 3 9 # 2 5 8 12 # 4 def test_is_empty(self): self.assertEqual(True, self.bt.is_empty()) def test_is_not_empty(self): self.bt.root = Node(5) self.assertEqual(False, self.bt.is_empty()) def test_size_zero(self): self.assertEqual(0, self.bt.size()) def test_size_one(self): self.bt.root = Node(5) self.assertEqual(1, self.bt.size()) def test_size_many(self): self.bt.root = Node(6) n3 = self.bt.root.left_child = Node(3) n3.left_child = Node(2) n5 = n3.right_child = Node(5) n5.left_child = Node(4) n9 = self.bt.root.right_child = Node(9) n9.left_child = Node(8) n9.right_child = Node(12) self.assertEqual(8, self.bt.size()) def test_size_many_unbalanced(self): self.bt.root = Node(6) n5 = self.bt.root.left_child = Node(5) n4 = n5.left_child = Node(4) n3 = n4.left_child = Node(3) n3.left_child = Node(2) n7 = self.bt.root.right_child = Node(7) n8 = n7.right_child = Node(8) n8.right_child = Node(9) self.assertEqual(8, self.bt.size()) def test_contains_root(self): self.bt.root = Node(6) self.bt.root.left_child = Node(3) self.bt.root.right_child = Node(9) self.assertEqual(True, self.bt.contains(6)) def test_contains_internal(self): self.bt.root = Node(6) n3 = self.bt.root.left_child = Node(3) n3.left_child = Node(2) n5 = n3.right_child = Node(5) n5.left_child = Node(4) n9 = self.bt.root.right_child = Node(9) n9.left_child = Node(8) n9.right_child = Node(12) self.assertEqual(True, self.bt.contains(3)) self.assertEqual(True, self.bt.contains(5)) self.assertEqual(True, self.bt.contains(9)) def test_contains_leafs(self): self.bt.root = Node(6) n3 = self.bt.root.left_child = Node(3) n3.left_child = Node(2) n5 = n3.right_child = Node(5) n5.left_child = Node(4) n9 = self.bt.root.right_child = Node(9) n9.left_child = Node(8) n9.right_child = Node(12) self.assertEqual(True, self.bt.contains(2)) self.assertEqual(True, self.bt.contains(4)) self.assertEqual(True, self.bt.contains(8)) self.assertEqual(True, self.bt.contains(12)) def test_contains_not(self): self.bt.root = Node(6) n3 = self.bt.root.left_child = Node(3) n3.left_child = Node(2) n5 = n3.right_child = Node(5) n5.left_child = Node(4) n9 = self.bt.root.right_child = Node(9) n9.left_child = Node(8) n9.right_child = Node(12) self.assertEqual(False, self.bt.contains(10)) self.assertEqual(False, self.bt.contains(7)) self.assertEqual(False, self.bt.contains(11)) def test_pre_order_empty(self): self.assertEqual([], self.bt.pre_order()) def test_pre_order_one(self): self.bt.root = Node(1) self.assertEqual([1], self.bt.pre_order()) def test_pre_order_many(self): self.bt.root = Node(6) n3 = self.bt.root.left_child = Node(3) n3.left_child = Node(2) n5 = n3.right_child = Node(5) n5.left_child = Node(4) n9 = self.bt.root.right_child = Node(9) n9.left_child = Node(8) n9.right_child = Node(12) self.assertEqual([6, 3, 2, 5, 4, 9, 8, 12], self.bt.pre_order()) def test_post_order(self): self.bt.root = Node(6) n3 = self.bt.root.left_child = Node(3) n3.left_child = Node(2) n5 = n3.right_child = Node(5) n5.left_child = Node(4) n9 = self.bt.root.right_child = Node(9) n9.left_child = Node(8) n9.right_child = Node(12) self.assertEqual([2, 4, 5, 3, 8, 12, 9, 6], self.bt.post_order()) def test_in_order(self): self.bt.root = Node(6) n3 = self.bt.root.left_child = Node(3) n3.left_child = Node(2) n5 = n3.right_child = Node(5) n5.left_child = Node(4) n9 = self.bt.root.right_child = Node(9) n9.left_child = Node(8) n9.right_child = Node(12) self.assertEqual([2, 3, 4, 5, 6, 8, 9, 12], self.bt.in_order()) def test_level_order_empty(self): self.assertEqual([], self.bt.level_order()) def test_level_order_one(self): self.bt.root = Node(1) self.assertEqual([1], self.bt.level_order()) def test_level_order_many(self): self.bt.root = Node(6) n3 = self.bt.root.left_child = Node(3) n3.left_child = Node(2) n5 = n3.right_child = Node(5) n5.left_child = Node(4) n9 = self.bt.root.right_child = Node(9) n9.left_child = Node(8) n9.right_child = Node(12) self.assertEqual([6, 3, 9, 2, 5, 8, 12, 4], self.bt.level_order()) def test_height_zero(self): self.bt.root = None self.assertEqual(0, self.bt.height()) def test_height_one(self): self.bt.root = Node(4) self.assertEqual(0, self.bt.height()) def test_height_more(self): self.bt.root = Node(6) n3 = self.bt.root.left_child = Node(3) n3.left_child = Node(2) n5 = n3.right_child = Node(5) n5.left_child = Node(4) n9 = self.bt.root.right_child = Node(9) n9.left_child = Node(8) n9.right_child = Node(12) self.assertEqual(3, self.bt.height())