free allocated memory in tree average method