Fix all memory leaks in trees.c