Beispiel #1
0
    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_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_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_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"
        )
Beispiel #5
0
    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"
        )
Beispiel #6
0
    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"
        )
Beispiel #7
0
    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"
        )
Beispiel #8
0
    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"
        )
Beispiel #9
0
 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"
        )