functalized the tree filling procedure