changed tree iteration algorithm, tree writing