Tree addition free of memory errors and leaks