예제 #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_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"
        )
예제 #4
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"
        )
예제 #5
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"
        )
예제 #6
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"
        )
예제 #7
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")
예제 #8
0
    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"
        )