paths_till_now[j] = paths_till_now[i][:] j += 1 # print("j value : ",j) sums_till_now = sums_till_now[:j] paths_till_now = paths_till_now[:j] else: paths_till_now = [] sums_till_now = [] # print("Path : ",paths_till_now) for child in node.children: # print("Before passing to child :{0}, Path Values :{1} , Current Value : {2} ".format(child.value,paths_till_now,current_value)) print_paths(child, sum_req, paths_till_now[:], sums_till_now[:]) if __name__ == "__main__": print( "The aim of the program is to print all possile paths in a tree that sums up to a given value. Hence only enter integer values. Also when prompted for tree string enter the prefix string form as given in questions preceedint this question" ) while True: print( "Enter None if you want to stop, when prompted for a tree string") sample_tree = tree.Tree() tree_string = input("Enter tree string \n ") if tree_string == 'None': break sample_tree.create_tree_prefix_string(tree_string) sum_req = input("Sum for which you want the paths \n") sum_req = int(sum_req) sum_paths(sample_tree, sum_req)
if not len(first_tree_node.children) == len(second_tree_node.children): return False for i in range(len(first_tree_node.children)): similarity = check_if_same(first_tree_node.children[i], second_tree_node.children[i]) if not similarity: return False return True if __name__ == '__main__': print( "Enter two tree_string, where tree string is a string showind the preorder traversal of the tree with markers to denote level. Eg, assume we have a tree with root node x and child nodes a and b, then the input string is as follows x(a)(b)" ) while True: print("===========================================") print( "Please make sure that the first tree is larger than or equal to the second tree, If you want to stop the program enter None" ) first_tree_string = input("Enter first tree string \n") if first_tree_string == 'None': break second_tree_string = input("Enter second tree string \n") first_tree = tree.Tree() second_tree = tree.Tree() first_tree.create_tree_prefix_string(first_tree_string) second_tree.create_tree_prefix_string(second_tree_string) print("Tree 2 is Subtree of Tree 1 : ", find_if_subtree(first_tree, second_tree)) print("===========================================")