Fix a bug in tree_copy with circular references