I am working on a union-find algorithm, using the following :
HashMap<E,E> parent = new HashMap<E,E>();
I have written this method to find the last member of a family
public E find(E src)
{
while (parent.get(src) != null)
src = parent.get(src);
return src;
}
It works, but the thing is that I am going to work on this hashmap with big sets ; so I want to modify the find method so that it sets find(src) as the "father" of src. But I can't do that as it is, and I have the intuition of the reason why : if I try to do a copy in the beginning of the method
E copy = src;
And then at the end of the method
parent.put(copy,src);
It doesn't work because it does not actually do a "proper" copy. I have tried cloning parent but it doesn't work either.
Thank you all, and merry christmas !