add trim, output_tree and update build_tree utility functions.