def print_tree(tree, root, w=15):
    """ Print a constituency tree in a nice horizontal way. """
    has_children = True
    prev_layer = [(root, True)]
    while has_children:
        has_children = False
        new_layer = []
        print('|', end='')
        for part, visible in prev_layer:
            f = '{:^' + str(w * (part.right - part.left) - 1) + '}|'
            print(f.format(part.lhs if visible else ''), end='')
            if tree[part]:
                has_children = True
                for child in tree[part]:
                    new_layer.append((child, True))
            else:
                new_layer.append((part, False))
        print('')
        prev_layer = new_layer
    f = '{:^' + str(w - 1) + '}|'
    print('|', end='')
    for word, visible in prev_layer:
        print(f.format(word.rhs[0]), end='')
    print('')
