write tree method