Doubt regarding java HashMaps

Revision en1, by sanket407, 2017-06-29 18:04:36

I am sure most of you must be familiar with union find data structure and the recursive findParent() in it. I usually use array par[] for it. But today i couldnt use array due to large range of values. So i used HashMaps instead. So foll. code was malfunctioning :

return par.get(x) < 0 ? x : (par.put(x, root(par.get(x)))); it was returning x even if par.get(x) > 0.

So i converted it to :

if(par.get(x) < 0)
    {  
             return x;
             }
    else
    {
       int p  = root(par.get(x));
       par.put(x, p);
       return p;
    }

And it worked fine . So what why did the first code fail ?? any inputs ??

Tags union find, #dsu

History

 
 
 
 
Revisions
 
 
  Rev. Lang. By When Δ Comment
en1 English sanket407 2017-06-29 18:04:36 683 Initial revision (published)