Example #1
0
from Tree.GenericTree.GenericTree import GenericTree
from Utils.Array import input_array


def find_sum_of_all_nodes(root):
    if root is None:
        return 0  # no nodes, so sum is 0

    subtree_sum = 0
    for child in root.children:
        subtree_sum += find_sum_of_all_nodes(child)

    total_sum = subtree_sum + root.data
    return total_sum


if __name__ == "__main__":
    root = GenericTree.single_line_input(input_array())
    # GenericTree.print_level_order(root)
    print(find_sum_of_all_nodes(root))
"""
10 3 20 30 40 2 40 50 0 0 0 0
190
"""
    # both root should have same data and same no of childerns
    if root_a.data != root_b.data or \
            len(root_a.children) != len(root_b.children):
        return False

    children_count = len(root_b.children)  # or len(root_a.children)
    for i in range(children_count):
        child_a = root_a.children[i]
        child_b = root_b.children[i]
        identical = is_structurally_identical(child_a, child_b)
        if not identical:
            return False  # else keep on checking for other children pairs

    return True  # if not yet returned


if __name__ == "__main__":
    tree_root_a = GenericTree.single_line_input(input_array())
    tree_root_b = GenericTree.single_line_input(input_array())
    print(is_structurally_identical(tree_root_a, tree_root_b))

"""
10 3 20 30 40 2 40 50 0 0 0 0 
10 3 20 30 40 2 40 50 0 0 0 0
True <<< Output

10 3 20 30 40 2 40 50 0 0 0 0 
10 3 2 30 40 2 40 50 0 0 0 0
False <<< Output
"""
Example #3
0
    queue = deque()
    queue.append(root)
    while len(queue) != 0:
        curr_root = queue.popleft()
        curr_root_sum = curr_root.data
        # print(curr_root.data)     # print level-order to first check if you have written correct level-order-traversal
        for child in curr_root.children:
            curr_root_sum += child.data
            queue.append(child)

        if curr_root_sum > max_sum:
            max_sum = curr_root_sum
            max_node = curr_root

    print("Max Sum :", max_sum)
    return max_node


if __name__ == "__main__":
    array = input_array()
    tree_root = GenericTree.single_line_input(array)
    node = find_node_with_max_sum_including_children(tree_root)
    print(node.data)
"""
10 3 2 3 4 2 100 200 1 5 1 8 0 0 0 0
2 <<<<< MaxSum 302

5 3 1 2 3 1 15 2 4 5 1 6 0 0 0 0
1 <<<<< MaxSum 16
"""
    if root is None:
        return 0

    count = 1 if root.data > x else 0  # handling the root

    # asking results from subtrees
    for child in root.children:
        count += count_nodes_greater_then_x(child, x)

    return count


if __name__ == "__main__":
    array = input_array()
    value_x = array[0]
    del array[0]  # Removing the first element of the array, so remaining is the tree input
    tree_node = GenericTree.single_line_input(array)
    count = count_nodes_greater_then_x(tree_node, value_x)
    print(count)

"""
Single Line : First Integer denotes x and rest of the elements in level order form separated by space. Order is - 
Root_data, n (No_Of_Child_Of_Root), n children, and so on for every element 

35 10 3 20 30 40 2 40 50 0 0 0 0
3       << OUTPUT

10 10 3 20 30 40 2 40 50 0 0 0 0
5       << OUTPUT
"""