def heapify(tree):
    """Rearrange a heap to maintain the heap property

    key of the root node must be more extreme than its children's key.
    In case the root key is not more extreme, swap it with the most extreme child's key
    and heapify child's subtree
    """
    root = tree.root
    min_root = root
    for child in root.children:
        #if our child key is less than the root, save new minimum and swap
        if child.key < root.key:
            min_root = child
            child.key, root.key = root.key, child.key

    return heapify(min_root)