change aggreagateBy function, implement tree