def test_find_element_iterative(self): print("TEST SEARCH ELEMENT - ITERATIVE") print("===========================================================") SampleBTree.print_9() print("Element to search: 15.", end=" ") found = find_element_iterative(self.root, 15) self.assertEqual(True, found) print("Element found: {0}".format(found), end="\n") print("Element to search: 34.", end=" ") found = find_element_iterative(self.root, 34) self.assertEqual(False, found) print("Element found: {0}".format(found), end="\n") print("Element to search: 10.", end=" ") found = find_element_iterative(self.root, 10) self.assertEqual(True, found) print("Element found: {0}".format(found), end="\n") print("Element to search: 11.", end=" ") found = find_element_iterative(self.root, 11) self.assertEqual(True, found) print("Element found: {0}".format(found), end="\n") print("Element to search: 63.", end=" ") found = find_element_iterative(self.root, 63) self.assertEqual(False, found) print("Element found: {0}".format(found), end=" ") print( "\n>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>\n\n")
def test_diameter(self): print("TEST LEVEL OF MAXIMUM SUM IN A BINARY TREE") print("===========================================================") root = SampleBTree.create_1() SampleBTree.print_1() max_sum_level, sum = level_with_maximum_sum(root) print("Maximum sum = {0}".format(sum)) print("Maximum sum level = {0}".format(max_sum_level), end=" ") print("\n\n") root = SampleBTree.create_3() SampleBTree.print_3() max_sum_level, sum = level_with_maximum_sum(root) print("Maximum sum = {0}".format(sum)) print("Maximum sum level = {0}".format(max_sum_level), end=" ") print("\n\n") root = SampleBTree.create_4() SampleBTree.print_4() max_sum_level, sum = level_with_maximum_sum(root) print("Maximum sum = {0}".format(sum)) print("Maximum sum level = {0}".format(max_sum_level), end=" ") print("\n\n") root = SampleBTree.create_6() SampleBTree.print_6() max_sum_level, sum = level_with_maximum_sum(root) print("Maximum sum = {0}".format(sum)) print("Maximum sum level = {0}".format(max_sum_level), end=" ") print("\n\n") root = SampleBTree.create_9() SampleBTree.print_9() max_sum_level, sum = level_with_maximum_sum(root) print("Maximum sum = {0}".format(sum)) print("Maximum sum level = {0}".format(max_sum_level)) print("\n\n") root = SampleBTree.create_left_weighted() SampleBTree.print_left_weighted() max_sum_level, sum = level_with_maximum_sum(root) print("Maximum sum = {0}".format(sum)) print("Maximum sum level = {0}".format(max_sum_level)) print("\n\n") root = SampleBTree.create_right_weighted() SampleBTree.print_right_weighted() max_sum_level, sum = level_with_maximum_sum(root) print("Maximum sum = {0}".format(sum)) print("Maximum sum level = {0}".format(max_sum_level)) print( "\n>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>\n\n" )
def test_find_leaf_nodes(self): print("TEST FIND LEAF NODES IN A BINARY TREE") print("===========================================================") SampleBTree.print_9() leaf_count = find_leaf_nodes(self.root) print("Count of leaf nodes = {0}".format(leaf_count), end=" ") print( "\n>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>\n\n" )
def test_find_deepest_node(self): print("TEST FIND DEEPEST NODE OF A BINARY TREE") print("===========================================================") SampleBTree.print_9() node = find_deepest_node(self.root) self.assertEqual(4, node.data) print("Deepest node = {0}".format(node.data), end=" ") print( "\n>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>\n\n")
def test_find_maximum_element_iterative(self): print("TEST MAXIMUM ELEMENT - ITERATIVE") print("===========================================================") SampleBTree.print_9() maximum = find_maximum_element_iterative(self.root) self.assertEqual(62, maximum) print("Maximum element: " + str(maximum), end=" ") print( "\n>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>\n\n" )
def test_insert_into_tree(self): print("TEST REVERSE LEVEL ORDER") print("===========================================================") SampleBTree.print_9() print("Reverse level order traversal: ", end=" ") reverse_level_order(self.root) print( "\n>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>\n\n" )
def test_structurally_identical(self): print("TEST WHETHER TWO BINARY TREES ARE STRUCTURALLY IDENTICAL") print("===========================================================") root1 = SampleBTree.create_1() root2 = SampleBTree.create_3() print("First Tree:") SampleBTree.print_1(message=False) print("Second Tree:") SampleBTree.print_3(message=False) identical = structurally_identical(root1, root2) self.assertEqual(False, identical) print("Structurally Identical: {0}".format(identical)) print("\n---------------------------------\n") root1 = SampleBTree.create_4() root2 = SampleBTree.create_4() print("First Tree:") SampleBTree.print_4(message=False) print("Second Tree:") SampleBTree.print_4(message=False) identical = structurally_identical(root1, root2) self.assertEqual(True, identical) print("Structurally Identical: {0}".format(identical)) print("\n---------------------------------\n") root1 = SampleBTree.create_4() root2 = SampleBTree.create_9() print("First Tree:") SampleBTree.print_4(message=False) print("Second Tree:") SampleBTree.print_9(message=False) identical = structurally_identical(root1, root2) self.assertEqual(False, identical) print("Structurally Identical: {0}".format(identical)) print("\n---------------------------------\n") root1 = SampleBTree.create_6() root2 = SampleBTree.create_4() print("First Tree:") SampleBTree.print_6(message=False) print("Second Tree:") SampleBTree.print_4(message=False) identical = structurally_identical(root1, root2) self.assertEqual(False, identical) print("Structurally Identical: {0}".format(identical), end="") print( "\n>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>\n\n")
def test_diameter(self): print("TEST ALL PATHS FROM ROOT TO LEAF IN A BINARY TREE") print("===========================================================") root = SampleBTree.create_1() SampleBTree.print_1() print("All root to leaf paths:") all_paths_from_root_to_leaf(root) print("\n\n") root = SampleBTree.create_3() SampleBTree.print_3() print("All root to leaf paths:") all_paths_from_root_to_leaf(root) print("\n\n") root = SampleBTree.create_4() SampleBTree.print_4() print("All root to leaf paths:") all_paths_from_root_to_leaf(root) print("\n\n") root = SampleBTree.create_6() SampleBTree.print_6() print("All root to leaf paths:") all_paths_from_root_to_leaf(root) print("\n\n") root = SampleBTree.create_9() SampleBTree.print_9() print("All root to leaf paths:") all_paths_from_root_to_leaf(root) print("\n\n") root = SampleBTree.create_left_weighted() SampleBTree.print_left_weighted() print("All root to leaf paths:") all_paths_from_root_to_leaf(root) print("\n\n") root = SampleBTree.create_right_weighted() SampleBTree.print_right_weighted() print("All root to leaf paths:") all_paths_from_root_to_leaf(root) print( "\n>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>\n\n" )
def test_levelorder(self): print("TEST LEVELORDER TRAVERSAL OF A BINARY TREE") print("===========================================================") root = SampleBTree.create_1() SampleBTree.print_1() print("Levelorder traversal:", end=" ") levelorder(root) print("\n\n") root = SampleBTree.create_3() SampleBTree.print_3() print("Levelorder traversal:", end=" ") levelorder(root) print("\n\n") root = SampleBTree.create_4() SampleBTree.print_4() print("Levelorder traversal:", end=" ") levelorder(root) print("\n\n") root = SampleBTree.create_6() SampleBTree.print_6() print("Levelorder traversal:", end=" ") levelorder(root) print("\n\n") root = SampleBTree.create_9() SampleBTree.print_9() print("Levelorder traversal:", end=" ") levelorder(root) print("\n\n") root = SampleBTree.create_left_weighted() SampleBTree.print_left_weighted() print("Levelorder traversal:", end=" ") levelorder(root) print("\n\n") root = SampleBTree.create_right_weighted() SampleBTree.print_right_weighted() print("Levelorder traversal:", end=" ") levelorder(root) print( "\n>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>\n\n" )
def test_diameter(self): print("TEST DIAMETER OF A BINARY TREE") print("===========================================================") root = SampleBTree.create_1() SampleBTree.print_1() diameter = find_diameter(root) print("Diameter of the tree = {0}".format(diameter), end=" ") print("\n\n") root = SampleBTree.create_3() SampleBTree.print_3() diameter = find_diameter(root) print("Diameter of the tree = {0}".format(diameter), end=" ") print("\n\n") root = SampleBTree.create_4() SampleBTree.print_4() diameter = find_diameter(root) print("Diameter of the tree = {0}".format(diameter), end=" ") print("\n\n") root = SampleBTree.create_6() SampleBTree.print_6() diameter = find_diameter(root) print("Diameter of the tree = {0}".format(diameter), end=" ") print("\n\n") root = SampleBTree.create_9() SampleBTree.print_9() diameter = find_diameter(root) print("Diameter of the tree = {0}".format(diameter)) print("\n\n") root = SampleBTree.create_left_weighted() SampleBTree.print_left_weighted() diameter = find_diameter(root) print("Diameter of the tree = {0}".format(diameter)) print("\n\n") root = SampleBTree.create_right_weighted() SampleBTree.print_right_weighted() diameter = find_diameter(root) print("Diameter of the tree = {0}".format(diameter)) print( "\n>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>\n\n" )
def test_postorder_recursive(self): print("TEST POSTORDER TRAVERSAL OF A BINARY TREE - RECURSIVE") print("===========================================================") root = SampleBTree.create_1() SampleBTree.print_1() print("Postorder traversal:", end=" ") postorder_iterative(root) print("\n\n") root = SampleBTree.create_3() SampleBTree.print_3() print("Postorder traversal:", end=" ") postorder_iterative(root) print("\n\n") root = SampleBTree.create_4() SampleBTree.print_4() print("Postorder traversal:", end=" ") postorder_iterative(root) print("\n\n") root = SampleBTree.create_6() SampleBTree.print_6() print("Postorder traversal:", end=" ") postorder_iterative(root) print("\n\n") root = SampleBTree.create_9() SampleBTree.print_9() print("Postorder traversal:", end=" ") postorder_iterative(root) print("\n\n") root = SampleBTree.create_left_weighted() SampleBTree.print_left_weighted() print("Postorder traversal:", end=" ") postorder_iterative(root) print("\n\n") root = SampleBTree.create_right_weighted() SampleBTree.print_right_weighted() print("Postorder traversal:", end=" ") postorder_iterative(root) print( "\n>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>\n\n" )
def test_inorder_iterative(self): print("TEST INORDER TRAVERSAL OF A BINARY TREE - ITERATIVE") print("===========================================================") root = SampleBTree.create_1() SampleBTree.print_1() print("Inorder traversal:", end=" ") inorder_iterative(root) print("\n\n") root = SampleBTree.create_3() SampleBTree.print_3() print("Inorder traversal:", end=" ") inorder_iterative(root) print("\n\n") root = SampleBTree.create_4() SampleBTree.print_4() print("Inorder traversal:", end=" ") inorder_iterative(root) print("\n\n") root = SampleBTree.create_6() SampleBTree.print_6() print("Inorder traversal:", end=" ") inorder_iterative(root) print("\n\n") root = SampleBTree.create_9() SampleBTree.print_9() print("Inorder traversal:", end=" ") inorder_iterative(root) print("\n\n") root = SampleBTree.create_left_weighted() SampleBTree.print_left_weighted() print("Inorder traversal:", end=" ") inorder_iterative(root) print("\n\n") root = SampleBTree.create_right_weighted() SampleBTree.print_right_weighted() print("Inorder traversal:", end=" ") inorder_iterative(root) print( "\n>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>\n\n" )
def test_insert_into_tree(self): print("TEST INSERT INTO BINARY TREE") print("===========================================================") SampleBTree.print_9() print("Inorder traversal before insertion : ", end=" ") inorder(self.root) print() insert_into_binary_tree(self.root, 12) print("Inorder traversal after inserting 12: ", end=" ") inorder(self.root) print( "\n>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>\n\n")
def test_find_height_recursive(self): print("TEST FIND HEIGHT OF A BINARY TREE - RECURSIVE") print("===========================================================") SampleBTree.print_9() height = find_height_recursive(self.root) self.assertEqual(5, height) print("Height of given tree = {0}".format(height), end="\n\n") self.root = BTreeNode(10) print("Tree to operate: ") print(" " * 20 + "10\n") height = find_height_recursive(self.root) self.assertEqual(1, height) print("Height of given tree = {0}".format(height), end=" ") print( "\n>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>\n\n")
def test_find_height_iterative(self): print("TEST FIND HEIGHT OF A BINARY TREE - ITERATIVE") print("===========================================================") SampleBTree.print_9() size = find_height_iterative(self.root) self.assertEqual(5, size) print("Height of given tree = {0}".format(size), end="\n\n") self.root = BTreeNode(10) print("Tree to operate: ") print(" " * 20 + "10\n") size = find_height_iterative(self.root) self.assertEqual(1, size) print("Height of given tree = {0}".format(size), end=" ") print( "\n>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>\n\n")
def test_find_size_recursive(self): print("TEST FIND SIZE OF A BINARY TREE - RECURSIVE") print("===========================================================") SampleBTree.print_9() size = find_size_recursive(self.root) self.assertEqual(9, size) print("Size of given tree = {0}".format(size), end="\n\n") self.root = BTreeNode(10) print("Tree to operate: ") print(" " * 20 + "10\n") size = find_size_recursive(self.root) self.assertEqual(1, size) print("Size of given tree = {0}".format(size), end=" ") print( "\n>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>\n\n" )
def test_delete_node(self): print("TEST DELETE NODE OF A BINARY TREE") print("===========================================================") root = SampleBTree.create_1() SampleBTree.print_1() node_to_delete = 15 print("Node to delete: {0}".format(node_to_delete)) print("Inorder traversal before delete:", end=" ") inorder(root) print() delete_node(root, node_to_delete) print("Inorder traversal after delete:", end=" ") inorder(root) print("\n\n") root = SampleBTree.create_3() SampleBTree.print_3() node_to_delete = 67 print("Node to delete: {0}".format(node_to_delete)) print("Inorder traversal before delete:", end=" ") inorder(root) print() delete_node(root, node_to_delete) print("Inorder traversal after delete:", end=" ") inorder(root) print("\n\n") root = SampleBTree.create_4() SampleBTree.print_4() node_to_delete = 86 print("Node to delete: {0}".format(node_to_delete)) print("Inorder traversal before delete:", end=" ") inorder(root) print() delete_node(root, node_to_delete) print("Inorder traversal after delete:", end=" ") inorder(root) print("\n\n") root = SampleBTree.create_6() SampleBTree.print_6() node_to_delete = 9 print("Node to delete: {0}".format(node_to_delete)) print("Inorder traversal before delete:", end=" ") inorder(root) print() delete_node(root, node_to_delete) print("Inorder traversal after delete:", end=" ") inorder(root) print("\n\n") root = SampleBTree.create_9() SampleBTree.print_9() node_to_delete = 8 print("Node to delete: {0}".format(node_to_delete)) print("Inorder traversal before delete:", end=" ") inorder(root) print() delete_node(root, node_to_delete) print("Inorder traversal after delete:", end=" ") inorder(root) print( "\n>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>\n\n" )
def test_diameter(self): print("TEST PATH WITH SUM EXISTS IN A BINARY TREE") print("===========================================================") root = SampleBTree.create_1() SampleBTree.print_1() sum = 14 path_exists = has_path_with_sum(root, sum) self.assertEqual(False, path_exists) print("Path with sum {0} exists: {1}".format(sum, path_exists)) print("\n\n") root = SampleBTree.create_3() SampleBTree.print_3() sum = 90 path_exists = has_path_with_sum(root, sum) self.assertEqual(True, path_exists) print("Path with sum {0} exists: {1}".format(sum, path_exists)) print("\n\n") root = SampleBTree.create_4() SampleBTree.print_4() sum = 45 path_exists = has_path_with_sum(root, sum) self.assertEqual(False, path_exists) print("Path with sum {0} exists: {1}".format(sum, path_exists)) print("\n\n") root = SampleBTree.create_6() SampleBTree.print_6() sum = 34 path_exists = has_path_with_sum(root, sum) self.assertEqual(True, path_exists) print("Path with sum {0} exists: {1}".format(sum, path_exists)) print("\n\n") root = SampleBTree.create_9() SampleBTree.print_9() sum = 78 path_exists = has_path_with_sum(root, sum) self.assertEqual(False, path_exists) print("Path with sum {0} exists: {1}".format(sum, path_exists)) print("\n\n") root = SampleBTree.create_left_weighted() SampleBTree.print_left_weighted() sum = 10 path_exists = has_path_with_sum(root, sum) self.assertEqual(False, path_exists) print("Path with sum {0} exists: {1}".format(sum, path_exists)) print("\n\n") root = SampleBTree.create_right_weighted() SampleBTree.print_right_weighted() sum = 21 path_exists = has_path_with_sum(root, sum) self.assertEqual(True, path_exists) print("Path with sum {0} exists: {1}".format(sum, path_exists)) print( "\n>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>\n\n")
def test_mirror(self): print("TEST MIRROR OF A BINARY TREE") print("===========================================================") root = SampleBTree.create_1() SampleBTree.print_1() print("Inorder traversal of the tree:", end=" ") inorder(root) print() mirror(root) print("Inorder traversal of the mirror:", end=" ") inorder(root) print("\n\n") root = SampleBTree.create_3() SampleBTree.print_3() print("Inorder traversal of the tree:", end=" ") inorder(root) print() mirror(root) print("Inorder traversal of the mirror:", end=" ") inorder(root) print("\n\n") root = SampleBTree.create_4() SampleBTree.print_4() print("Inorder traversal of the tree:", end=" ") inorder(root) print() mirror(root) print("Inorder traversal of the mirror:", end=" ") inorder(root) print("\n\n") root = SampleBTree.create_6() SampleBTree.print_6() print("Inorder traversal of the tree:", end=" ") inorder(root) print() mirror(root) print("Inorder traversal of the mirror:", end=" ") inorder(root) print("\n\n") root = SampleBTree.create_9() SampleBTree.print_9() print("Inorder traversal of the tree:", end=" ") inorder(root) print() mirror(root) print("Inorder traversal of the mirror:", end=" ") inorder(root) print("\n\n") root = SampleBTree.create_left_weighted() SampleBTree.print_left_weighted() print("Inorder traversal of the tree:", end=" ") inorder(root) print() mirror(root) print("Inorder traversal of the mirror:", end=" ") inorder(root) print("\n\n") root = SampleBTree.create_right_weighted() SampleBTree.print_right_weighted() print("Inorder traversal of the tree:", end=" ") inorder(root) print() mirror(root) print("Inorder traversal of the mirror:", end=" ") inorder(root) print( "\n>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>\n\n" )