I don’t know as your code is quite hard to read, but it could be.
You can free the variable by deleting it via del tree.
In case the memory is not released, another object would hold a reference to tree.