factorizing a tree